hdckit
Version:
A pure Node.js client for the OpenHarmony Device Connector
59 lines (58 loc) • 1.93 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const Emitter_1 = __importDefault(require("licia/Emitter"));
const util_1 = require("./util");
const contain_1 = __importDefault(require("licia/contain"));
class Tracker extends Emitter_1.default {
constructor(command) {
super();
this.command = command;
this.targetList = [];
this.ended = false;
this.read();
const socket = this.command.connection.socket;
socket.on('end', () => {
if (!this.ended) {
this.ended = true;
this.emit('error', new Error('Connection closed'));
}
});
socket.on('error', (err) => this.emit('error', err));
}
end() {
this.ended = true;
this.command.connection.end();
}
async read() {
if (this.ended) {
return;
}
this.command.send('list targets');
const data = await this.command.connection.readValue();
const targets = (0, util_1.readTargets)(data.toString());
this.update(targets);
setTimeout(() => this.read(), 1000);
}
update(newList) {
for (let i = 0, len = newList.length; i < len; i++) {
const target = newList[i];
if ((0, contain_1.default)(this.targetList, target)) {
continue;
}
else {
this.emit('add', target);
}
}
for (let i = 0, len = this.targetList.length; i < len; i++) {
const target = this.targetList[i];
if (!(0, contain_1.default)(newList, target)) {
this.emit('remove', target);
}
}
this.targetList = newList;
}
}
exports.default = Tracker;