-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathtask.ts
More file actions
147 lines (132 loc) · 3.94 KB
/
task.ts
File metadata and controls
147 lines (132 loc) · 3.94 KB
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
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
142
143
144
145
146
147
// deno-lint-ignore-file no-unsafe-finally
import { DelimiterContext, ErrorContext } from "./delimiter.ts";
import { createCoroutine } from "./coroutine.ts";
import { Delimiter } from "./delimiter.ts";
import { createFuture } from "./future.ts";
import { Ok } from "./result.ts";
import { createScopeInternal, type ScopeInternal } from "./scope-internal.ts";
import type { Coroutine, Operation, Scope, Task } from "./types.ts";
import { encapsulate, TaskGroupContext } from "./task-group.ts";
import { useScope } from "./scope.ts";
export interface TaskOptions<T> {
owner: ScopeInternal;
operation(): Operation<T>;
}
export interface NewTask<T> {
scope: Scope;
routine: Coroutine;
task: Task<T>;
start(): void;
}
export function createTask<T>(options: TaskOptions<T>): NewTask<T> {
let { owner, operation } = options;
let [scope, destroy] = createScopeInternal(owner);
let future = createFuture<T>();
let task = Object.defineProperties(future.future, {
halt: {
enumerable: false,
value() {
return Object.defineProperties(Object.create(Promise.prototype), {
[Symbol.iterator]: {
enumerable: false,
value: destroy,
},
then: {
enumerable: false,
value(...args: Parameters<Promise<void>["then"]>) {
return owner.run(destroy).then(...args);
},
},
catch: {
enumerable: false,
value(...args: Parameters<Promise<void>["catch"]>) {
return owner.run(destroy).catch(...args);
},
},
finally: {
enumerable: false,
value(...args: Parameters<Promise<void>["finally"]>) {
return owner.run(destroy).finally(...args);
},
},
});
},
},
[Symbol.iterator]: {
enumerable: false,
value: future.future[Symbol.iterator],
},
[Symbol.toStringTag]: {
enumerable: false,
value: "Task",
},
}) as Task<T>;
let top = new Delimiter<T>(() => encapsulate(operation));
scope.set(DelimiterContext, top as Delimiter<unknown>);
let group = scope.expect(TaskGroupContext);
group.add(task);
let boundary = owner.expect(ErrorContext);
scope.set(ErrorContext, top);
// Use setFinalizer to ensure the delimiter closes BEFORE child destructors run.
// This allows the task's finally block to communicate with spawned children.
scope.setFinalizer(function* () {
try {
yield* top.close();
} finally {
group.delete(task);
let { outcome } = top;
if (outcome!.exists) {
let result = outcome!.value;
if (result.ok) {
future.resolve(result.value);
} else {
let { error } = result;
future.reject(error);
boundary.raise(error);
}
} else {
future.reject(new Error("halted"));
}
}
});
let routine = createCoroutine({
scope,
*operation() {
try {
yield* top;
} finally {
yield* destroy();
}
},
});
let start = () => routine.next(Ok());
return { scope, routine, task, start };
}
export function* trap<T>(operation: () => Operation<T>): Operation<T> {
let scope = yield* useScope();
let original = {
error: scope.expect(ErrorContext),
delimiter: scope.expect(DelimiterContext),
};
let delimiter = new Delimiter(operation, original.delimiter);
scope.set(ErrorContext, delimiter);
scope.set(DelimiterContext, delimiter as Delimiter<unknown>);
try {
yield* delimiter;
} finally {
scope.set(ErrorContext, original.error);
scope.set(DelimiterContext, original.delimiter);
let outcome = delimiter.outcome!;
return (yield {
description: "trap return",
enter(resolve) {
if (outcome.exists) {
resolve(outcome.value);
} else {
original.delimiter.interrupt();
}
return (didExit) => didExit(Ok());
},
}) as T;
}
}