UNPKG

@u4/adbkit-monkey

Version:

A Node.js interface to the Android monkey tool.

88 lines (87 loc) 2.92 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); /* * 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")); const queue_1 = __importDefault(require("./queue")); const multi_1 = __importDefault(require("./multi")); const parser_1 = __importDefault(require("./parser")); const stream_1 = require("stream"); class Client extends api_1.default { constructor() { super(); this.commandQueue = new queue_1.default(); this.parser = new parser_1.default(); this.stream = null; } _hook() { this.stream.on('data', (data) => { return this.parser.parse(data); }); this.stream.on('error', (err) => { return this.emit('error', err); }); this.stream.on('end', () => { return this.emit('end'); }); this.stream.on('finish', () => { return this.emit('finish'); }); this.parser.on('reply', (reply) => { return this._consume(reply); }); this.parser.on('error', (err) => { return this.emit('error', err); }); } _consume(reply) { let command; if ((command = this.commandQueue.dequeue())) { if (reply.isError()) { command.callback(reply.toError(), null, command.command); } else { command.callback(null, reply.value, command.command); } } else { throw new Error("Command queue depleted, but replies still coming in"); } } connect(stream) { if (stream instanceof stream_1.Duplex) this.stream = stream; this._hook(); return this; } end() { this.stream.end(); return this; } send(commands, callback) { if (Array.isArray(commands)) { for (const command of Array.from(commands)) { this.commandQueue.enqueue(new command_1.default(command, callback)); } this.stream.write(`${commands.join('\n')}\n`); } else { this.commandQueue.enqueue(new command_1.default(commands, callback)); this.stream.write(`${commands}\n`); } return this; } multi() { return new multi_1.default(this); } } exports.default = Client;