UNPKG

@u4/adbkit

Version:

A Typescript client for the Android Debug Bridge.

68 lines 2.27 kB
/* eslint-disable @typescript-eslint/no-explicit-any */ import EventEmitter from 'node:events'; import { AdbPrematureEOFError } from './errors.js'; export default class JdwpTracker extends EventEmitter { // 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 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; } } //# sourceMappingURL=jdwptracker.js.map