2019-03-01 23:26:40 +01:00
|
|
|
import { DeleteStore } from './DeleteStore.js'
|
2018-11-27 14:59:12 +01:00
|
|
|
import { OperationStore } from './OperationStore.js'
|
|
|
|
|
import { StateStore } from './StateStore.js'
|
2019-03-10 23:26:53 +01:00
|
|
|
import * as random from 'lib0/random.js'
|
2018-11-27 14:59:12 +01:00
|
|
|
import { createRootID } from './ID.js'
|
2019-03-10 23:26:53 +01:00
|
|
|
import { Observable } from 'lib0/observable.js'
|
2018-11-27 14:59:12 +01:00
|
|
|
import { Transaction } from './Transaction.js'
|
2018-03-23 01:55:47 +01:00
|
|
|
|
2018-03-05 03:03:40 +01:00
|
|
|
/**
|
|
|
|
|
* Anything that can be encoded with `JSON.stringify` and can be decoded with
|
|
|
|
|
* `JSON.parse`.
|
|
|
|
|
*
|
|
|
|
|
* The following property should hold:
|
|
|
|
|
* `JSON.parse(JSON.stringify(key))===key`
|
|
|
|
|
*
|
|
|
|
|
* At the moment the only safe values are number and string.
|
|
|
|
|
*
|
2018-10-29 21:58:21 +01:00
|
|
|
* @typedef {(number|string|Object)} encodable
|
2018-03-05 03:03:40 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* A Yjs instance handles the state of shared data.
|
|
|
|
|
*/
|
2019-03-01 23:26:40 +01:00
|
|
|
export class Y extends Observable {
|
2018-11-25 05:43:18 +01:00
|
|
|
/**
|
2018-12-03 17:09:00 +01:00
|
|
|
* @param {Object} [conf] configuration
|
2018-11-25 05:43:18 +01:00
|
|
|
*/
|
2018-12-03 17:09:00 +01:00
|
|
|
constructor (conf = {}) {
|
2017-10-16 04:53:12 +02:00
|
|
|
super()
|
2018-05-02 18:42:18 +02:00
|
|
|
this.gcEnabled = conf.gc || false
|
2018-01-08 17:30:30 +01:00
|
|
|
this._contentReady = false
|
2019-03-01 23:26:40 +01:00
|
|
|
this.userID = random.uint32()
|
2018-03-05 03:03:40 +01:00
|
|
|
// TODO: This should be a Map so we can use encodables as keys
|
2018-10-29 21:58:21 +01:00
|
|
|
this._map = new Map()
|
|
|
|
|
this.ds = new DeleteStore()
|
2017-10-11 03:41:54 +02:00
|
|
|
this.os = new OperationStore(this)
|
|
|
|
|
this.ss = new StateStore(this)
|
|
|
|
|
this._missingStructs = new Map()
|
2017-10-16 04:53:12 +02:00
|
|
|
this._readyToIntegrate = []
|
2017-10-22 19:12:50 +02:00
|
|
|
this._transaction = null
|
2017-12-24 03:18:00 +01:00
|
|
|
this.connected = false
|
2018-03-23 01:55:47 +01:00
|
|
|
// for compatibility with isParentOf
|
|
|
|
|
this._parent = null
|
2018-04-23 13:25:30 +02:00
|
|
|
this._hasUndoManager = false
|
2018-10-29 21:58:21 +01:00
|
|
|
this._deleted = false // for compatiblity of having this as a parent for types
|
|
|
|
|
this._id = null
|
2017-10-19 17:36:28 +02:00
|
|
|
}
|
|
|
|
|
_beforeChange () {}
|
2018-10-29 21:58:21 +01:00
|
|
|
_callObserver (transaction, subs, remote) {}
|
2018-03-05 03:03:40 +01:00
|
|
|
/**
|
|
|
|
|
* Changes that happen inside of a transaction are bundled. This means that
|
|
|
|
|
* the observer fires _after_ the transaction is finished and that all changes
|
|
|
|
|
* that happened inside of the transaction are sent as one message to the
|
|
|
|
|
* other peers.
|
|
|
|
|
*
|
|
|
|
|
* @param {Function} f The function that should be executed as a transaction
|
2018-03-06 03:17:36 +01:00
|
|
|
* @param {?Boolean} remote Optional. Whether this transaction is initiated by
|
|
|
|
|
* a remote peer. This should not be set manually!
|
|
|
|
|
* Defaults to false.
|
2018-03-05 03:03:40 +01:00
|
|
|
*/
|
2017-10-22 19:12:50 +02:00
|
|
|
transact (f, remote = false) {
|
|
|
|
|
let initialCall = this._transaction === null
|
|
|
|
|
if (initialCall) {
|
|
|
|
|
this._transaction = new Transaction(this)
|
2019-03-01 23:26:40 +01:00
|
|
|
this.emit('beforeTransaction', [this, this._transaction, remote])
|
2017-10-22 19:12:50 +02:00
|
|
|
}
|
2017-10-19 17:36:28 +02:00
|
|
|
try {
|
2017-10-27 22:28:32 +02:00
|
|
|
f(this)
|
2017-10-19 17:36:28 +02:00
|
|
|
} catch (e) {
|
|
|
|
|
console.error(e)
|
|
|
|
|
}
|
2017-10-22 19:12:50 +02:00
|
|
|
if (initialCall) {
|
2019-03-01 23:26:40 +01:00
|
|
|
this.emit('beforeObserverCalls', [this, this._transaction, remote])
|
2017-11-12 13:34:23 -08:00
|
|
|
const transaction = this._transaction
|
|
|
|
|
this._transaction = null
|
2017-10-22 19:12:50 +02:00
|
|
|
// emit change events on changed types
|
2018-11-25 03:17:00 +01:00
|
|
|
transaction.changedTypes.forEach((subs, type) => {
|
2017-11-07 22:44:43 -08:00
|
|
|
if (!type._deleted) {
|
2017-11-12 13:34:23 -08:00
|
|
|
type._callObserver(transaction, subs, remote)
|
2017-11-07 22:44:43 -08:00
|
|
|
}
|
|
|
|
|
})
|
2018-11-25 03:17:00 +01:00
|
|
|
transaction.changedParentTypes.forEach((events, type) => {
|
2017-11-07 22:44:43 -08:00
|
|
|
if (!type._deleted) {
|
2017-11-08 17:31:12 -08:00
|
|
|
events = events
|
|
|
|
|
.filter(event =>
|
|
|
|
|
!event.target._deleted
|
|
|
|
|
)
|
|
|
|
|
events
|
|
|
|
|
.forEach(event => {
|
|
|
|
|
event.currentTarget = type
|
|
|
|
|
})
|
2017-11-07 22:44:43 -08:00
|
|
|
// we don't have to check for events.length
|
|
|
|
|
// because there is no way events is empty..
|
2017-11-12 13:34:23 -08:00
|
|
|
type._deepEventHandler.callEventListeners(transaction, events)
|
2017-11-07 22:44:43 -08:00
|
|
|
}
|
2017-10-22 19:12:50 +02:00
|
|
|
})
|
|
|
|
|
// when all changes & events are processed, emit afterTransaction event
|
2019-03-01 23:26:40 +01:00
|
|
|
this.emit('afterTransaction', [this, transaction, remote])
|
2017-10-19 17:36:28 +02:00
|
|
|
}
|
2017-10-16 04:53:12 +02:00
|
|
|
}
|
2018-03-05 03:03:40 +01:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Fake _start for root properties (y.set('name', type))
|
2018-11-25 22:39:30 +01:00
|
|
|
*
|
|
|
|
|
* @private
|
2018-03-05 03:03:40 +01:00
|
|
|
*/
|
2017-10-16 04:53:12 +02:00
|
|
|
get _start () {
|
|
|
|
|
return null
|
|
|
|
|
}
|
2018-03-05 03:03:40 +01:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Fake _start for root properties (y.set('name', type))
|
2018-11-25 22:39:30 +01:00
|
|
|
*
|
|
|
|
|
* @private
|
2018-03-05 03:03:40 +01:00
|
|
|
*/
|
2018-10-29 21:58:21 +01:00
|
|
|
set _start (start) {}
|
2018-03-05 03:03:40 +01:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Define a shared data type.
|
|
|
|
|
*
|
|
|
|
|
* Multiple calls of `y.define(name, TypeConstructor)` yield the same result
|
|
|
|
|
* and do not overwrite each other. I.e.
|
|
|
|
|
* `y.define(name, type) === y.define(name, type)`
|
|
|
|
|
*
|
2018-10-29 21:58:21 +01:00
|
|
|
* After this method is called, the type is also available on `y._map.get(name)`.
|
2018-03-05 03:03:40 +01:00
|
|
|
*
|
|
|
|
|
* *Best Practices:*
|
|
|
|
|
* Either define all types right after the Yjs instance is created or always
|
|
|
|
|
* use `y.define(..)` when accessing a type.
|
|
|
|
|
*
|
|
|
|
|
* @example
|
|
|
|
|
* // Option 1
|
|
|
|
|
* const y = new Y(..)
|
|
|
|
|
* y.define('myArray', YArray)
|
|
|
|
|
* y.define('myMap', YMap)
|
2018-10-29 21:58:21 +01:00
|
|
|
* // .. when accessing the type use y._map.get(name)
|
2018-03-05 03:03:40 +01:00
|
|
|
* y.share.myArray.insert(..)
|
|
|
|
|
* y.share.myMap.set(..)
|
|
|
|
|
*
|
|
|
|
|
* // Option2
|
|
|
|
|
* const y = new Y(..)
|
|
|
|
|
* // .. when accessing the type use `y.define(..)`
|
|
|
|
|
* y.define('myArray', YArray).insert(..)
|
|
|
|
|
* y.define('myMap', YMap).set(..)
|
|
|
|
|
*
|
|
|
|
|
* @param {String} name
|
2018-10-29 21:58:21 +01:00
|
|
|
* @param {Function} TypeConstructor The constructor of the type definition
|
2019-01-09 23:54:36 +01:00
|
|
|
* @returns {any} The created type. Constructed with TypeConstructor
|
2018-03-05 03:03:40 +01:00
|
|
|
*/
|
2017-11-07 18:31:04 -08:00
|
|
|
define (name, TypeConstructor) {
|
2018-10-29 21:58:21 +01:00
|
|
|
let id = createRootID(name, TypeConstructor)
|
2017-10-11 03:41:54 +02:00
|
|
|
let type = this.os.get(id)
|
2018-10-29 21:58:21 +01:00
|
|
|
if (this._map.get(name) === undefined) {
|
|
|
|
|
this._map.set(name, type)
|
|
|
|
|
} else if (this._map.get(name) !== type) {
|
2017-11-26 14:39:47 -08:00
|
|
|
throw new Error('Type is already defined with a different constructor')
|
2017-10-11 03:41:54 +02:00
|
|
|
}
|
|
|
|
|
return type
|
|
|
|
|
}
|
2018-03-05 03:03:40 +01:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get a defined type. The type must be defined locally. First define the
|
|
|
|
|
* type with {@link define}.
|
|
|
|
|
*
|
|
|
|
|
* This returns the same value as `y.share[name]`
|
|
|
|
|
*
|
|
|
|
|
* @param {String} name The typename
|
2019-01-09 23:54:36 +01:00
|
|
|
* @return {any}
|
2018-03-05 03:03:40 +01:00
|
|
|
*/
|
2017-11-07 18:31:04 -08:00
|
|
|
get (name) {
|
2018-10-29 21:58:21 +01:00
|
|
|
return this._map.get(name)
|
2017-10-11 03:41:54 +02:00
|
|
|
}
|
2018-03-05 03:03:40 +01:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Disconnect from the room, and destroy all traces of this Yjs instance.
|
|
|
|
|
*/
|
2017-10-11 03:41:54 +02:00
|
|
|
destroy () {
|
2019-03-01 23:26:40 +01:00
|
|
|
this.emit('destroyed', [true])
|
2017-12-24 03:18:00 +01:00
|
|
|
super.destroy()
|
2017-10-11 03:41:54 +02:00
|
|
|
}
|
|
|
|
|
}
|