awv3
Version:
⚡ AWV3 embedded CAD
62 lines (47 loc) • 2.02 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = undefined;
var _promise = require("babel-runtime/core-js/promise");
var _promise2 = _interopRequireDefault(_promise);
var _classCallCheck2 = require("babel-runtime/helpers/classCallCheck");
var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);
var _createClass2 = require("babel-runtime/helpers/createClass");
var _createClass3 = _interopRequireDefault(_createClass2);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
/*
* Combine multiple calls to run (e.g. session.request) into minimal number of group calls.
* All calls to run will be remembered and executed at once at the next macrotask
* (i.e. after all already resolved promises/async functions are executed).
* commandRunner must be a function that accepts an array of arguments (commands)
* and returns an array of results (possibly promises) of the same length.
* It's intented to use a wrapper around session.execute as a commandRunner.
*/
var MultiRunner = function () {
function MultiRunner(commandRunner) {
(0, _classCallCheck3.default)(this, MultiRunner);
this.commandRunner = commandRunner;
this.commands = [];
this.resolves = [];
}
(0, _createClass3.default)(MultiRunner, [{
key: "run",
value: function run(command) {
var _this = this;
if (this.commands.push(command) === 1) window.setTimeout(resolveCommands, 0, this.commandRunner, this.commands, this.resolves);
return new _promise2.default(function (resolve) {
return _this.resolves.push(resolve);
});
}
}]);
return MultiRunner;
}();
exports.default = MultiRunner;
function resolveCommands(commandRunner, commands, resolves) {
commandRunner(commands).forEach(function (result, idx) {
return resolves[idx](result);
});
commands.splice(0);
resolves.splice(0);
}