implement when-handler

This commit is contained in:
Kevin Jahns 2018-01-18 18:44:20 +01:00
parent 0b510b64a3
commit fc3a4c376c
3 changed files with 48 additions and 2 deletions

View File

@ -47,12 +47,33 @@ export default class AbstractPersistence {
} }
deinit (y) { deinit (y) {
this.ys.delete(y) this.ys.delete(y)
y.persistence = null
} }
destroy () { destroy () {
this.ys = null this.ys = null
} }
/**
* Remove all persisted data that belongs to a room.
* Automatically destroys all Yjs all Yjs instances that persist to
* the room. If `destroyYjsInstances = false` the persistence functionality
* will be removed from the Yjs instances.
*
* ** Must be overwritten! **
*/
removePersistedData (room, destroyYjsInstances = true) {
this.ys.forEach((cnf, y) => {
if (y.room === room) {
if (destroyYjsInstances) {
y.destroy()
} else {
this.deinit(y)
}
}
})
}
/* overwrite */ /* overwrite */
saveUpdate (buffer) { saveUpdate (buffer) {
} }
@ -77,17 +98,17 @@ 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._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._setContentReady()
} }
} }
}) })
y.emit('persistenceReady')
}) })
} }
/* overwrite */ /* overwrite */
persist (y) { persist (y) {
return toBinary(y).createBuffer() return toBinary(y).createBuffer()

View File

@ -1,6 +1,7 @@
export default class NamedEventHandler { export default class NamedEventHandler {
constructor () { constructor () {
this._eventListener = new Map() this._eventListener = new Map()
this._stateListener = new Map()
} }
_getListener (name) { _getListener (name) {
let listeners = this._eventListener.get(name) let listeners = this._eventListener.get(name)
@ -21,6 +22,20 @@ export default class NamedEventHandler {
let listeners = this._getListener(name) let listeners = this._getListener(name)
listeners.on.add(f) listeners.on.add(f)
} }
_initStateListener (name) {
let state = this._stateListener.get(name)
if (state === undefined) {
state = {}
state.promise = new Promise(function (resolve) {
state.resolve = resolve
})
this._stateListener.set(name, state)
}
return state
}
when (name) {
return this._initStateListener(name).promise
}
off (name, f) { off (name, f) {
if (name == null || f == null) { if (name == null || f == null) {
throw new Error('You must specify event name and function!') throw new Error('You must specify event name and function!')
@ -32,6 +47,7 @@ export default class NamedEventHandler {
} }
} }
emit (name, ...args) { emit (name, ...args) {
this._initStateListener(name).resolve()
const listener = this._eventListener.get(name) const listener = this._eventListener.get(name)
if (listener !== undefined) { if (listener !== undefined) {
listener.on.forEach(f => f.apply(null, args)) listener.on.forEach(f => f.apply(null, args))

View File

@ -62,6 +62,15 @@ export default class Y extends NamedEventHandler {
this.emit('content') this.emit('content')
} }
} }
whenContentReady () {
if (this._contentReady) {
return Promise.resolve()
} else {
return new Promise(resolve => {
this.once('content', resolve)
})
}
}
_beforeChange () {} _beforeChange () {}
transact (f, remote = false) { transact (f, remote = false) {
let initialCall = this._transaction === null let initialCall = this._transaction === null