@u4/adbkit
Version:
A Typescript client for the Android Debug Bridge.
74 lines • 2.51 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 */
const events_1 = __importDefault(require("events"));
const errors_1 = require("./errors");
class JdwpTracker extends events_1.default {
// private reader: Promise<JdwpTracker | boolean>;
constructor(command) {
super();
this.command = command;
this.pids = [];
this.pidMap = Object.create(null);
this.on = (event, listener) => super.on(event, listener);
this.off = (event, listener) => super.off(event, listener);
this.once = (event, listener) => super.once(event, listener);
this.emit = (event, ...args) => super.emit(event, ...args);
this.command = command;
this.pids = [];
this.pidMap = Object.create(null);
// this.reader =
this.read().catch(err => {
if (err instanceof errors_1.AdbPrematureEOFError) {
return this.emit('end');
}
this.command.connection.end();
this.emit('error', err);
return this.emit('end');
});
}
async read() {
const list = await this.command.parser.readValue('utf8');
const pids = list.split('\n');
const maybeEmpty = pids.pop();
if (maybeEmpty) {
pids.push(maybeEmpty);
}
return this.update(pids);
}
update(newList) {
const changeSet = {
removed: [],
added: [],
};
const newMap = Object.create(null);
for (let i = 0, len = newList.length; i < len; i++) {
const pid = newList[i];
if (!this.pidMap[pid]) {
changeSet.added.push(pid);
this.emit('add', pid);
newMap[pid] = pid;
}
}
const ref = this.pids;
for (let j = 0, len1 = ref.length; j < len1; j++) {
const pid = ref[j];
if (!newMap[pid]) {
changeSet.removed.push(pid);
this.emit('remove', pid);
}
}
this.pids = newList;
this.pidMap = newMap;
this.emit('changeSet', changeSet, newList);
return this;
}
end() {
return this;
}
}
exports.default = JdwpTracker;
//# sourceMappingURL=jdwptracker.js.map