diff --git a/declarations/Structs.js b/declarations/Structs.js index 98212093..7044e20d 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 1c4e6b47..0a1bb4e1 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 8c7a1c39..ce28f673 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 c4b38d47..48671fa6 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 1c62c273..fdec1074 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 4cd4a4b1..dc0de35b 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 018876d7..e02b403d 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 df34586c..191f1b0f 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 */ { 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)