-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Spiff up the scope documentation. The `ScopeInternal` class was moved to its own file so that it can be imported internally, but is not exported from the main module.
- Loading branch information
Showing
5 changed files
with
184 additions
and
101 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import { Children, Generation } from "./contexts.ts"; | ||
import { Err, Ok, unbox } from "./result.ts"; | ||
import { createTask } from "./task.ts"; | ||
import type { Context, Operation, Scope, Task } from "./types.ts"; | ||
|
||
export function createScopeInternal( | ||
parent?: Scope, | ||
): [ScopeInternal, () => Operation<void>] { | ||
let destructors = new Set<() => Operation<void>>(); | ||
|
||
let contexts: Record<string, unknown> = Object.create( | ||
parent ? (parent as ScopeInternal).contexts : null, | ||
); | ||
let scope: ScopeInternal = Object.create({ | ||
[Symbol.toStringTag]: "Scope", | ||
contexts, | ||
get<T>(context: Context<T>): T | undefined { | ||
return (contexts[context.name] ?? context.defaultValue) as T | undefined; | ||
}, | ||
set<T>(context: Context<T>, value: T): T { | ||
return contexts[context.name] = value; | ||
}, | ||
expect<T>(context: Context<T>): T { | ||
let value = scope.get(context); | ||
if (typeof value === "undefined") { | ||
let error = new Error(context.name); | ||
error.name = `MissingContextError`; | ||
throw error; | ||
} | ||
return value; | ||
}, | ||
delete<T>(context: Context<T>): boolean { | ||
return delete contexts[context.name]; | ||
}, | ||
hasOwn<T>(context: Context<T>): boolean { | ||
return !!Reflect.getOwnPropertyDescriptor(contexts, context.name); | ||
}, | ||
run<T>(operation: () => Operation<T>): Task<T> { | ||
let { task, start } = createTask({ operation, owner: scope }); | ||
start(); | ||
return task; | ||
}, | ||
spawn<T>(operation: () => Operation<T>): Operation<Task<T>> { | ||
return { | ||
*[Symbol.iterator]() { | ||
let { task, start } = createTask({ operation, owner: scope }); | ||
start(); | ||
return task; | ||
}, | ||
}; | ||
}, | ||
|
||
ensure(op: () => Operation<void>): () => void { | ||
destructors.add(op); | ||
return () => destructors.delete(op); | ||
}, | ||
}); | ||
|
||
scope.set(Generation, scope.expect(Generation) + 1); | ||
scope.set(Children, new Set()); | ||
parent?.expect(Children).add(scope); | ||
|
||
let unbind = parent ? (parent as ScopeInternal).ensure(destroy) : () => {}; | ||
|
||
function* destroy(): Operation<void> { | ||
parent?.expect(Children).delete(scope); | ||
unbind(); | ||
let outcome = Ok(); | ||
for (let destructor of [...destructors].reverse()) { | ||
try { | ||
destructors.delete(destructor); | ||
yield* destructor(); | ||
} catch (error) { | ||
outcome = Err(error as Error); | ||
} | ||
} | ||
unbox(outcome); | ||
} | ||
|
||
return [scope, destroy]; | ||
} | ||
|
||
export interface ScopeInternal extends Scope { | ||
contexts: Record<string, unknown>; | ||
ensure(op: () => Operation<void>): () => void; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters