From f32ff1b613a22ca3248cf5c36536e34b7ee375ea Mon Sep 17 00:00:00 2001
From: Kevin Jahns <kevin.jahns@rwth-aachen.de>
Date: Mon, 31 Oct 2016 01:17:24 +0100
Subject: [PATCH] fixed several consistency issues with y-indexeddb.
 Implemented support for .close() - a soft replacement for .destroy()

---
 README.md          |  8 ++++++--
 src/Database.js    | 17 ++++++++---------
 src/Struct.js      |  6 +++++-
 src/Transaction.js |  3 ++-
 src/y.js           | 24 +++++++++++++++++++-----
 5 files changed, 40 insertions(+), 18 deletions(-)

diff --git a/README.md b/README.md
index 72bd4a0a..d45a44ea 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 dbbec5e7..2b5e5ca4 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 592e2321..0758c979 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 bebac861..b6d91828 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 17c68e70..51420cb2 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()
     })
   }
 }
-- 
GitLab