diff --git a/README.md b/README.md index 72bd4a0ab689a8c0a01e4a2c72e524536313a506..d45a44ea13e0f9203f4b2fcf938f7990f802a51a 100644 --- a/README.md +++ b/README.md @@ -198,12 +198,16 @@ The promise returns an instance of Y. We denote it with a lower case `y`. * Force to disconnect this instance from the other instances * y.connector.reconnect() * Try to reconnect to the other instances (needs to be supported by the connector) - * Not supported by y-xmpp -* y.destroy() + * Not supported by y-xmpp +* y.close() * Destroy this object. * Destroys all types (they will throw weird errors if you still use them) * Disconnects from the other instances (via connector) + * Returns a promise +* y.destroy() + * calls y.close() * Removes all data from the database + * Returns a promise * y.db.stopGarbageCollector() * Stop the garbage collector. Call y.db.garbageCollect() to continue garbage collection * y.db.gcTimeout :: Number (defaults to 50000 ms) diff --git a/src/Database.js b/src/Database.js index dbbec5e76193e4c5bd52d3105f6ffbb0b839e066..2b5e5ca45d557ee44c7a03e9b2e43254840fb49b 100644 --- a/src/Database.js +++ b/src/Database.js @@ -239,10 +239,7 @@ module.exports = function (Y /* :any */) { this.gc2 = this.gc2.filter(filter) delete op.gc } - * destroy () { - clearInterval(this.gcInterval) - this.gcInterval = null - this.stopRepairCheck() + destroyTypes () { for (var key in this.initializedTypes) { var type = this.initializedTypes[key] if (type._destroy != null) { @@ -252,6 +249,11 @@ module.exports = function (Y /* :any */) { } } } + * destroy () { + clearInterval(this.gcInterval) + this.gcInterval = null + this.stopRepairCheck() + } setUserId (userId) { if (!this.userIdPromise.inProgress) { this.userIdPromise.inProgress = true @@ -434,8 +436,7 @@ module.exports = function (Y /* :any */) { */ * operationAdded (transaction, op) { if (op.struct === 'Delete') { - var target = yield* transaction.getInsertion(op.target) - var type = this.initializedTypes[JSON.stringify(target.parent)] + var type = this.initializedTypes[JSON.stringify(op.targetParent)] if (type != null) { yield* type._changed(transaction, op) } @@ -503,10 +504,8 @@ module.exports = function (Y /* :any */) { resolve: resolve, promise: promise } - return promise - } else { - return this.transactionsFinished.promise } + return this.transactionsFinished.promise } else { return Promise.resolve() } diff --git a/src/Struct.js b/src/Struct.js index 592e23216491f30b19c187aff690a3bcff8a2230..0758c979c31e68601fb6033136117be38b585be0 100644 --- a/src/Struct.js +++ b/src/Struct.js @@ -30,7 +30,11 @@ module.exports = function (Y/* :any */) { */ Delete: { encode: function (op) { - return op + return { + target: op.target, + length: op.length || 0, + struct: 'Delete' + } }, requiredOps: function (op) { return [] // [op.target] diff --git a/src/Transaction.js b/src/Transaction.js index bebac861deedfb291e23a5d0222d5ea8f4f12238..b6d918285c6be4d4de51bfeb9edd8bc69dd7e89d 100644 --- a/src/Transaction.js +++ b/src/Transaction.js @@ -206,7 +206,8 @@ module.exports = function (Y/* :any */) { yield* this.store.operationAdded(this, { struct: 'Delete', target: target.id, - length: targetLength + length: targetLength, + targetParent: target.parent }) } // need to gc in the end! diff --git a/src/y.js b/src/y.js index 17c68e7090abd61706f41b15c5c82c16996dbe25..51420cb246a70518667d197454cb58a4747d8e9c 100644 --- a/src/y.js +++ b/src/y.js @@ -190,16 +190,30 @@ class YConfig { return this.connector.reconnect() } destroy () { + var self = this + return this.close().then(function () { + if (self.db.deleteDB != null) { + return self.db.deleteDB() + } else { + return Promise.resolve() + } + }) + } + close () { + var self = this + this.share = null if (this.connector.destroy != null) { this.connector.destroy() } else { this.connector.disconnect() } - var self = this - this.db.requestTransaction(function * () { - yield* self.db.destroy() - self.connector = null - self.db = null + return this.db.whenTransactionsFinished(function () { + this.db.destroyTypes() + // make sure to wait for all transactions before destroying the db + this.db.requestTransaction(function * () { + yield* self.db.destroy() + }) + return this.db.whenTransactionsFinished() }) } }