diff --git a/declarations/Structs.js b/declarations/Structs.js index 9821209364d84abf44d92467ad012e54a2136ccd..7044e20d4d4b94cd1a651294e22e74064ea213dc 100644 --- a/declarations/Structs.js +++ b/declarations/Structs.js @@ -16,9 +16,9 @@ type Operation = Struct type Insertion = { id: Id, - left: Id, - origin: Id, - right: Id, + left: ?Id, + origin: ?Id, + right: ?Id, parent: Id, parentSub: ?Id, opContent: ?Id, diff --git a/src/Connector.js b/src/Connector.js index 1c4e6b47dafc32bf70bc8d160ab82406c26a8a64..0a1bb4e1c053bdc12b20ddc3ce3fe00d02a0b4cd 100644 --- a/src/Connector.js +++ b/src/Connector.js @@ -1,7 +1,7 @@ /* @flow */ 'use strict' -module.exports = function (Y/* :YGlobal */) { +module.exports = function (Y/* :any */) { class AbstractConnector { /* :: y: YConfig; diff --git a/src/Database.js b/src/Database.js index 8c7a1c398d1f83d219c165bf192fab7c4a2417be..ce28f673fc3d57a3d62eea70aa3405a2e606694e 100644 --- a/src/Database.js +++ b/src/Database.js @@ -1,7 +1,7 @@ /* @flow */ 'use strict' -module.exports = function (Y /* :YGlobal */) { +module.exports = function (Y /* :any */) { /* Partial definition of an OperationStore. TODO: name it Database, operation store only holds operations. diff --git a/src/Struct.js b/src/Struct.js index c4b38d471b255a976fece5710f6ce5b2e253a586..48671fa6aa3a78fd1f2f5dcdaaf770e87a8d0543 100644 --- a/src/Struct.js +++ b/src/Struct.js @@ -19,7 +19,7 @@ * requiredOps - Operations that are required to execute this operation. */ -module.exports = function (Y/* :YGlobal */) { +module.exports = function (Y/* :any */) { var Struct = { /* This is the only operation that is actually not a structure, because it is not stored in the OS. This is why it _does not_ have an id diff --git a/src/Transaction.js b/src/Transaction.js index 1c62c2736595ddab3ca778cf68295206d53b0296..fdec10749c76dde41f1ebf58de0a381b4bef2cbc 100644 --- a/src/Transaction.js +++ b/src/Transaction.js @@ -74,7 +74,7 @@ - this is called only by `getOperations(startSS)`. It makes an operation applyable on a given SS. */ -module.exports = function (Y/* :YGlobal */) { +module.exports = function (Y/* :any */) { class TransactionInterface { /* :: store: Y.AbstractDatabase; diff --git a/src/Types/Map.js b/src/Types/Map.js index 4cd4a4b16bd4e451ccf299ecc4f23978e01db154..dc0de35bef91e9aaae18ec3cbda832f98a5ff852 100644 --- a/src/Types/Map.js +++ b/src/Types/Map.js @@ -1,7 +1,16 @@ +/* @flow */ 'use strict' -module.exports = function (Y) { +module.exports = function (Y /* :any */) { class YMap { + /* :: + _model: Id; + os: Y.AbstractDatabase; + map: Object; + contents: any; + opContents: Object; + eventHandler: Function; + */ constructor (os, model, contents, opContents) { this._model = model.id this.os = os @@ -22,7 +31,8 @@ module.exports = function (Y) { oldValue = () => {// eslint-disable-line return new Promise((resolve) => { this.os.requestTransaction(function *() {// eslint-disable-line - resolve(yield* this.getType(prevType)) + var type = yield* this.getType(prevType) + resolve(type) }) }) } @@ -48,15 +58,20 @@ module.exports = function (Y) { } } this.map[key] = op.id - var insertEvent = { - name: key, - object: this - } + var insertEvent if (oldValue === undefined) { - insertEvent.type = 'add' + insertEvent = { + name: key, + object: this, + type: 'add' + } } else { - insertEvent.type = 'update' - insertEvent.oldValue = oldValue + insertEvent = { + name: key, + object: this, + oldValue: oldValue, + type: 'update' + } } userEvents.push(insertEvent) } @@ -92,7 +107,8 @@ module.exports = function (Y) { return new Promise((resolve) => { var oid = this.opContents[key] this.os.requestTransaction(function *() { - resolve(yield* this.getType(oid)) + var type = yield* this.getType(oid) + resolve(type) }) }) } @@ -133,7 +149,7 @@ module.exports = function (Y) { // if not, apply immediately on this type an call event var right = this.map[key] || null - var insert = { + var insert /* :any */ = { left: null, right: right, origin: null, @@ -236,7 +252,9 @@ module.exports = function (Y) { for (var e in events) { var event = events[e] if (event.name === path[0]) { - deleteChildObservers() + if (deleteChildObservers != null) { + deleteChildObservers() + } if (event.type === 'add' || event.type === 'update') { resetObserverPath() } @@ -248,8 +266,10 @@ module.exports = function (Y) { return resetObserverPath().then( // this promise contains a function that deletes all the child observers // and how to unobserve the observe from this object - Promise.resolve(function () { - deleteChildObservers() + new Promise.resolve(function () { // eslint-disable-line + if (deleteChildObservers != null) { + deleteChildObservers() + } self.unobserve(observer) }) ) diff --git a/src/Utils.js b/src/Utils.js index 018876d734814263aa008feb0f4ec66f8f38caf8..e02b403d7271b514b1b9d1c601a5edd58fd24bb1 100644 --- a/src/Utils.js +++ b/src/Utils.js @@ -23,7 +23,7 @@ database request to finish). EventHandler will help you to make your type synchronous. */ -module.exports = function (Y /* : YGlobal*/) { +module.exports = function (Y /* : any*/) { Y.utils = {} class EventHandler { diff --git a/src/y.js b/src/y.js index df34586cd9736e5ec27188986edeed3cc6aea231..191f1b0f2dd802c79c1c70028f7571b6e2323650 100644 --- a/src/y.js +++ b/src/y.js @@ -1,3 +1,4 @@ +/* @flow */ 'use strict' require('./Connector.js')(Y) @@ -55,7 +56,37 @@ function requestModules (modules) { require('./Types/Map.js')(Y) -function Y (opts) { +/* :: +type MemoryOptions = { + name: 'memory' +} +type IndexedDBOptions = { + name: 'indexeddb', + namespace: string +} +type DbOptions = MemoryOptions | IndexedDBOptions + +type WebRTCOptions = { + name: 'webrtc', + room: string +} +type WebsocketsClientOptions = { + name: 'websockets-client', + room: string +} +type ConnectionOptions = WebRTCOptions | WebsocketsClientOptions + +type TypesOptions = Array<'array'|'map'|'text'> + +type YOptions = { + connector: ConnectionOptions, + db: DbOptions, + types: TypesOptions, + sourceDir: string +} +*/ + +function Y (opts/* :YOptions */) /* :Promise<YConfig> */ { opts.types = opts.types != null ? opts.types : [] var modules = [opts.db.name, opts.connector.name].concat(opts.types) Y.sourceDir = opts.sourceDir @@ -71,6 +102,10 @@ function Y (opts) { } class YConfig { + /* :: + db: Y.AbstractDatabase; + connector: Y.AbstractConnector; + */ constructor (opts, callback) { this.db = new Y[opts.db.name](this, opts.db) this.connector = new Y[opts.connector.name](this, opts.connector)