@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.
Files changed (196) hide show
  1. package/CHANGELOG.json +33 -0
  2. package/CHANGELOG.md +18 -1
  3. package/LICENSE +21 -21
  4. package/README.md +247 -247
  5. package/esm/adb.d.ts +15 -3
  6. package/esm/adb.d.ts.map +1 -1
  7. package/esm/adb.js +18 -6
  8. package/esm/adb.js.map +1 -1
  9. package/esm/auth.d.ts.map +1 -1
  10. package/esm/auth.js.map +1 -1
  11. package/esm/backend.d.ts +2 -2
  12. package/esm/backend.d.ts.map +1 -1
  13. package/esm/commands/base.d.ts.map +1 -1
  14. package/esm/commands/base.js.map +1 -1
  15. package/esm/commands/framebuffer.d.ts +17 -1
  16. package/esm/commands/framebuffer.d.ts.map +1 -1
  17. package/esm/commands/framebuffer.js +4 -15
  18. package/esm/commands/framebuffer.js.map +1 -1
  19. package/esm/commands/install.d.ts +2 -2
  20. package/esm/commands/install.d.ts.map +1 -1
  21. package/esm/commands/install.js +3 -2
  22. package/esm/commands/install.js.map +1 -1
  23. package/esm/commands/power.d.ts +1 -1
  24. package/esm/commands/power.js +1 -1
  25. package/esm/commands/reverse.d.ts +3 -3
  26. package/esm/commands/reverse.d.ts.map +1 -1
  27. package/esm/commands/reverse.js +9 -9
  28. package/esm/commands/reverse.js.map +1 -1
  29. package/esm/commands/subprocess/command.d.ts +52 -0
  30. package/esm/commands/subprocess/command.d.ts.map +1 -0
  31. package/esm/commands/subprocess/command.js +83 -0
  32. package/esm/commands/subprocess/command.js.map +1 -0
  33. package/esm/commands/subprocess/index.d.ts +1 -53
  34. package/esm/commands/subprocess/index.d.ts.map +1 -1
  35. package/esm/commands/subprocess/index.js +1 -85
  36. package/esm/commands/subprocess/index.js.map +1 -1
  37. package/esm/commands/subprocess/protocols/none.d.ts +5 -4
  38. package/esm/commands/subprocess/protocols/none.d.ts.map +1 -1
  39. package/esm/commands/subprocess/protocols/none.js +11 -8
  40. package/esm/commands/subprocess/protocols/none.js.map +1 -1
  41. package/esm/commands/subprocess/protocols/shell.d.ts +4 -4
  42. package/esm/commands/subprocess/protocols/shell.d.ts.map +1 -1
  43. package/esm/commands/subprocess/protocols/shell.js +6 -6
  44. package/esm/commands/subprocess/protocols/shell.js.map +1 -1
  45. package/esm/commands/subprocess/protocols/types.d.ts +7 -7
  46. package/esm/commands/subprocess/protocols/types.d.ts.map +1 -1
  47. package/esm/commands/sync/index.d.ts +1 -1
  48. package/esm/commands/sync/index.d.ts.map +1 -1
  49. package/esm/commands/sync/index.js +1 -1
  50. package/esm/commands/sync/index.js.map +1 -1
  51. package/esm/commands/sync/list.d.ts +2 -2
  52. package/esm/commands/sync/list.d.ts.map +1 -1
  53. package/esm/commands/sync/list.js +15 -43
  54. package/esm/commands/sync/list.js.map +1 -1
  55. package/esm/commands/sync/pull.d.ts +3 -2
  56. package/esm/commands/sync/pull.d.ts.map +1 -1
  57. package/esm/commands/sync/pull.js +15 -17
  58. package/esm/commands/sync/pull.js.map +1 -1
  59. package/esm/commands/sync/push.d.ts +2 -2
  60. package/esm/commands/sync/push.d.ts.map +1 -1
  61. package/esm/commands/sync/push.js +2 -5
  62. package/esm/commands/sync/push.js.map +1 -1
  63. package/esm/commands/sync/request.d.ts +1 -1
  64. package/esm/commands/sync/request.d.ts.map +1 -1
  65. package/esm/commands/sync/request.js +1 -1
  66. package/esm/commands/sync/request.js.map +1 -1
  67. package/esm/commands/sync/response.d.ts +4 -9
  68. package/esm/commands/sync/response.d.ts.map +1 -1
  69. package/esm/commands/sync/response.js +32 -23
  70. package/esm/commands/sync/response.js.map +1 -1
  71. package/esm/commands/sync/stat.d.ts +3 -3
  72. package/esm/commands/sync/stat.d.ts.map +1 -1
  73. package/esm/commands/sync/stat.js +13 -30
  74. package/esm/commands/sync/stat.js.map +1 -1
  75. package/esm/commands/sync/sync.d.ts +2 -2
  76. package/esm/commands/sync/sync.d.ts.map +1 -1
  77. package/esm/commands/sync/sync.js +7 -5
  78. package/esm/commands/sync/sync.js.map +1 -1
  79. package/esm/crypto.js +1 -1
  80. package/esm/crypto.js.map +1 -1
  81. package/esm/index.d.ts +0 -1
  82. package/esm/index.d.ts.map +1 -1
  83. package/esm/index.js +0 -1
  84. package/esm/index.js.map +1 -1
  85. package/esm/packet.d.ts +1 -1
  86. package/esm/packet.d.ts.map +1 -1
  87. package/esm/packet.js +1 -1
  88. package/esm/packet.js.map +1 -1
  89. package/esm/socket/dispatcher.d.ts +11 -3
  90. package/esm/socket/dispatcher.d.ts.map +1 -1
  91. package/esm/socket/dispatcher.js +19 -11
  92. package/esm/socket/dispatcher.js.map +1 -1
  93. package/esm/socket/index.d.ts +1 -1
  94. package/esm/socket/index.d.ts.map +1 -1
  95. package/esm/socket/index.js +1 -1
  96. package/esm/socket/index.js.map +1 -1
  97. package/esm/socket/socket.d.ts +9 -4
  98. package/esm/socket/socket.d.ts.map +1 -1
  99. package/esm/socket/socket.js +20 -10
  100. package/esm/socket/socket.js.map +1 -1
  101. package/esm/utils/base64.d.ts +23 -0
  102. package/esm/utils/base64.d.ts.map +1 -1
  103. package/esm/utils/base64.js +6 -0
  104. package/esm/utils/base64.js.map +1 -1
  105. package/package.json +13 -14
  106. package/src/adb.ts +285 -273
  107. package/src/auth.ts +174 -173
  108. package/src/backend.ts +12 -11
  109. package/src/commands/base.ts +12 -11
  110. package/src/commands/framebuffer.ts +78 -73
  111. package/src/commands/index.ts +8 -8
  112. package/src/commands/install.ts +32 -30
  113. package/src/commands/power.ts +54 -54
  114. package/src/commands/reverse.ts +141 -140
  115. package/src/commands/subprocess/command.ts +131 -0
  116. package/src/commands/subprocess/index.ts +3 -139
  117. package/src/commands/subprocess/protocols/none.ts +73 -68
  118. package/src/commands/subprocess/protocols/shell.ts +191 -190
  119. package/src/commands/subprocess/protocols/types.ts +60 -59
  120. package/src/commands/subprocess/utils.ts +20 -20
  121. package/src/commands/sync/index.ts +7 -7
  122. package/src/commands/sync/list.ts +54 -86
  123. package/src/commands/sync/pull.ts +45 -46
  124. package/src/commands/sync/push.ts +38 -41
  125. package/src/commands/sync/request.ts +51 -50
  126. package/src/commands/sync/response.ts +68 -64
  127. package/src/commands/sync/stat.ts +129 -150
  128. package/src/commands/sync/sync.ts +182 -181
  129. package/src/commands/tcpip.ts +21 -21
  130. package/src/crypto.ts +296 -296
  131. package/src/features.ts +9 -9
  132. package/src/index.ts +10 -11
  133. package/src/packet.ts +80 -80
  134. package/src/socket/dispatcher.ts +310 -304
  135. package/src/socket/index.ts +2 -2
  136. package/src/socket/socket.ts +156 -144
  137. package/src/utils/auto-reset-event.ts +41 -41
  138. package/src/utils/base64.ts +329 -306
  139. package/src/utils/index.ts +3 -3
  140. package/tsconfig.build.json +3 -3
  141. package/tsconfig.build.tsbuildinfo +1 -1
  142. package/esm/commands/subprocess/legacy.d.ts +0 -35
  143. package/esm/commands/subprocess/legacy.d.ts.map +0 -1
  144. package/esm/commands/subprocess/legacy.js +0 -53
  145. package/esm/commands/subprocess/legacy.js.map +0 -1
  146. package/esm/commands/subprocess/protocol.d.ts +0 -39
  147. package/esm/commands/subprocess/protocol.d.ts.map +0 -1
  148. package/esm/commands/subprocess/protocol.js +0 -157
  149. package/esm/commands/subprocess/protocol.js.map +0 -1
  150. package/esm/commands/subprocess/types.d.ts +0 -50
  151. package/esm/commands/subprocess/types.d.ts.map +0 -1
  152. package/esm/commands/subprocess/types.js +0 -2
  153. package/esm/commands/subprocess/types.js.map +0 -1
  154. package/esm/socket/controller.d.ts +0 -37
  155. package/esm/socket/controller.d.ts.map +0 -1
  156. package/esm/socket/controller.js +0 -66
  157. package/esm/socket/controller.js.map +0 -1
  158. package/esm/stream/buffered.d.ts +0 -35
  159. package/esm/stream/buffered.d.ts.map +0 -1
  160. package/esm/stream/buffered.js +0 -123
  161. package/esm/stream/buffered.js.map +0 -1
  162. package/esm/stream/detect.bak.d.ts +0 -240
  163. package/esm/stream/detect.bak.d.ts.map +0 -1
  164. package/esm/stream/detect.bak.js +0 -60
  165. package/esm/stream/detect.bak.js.map +0 -1
  166. package/esm/stream/detect.d.ts +0 -2
  167. package/esm/stream/detect.d.ts.map +0 -1
  168. package/esm/stream/detect.js +0 -5
  169. package/esm/stream/detect.js.map +0 -1
  170. package/esm/stream/detect.native.d.ts +0 -240
  171. package/esm/stream/detect.native.d.ts.map +0 -1
  172. package/esm/stream/detect.native.js +0 -60
  173. package/esm/stream/detect.native.js.map +0 -1
  174. package/esm/stream/detect.polyfill.d.ts +0 -18
  175. package/esm/stream/detect.polyfill.d.ts.map +0 -1
  176. package/esm/stream/detect.polyfill.js +0 -9
  177. package/esm/stream/detect.polyfill.js.map +0 -1
  178. package/esm/stream/index.d.ts +0 -4
  179. package/esm/stream/index.d.ts.map +0 -1
  180. package/esm/stream/index.js +0 -4
  181. package/esm/stream/index.js.map +0 -1
  182. package/esm/stream/transform.d.ts +0 -104
  183. package/esm/stream/transform.d.ts.map +0 -1
  184. package/esm/stream/transform.js +0 -373
  185. package/esm/stream/transform.js.map +0 -1
  186. package/esm/utils/encoding.d.ts +0 -3
  187. package/esm/utils/encoding.d.ts.map +0 -1
  188. package/esm/utils/encoding.js +0 -11
  189. package/esm/utils/encoding.js.map +0 -1
  190. package/src/stream/buffered.ts +0 -147
  191. package/src/stream/detect.native.ts +0 -362
  192. package/src/stream/detect.polyfill.ts +0 -28
  193. package/src/stream/detect.ts +0 -5
  194. package/src/stream/index.ts +0 -3
  195. package/src/stream/transform.ts +0 -447
  196. package/tsconfig.test.json +0 -10
@@ -1,144 +1,156 @@
1
- import { PromiseResolver } from "@yume-chan/async";
2
- import type { Disposable } from "@yume-chan/event";
3
- import { AdbCommand } from '../packet.js';
4
- import { ChunkStream, DuplexStreamFactory, pipeFrom, PushReadableStream, WritableStream, type PushReadableStreamController, type ReadableStream, type ReadableWritablePair } from '../stream/index.js';
5
- import type { AdbPacketDispatcher, Closeable } from './dispatcher.js';
6
-
7
- export interface AdbSocketInfo {
8
- localId: number;
9
- remoteId: number;
10
-
11
- localCreated: boolean;
12
- serviceString: string;
13
- }
14
-
15
- export interface AdbSocketConstructionOptions extends AdbSocketInfo {
16
- dispatcher: AdbPacketDispatcher;
17
-
18
- highWaterMark?: number | undefined;
19
- }
20
-
21
- export class AdbSocketController implements AdbSocketInfo, ReadableWritablePair<Uint8Array, Uint8Array>, Closeable, Disposable {
22
- private readonly dispatcher!: AdbPacketDispatcher;
23
-
24
- public readonly localId!: number;
25
- public readonly remoteId!: number;
26
- public readonly localCreated!: boolean;
27
- public readonly serviceString!: string;
28
-
29
- private _factory: DuplexStreamFactory<Uint8Array, Uint8Array>;
30
-
31
- private _readable: ReadableStream<Uint8Array>;
32
- private _readableController!: PushReadableStreamController<Uint8Array>;
33
- public get readable() { return this._readable; }
34
-
35
- private _writePromise: PromiseResolver<void> | undefined;
36
- public readonly writable: WritableStream<Uint8Array>;
37
-
38
- private _closed = false;
39
- public get closed() { return this._closed; }
40
-
41
- private _socket: AdbSocket;
42
- public get socket() { return this._socket; }
43
-
44
- public constructor(options: AdbSocketConstructionOptions) {
45
- Object.assign(this, options);
46
-
47
- // Check this image to help you understand the stream graph
48
- // cspell: disable-next-line
49
- // https://www.plantuml.com/plantuml/png/TL0zoeGm4ErpYc3l5JxyS0yWM6mX5j4C6p4cxcJ25ejttuGX88ZftizxUKmJI275pGhXl0PP_UkfK_CAz5Z2hcWsW9Ny2fdU4C1f5aSchFVxA8vJjlTPRhqZzDQMRB7AklwJ0xXtX0ZSKH1h24ghoKAdGY23FhxC4nS2pDvxzIvxb-8THU0XlEQJ-ZB7SnXTAvc_LhOckhMdLBnbtndpb-SB7a8q2SRD_W00
50
-
51
- this._factory = new DuplexStreamFactory<Uint8Array, Uint8Array>({
52
- close: async () => {
53
- await this.dispatcher.sendPacket(
54
- AdbCommand.Close,
55
- this.localId,
56
- this.remoteId
57
- );
58
-
59
- // Don't `dispose` here, we need to wait for `CLSE` response packet.
60
- return false;
61
- },
62
- dispose: () => {
63
- this._closed = true;
64
-
65
- // Error out the pending writes
66
- this._writePromise?.reject(new Error('Socket closed'));
67
- },
68
- });
69
-
70
- this._readable = this._factory.wrapReadable(
71
- new PushReadableStream(controller => {
72
- this._readableController = controller;
73
- }, {
74
- highWaterMark: options.highWaterMark ?? 16 * 1024,
75
- size(chunk) { return chunk.byteLength; }
76
- })
77
- );
78
-
79
- this.writable = pipeFrom(
80
- this._factory.createWritable(
81
- new WritableStream({
82
- write: async (chunk) => {
83
- // Wait for an ack packet
84
- this._writePromise = new PromiseResolver();
85
- await this.dispatcher.sendPacket(
86
- AdbCommand.Write,
87
- this.localId,
88
- this.remoteId,
89
- chunk
90
- );
91
- await this._writePromise.promise;
92
- }
93
- }),
94
- ),
95
- new ChunkStream(this.dispatcher.options.maxPayloadSize)
96
- );
97
-
98
- this._socket = new AdbSocket(this);
99
- }
100
-
101
- public async enqueue(packet: Uint8Array) {
102
- await this._readableController.enqueue(packet);
103
- }
104
-
105
- public ack() {
106
- this._writePromise?.resolve();
107
- }
108
-
109
- public async close(): Promise<void> {
110
- this._factory.close();
111
- }
112
-
113
- public dispose() {
114
- this._factory.dispose();
115
- }
116
- }
117
-
118
- /**
119
- * AdbSocket is a duplex stream.
120
- *
121
- * To close it, call either `socket.close()`,
122
- * `socket.readable.cancel()`, `socket.readable.getReader().cancel()`,
123
- * `socket.writable.abort()`, `socket.writable.getWriter().abort()`,
124
- * `socket.writable.close()` or `socket.writable.getWriter().close()`.
125
- */
126
- export class AdbSocket implements AdbSocketInfo, ReadableWritablePair<Uint8Array, Uint8Array>{
127
- private _controller: AdbSocketController;
128
-
129
- public get localId(): number { return this._controller.localId; }
130
- public get remoteId(): number { return this._controller.remoteId; }
131
- public get localCreated(): boolean { return this._controller.localCreated; }
132
- public get serviceString(): string { return this._controller.serviceString; }
133
-
134
- public get readable(): ReadableStream<Uint8Array> { return this._controller.readable; }
135
- public get writable(): WritableStream<Uint8Array> { return this._controller.writable; }
136
-
137
- public constructor(controller: AdbSocketController) {
138
- this._controller = controller;
139
- }
140
-
141
- public close() {
142
- return this._controller.close();
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
+ }