@u4/adbkit-monkey
Version:
A Node.js interface to the Android monkey tool.
77 lines (76 loc) • 2.48 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
/* eslint-disable @typescript-eslint/no-explicit-any */
/*
* decaffeinate suggestions:
* DS002: Fix invalid constructor
* DS101: Remove unnecessary use of Array.from
* DS102: Remove unnecessary code created because of implicit returns
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/
const api_1 = __importDefault(require("./api"));
const command_1 = __importDefault(require("./command"));
class Multi extends api_1.default {
constructor(monkey) {
super();
this.monkey = monkey;
this.commands = [];
this.replies = [];
this.errors = [];
this.sent = false;
this.callback = null;
this.counter = 0;
this.collector = (err, result, cmd) => {
if (err) {
this.errors.push(`${cmd}: ${err.message}`);
}
this.replies.push(result);
this.counter -= 1;
return this._maybeFinish();
};
}
_maybeFinish() {
if (this.counter === 0) {
if (this.errors.length) {
setImmediate(() => {
return this.callback(new Error(this.errors.join(', ')));
});
}
else {
setImmediate(() => {
return this.callback(null, this.replies);
});
}
}
}
_forbidReuse() {
if (this.sent) {
throw new Error("Reuse not supported");
}
}
send(command) {
this._forbidReuse();
this.commands.push(new command_1.default(command, this.collector));
}
execute(callback) {
this._forbidReuse();
this.counter = this.commands.length;
this.sent = true;
this.callback = callback;
if (this.counter === 0) {
return;
}
const parts = [];
for (const command of Array.from(this.commands)) {
this.monkey.commandQueue.enqueue(command);
parts.push(command.command);
}
parts.push('');
this.commands = [];
this.monkey.stream.write(parts.join('\n'));
}
}
exports.default = Multi;