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)
}
let persistence = new Y.IndexedDBPersistence()
let persistence = null // new Y.IndexedDBPersistence()
// initialize a shared object. This function call returns a promise!
let y = new Y({
connector: {
name: 'websockets-client',
url: 'http://127.0.0.1:1234',
room: 'html-editor-example6'
room: 'x'
// maxBufferLength: 100
}
}, persistence)

View File

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

View File

@ -24,11 +24,11 @@ export default class AbstractPersistence {
cnf = getFreshCnf()
this.ys.set(y, cnf)
this.init(y)
y.on('afterTransaction', () => {
y.on('afterTransaction', (y, transaction) => {
let cnf = this.ys.get(y)
if (cnf.len > 0) {
cnf.buffer.setUint32(0, cnf.len)
this.saveUpdate(y, cnf.buffer.createBuffer())
this.saveUpdate(y, cnf.buffer.createBuffer(), transaction)
let _cnf = getFreshCnf()
for (let key in _cnf) {
cnf[key] = _cnf[key]
@ -66,12 +66,12 @@ export default class AbstractPersistence {
y.transact(function () {
if (model != null) {
fromBinary(y, new BinaryDecoder(new Uint8Array(model)))
y.loaded = true
y._setContentReady()
}
if (updates != null) {
for (let i = 0; i < updates.length; 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 Transaction from './Transaction.js'
import YIndexedDB from '../../y-indexeddb/src/y-indexeddb.js'
export default class Y extends NamedEventHandler {
constructor (opts, persistence) {
super()
this._loaded = false
this._contentReady = false
this._opts = opts
this.userID = opts._userID != null ? opts._userID : generateUserID()
this.share = {}
@ -41,8 +39,9 @@ export default class Y extends NamedEventHandler {
let initConnection = () => {
this.connector = new Y[opts.connector.name](this, opts.connector)
this.connected = true
this.emit('connectorReady')
}
if (persistence !== undefined) {
if (persistence != null) {
this.persistence = persistence
persistence._init(this).then(initConnection)
} else {
@ -50,13 +49,10 @@ export default class Y extends NamedEventHandler {
initConnection()
}
}
get loaded () {
return this._loaded
}
set loaded (val) {
if (this._loaded === false && val) {
this._loaded = true
this.emit('loaded')
_setContentReady () {
if (!this._contentReady) {
this._contentReady = true
this.emit('content')
}
}
_beforeChange () {}
@ -177,7 +173,7 @@ Y.extend = function extendYjs () {
// TODO: The following assignments should be moved to yjs-dist
Y.AbstractConnector = Connector
Y.Persisence = Persistence
Y.AbstractPersistence = Persistence
Y.Array = YArray
Y.Map = YMap
Y.Text = YText