there are some cases that may lead to inconsistencies. Currently, only the master-slave method is a reliable sync method
120 lines
3.0 KiB
JavaScript
120 lines
3.0 KiB
JavaScript
var Engine;
|
|
|
|
if (typeof window !== "undefined" && window !== null) {
|
|
window.unprocessed_counter = 0;
|
|
}
|
|
|
|
if (typeof window !== "undefined" && window !== null) {
|
|
window.unprocessed_exec_counter = 0;
|
|
}
|
|
|
|
if (typeof window !== "undefined" && window !== null) {
|
|
window.unprocessed_types = [];
|
|
}
|
|
|
|
Engine = (function() {
|
|
function Engine(HB, types) {
|
|
this.HB = HB;
|
|
this.types = types;
|
|
this.unprocessed_ops = [];
|
|
}
|
|
|
|
Engine.prototype.parseOperation = function(json) {
|
|
var type;
|
|
type = this.types[json.type];
|
|
if ((type != null ? type.parse : void 0) != null) {
|
|
return type.parse(json);
|
|
} else {
|
|
throw new Error("You forgot to specify a parser for type " + json.type + ". The message is " + (JSON.stringify(json)) + ".");
|
|
}
|
|
};
|
|
|
|
|
|
/*
|
|
applyOpsBundle: (ops_json)->
|
|
ops = []
|
|
for o in ops_json
|
|
ops.push @parseOperation o
|
|
for o in ops
|
|
if not o.execute()
|
|
@unprocessed_ops.push o
|
|
@tryUnprocessed()
|
|
*/
|
|
|
|
Engine.prototype.applyOpsCheckDouble = function(ops_json) {
|
|
var o, _i, _len, _results;
|
|
_results = [];
|
|
for (_i = 0, _len = ops_json.length; _i < _len; _i++) {
|
|
o = ops_json[_i];
|
|
if (this.HB.getOperation(o.uid) == null) {
|
|
_results.push(this.applyOp(o));
|
|
} else {
|
|
_results.push(void 0);
|
|
}
|
|
}
|
|
return _results;
|
|
};
|
|
|
|
Engine.prototype.applyOps = function(ops_json) {
|
|
return this.applyOp(ops_json);
|
|
};
|
|
|
|
Engine.prototype.applyOp = function(op_json_array, fromHB) {
|
|
var o, op_json, _i, _len;
|
|
if (fromHB == null) {
|
|
fromHB = false;
|
|
}
|
|
if (op_json_array.constructor !== Array) {
|
|
op_json_array = [op_json_array];
|
|
}
|
|
for (_i = 0, _len = op_json_array.length; _i < _len; _i++) {
|
|
op_json = op_json_array[_i];
|
|
if (fromHB) {
|
|
op_json.fromHB = "true";
|
|
}
|
|
o = this.parseOperation(op_json);
|
|
if (op_json.fromHB != null) {
|
|
o.fromHB = op_json.fromHB;
|
|
}
|
|
if (this.HB.getOperation(o) != null) {
|
|
|
|
} else if (((!this.HB.isExpectedOperation(o)) && (o.fromHB == null)) || (!o.execute())) {
|
|
this.unprocessed_ops.push(o);
|
|
if (typeof window !== "undefined" && window !== null) {
|
|
window.unprocessed_types.push(o.type);
|
|
}
|
|
}
|
|
}
|
|
return this.tryUnprocessed();
|
|
};
|
|
|
|
Engine.prototype.tryUnprocessed = function() {
|
|
var old_length, op, unprocessed, _i, _len, _ref;
|
|
while (true) {
|
|
old_length = this.unprocessed_ops.length;
|
|
unprocessed = [];
|
|
_ref = this.unprocessed_ops;
|
|
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
|
|
op = _ref[_i];
|
|
if (this.HB.getOperation(op) != null) {
|
|
|
|
} else if ((!this.HB.isExpectedOperation(op) && (op.fromHB == null)) || (!op.execute())) {
|
|
unprocessed.push(op);
|
|
}
|
|
}
|
|
this.unprocessed_ops = unprocessed;
|
|
if (this.unprocessed_ops.length === old_length) {
|
|
break;
|
|
}
|
|
}
|
|
if (this.unprocessed_ops.length !== 0) {
|
|
return this.HB.invokeSync();
|
|
}
|
|
};
|
|
|
|
return Engine;
|
|
|
|
})();
|
|
|
|
module.exports = Engine;
|