persistence improvements
This commit is contained in:
parent
07cf0b3436
commit
1a22fdd45e
@ -2,6 +2,7 @@ import BinaryEncoder from './Binary/Encoder.js'
|
||||
import BinaryDecoder from './Binary/Decoder.js'
|
||||
import { toBinary, fromBinary } from './MessageHandler/binaryEncode.js'
|
||||
import { integrateRemoteStructs } from './MessageHandler/integrateRemoteStructs.js'
|
||||
import { createMutualExclude } from './Util/mutualExclude.js'
|
||||
|
||||
function getFreshCnf () {
|
||||
let buffer = new BinaryEncoder()
|
||||
@ -16,6 +17,7 @@ export default class AbstractPersistence {
|
||||
constructor (opts) {
|
||||
this.opts = opts
|
||||
this.ys = new Map()
|
||||
this.mutualExclude = createMutualExclude()
|
||||
}
|
||||
|
||||
_init (y) {
|
||||
@ -45,6 +47,10 @@ export default class AbstractPersistence {
|
||||
this.ys.delete(y)
|
||||
}
|
||||
|
||||
destroy () {
|
||||
this.ys = null
|
||||
}
|
||||
|
||||
/* overwrite */
|
||||
saveUpdate (buffer) {
|
||||
}
|
||||
@ -56,24 +62,28 @@ export default class AbstractPersistence {
|
||||
saveStruct (y, struct) {
|
||||
let cnf = this.ys.get(y)
|
||||
if (cnf !== undefined) {
|
||||
struct._toBinary(cnf.buffer)
|
||||
cnf.len++
|
||||
this.mutualExclude(function () {
|
||||
struct._toBinary(cnf.buffer)
|
||||
cnf.len++
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/* overwrite */
|
||||
retrieve (y, model, updates) {
|
||||
y.transact(function () {
|
||||
if (model != null) {
|
||||
fromBinary(y, new BinaryDecoder(new Uint8Array(model)))
|
||||
y._setContentReady()
|
||||
}
|
||||
if (updates != null) {
|
||||
for (let i = 0; i < updates.length; i++) {
|
||||
integrateRemoteStructs(y, new BinaryDecoder(new Uint8Array(updates[i])))
|
||||
this.mutualExclude(function () {
|
||||
y.transact(function () {
|
||||
if (model != null) {
|
||||
fromBinary(y, new BinaryDecoder(new Uint8Array(model)))
|
||||
y._setContentReady()
|
||||
}
|
||||
}
|
||||
if (updates != null) {
|
||||
for (let i = 0; i < updates.length; i++) {
|
||||
integrateRemoteStructs(y, new BinaryDecoder(new Uint8Array(updates[i])))
|
||||
y._setContentReady()
|
||||
}
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
/* overwrite */
|
||||
|
29
src/Y.js
29
src/Y.js
@ -22,11 +22,15 @@ import debug from 'debug'
|
||||
import Transaction from './Transaction.js'
|
||||
|
||||
export default class Y extends NamedEventHandler {
|
||||
constructor (opts, persistence) {
|
||||
constructor (room, opts, persistence) {
|
||||
super()
|
||||
this.room = room
|
||||
if (opts != null) {
|
||||
opts.connector.room = room
|
||||
}
|
||||
this._contentReady = false
|
||||
this._opts = opts
|
||||
this.userID = opts._userID != null ? opts._userID : generateUserID()
|
||||
this.userID = generateUserID()
|
||||
this.share = {}
|
||||
this.ds = new DeleteStore(this)
|
||||
this.os = new OperationStore(this)
|
||||
@ -37,9 +41,11 @@ export default class Y extends NamedEventHandler {
|
||||
this.connector = null
|
||||
this.connected = false
|
||||
let initConnection = () => {
|
||||
this.connector = new Y[opts.connector.name](this, opts.connector)
|
||||
this.connected = true
|
||||
this.emit('connectorReady')
|
||||
if (opts != null) {
|
||||
this.connector = new Y[opts.connector.name](this, opts.connector)
|
||||
this.connected = true
|
||||
this.emit('connectorReady')
|
||||
}
|
||||
}
|
||||
if (persistence != null) {
|
||||
this.persistence = persistence
|
||||
@ -103,9 +109,6 @@ export default class Y extends NamedEventHandler {
|
||||
set _start (start) {
|
||||
return null
|
||||
}
|
||||
get room () {
|
||||
return this._opts.connector.room
|
||||
}
|
||||
define (name, TypeConstructor) {
|
||||
let id = new RootID(name, TypeConstructor)
|
||||
let type = this.os.get(id)
|
||||
@ -138,10 +141,12 @@ export default class Y extends NamedEventHandler {
|
||||
destroy () {
|
||||
super.destroy()
|
||||
this.share = null
|
||||
if (this.connector.destroy != null) {
|
||||
this.connector.destroy()
|
||||
} else {
|
||||
this.connector.disconnect()
|
||||
if (this.connector != null) {
|
||||
if (this.connector.destroy != null) {
|
||||
this.connector.destroy()
|
||||
} else {
|
||||
this.connector.disconnect()
|
||||
}
|
||||
}
|
||||
if (this.persistence !== null) {
|
||||
this.persistence.deinit(this)
|
||||
|
@ -145,7 +145,7 @@ export async function initArrays (t, opts) {
|
||||
} else {
|
||||
connOpts = Object.assign({ role: 'slave' }, conn)
|
||||
}
|
||||
let y = new Y({
|
||||
let y = new Y(connOpts.room, {
|
||||
_userID: i, // evil hackery, don't try this at home
|
||||
connector: connOpts
|
||||
})
|
||||
|
Loading…
x
Reference in New Issue
Block a user