awv3
Version:
⚡ AWV3 embedded CAD
40 lines (34 loc) • 1.28 kB
JavaScript
/*
* 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 =
/*#__PURE__*/
function () {
function MultiRunner(commandRunner) {
this.commandRunner = commandRunner;
this.commands = [];
this.resolves = [];
}
var _proto = MultiRunner.prototype;
_proto.run = function run(command) {
var _this = this;
if (this.commands.push(command) === 1) window.setTimeout(resolveCommands, 0, this.commandRunner, this.commands, this.resolves);
return new Promise(function (resolve) {
return _this.resolves.push(resolve);
});
};
return MultiRunner;
}();
export { MultiRunner as default };
function resolveCommands(commandRunner, commands, resolves) {
commandRunner(commands).forEach(function (result, idx) {
return resolves[idx](result);
});
commands.splice(0);
resolves.splice(0);
}