persistence improvements

This commit is contained in:
Kevin Jahns 2018-01-10 00:11:25 +01:00
parent 07cf0b3436
commit 1a22fdd45e
3 changed files with 39 additions and 24 deletions

View File

@ -2,6 +2,7 @@ import BinaryEncoder from './Binary/Encoder.js'
import BinaryDecoder from './Binary/Decoder.js' import BinaryDecoder from './Binary/Decoder.js'
import { toBinary, fromBinary } from './MessageHandler/binaryEncode.js' import { toBinary, fromBinary } from './MessageHandler/binaryEncode.js'
import { integrateRemoteStructs } from './MessageHandler/integrateRemoteStructs.js' import { integrateRemoteStructs } from './MessageHandler/integrateRemoteStructs.js'
import { createMutualExclude } from './Util/mutualExclude.js'
function getFreshCnf () { function getFreshCnf () {
let buffer = new BinaryEncoder() let buffer = new BinaryEncoder()
@ -16,6 +17,7 @@ export default class AbstractPersistence {
constructor (opts) { constructor (opts) {
this.opts = opts this.opts = opts
this.ys = new Map() this.ys = new Map()
this.mutualExclude = createMutualExclude()
} }
_init (y) { _init (y) {
@ -45,6 +47,10 @@ export default class AbstractPersistence {
this.ys.delete(y) this.ys.delete(y)
} }
destroy () {
this.ys = null
}
/* overwrite */ /* overwrite */
saveUpdate (buffer) { saveUpdate (buffer) {
} }
@ -56,24 +62,28 @@ export default class AbstractPersistence {
saveStruct (y, struct) { saveStruct (y, struct) {
let cnf = this.ys.get(y) let cnf = this.ys.get(y)
if (cnf !== undefined) { if (cnf !== undefined) {
struct._toBinary(cnf.buffer) this.mutualExclude(function () {
cnf.len++ struct._toBinary(cnf.buffer)
cnf.len++
})
} }
} }
/* overwrite */ /* overwrite */
retrieve (y, model, updates) { retrieve (y, model, updates) {
y.transact(function () { this.mutualExclude(function () {
if (model != null) { y.transact(function () {
fromBinary(y, new BinaryDecoder(new Uint8Array(model))) if (model != null) {
y._setContentReady() fromBinary(y, new BinaryDecoder(new Uint8Array(model)))
}
if (updates != null) {
for (let i = 0; i < updates.length; i++) {
integrateRemoteStructs(y, new BinaryDecoder(new Uint8Array(updates[i])))
y._setContentReady() y._setContentReady()
} }
} if (updates != null) {
for (let i = 0; i < updates.length; i++) {
integrateRemoteStructs(y, new BinaryDecoder(new Uint8Array(updates[i])))
y._setContentReady()
}
}
})
}) })
} }
/* overwrite */ /* overwrite */

View File

@ -22,11 +22,15 @@ import debug from 'debug'
import Transaction from './Transaction.js' import Transaction from './Transaction.js'
export default class Y extends NamedEventHandler { export default class Y extends NamedEventHandler {
constructor (opts, persistence) { constructor (room, opts, persistence) {
super() super()
this.room = room
if (opts != null) {
opts.connector.room = room
}
this._contentReady = false this._contentReady = false
this._opts = opts this._opts = opts
this.userID = opts._userID != null ? opts._userID : generateUserID() this.userID = generateUserID()
this.share = {} this.share = {}
this.ds = new DeleteStore(this) this.ds = new DeleteStore(this)
this.os = new OperationStore(this) this.os = new OperationStore(this)
@ -37,9 +41,11 @@ export default class Y extends NamedEventHandler {
this.connector = null this.connector = null
this.connected = false this.connected = false
let initConnection = () => { let initConnection = () => {
this.connector = new Y[opts.connector.name](this, opts.connector) if (opts != null) {
this.connected = true this.connector = new Y[opts.connector.name](this, opts.connector)
this.emit('connectorReady') this.connected = true
this.emit('connectorReady')
}
} }
if (persistence != null) { if (persistence != null) {
this.persistence = persistence this.persistence = persistence
@ -103,9 +109,6 @@ export default class Y extends NamedEventHandler {
set _start (start) { set _start (start) {
return null return null
} }
get room () {
return this._opts.connector.room
}
define (name, TypeConstructor) { define (name, TypeConstructor) {
let id = new RootID(name, TypeConstructor) let id = new RootID(name, TypeConstructor)
let type = this.os.get(id) let type = this.os.get(id)
@ -138,10 +141,12 @@ export default class Y extends NamedEventHandler {
destroy () { destroy () {
super.destroy() super.destroy()
this.share = null this.share = null
if (this.connector.destroy != null) { if (this.connector != null) {
this.connector.destroy() if (this.connector.destroy != null) {
} else { this.connector.destroy()
this.connector.disconnect() } else {
this.connector.disconnect()
}
} }
if (this.persistence !== null) { if (this.persistence !== null) {
this.persistence.deinit(this) this.persistence.deinit(this)

View File

@ -145,7 +145,7 @@ export async function initArrays (t, opts) {
} else { } else {
connOpts = Object.assign({ role: 'slave' }, conn) 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 _userID: i, // evil hackery, don't try this at home
connector: connOpts connector: connOpts
}) })