'a'
mh-two-thousand-and-two
2024-04-12 44d2c92345cd156a59fc327b3060292a282d2893
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
"use strict";
const EventEmitter = require("events");
const util_1 = require("util");
const adbCommander = require("adb-commander");
const debugAdb = util_1.debuglog('adb');
class ADBDevicesEmitter extends EventEmitter {
    constructor() {
        super(...arguments);
        this._stopFlag = false;
        this._builtinEvents = ['deviceAdded', 'deviceRemoved'];
        this._snList = [];
    }
    addEventListener(eventType, listener) {
        debugAdb(`addEventListener(): start for ${eventType}`);
        const idx = this._builtinEvents.indexOf(eventType);
        if (idx >= 0) {
            this.on(eventType, listener);
        }
    }
    start() {
        debugAdb(`start(): start`);
        this._stopFlag = false;
        this._execPolling(async (resolve) => {
            debugAdb(`execPolling(): callback start`);
            const { deviceList, err } = await adbCommander.deviceList();
            debugAdb(`execPolling():\nfound deviceList: ${JSON.stringify(deviceList)},\n_snList: ${JSON.stringify(this._snList)}`);
            if (err) {
                console.error(`adb-commander: adb获取手机设备失败`, JSON.stringify(err));
                return resolve(true);
            }
            const deviceListState = this._detectDeviceListChange(deviceList);
            debugAdb(`execPolling():\nnewlyAdded devices: ${JSON.stringify(deviceListState.newlyAdded)}\nremoved devices: ${JSON.stringify(deviceListState.removed)}`);
            deviceListState.newlyAdded.forEach(sn => {
                this.emit('deviceAdded', { sn });
            });
            deviceListState.removed.forEach(sn => {
                this.emit('deviceRemoved', { sn });
            });
            debugAdb(`execPolling(): go to next polling`);
            return resolve(true);
        });
    }
    stop() {
        this._stopFlag = true;
    }
    _detectDeviceListChange(currentList) {
        const result = {
            newlyAdded: [],
            removed: [],
        };
        result.newlyAdded = currentList.filter(item => {
            return this._snList.indexOf(item) < 0;
        });
        result.removed = this._snList.filter(item => {
            return currentList.indexOf(item) < 0;
        });
        this._snList = currentList;
        return result;
    }
    _execPolling(callback) {
        const delay = 5e3;
        const polling = () => {
            new Promise((res, rej) => {
                debugAdb(`execPolling(): promise def start`);
                callback.apply(this, [res, rej]);
            })
                .then(next => {
                next &&
                    !this._stopFlag &&
                    setTimeout(() => {
                        debugAdb(`execPolling(): start polling again`);
                        polling();
                    }, delay);
            })
                .catch(err => {
                console.error(`adb-commander: 退出轮询: ${err.message}`);
            });
        };
        polling();
    }
}
const adbDevicesEmitter = new ADBDevicesEmitter();
module.exports = adbDevicesEmitter;