Skip to content

Commit 253ce61

Browse files
authored
♻️ simplify spawn() by delegating to scope.run (#1078)
We're going to end up wrapping unified APIs around most Effection functions so that they can be decorated with middleware. As such, there is no need to have separate code paths for Scope.run() and Scope.spawn() since they end up calling the same thing under the hood. This has `spawn()`` delegate to `Scope.run()` so that the apis can be unified and wrapped as one. In other words, wraping `Scope.run()` is the same as wrapping `spawn()`.
1 parent d9788cc commit 253ce61

File tree

1 file changed

+6
-17
lines changed

1 file changed

+6
-17
lines changed

lib/spawn.ts

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
import { Ok } from "./result.ts";
2-
import type { ScopeInternal } from "./scope-internal.ts";
3-
import { createTask, type NewTask } from "./task.ts";
4-
import type { Effect, Operation, Task } from "./types.ts";
1+
import { useScope } from "./scope.ts";
2+
import type { Operation, Task } from "./types.ts";
53

64
/**
75
* Run another operation concurrently as a child of the current one.
@@ -30,20 +28,11 @@ import type { Effect, Operation, Task } from "./types.ts";
3028
* @typeParam T the type that the spawned task evaluates to
3129
* @returns a {@link Task} representing a handle to the running operation
3230
*/
33-
export function* spawn<T>(op: () => Operation<T>): Operation<Task<T>> {
34-
let { task, start } = (yield Spawn(op)) as NewTask<T>;
35-
start();
36-
return task;
37-
}
38-
39-
function Spawn<T>(operation: () => Operation<T>): Effect<NewTask<T>> {
31+
export function spawn<T>(op: () => Operation<T>): Operation<Task<T>> {
4032
return {
41-
description: `spawn(${operation.name})`,
42-
enter: (resolve, { scope }) => {
43-
resolve(Ok(createTask({ owner: scope as ScopeInternal, operation })));
44-
return (done) => {
45-
done(Ok());
46-
};
33+
*[Symbol.iterator]() {
34+
let scope = yield* useScope();
35+
return scope.run(op);
4736
},
4837
};
4938
}

0 commit comments

Comments
 (0)