outsourcing some code. custom types definition change
This commit is contained in:
		
							parent
							
								
									60b75d1862
								
							
						
					
					
						commit
						f9f8228db6
					
				@ -33,6 +33,7 @@
 | 
				
			|||||||
    "compareAllUsers": true,
 | 
					    "compareAllUsers": true,
 | 
				
			||||||
    "createUsers": true,
 | 
					    "createUsers": true,
 | 
				
			||||||
    "getRandomNumber": true,
 | 
					    "getRandomNumber": true,
 | 
				
			||||||
    "applyRandomTransactions": true
 | 
					    "applyRandomTransactions": true,
 | 
				
			||||||
 | 
					    "CustomType": true
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
@ -9,7 +9,7 @@ class AbstractTransaction { //eslint-disable-line no-unused-vars
 | 
				
			|||||||
    if (t == null) {
 | 
					    if (t == null) {
 | 
				
			||||||
      var op = yield* this.getOperation(id);
 | 
					      var op = yield* this.getOperation(id);
 | 
				
			||||||
      if (op != null) {
 | 
					      if (op != null) {
 | 
				
			||||||
        t = yield* Y[op.type].create.call(this, this.store, op);
 | 
					        t = yield* Y[op.type].initType.call(this, this.store, op);
 | 
				
			||||||
        this.store.initializedTypes[sid] = t;
 | 
					        this.store.initializedTypes[sid] = t;
 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
@ -17,7 +17,7 @@ class AbstractTransaction { //eslint-disable-line no-unused-vars
 | 
				
			|||||||
  }
 | 
					  }
 | 
				
			||||||
  *createType (model) {
 | 
					  *createType (model) {
 | 
				
			||||||
    var sid = JSON.stringify(model.id);
 | 
					    var sid = JSON.stringify(model.id);
 | 
				
			||||||
    var t = yield* Y[model.type].create(this.store, model);
 | 
					    var t = yield* Y[model.type].initType.call(this, this.store, model);
 | 
				
			||||||
    this.store.initializedTypes[sid] = t;
 | 
					    this.store.initializedTypes[sid] = t;
 | 
				
			||||||
    return t;
 | 
					    return t;
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
				
			|||||||
@ -155,23 +155,26 @@
 | 
				
			|||||||
    }
 | 
					    }
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  Y.Array = function* _YArray(){
 | 
					  Y.Array = new CustomType({
 | 
				
			||||||
    var model = {
 | 
					    class: YArray,
 | 
				
			||||||
      start: null,
 | 
					    createType: function* YArrayCreator () {
 | 
				
			||||||
      end: null,
 | 
					      var model = {
 | 
				
			||||||
      struct: "List",
 | 
					        start: null,
 | 
				
			||||||
      type: "Array",
 | 
					        end: null,
 | 
				
			||||||
      id: this.store.getNextOpId()
 | 
					        struct: "List",
 | 
				
			||||||
    };
 | 
					        type: "Array",
 | 
				
			||||||
    yield* this.applyCreatedOperations([model]);
 | 
					        id: this.store.getNextOpId()
 | 
				
			||||||
    return yield* this.createType(model);
 | 
					      };
 | 
				
			||||||
  };
 | 
					      yield* this.applyCreatedOperations([model]);
 | 
				
			||||||
  Y.Array.create = function* YArrayCreate(os, model){
 | 
					      return yield* this.createType(model);
 | 
				
			||||||
    var valArray = [];
 | 
					    },
 | 
				
			||||||
    var idArray = yield* Y.Struct.List.map.call(this, model, function(c){
 | 
					    initType: function* YArrayInitializer(os, model){
 | 
				
			||||||
      valArray.push(c.content);
 | 
					      var valArray = [];
 | 
				
			||||||
      return JSON.stringify(c.id);
 | 
					      var idArray = yield* Y.Struct.List.map.call(this, model, function(c){
 | 
				
			||||||
    });
 | 
					        valArray.push(c.content);
 | 
				
			||||||
    return new YArray(os, model.id, idArray, valArray);
 | 
					        return JSON.stringify(c.id);
 | 
				
			||||||
  };
 | 
					      });
 | 
				
			||||||
 | 
					      return new YArray(os, model.id, idArray, valArray);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					  });
 | 
				
			||||||
})();
 | 
					})();
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										123
									
								
								src/Types/Map.js
									
									
									
									
									
								
							
							
						
						
									
										123
									
								
								src/Types/Map.js
									
									
									
									
									
								
							@ -1,92 +1,5 @@
 | 
				
			|||||||
 | 
					 | 
				
			||||||
var GeneratorFunction = (function*(){}).constructor;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
class EventHandler {
 | 
					 | 
				
			||||||
  constructor (onevent) {
 | 
					 | 
				
			||||||
    this.waiting = [];
 | 
					 | 
				
			||||||
    this.awaiting = 0;
 | 
					 | 
				
			||||||
    this.onevent = onevent;
 | 
					 | 
				
			||||||
    this.userEventListeners = [];
 | 
					 | 
				
			||||||
  }
 | 
					 | 
				
			||||||
  receivedOp (op) {
 | 
					 | 
				
			||||||
    if (this.awaiting <= 0) {
 | 
					 | 
				
			||||||
      this.onevent([op]);
 | 
					 | 
				
			||||||
    } else {
 | 
					 | 
				
			||||||
      this.waiting.push(copyObject(op));
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
  }
 | 
					 | 
				
			||||||
  awaitAndPrematurelyCall (ops) {
 | 
					 | 
				
			||||||
    this.awaiting++;
 | 
					 | 
				
			||||||
    this.onevent(ops);
 | 
					 | 
				
			||||||
  }
 | 
					 | 
				
			||||||
  addUserEventListener (f) {
 | 
					 | 
				
			||||||
    this.userEventListeners.push(f);
 | 
					 | 
				
			||||||
  }
 | 
					 | 
				
			||||||
  removeUserEventListener (f) {
 | 
					 | 
				
			||||||
    this.userEventListeners = this.userEventListeners.filter(function(g){
 | 
					 | 
				
			||||||
      return f !== g;
 | 
					 | 
				
			||||||
    });
 | 
					 | 
				
			||||||
  }
 | 
					 | 
				
			||||||
  removeAllUserEventListeners () {
 | 
					 | 
				
			||||||
    this.userEventListeners = [];
 | 
					 | 
				
			||||||
  }
 | 
					 | 
				
			||||||
  callUserEventListeners (event) {
 | 
					 | 
				
			||||||
    for (var i in this.userEventListeners) {
 | 
					 | 
				
			||||||
      try {
 | 
					 | 
				
			||||||
        this.userEventListeners[i](event);
 | 
					 | 
				
			||||||
      } catch (e) {
 | 
					 | 
				
			||||||
        console.log("User events must not throw Errors!");//eslint-disable-line
 | 
					 | 
				
			||||||
      }
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
  }
 | 
					 | 
				
			||||||
  awaitedLastInserts (n) {
 | 
					 | 
				
			||||||
    var ops = this.waiting.splice(this.waiting.length - n);
 | 
					 | 
				
			||||||
    for (var oid = 0; oid < ops.length; oid++) {
 | 
					 | 
				
			||||||
      var op = ops[oid];
 | 
					 | 
				
			||||||
      for (var i = this.waiting.length - 1; i >= 0; i--) {
 | 
					 | 
				
			||||||
        let w = this.waiting[i];
 | 
					 | 
				
			||||||
        if (compareIds(op.left, w.id)) {
 | 
					 | 
				
			||||||
          // include the effect of op in w
 | 
					 | 
				
			||||||
          w.right = op.id;
 | 
					 | 
				
			||||||
          // exclude the effect of w in op
 | 
					 | 
				
			||||||
          op.left = w.left;
 | 
					 | 
				
			||||||
        } else if (compareIds(op.right, w.id)) {
 | 
					 | 
				
			||||||
          // similar..
 | 
					 | 
				
			||||||
          w.left = op.id;
 | 
					 | 
				
			||||||
          op.right = w.right;
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
      }
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
    this.tryCallEvents();
 | 
					 | 
				
			||||||
  }
 | 
					 | 
				
			||||||
  awaitedLastDeletes (n, newLeft) {
 | 
					 | 
				
			||||||
    var ops = this.waiting.splice(this.waiting.length - n);
 | 
					 | 
				
			||||||
    for (var j in ops) {
 | 
					 | 
				
			||||||
      var del = ops[j];
 | 
					 | 
				
			||||||
      if (newLeft != null) {
 | 
					 | 
				
			||||||
        for (var i in this.waiting) {
 | 
					 | 
				
			||||||
          let w = this.waiting[i];
 | 
					 | 
				
			||||||
          // We will just care about w.left
 | 
					 | 
				
			||||||
          if (compareIds(del.target, w.left)) {
 | 
					 | 
				
			||||||
            del.left = newLeft;
 | 
					 | 
				
			||||||
          }
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
      }
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
    this.tryCallEvents();
 | 
					 | 
				
			||||||
  }
 | 
					 | 
				
			||||||
  tryCallEvents () {
 | 
					 | 
				
			||||||
    this.awaiting--;
 | 
					 | 
				
			||||||
    if (this.awaiting <= 0 && this.waiting.length > 0) {
 | 
					 | 
				
			||||||
      var events = this.waiting;
 | 
					 | 
				
			||||||
      this.waiting = [];
 | 
					 | 
				
			||||||
      this.onevent(events);
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
  }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
(function(){
 | 
					(function(){
 | 
				
			||||||
  class Map {
 | 
					  class YMap {
 | 
				
			||||||
    constructor (os, model) {
 | 
					    constructor (os, model) {
 | 
				
			||||||
      this._model = model.id;
 | 
					      this._model = model.id;
 | 
				
			||||||
      this.os = os;
 | 
					      this.os = os;
 | 
				
			||||||
@ -210,10 +123,10 @@ class EventHandler {
 | 
				
			|||||||
        struct: "Insert"
 | 
					        struct: "Insert"
 | 
				
			||||||
      };
 | 
					      };
 | 
				
			||||||
      var def = Promise.defer();
 | 
					      var def = Promise.defer();
 | 
				
			||||||
      if ( value != null && value.constructor === GeneratorFunction) {
 | 
					      if ( value instanceof CustomType) {
 | 
				
			||||||
        // construct a new type
 | 
					        // construct a new type
 | 
				
			||||||
        this.os.requestTransaction(function*(){
 | 
					        this.os.requestTransaction(function*(){
 | 
				
			||||||
          var type = yield* value.call(this);
 | 
					          var type = yield* value.createType.call(this);
 | 
				
			||||||
          insert.opContent = type._model;
 | 
					          insert.opContent = type._model;
 | 
				
			||||||
          insert.id = this.store.getNextOpId();
 | 
					          insert.id = this.store.getNextOpId();
 | 
				
			||||||
          yield* this.applyCreatedOperations([insert]);
 | 
					          yield* this.applyCreatedOperations([insert]);
 | 
				
			||||||
@ -243,18 +156,20 @@ class EventHandler {
 | 
				
			|||||||
      this.eventHandler.receivedOp(op);
 | 
					      this.eventHandler.receivedOp(op);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					  Y.Map = new CustomType({
 | 
				
			||||||
  Y.Map = function* YMap(){
 | 
					    class: YMap,
 | 
				
			||||||
    var model = {
 | 
					    createType: function* YMapCreator(){
 | 
				
			||||||
      map: {},
 | 
					      var model = {
 | 
				
			||||||
      struct: "Map",
 | 
					        map: {},
 | 
				
			||||||
      type: "Map",
 | 
					        struct: "Map",
 | 
				
			||||||
      id: this.store.getNextOpId()
 | 
					        type: "Map",
 | 
				
			||||||
    };
 | 
					        id: this.store.getNextOpId()
 | 
				
			||||||
    yield* this.applyCreatedOperations([model]);
 | 
					      };
 | 
				
			||||||
    return yield* this.createType(model);
 | 
					      yield* this.applyCreatedOperations([model]);
 | 
				
			||||||
  };
 | 
					      return yield* this.createType(model);
 | 
				
			||||||
  Y.Map.create = function* YMapCreate(os, model){
 | 
					    },
 | 
				
			||||||
    return new Map(os, model);
 | 
					    initType: function* YMapInitializer(os, model){
 | 
				
			||||||
  };
 | 
					      return new YMap(os, model);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					  });
 | 
				
			||||||
})();
 | 
					})();
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										0
									
								
								src/Types/TextBind.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										0
									
								
								src/Types/TextBind.js
									
									
									
									
									
										Normal file
									
								
							
							
								
								
									
										100
									
								
								src/Utils.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										100
									
								
								src/Utils.js
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,100 @@
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
 | 
					var GeneratorFunction = (function*(){}).constructor;//eslint-disable-line
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					class EventHandler { //eslint-disable-line
 | 
				
			||||||
 | 
					  constructor (onevent) {
 | 
				
			||||||
 | 
					    this.waiting = [];
 | 
				
			||||||
 | 
					    this.awaiting = 0;
 | 
				
			||||||
 | 
					    this.onevent = onevent;
 | 
				
			||||||
 | 
					    this.userEventListeners = [];
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					  receivedOp (op) {
 | 
				
			||||||
 | 
					    if (this.awaiting <= 0) {
 | 
				
			||||||
 | 
					      this.onevent([op]);
 | 
				
			||||||
 | 
					    } else {
 | 
				
			||||||
 | 
					      this.waiting.push(copyObject(op));
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					  awaitAndPrematurelyCall (ops) {
 | 
				
			||||||
 | 
					    this.awaiting++;
 | 
				
			||||||
 | 
					    this.onevent(ops);
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					  addUserEventListener (f) {
 | 
				
			||||||
 | 
					    this.userEventListeners.push(f);
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					  removeUserEventListener (f) {
 | 
				
			||||||
 | 
					    this.userEventListeners = this.userEventListeners.filter(function(g){
 | 
				
			||||||
 | 
					      return f !== g;
 | 
				
			||||||
 | 
					    });
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					  removeAllUserEventListeners () {
 | 
				
			||||||
 | 
					    this.userEventListeners = [];
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					  callUserEventListeners (event) {
 | 
				
			||||||
 | 
					    for (var i in this.userEventListeners) {
 | 
				
			||||||
 | 
					      try {
 | 
				
			||||||
 | 
					        this.userEventListeners[i](event);
 | 
				
			||||||
 | 
					      } catch (e) {
 | 
				
			||||||
 | 
					        console.log("User events must not throw Errors!");//eslint-disable-line
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					  awaitedLastInserts (n) {
 | 
				
			||||||
 | 
					    var ops = this.waiting.splice(this.waiting.length - n);
 | 
				
			||||||
 | 
					    for (var oid = 0; oid < ops.length; oid++) {
 | 
				
			||||||
 | 
					      var op = ops[oid];
 | 
				
			||||||
 | 
					      for (var i = this.waiting.length - 1; i >= 0; i--) {
 | 
				
			||||||
 | 
					        let w = this.waiting[i];
 | 
				
			||||||
 | 
					        if (compareIds(op.left, w.id)) {
 | 
				
			||||||
 | 
					          // include the effect of op in w
 | 
				
			||||||
 | 
					          w.right = op.id;
 | 
				
			||||||
 | 
					          // exclude the effect of w in op
 | 
				
			||||||
 | 
					          op.left = w.left;
 | 
				
			||||||
 | 
					        } else if (compareIds(op.right, w.id)) {
 | 
				
			||||||
 | 
					          // similar..
 | 
				
			||||||
 | 
					          w.left = op.id;
 | 
				
			||||||
 | 
					          op.right = w.right;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    this.tryCallEvents();
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					  awaitedLastDeletes (n, newLeft) {
 | 
				
			||||||
 | 
					    var ops = this.waiting.splice(this.waiting.length - n);
 | 
				
			||||||
 | 
					    for (var j in ops) {
 | 
				
			||||||
 | 
					      var del = ops[j];
 | 
				
			||||||
 | 
					      if (newLeft != null) {
 | 
				
			||||||
 | 
					        for (var i in this.waiting) {
 | 
				
			||||||
 | 
					          let w = this.waiting[i];
 | 
				
			||||||
 | 
					          // We will just care about w.left
 | 
				
			||||||
 | 
					          if (compareIds(del.target, w.left)) {
 | 
				
			||||||
 | 
					            del.left = newLeft;
 | 
				
			||||||
 | 
					          }
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    this.tryCallEvents();
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					  tryCallEvents () {
 | 
				
			||||||
 | 
					    this.awaiting--;
 | 
				
			||||||
 | 
					    if (this.awaiting <= 0 && this.waiting.length > 0) {
 | 
				
			||||||
 | 
					      var events = this.waiting;
 | 
				
			||||||
 | 
					      this.waiting = [];
 | 
				
			||||||
 | 
					      this.onevent(events);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					class CustomType { //eslint-disable-line
 | 
				
			||||||
 | 
					  constructor (def) {
 | 
				
			||||||
 | 
					    if (def.createType == null
 | 
				
			||||||
 | 
					      || def.initType == null
 | 
				
			||||||
 | 
					      || def.class == null
 | 
				
			||||||
 | 
					    ) {
 | 
				
			||||||
 | 
					      throw new Error("Custom type was not initialized correctly!");
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    this.createType = def.createType;
 | 
				
			||||||
 | 
					    this.initType = def.initType;
 | 
				
			||||||
 | 
					    this.class = def.class;
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user