export AbstractPersistence

This commit is contained in:
Kevin Jahns 2018-01-08 17:30:30 +01:00
parent 5a68b9f4ad
commit 07cf0b3436
4 changed files with 15 additions and 19 deletions

View File

@ -4,14 +4,14 @@ window.onload = function () {
window.yXmlType.bindToDom(document.body) window.yXmlType.bindToDom(document.body)
} }
let persistence = new Y.IndexedDBPersistence() let persistence = null // new Y.IndexedDBPersistence()
// initialize a shared object. This function call returns a promise! // initialize a shared object. This function call returns a promise!
let y = new Y({ let y = new Y({
connector: { connector: {
name: 'websockets-client', name: 'websockets-client',
url: 'http://127.0.0.1:1234', url: 'http://127.0.0.1:1234',
room: 'html-editor-example6' room: 'x'
// maxBufferLength: 100 // maxBufferLength: 100
} }
}, persistence) }, persistence)

View File

@ -132,7 +132,7 @@ export default class AbstractConnector {
f() f()
} }
this.whenSyncedListeners = [] this.whenSyncedListeners = []
this.y.loaded = true this.y._setContentReady()
this.y.emit('synced') this.y.emit('synced')
} }
} }

View File

@ -24,11 +24,11 @@ export default class AbstractPersistence {
cnf = getFreshCnf() cnf = getFreshCnf()
this.ys.set(y, cnf) this.ys.set(y, cnf)
this.init(y) this.init(y)
y.on('afterTransaction', () => { y.on('afterTransaction', (y, transaction) => {
let cnf = this.ys.get(y) let cnf = this.ys.get(y)
if (cnf.len > 0) { if (cnf.len > 0) {
cnf.buffer.setUint32(0, cnf.len) cnf.buffer.setUint32(0, cnf.len)
this.saveUpdate(y, cnf.buffer.createBuffer()) this.saveUpdate(y, cnf.buffer.createBuffer(), transaction)
let _cnf = getFreshCnf() let _cnf = getFreshCnf()
for (let key in _cnf) { for (let key in _cnf) {
cnf[key] = _cnf[key] cnf[key] = _cnf[key]
@ -66,12 +66,12 @@ export default class AbstractPersistence {
y.transact(function () { y.transact(function () {
if (model != null) { if (model != null) {
fromBinary(y, new BinaryDecoder(new Uint8Array(model))) fromBinary(y, new BinaryDecoder(new Uint8Array(model)))
y.loaded = true y._setContentReady()
} }
if (updates != null) { if (updates != null) {
for (let i = 0; i < updates.length; i++) { for (let i = 0; i < updates.length; i++) {
integrateRemoteStructs(y, new BinaryDecoder(new Uint8Array(updates[i]))) integrateRemoteStructs(y, new BinaryDecoder(new Uint8Array(updates[i])))
y.loaded = true y._setContentReady()
} }
} }
}) })

View File

@ -21,12 +21,10 @@ import { addStruct as addType } from './Util/structReferences.js'
import debug from 'debug' import debug from 'debug'
import Transaction from './Transaction.js' import Transaction from './Transaction.js'
import YIndexedDB from '../../y-indexeddb/src/y-indexeddb.js'
export default class Y extends NamedEventHandler { export default class Y extends NamedEventHandler {
constructor (opts, persistence) { constructor (opts, persistence) {
super() super()
this._loaded = false this._contentReady = false
this._opts = opts this._opts = opts
this.userID = opts._userID != null ? opts._userID : generateUserID() this.userID = opts._userID != null ? opts._userID : generateUserID()
this.share = {} this.share = {}
@ -41,8 +39,9 @@ export default class Y extends NamedEventHandler {
let initConnection = () => { let initConnection = () => {
this.connector = new Y[opts.connector.name](this, opts.connector) this.connector = new Y[opts.connector.name](this, opts.connector)
this.connected = true this.connected = true
this.emit('connectorReady')
} }
if (persistence !== undefined) { if (persistence != null) {
this.persistence = persistence this.persistence = persistence
persistence._init(this).then(initConnection) persistence._init(this).then(initConnection)
} else { } else {
@ -50,13 +49,10 @@ export default class Y extends NamedEventHandler {
initConnection() initConnection()
} }
} }
get loaded () { _setContentReady () {
return this._loaded if (!this._contentReady) {
} this._contentReady = true
set loaded (val) { this.emit('content')
if (this._loaded === false && val) {
this._loaded = true
this.emit('loaded')
} }
} }
_beforeChange () {} _beforeChange () {}
@ -177,7 +173,7 @@ Y.extend = function extendYjs () {
// TODO: The following assignments should be moved to yjs-dist // TODO: The following assignments should be moved to yjs-dist
Y.AbstractConnector = Connector Y.AbstractConnector = Connector
Y.Persisence = Persistence Y.AbstractPersistence = Persistence
Y.Array = YArray Y.Array = YArray
Y.Map = YMap Y.Map = YMap
Y.Text = YText Y.Text = YText