From 34f365cd8f571e35d2c9be24754c013a639e6425 Mon Sep 17 00:00:00 2001
From: Kevin Jahns <kevin.jahns@rwth-aachen.de>
Date: Fri, 26 Aug 2016 13:53:31 +0200
Subject: [PATCH] implemented support for synchronous type creation

---
 src/Database.js | 21 ++++++++++++++-------
 src/Utils.js    | 22 +++++++++++++++-------
 src/y.js        |  2 +-
 3 files changed, 30 insertions(+), 15 deletions(-)

diff --git a/src/Database.js b/src/Database.js
index 26d95b85..4b483629 100644
--- a/src/Database.js
+++ b/src/Database.js
@@ -506,11 +506,16 @@ module.exports = function (Y /* :any */) {
       }
     }
     /*
-      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!
+      Get a created/initialized type.
     */
-    * getType (id, args) {
+    getType (id) {
+      return this.initializedTypes[JSON.stringify(id)]
+    }
+    /*
+      Init type. This is called when a remote operation is retrieved, and transformed to a type
+      TODO: delete type from store.initializedTypes[id] when corresponding id was deleted! 
+    */
+    * initType (id, args) {
       var sid = JSON.stringify(id)
       var t = this.store.initializedTypes[sid]
       if (t == null) {
@@ -522,6 +527,9 @@ module.exports = function (Y /* :any */) {
       }
       return t
     }
+    /*
+     Create type. This is called when the local user creates a type (which is a synchronous action)
+    */
     createType (typedefinition, id) {
       var structname = typedefinition[0].struct
       id = id || this.getNextOpId(1)
@@ -534,16 +542,15 @@ module.exports = function (Y /* :any */) {
       }
       */
       this.requestTransaction(function * () {
-        if (op[0] === '_') {
+        if (op.id[0] === '_') {
           yield* this.setOperation(op)
         } else {
           yield* this.applyCreatedOperations([op])
         }
       })
-      var t = Y[op.type].typeDefinition.createNewType(this, op)
+      var t = Y[op.type].typeDefinition.createType(this, op, typedefinition[1])
       this.initializedTypes[JSON.stringify(op.id)] = t
       return t
-      //return yield* this.getType(id, typedefinition[1])
     }
   }
   Y.AbstractDatabase = AbstractDatabase
diff --git a/src/Utils.js b/src/Utils.js
index 42202f58..41cf74b4 100644
--- a/src/Utils.js
+++ b/src/Utils.js
@@ -457,6 +457,14 @@ module.exports = function (Y /* : any*/) {
   }
   Y.utils.EventHandler = EventHandler
 
+  /*
+    Default class of custom types!
+  */
+  class CustomType {
+
+  }
+  Y.utils.CustomType = CustomType
+
   /*
     A wrapper for the definition of a custom type.
     Every custom type must have three properties:
@@ -468,7 +476,7 @@ module.exports = function (Y /* : any*/) {
     * class
       - the constructor of the custom type (e.g. in order to inherit from a type)
   */
-  class CustomType { // eslint-disable-line
+  class CustomTypeDefinition { // eslint-disable-line
     /* ::
     struct: any;
     initType: any;
@@ -480,13 +488,13 @@ module.exports = function (Y /* : any*/) {
         def.initType == null ||
         def.class == null ||
         def.name == null ||
-        def.createNewType == null
+        def.createType == null
       ) {
         throw new Error('Custom type was not initialized correctly!')
       }
       this.struct = def.struct
       this.initType = def.initType
-      this.createNewType = def.createNewType
+      this.createType = def.createType
       this.class = def.class
       this.name = def.name
       if (def.appendAdditionalInfo != null) {
@@ -498,13 +506,13 @@ module.exports = function (Y /* : any*/) {
       this.parseArguments.typeDefinition = this
     }
   }
-  Y.utils.CustomType = CustomType
+  Y.utils.CustomTypeDefinition = CustomTypeDefinition
 
   Y.utils.isTypeDefinition = function isTypeDefinition (v) {
     if (v != null) {
-      if (v instanceof Y.utils.CustomType) return [v]
-      else if (v.constructor === Array && v[0] instanceof Y.utils.CustomType) return v
-      else if (v instanceof Function && v.typeDefinition instanceof Y.utils.CustomType) return [v.typeDefinition]
+      if (v instanceof Y.utils.CustomTypeDefinition) return [v]
+      else if (v.constructor === Array && v[0] instanceof Y.utils.CustomTypeDefinition) return v
+      else if (v instanceof Function && v.typeDefinition instanceof Y.utils.CustomTypeDefinition) return [v.typeDefinition]
     }
     return false
   }
diff --git a/src/y.js b/src/y.js
index 1edde138..8bce847a 100644
--- a/src/y.js
+++ b/src/y.js
@@ -14,7 +14,7 @@ module.exports = Y
 Y.requiringModules = requiringModules
 
 Y.extend = function (name, value) {
-  if (value instanceof Y.utils.CustomType) {
+  if (value instanceof Y.utils.CustomTypeDefinition) {
     Y[name] = value.parseArguments
   } else {
     Y[name] = value
-- 
GitLab