@yume-chan/adb 0.0.15 → 0.0.17
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.json +33 -0
- package/CHANGELOG.md +18 -1
- package/LICENSE +21 -21
- package/README.md +247 -247
- package/esm/adb.d.ts +15 -3
- package/esm/adb.d.ts.map +1 -1
- package/esm/adb.js +18 -6
- package/esm/adb.js.map +1 -1
- package/esm/auth.d.ts.map +1 -1
- package/esm/auth.js.map +1 -1
- package/esm/backend.d.ts +2 -2
- package/esm/backend.d.ts.map +1 -1
- package/esm/commands/base.d.ts.map +1 -1
- package/esm/commands/base.js.map +1 -1
- package/esm/commands/framebuffer.d.ts +17 -1
- package/esm/commands/framebuffer.d.ts.map +1 -1
- package/esm/commands/framebuffer.js +4 -15
- package/esm/commands/framebuffer.js.map +1 -1
- package/esm/commands/install.d.ts +2 -2
- package/esm/commands/install.d.ts.map +1 -1
- package/esm/commands/install.js +3 -2
- package/esm/commands/install.js.map +1 -1
- package/esm/commands/power.d.ts +1 -1
- package/esm/commands/power.js +1 -1
- package/esm/commands/reverse.d.ts +3 -3
- package/esm/commands/reverse.d.ts.map +1 -1
- package/esm/commands/reverse.js +9 -9
- package/esm/commands/reverse.js.map +1 -1
- package/esm/commands/subprocess/command.d.ts +52 -0
- package/esm/commands/subprocess/command.d.ts.map +1 -0
- package/esm/commands/subprocess/command.js +83 -0
- package/esm/commands/subprocess/command.js.map +1 -0
- package/esm/commands/subprocess/index.d.ts +1 -53
- package/esm/commands/subprocess/index.d.ts.map +1 -1
- package/esm/commands/subprocess/index.js +1 -85
- package/esm/commands/subprocess/index.js.map +1 -1
- package/esm/commands/subprocess/protocols/none.d.ts +5 -4
- package/esm/commands/subprocess/protocols/none.d.ts.map +1 -1
- package/esm/commands/subprocess/protocols/none.js +11 -8
- package/esm/commands/subprocess/protocols/none.js.map +1 -1
- package/esm/commands/subprocess/protocols/shell.d.ts +4 -4
- package/esm/commands/subprocess/protocols/shell.d.ts.map +1 -1
- package/esm/commands/subprocess/protocols/shell.js +6 -6
- package/esm/commands/subprocess/protocols/shell.js.map +1 -1
- package/esm/commands/subprocess/protocols/types.d.ts +7 -7
- package/esm/commands/subprocess/protocols/types.d.ts.map +1 -1
- package/esm/commands/sync/index.d.ts +1 -1
- package/esm/commands/sync/index.d.ts.map +1 -1
- package/esm/commands/sync/index.js +1 -1
- package/esm/commands/sync/index.js.map +1 -1
- package/esm/commands/sync/list.d.ts +2 -2
- package/esm/commands/sync/list.d.ts.map +1 -1
- package/esm/commands/sync/list.js +15 -43
- package/esm/commands/sync/list.js.map +1 -1
- package/esm/commands/sync/pull.d.ts +3 -2
- package/esm/commands/sync/pull.d.ts.map +1 -1
- package/esm/commands/sync/pull.js +15 -17
- package/esm/commands/sync/pull.js.map +1 -1
- package/esm/commands/sync/push.d.ts +2 -2
- package/esm/commands/sync/push.d.ts.map +1 -1
- package/esm/commands/sync/push.js +2 -5
- package/esm/commands/sync/push.js.map +1 -1
- package/esm/commands/sync/request.d.ts +1 -1
- package/esm/commands/sync/request.d.ts.map +1 -1
- package/esm/commands/sync/request.js +1 -1
- package/esm/commands/sync/request.js.map +1 -1
- package/esm/commands/sync/response.d.ts +4 -9
- package/esm/commands/sync/response.d.ts.map +1 -1
- package/esm/commands/sync/response.js +32 -23
- package/esm/commands/sync/response.js.map +1 -1
- package/esm/commands/sync/stat.d.ts +3 -3
- package/esm/commands/sync/stat.d.ts.map +1 -1
- package/esm/commands/sync/stat.js +13 -30
- package/esm/commands/sync/stat.js.map +1 -1
- package/esm/commands/sync/sync.d.ts +2 -2
- package/esm/commands/sync/sync.d.ts.map +1 -1
- package/esm/commands/sync/sync.js +7 -5
- package/esm/commands/sync/sync.js.map +1 -1
- package/esm/crypto.js +1 -1
- package/esm/crypto.js.map +1 -1
- package/esm/index.d.ts +0 -1
- package/esm/index.d.ts.map +1 -1
- package/esm/index.js +0 -1
- package/esm/index.js.map +1 -1
- package/esm/packet.d.ts +1 -1
- package/esm/packet.d.ts.map +1 -1
- package/esm/packet.js +1 -1
- package/esm/packet.js.map +1 -1
- package/esm/socket/dispatcher.d.ts +11 -3
- package/esm/socket/dispatcher.d.ts.map +1 -1
- package/esm/socket/dispatcher.js +19 -11
- package/esm/socket/dispatcher.js.map +1 -1
- package/esm/socket/index.d.ts +1 -1
- package/esm/socket/index.d.ts.map +1 -1
- package/esm/socket/index.js +1 -1
- package/esm/socket/index.js.map +1 -1
- package/esm/socket/socket.d.ts +9 -4
- package/esm/socket/socket.d.ts.map +1 -1
- package/esm/socket/socket.js +20 -10
- package/esm/socket/socket.js.map +1 -1
- package/esm/utils/base64.d.ts +23 -0
- package/esm/utils/base64.d.ts.map +1 -1
- package/esm/utils/base64.js +6 -0
- package/esm/utils/base64.js.map +1 -1
- package/package.json +13 -14
- package/src/adb.ts +285 -273
- package/src/auth.ts +174 -173
- package/src/backend.ts +12 -11
- package/src/commands/base.ts +12 -11
- package/src/commands/framebuffer.ts +78 -73
- package/src/commands/index.ts +8 -8
- package/src/commands/install.ts +32 -30
- package/src/commands/power.ts +54 -54
- package/src/commands/reverse.ts +141 -140
- package/src/commands/subprocess/command.ts +131 -0
- package/src/commands/subprocess/index.ts +3 -139
- package/src/commands/subprocess/protocols/none.ts +73 -68
- package/src/commands/subprocess/protocols/shell.ts +191 -190
- package/src/commands/subprocess/protocols/types.ts +60 -59
- package/src/commands/subprocess/utils.ts +20 -20
- package/src/commands/sync/index.ts +7 -7
- package/src/commands/sync/list.ts +54 -86
- package/src/commands/sync/pull.ts +45 -46
- package/src/commands/sync/push.ts +38 -41
- package/src/commands/sync/request.ts +51 -50
- package/src/commands/sync/response.ts +68 -64
- package/src/commands/sync/stat.ts +129 -150
- package/src/commands/sync/sync.ts +182 -181
- package/src/commands/tcpip.ts +21 -21
- package/src/crypto.ts +296 -296
- package/src/features.ts +9 -9
- package/src/index.ts +10 -11
- package/src/packet.ts +80 -80
- package/src/socket/dispatcher.ts +310 -304
- package/src/socket/index.ts +2 -2
- package/src/socket/socket.ts +156 -144
- package/src/utils/auto-reset-event.ts +41 -41
- package/src/utils/base64.ts +329 -306
- package/src/utils/index.ts +3 -3
- package/tsconfig.build.json +3 -3
- package/tsconfig.build.tsbuildinfo +1 -1
- package/esm/commands/subprocess/legacy.d.ts +0 -35
- package/esm/commands/subprocess/legacy.d.ts.map +0 -1
- package/esm/commands/subprocess/legacy.js +0 -53
- package/esm/commands/subprocess/legacy.js.map +0 -1
- package/esm/commands/subprocess/protocol.d.ts +0 -39
- package/esm/commands/subprocess/protocol.d.ts.map +0 -1
- package/esm/commands/subprocess/protocol.js +0 -157
- package/esm/commands/subprocess/protocol.js.map +0 -1
- package/esm/commands/subprocess/types.d.ts +0 -50
- package/esm/commands/subprocess/types.d.ts.map +0 -1
- package/esm/commands/subprocess/types.js +0 -2
- package/esm/commands/subprocess/types.js.map +0 -1
- package/esm/socket/controller.d.ts +0 -37
- package/esm/socket/controller.d.ts.map +0 -1
- package/esm/socket/controller.js +0 -66
- package/esm/socket/controller.js.map +0 -1
- package/esm/stream/buffered.d.ts +0 -35
- package/esm/stream/buffered.d.ts.map +0 -1
- package/esm/stream/buffered.js +0 -123
- package/esm/stream/buffered.js.map +0 -1
- package/esm/stream/detect.bak.d.ts +0 -240
- package/esm/stream/detect.bak.d.ts.map +0 -1
- package/esm/stream/detect.bak.js +0 -60
- package/esm/stream/detect.bak.js.map +0 -1
- package/esm/stream/detect.d.ts +0 -2
- package/esm/stream/detect.d.ts.map +0 -1
- package/esm/stream/detect.js +0 -5
- package/esm/stream/detect.js.map +0 -1
- package/esm/stream/detect.native.d.ts +0 -240
- package/esm/stream/detect.native.d.ts.map +0 -1
- package/esm/stream/detect.native.js +0 -60
- package/esm/stream/detect.native.js.map +0 -1
- package/esm/stream/detect.polyfill.d.ts +0 -18
- package/esm/stream/detect.polyfill.d.ts.map +0 -1
- package/esm/stream/detect.polyfill.js +0 -9
- package/esm/stream/detect.polyfill.js.map +0 -1
- package/esm/stream/index.d.ts +0 -4
- package/esm/stream/index.d.ts.map +0 -1
- package/esm/stream/index.js +0 -4
- package/esm/stream/index.js.map +0 -1
- package/esm/stream/transform.d.ts +0 -104
- package/esm/stream/transform.d.ts.map +0 -1
- package/esm/stream/transform.js +0 -373
- package/esm/stream/transform.js.map +0 -1
- package/esm/utils/encoding.d.ts +0 -3
- package/esm/utils/encoding.d.ts.map +0 -1
- package/esm/utils/encoding.js +0 -11
- package/esm/utils/encoding.js.map +0 -1
- package/src/stream/buffered.ts +0 -147
- package/src/stream/detect.native.ts +0 -362
- package/src/stream/detect.polyfill.ts +0 -28
- package/src/stream/detect.ts +0 -5
- package/src/stream/index.ts +0 -3
- package/src/stream/transform.ts +0 -447
- package/tsconfig.test.json +0 -10
package/src/socket/socket.ts
CHANGED
|
@@ -1,144 +1,156 @@
|
|
|
1
|
-
import { PromiseResolver } from
|
|
2
|
-
import type { Disposable } from
|
|
3
|
-
import {
|
|
4
|
-
|
|
5
|
-
import
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
public readonly
|
|
26
|
-
public readonly
|
|
27
|
-
public readonly
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
private
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
this
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
this.
|
|
73
|
-
},
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
);
|
|
91
|
-
await this.
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
this.
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
this.
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
public
|
|
142
|
-
|
|
143
|
-
}
|
|
144
|
-
}
|
|
1
|
+
import { PromiseResolver } from '@yume-chan/async';
|
|
2
|
+
import type { Disposable } from '@yume-chan/event';
|
|
3
|
+
import { ChunkStream, DuplexStreamFactory, pipeFrom, PushReadableStream, WritableStream, type PushReadableStreamController, type ReadableStream, type ReadableWritablePair } from '@yume-chan/stream-extra';
|
|
4
|
+
|
|
5
|
+
import { AdbCommand } from '../packet.js';
|
|
6
|
+
import type { AdbPacketDispatcher, Closeable } from './dispatcher.js';
|
|
7
|
+
|
|
8
|
+
export interface AdbSocketInfo {
|
|
9
|
+
localId: number;
|
|
10
|
+
remoteId: number;
|
|
11
|
+
|
|
12
|
+
localCreated: boolean;
|
|
13
|
+
serviceString: string;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export interface AdbSocketConstructionOptions extends AdbSocketInfo {
|
|
17
|
+
dispatcher: AdbPacketDispatcher;
|
|
18
|
+
|
|
19
|
+
highWaterMark?: number | undefined;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export class AdbSocketController implements AdbSocketInfo, ReadableWritablePair<Uint8Array, Uint8Array>, Closeable, Disposable {
|
|
23
|
+
private readonly dispatcher!: AdbPacketDispatcher;
|
|
24
|
+
|
|
25
|
+
public readonly localId!: number;
|
|
26
|
+
public readonly remoteId!: number;
|
|
27
|
+
public readonly localCreated!: boolean;
|
|
28
|
+
public readonly serviceString!: string;
|
|
29
|
+
|
|
30
|
+
private _duplex: DuplexStreamFactory<Uint8Array, Uint8Array>;
|
|
31
|
+
|
|
32
|
+
private _readable: ReadableStream<Uint8Array>;
|
|
33
|
+
private _readableController!: PushReadableStreamController<Uint8Array>;
|
|
34
|
+
public get readable() { return this._readable; }
|
|
35
|
+
|
|
36
|
+
private _writePromise: PromiseResolver<void> | undefined;
|
|
37
|
+
public readonly writable: WritableStream<Uint8Array>;
|
|
38
|
+
|
|
39
|
+
private _closed = false;
|
|
40
|
+
/**
|
|
41
|
+
* Whether the socket is half-closed (i.e. the local side initiated the close).
|
|
42
|
+
*
|
|
43
|
+
* It's only used by dispatcher to avoid sending another `CLSE` packet to remote.
|
|
44
|
+
*/
|
|
45
|
+
public get closed() { return this._closed; }
|
|
46
|
+
|
|
47
|
+
private _socket: AdbSocket;
|
|
48
|
+
public get socket() { return this._socket; }
|
|
49
|
+
|
|
50
|
+
public constructor(options: AdbSocketConstructionOptions) {
|
|
51
|
+
Object.assign(this, options);
|
|
52
|
+
|
|
53
|
+
// Check this image to help you understand the stream graph
|
|
54
|
+
// cspell: disable-next-line
|
|
55
|
+
// https://www.plantuml.com/plantuml/png/TL0zoeGm4ErpYc3l5JxyS0yWM6mX5j4C6p4cxcJ25ejttuGX88ZftizxUKmJI275pGhXl0PP_UkfK_CAz5Z2hcWsW9Ny2fdU4C1f5aSchFVxA8vJjlTPRhqZzDQMRB7AklwJ0xXtX0ZSKH1h24ghoKAdGY23FhxC4nS2pDvxzIvxb-8THU0XlEQJ-ZB7SnXTAvc_LhOckhMdLBnbtndpb-SB7a8q2SRD_W00
|
|
56
|
+
|
|
57
|
+
this._duplex = new DuplexStreamFactory<Uint8Array, Uint8Array>({
|
|
58
|
+
close: async () => {
|
|
59
|
+
this._closed = true;
|
|
60
|
+
|
|
61
|
+
await this.dispatcher.sendPacket(
|
|
62
|
+
AdbCommand.Close,
|
|
63
|
+
this.localId,
|
|
64
|
+
this.remoteId
|
|
65
|
+
);
|
|
66
|
+
|
|
67
|
+
// Don't `dispose` here, we need to wait for `CLSE` response packet.
|
|
68
|
+
return false;
|
|
69
|
+
},
|
|
70
|
+
dispose: () => {
|
|
71
|
+
// Error out the pending writes
|
|
72
|
+
this._writePromise?.reject(new Error('Socket closed'));
|
|
73
|
+
},
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
this._readable = this._duplex.wrapReadable(
|
|
77
|
+
new PushReadableStream(controller => {
|
|
78
|
+
this._readableController = controller;
|
|
79
|
+
}, {
|
|
80
|
+
highWaterMark: options.highWaterMark ?? 16 * 1024,
|
|
81
|
+
size(chunk) { return chunk.byteLength; }
|
|
82
|
+
})
|
|
83
|
+
);
|
|
84
|
+
|
|
85
|
+
this.writable = pipeFrom(
|
|
86
|
+
this._duplex.createWritable(
|
|
87
|
+
new WritableStream({
|
|
88
|
+
write: async (chunk) => {
|
|
89
|
+
// Wait for an ack packet
|
|
90
|
+
this._writePromise = new PromiseResolver();
|
|
91
|
+
await this.dispatcher.sendPacket(
|
|
92
|
+
AdbCommand.Write,
|
|
93
|
+
this.localId,
|
|
94
|
+
this.remoteId,
|
|
95
|
+
chunk
|
|
96
|
+
);
|
|
97
|
+
await this._writePromise.promise;
|
|
98
|
+
}
|
|
99
|
+
}),
|
|
100
|
+
),
|
|
101
|
+
new ChunkStream(this.dispatcher.options.maxPayloadSize)
|
|
102
|
+
);
|
|
103
|
+
|
|
104
|
+
this._socket = new AdbSocket(this);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
public async enqueue(packet: Uint8Array) {
|
|
108
|
+
// Consumer may abort the `ReadableStream` to close the socket,
|
|
109
|
+
// it's OK to throw away further packets in this case.
|
|
110
|
+
if (this._readableController.abortSignal.aborted) {
|
|
111
|
+
return;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
await this._readableController.enqueue(packet);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
public ack() {
|
|
118
|
+
this._writePromise?.resolve();
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
public async close(): Promise<void> {
|
|
122
|
+
await this._duplex.close();
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
public dispose() {
|
|
126
|
+
this._duplex.dispose();
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* A duplex stream representing a socket to ADB daemon.
|
|
132
|
+
*
|
|
133
|
+
* To close it, call either `socket.close()`,
|
|
134
|
+
* `socket.readable.cancel()`, `socket.readable.getReader().cancel()`,
|
|
135
|
+
* `socket.writable.abort()`, `socket.writable.getWriter().abort()`,
|
|
136
|
+
* `socket.writable.close()` or `socket.writable.getWriter().close()`.
|
|
137
|
+
*/
|
|
138
|
+
export class AdbSocket implements AdbSocketInfo, ReadableWritablePair<Uint8Array, Uint8Array>{
|
|
139
|
+
private _controller: AdbSocketController;
|
|
140
|
+
|
|
141
|
+
public get localId(): number { return this._controller.localId; }
|
|
142
|
+
public get remoteId(): number { return this._controller.remoteId; }
|
|
143
|
+
public get localCreated(): boolean { return this._controller.localCreated; }
|
|
144
|
+
public get serviceString(): string { return this._controller.serviceString; }
|
|
145
|
+
|
|
146
|
+
public get readable(): ReadableStream<Uint8Array> { return this._controller.readable; }
|
|
147
|
+
public get writable(): WritableStream<Uint8Array> { return this._controller.writable; }
|
|
148
|
+
|
|
149
|
+
public constructor(controller: AdbSocketController) {
|
|
150
|
+
this._controller = controller;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
public close() {
|
|
154
|
+
return this._controller.close();
|
|
155
|
+
}
|
|
156
|
+
}
|
|
@@ -1,41 +1,41 @@
|
|
|
1
|
-
import { PromiseResolver } from '@yume-chan/async';
|
|
2
|
-
import type { Disposable } from '@yume-chan/event';
|
|
3
|
-
|
|
4
|
-
export class AutoResetEvent implements Disposable {
|
|
5
|
-
private readonly list: PromiseResolver<void>[] = [];
|
|
6
|
-
|
|
7
|
-
private blocking: boolean;
|
|
8
|
-
|
|
9
|
-
public constructor(initialSet = false) {
|
|
10
|
-
this.blocking = initialSet;
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
public wait(): Promise<void> {
|
|
14
|
-
if (!this.blocking) {
|
|
15
|
-
this.blocking = true;
|
|
16
|
-
|
|
17
|
-
if (this.list.length === 0) {
|
|
18
|
-
return Promise.resolve();
|
|
19
|
-
}
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
const resolver = new PromiseResolver<void>();
|
|
23
|
-
this.list.push(resolver);
|
|
24
|
-
return resolver.promise;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
public notify() {
|
|
28
|
-
if (this.list.length !== 0) {
|
|
29
|
-
this.list.pop()!.resolve();
|
|
30
|
-
} else {
|
|
31
|
-
this.blocking = false;
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
public dispose() {
|
|
36
|
-
for (const item of this.list) {
|
|
37
|
-
item.reject(new Error('The AutoResetEvent has been disposed'));
|
|
38
|
-
}
|
|
39
|
-
this.list.length = 0;
|
|
40
|
-
}
|
|
41
|
-
}
|
|
1
|
+
import { PromiseResolver } from '@yume-chan/async';
|
|
2
|
+
import type { Disposable } from '@yume-chan/event';
|
|
3
|
+
|
|
4
|
+
export class AutoResetEvent implements Disposable {
|
|
5
|
+
private readonly list: PromiseResolver<void>[] = [];
|
|
6
|
+
|
|
7
|
+
private blocking: boolean;
|
|
8
|
+
|
|
9
|
+
public constructor(initialSet = false) {
|
|
10
|
+
this.blocking = initialSet;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
public wait(): Promise<void> {
|
|
14
|
+
if (!this.blocking) {
|
|
15
|
+
this.blocking = true;
|
|
16
|
+
|
|
17
|
+
if (this.list.length === 0) {
|
|
18
|
+
return Promise.resolve();
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const resolver = new PromiseResolver<void>();
|
|
23
|
+
this.list.push(resolver);
|
|
24
|
+
return resolver.promise;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
public notify() {
|
|
28
|
+
if (this.list.length !== 0) {
|
|
29
|
+
this.list.pop()!.resolve();
|
|
30
|
+
} else {
|
|
31
|
+
this.blocking = false;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
public dispose() {
|
|
36
|
+
for (const item of this.list) {
|
|
37
|
+
item.reject(new Error('The AutoResetEvent has been disposed'));
|
|
38
|
+
}
|
|
39
|
+
this.list.length = 0;
|
|
40
|
+
}
|
|
41
|
+
}
|