-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
overlapped.ts
80 lines (72 loc) · 1.86 KB
/
overlapped.ts
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
import {
allocOVERLAPPED,
CancelIoEx,
GetOverlappedResultAsync,
} from "./api/System/IO.ts";
export type State = "pending" | "rejected" | "resolved" | "cancelled";
export class OverlappedPromise extends Promise<number> {
#inner = allocOVERLAPPED();
#bytesTransferred = new Uint32Array(1);
#state: State = "pending";
get buffer() {
return this.#inner;
}
get internal() {
return new BigUint64Array(this.#inner.buffer, 0, 1)[0];
}
get internalHigh() {
return new Uint32Array(this.#inner.buffer, 8, 1)[0];
}
constructor(
hFile:
| Deno.PointerValue
| ((
resolve: (value: number | PromiseLike<number>) => void,
reject: (reason?: any) => void,
) => void),
signal?: AbortSignal,
) {
if (typeof hFile === "function") {
super(hFile);
return;
}
let resolve: (value: number) => void;
let reject: (reason: any) => void;
super((iresolve, ireject) => {
resolve = iresolve;
reject = ireject;
});
if (signal) {
signal.addEventListener("abort", () => {
if (this.#state === "pending") {
this.#state = "cancelled";
CancelIoEx(hFile, this.#inner);
reject(new DOMException("Aborted", "AbortError"));
}
});
}
const getResult = (): any =>
GetOverlappedResultAsync(
hFile,
this.#inner,
new Uint8Array(this.#bytesTransferred.buffer),
true,
).then((result) => {
if (this.#state === "cancelled") {
return;
}
// why
if (this.internal === 259n) {
return getResult();
}
if (result) {
this.#state = "resolved";
resolve(this.internalHigh);
} else {
this.#state = "rejected";
reject(new Error("GetOverlappedResultAsync failed"));
}
});
getResult();
}
}