awv3
Version:
⚡ AWV3 embedded CAD
28 lines (25 loc) • 1.1 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.
*/
export default class MultiRunner {
constructor(commandRunner) {
this.commandRunner = commandRunner;
this.commands = [];
this.resolves = [];
}
run(command) {
if (this.commands.push(command) === 1)
window.setTimeout(resolveCommands, 0, this.commandRunner, this.commands, this.resolves);
return new Promise(resolve => this.resolves.push(resolve));
}
}
function resolveCommands(commandRunner, commands, resolves) {
commandRunner(commands).forEach((result, idx) => resolves[idx](result));
commands.splice(0);
resolves.splice(0);
}