109 lines
3.1 KiB
JavaScript
109 lines
3.1 KiB
JavaScript
(function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);throw new Error("Cannot find module '"+o+"'")}var f=n[o]={exports:{}};t[o][0].call(f.exports,function(e){var n=t[o][1][e];return s(n?n:e)},f,f.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
|
|
var Engine;
|
|
|
|
Engine = (function() {
|
|
function Engine(HB, parser) {
|
|
this.HB = HB;
|
|
this.parser = parser;
|
|
this.unprocessed_ops = [];
|
|
}
|
|
|
|
Engine.prototype.parseOperation = function(json) {
|
|
var typeParser;
|
|
typeParser = this.parser[json.type];
|
|
if (typeParser != null) {
|
|
return typeParser(json);
|
|
} else {
|
|
throw new Error("You forgot to specify a parser for type " + json.type + ". The message is " + (JSON.stringify(json)) + ".");
|
|
}
|
|
};
|
|
|
|
Engine.prototype.applyOpsBundle = function(ops_json) {
|
|
var o, ops, _i, _j, _k, _len, _len1, _len2;
|
|
ops = [];
|
|
for (_i = 0, _len = ops_json.length; _i < _len; _i++) {
|
|
o = ops_json[_i];
|
|
ops.push(this.parseOperation(o));
|
|
}
|
|
for (_j = 0, _len1 = ops.length; _j < _len1; _j++) {
|
|
o = ops[_j];
|
|
this.HB.addOperation(o);
|
|
}
|
|
for (_k = 0, _len2 = ops.length; _k < _len2; _k++) {
|
|
o = ops[_k];
|
|
if (!o.execute()) {
|
|
this.unprocessed_ops.push(o);
|
|
}
|
|
}
|
|
return this.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) {
|
|
var o, _i, _len, _results;
|
|
_results = [];
|
|
for (_i = 0, _len = ops_json.length; _i < _len; _i++) {
|
|
o = ops_json[_i];
|
|
_results.push(this.applyOp(o));
|
|
}
|
|
return _results;
|
|
};
|
|
|
|
Engine.prototype.applyOp = function(op_json) {
|
|
var o;
|
|
o = this.parseOperation(op_json);
|
|
this.HB.addToCounter(o);
|
|
if (!o.execute()) {
|
|
this.unprocessed_ops.push(o);
|
|
} else {
|
|
this.HB.addOperation(o);
|
|
}
|
|
return this.tryUnprocessed();
|
|
};
|
|
|
|
Engine.prototype.tryUnprocessed = function() {
|
|
var old_length, op, unprocessed, _i, _len, _ref, _results;
|
|
_results = [];
|
|
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 (!op.execute()) {
|
|
unprocessed.push(op);
|
|
} else {
|
|
this.HB.addOperation(op);
|
|
}
|
|
}
|
|
this.unprocessed_ops = unprocessed;
|
|
if (this.unprocessed_ops.length === old_length) {
|
|
break;
|
|
} else {
|
|
_results.push(void 0);
|
|
}
|
|
}
|
|
return _results;
|
|
};
|
|
|
|
return Engine;
|
|
|
|
})();
|
|
|
|
module.exports = Engine;
|
|
|
|
|
|
},{}]},{},[1]) |