From e1e94bcf5d43f1479fa5c1f63d4e1c73f7778751 Mon Sep 17 00:00:00 2001
From: Kevin Jahns <kevin.jahns@rwth-aachen.de>
Date: Thu, 25 Aug 2016 04:42:23 +0200
Subject: [PATCH] made createType synchronous

---
 src/Database.js    | 40 ++++++++++++++++++++++++++++++++++++
 src/Transaction.js | 51 ----------------------------------------------
 src/Utils.js       | 18 +++++++++++++---
 src/y.js           |  2 +-
 4 files changed, 56 insertions(+), 55 deletions(-)

diff --git a/src/Database.js b/src/Database.js
index 5d4a462c..26d95b85 100644
--- a/src/Database.js
+++ b/src/Database.js
@@ -505,6 +505,46 @@ module.exports = function (Y /* :any */) {
         }, 0)
       }
     }
+    /*
+      Get a type based on the id of its model.
+      If it does not exist yes, create it.
+      TODO: delete type from store.initializedTypes[id] when corresponding id was deleted!
+    */
+    * getType (id, args) {
+      var sid = JSON.stringify(id)
+      var t = this.store.initializedTypes[sid]
+      if (t == null) {
+        var op/* :MapStruct | ListStruct */ = yield* this.getOperation(id)
+        if (op != null) {
+          t = yield* Y[op.type].typeDefinition.initType.call(this, this.store, op, args)
+          this.store.initializedTypes[sid] = t
+        }
+      }
+      return t
+    }
+    createType (typedefinition, id) {
+      var structname = typedefinition[0].struct
+      id = id || this.getNextOpId(1)
+      var op = Y.Struct[structname].create(id)
+      op.type = typedefinition[0].name
+      
+      /* TODO: implement for y-xml support
+      if (typedefinition[0].appendAdditionalInfo != null) {
+        yield* typedefinition[0].appendAdditionalInfo.call(this, op, typedefinition[1])
+      }
+      */
+      this.requestTransaction(function * () {
+        if (op[0] === '_') {
+          yield* this.setOperation(op)
+        } else {
+          yield* this.applyCreatedOperations([op])
+        }
+      })
+      var t = Y[op.type].typeDefinition.createNewType(this, op)
+      this.initializedTypes[JSON.stringify(op.id)] = t
+      return t
+      //return yield* this.getType(id, typedefinition[1])
+    }
   }
   Y.AbstractDatabase = AbstractDatabase
 }
diff --git a/src/Transaction.js b/src/Transaction.js
index b837ee16..5daffb17 100644
--- a/src/Transaction.js
+++ b/src/Transaction.js
@@ -82,57 +82,6 @@ module.exports = function (Y/* :any */) {
     os: Store;
     ss: Store;
     */
-    /*
-      Get a type based on the id of its model.
-      If it does not exist yes, create it.
-      TODO: delete type from store.initializedTypes[id] when corresponding id was deleted!
-    */
-    * getType (id, args) {
-      var sid = JSON.stringify(id)
-      var t = this.store.initializedTypes[sid]
-      if (t == null) {
-        var op/* :MapStruct | ListStruct */ = yield* this.getOperation(id)
-        if (op != null) {
-          t = yield* Y[op.type].typeDefinition.initType.call(this, this.store, op, args)
-          this.store.initializedTypes[sid] = t
-        }
-      }
-      return t
-    }
-    * createType (typedefinition, id) {
-      var structname = typedefinition[0].struct
-      id = id || this.store.getNextOpId(1)
-      var op
-      if (id[0] === '_') {
-        op = yield* this.getOperation(id)
-      } else {
-        op = Y.Struct[structname].create(id)
-        op.type = typedefinition[0].name
-      }
-      if (typedefinition[0].appendAdditionalInfo != null) {
-        yield* typedefinition[0].appendAdditionalInfo.call(this, op, typedefinition[1])
-      }
-      if (op[0] === '_') {
-        yield* this.setOperation(op)
-      } else {
-        yield* this.applyCreatedOperations([op])
-      }
-      return yield* this.getType(id, typedefinition[1])
-    }
-    /* createType (typedefinition, id) {
-      var structname = typedefinition[0].struct
-      id = id || this.store.getNextOpId(1)
-      var op = Y.Struct[structname].create(id)
-      op.type = typedefinition[0].name
-      if (typedefinition[0].appendAdditionalInfo != null) {
-        yield* typedefinition[0].appendAdditionalInfo.call(this, op, typedefinition[1])
-      }
-      // yield* this.applyCreatedOperations([op])
-      yield* Y.Struct[op.struct].execute.call(this, op)
-      yield* this.addOperation(op)
-      yield* this.store.operationAdded(this, op)
-      return yield* this.getType(id, typedefinition[1])
-    }*/
     /*
       Apply operations that this user created (no remote ones!)
         * does not check for Struct.*.requiredOps()
diff --git a/src/Utils.js b/src/Utils.js
index 1b6bfe77..42202f58 100644
--- a/src/Utils.js
+++ b/src/Utils.js
@@ -238,7 +238,13 @@ module.exports = function (Y /* : any*/) {
           // finished with remaining operations
           self.waiting.push(d)
         }
-        checkDelete(op)
+        if (op.key == null) {
+          // deletes in list
+          checkDelete(op)
+        } else {
+          // deletes in map
+          this.waiting.push(op)
+        }
       } else {
         this.waiting.push(op)
       }
@@ -287,7 +293,11 @@ module.exports = function (Y /* : any*/) {
           var o = this.waiting[i]
           if (o.struct === 'Insert') {
             var _o = yield* transaction.getInsertion(o.id)
-            if (!Y.utils.compareIds(_o.id, o.id)) {
+            if (_o.parentSub != null && _o.left != null) {
+              // if o is an insertion of a map struc (parentSub is defined), then it shouldn't be necessary to compute left
+              this.waiting.splice(i,1)
+              i-- // update index
+            } else if (!Y.utils.compareIds(_o.id, o.id)) {
               // o got extended
               o.left = [o.id[0], o.id[1] - 1]
             } else if (_o.left == null) {
@@ -469,12 +479,14 @@ module.exports = function (Y /* : any*/) {
       if (def.struct == null ||
         def.initType == null ||
         def.class == null ||
-        def.name == null
+        def.name == null ||
+        def.createNewType == null
       ) {
         throw new Error('Custom type was not initialized correctly!')
       }
       this.struct = def.struct
       this.initType = def.initType
+      this.createNewType = def.createNewType
       this.class = def.class
       this.name = def.name
       if (def.appendAdditionalInfo != null) {
diff --git a/src/y.js b/src/y.js
index 681004ee..1edde138 100644
--- a/src/y.js
+++ b/src/y.js
@@ -151,7 +151,7 @@ class YConfig {
         var type = Y[typeName]
         var typedef = type.typeDefinition
         var id = ['_', typedef.struct + '_' + typeName + '_' + propertyname + '_' + typeConstructor]
-        share[propertyname] = yield* this.createType(type.apply(typedef, args), id)
+        share[propertyname] = this.store.createType(type.apply(typedef, args), id)
       }
       this.store.whenTransactionsFinished()
         .then(callback)
-- 
GitLab