-
Notifications
You must be signed in to change notification settings - Fork 2
feat(scope-eval): add new package for evaluating operations in isolated scopes #145
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
9681770
feat(scope-eval): add new package for evaluating operations in isolat…
taras 8f249eb
Make it stateless
taras e9a70fd
Fix syntax error in box.ts and apply formatting
taras fd9ab62
Clarify that use* resource functions are exempt from stateless stream…
taras 3a1164c
Split box/unbox into separate @effectionx/result package
taras 09a29c0
Export Result, Ok, Err types from @effectionx/result
taras 8181180
Fix unbox tests to use Ok/Err directly instead of box
taras bf0023f
Fix README unbox example to use Ok/Err directly
taras e1fbf8e
Remove @effectionx/result package, keep box/unbox in scope-eval
taras 0493165
Remove installation instructions from README
taras 3529e97
Wrap README examples in main() so they actually work
taras 2424186
Fix README description to explain resource retention vs Scope.run/spawn
taras b598942
Make 'no violations' output even more explicit - no summaries
taras ee0139e
Trigger CI
taras File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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,101 @@ | ||
| # @effectionx/scope-eval | ||
|
|
||
| Evaluate Effection operations in a scope while retaining resources. | ||
|
|
||
| --- | ||
|
|
||
| While `Scope.run` and `Scope.spawn` can evaluate operations in isolated scopes, resources are torn down once operations return. `useEvalScope` allows you to invoke operations in an existing scope, receive the result of evaluations, while retaining resources for the lifecycle of that scope. | ||
|
|
||
| ## Usage | ||
|
|
||
| ### useEvalScope | ||
|
|
||
| Create a scope that evaluates operations and retains their resources: | ||
|
|
||
| ```typescript | ||
| import { main, createContext } from "effection"; | ||
| import { useEvalScope } from "@effectionx/scope-eval"; | ||
|
|
||
| await main(function*() { | ||
| const context = createContext<string>("my-context"); | ||
|
|
||
| const evalScope = yield* useEvalScope(); | ||
|
|
||
| // Context not set yet | ||
| evalScope.scope.get(context); // => undefined | ||
|
|
||
| // Evaluate an operation that sets context | ||
| yield* evalScope.eval(function*() { | ||
| yield* context.set("Hello World!"); | ||
| }); | ||
|
|
||
| // Now the context is visible via the scope | ||
| evalScope.scope.get(context); // => "Hello World!" | ||
| }); | ||
| ``` | ||
|
|
||
| ### Error Handling | ||
|
|
||
| Operations are executed safely and return a `Result<T>`: | ||
|
|
||
| ```typescript | ||
| import { main } from "effection"; | ||
| import { useEvalScope } from "@effectionx/scope-eval"; | ||
|
|
||
| await main(function*() { | ||
| const evalScope = yield* useEvalScope(); | ||
|
|
||
| const result = yield* evalScope.eval(function*() { | ||
| throw new Error("something went wrong"); | ||
| }); | ||
|
|
||
| if (result.ok) { | ||
| console.log("Success:", result.value); | ||
| } else { | ||
| console.log("Error:", result.error.message); | ||
| } | ||
| }); | ||
| ``` | ||
|
|
||
| ### box / unbox | ||
|
|
||
| Utilities for capturing operation results as values: | ||
|
|
||
| ```typescript | ||
| import { main } from "effection"; | ||
| import { box, unbox } from "@effectionx/scope-eval"; | ||
|
|
||
| await main(function*() { | ||
| // Capture success or error as a Result | ||
| const result = yield* box(function*() { | ||
| return 42; | ||
| }); | ||
|
|
||
| // Extract value (throws if error) | ||
| const value = unbox(result); // => 42 | ||
| }); | ||
| ``` | ||
|
|
||
| ## API | ||
|
|
||
| ### `useEvalScope(): Operation<EvalScope>` | ||
|
|
||
| Creates an isolated scope for evaluating operations. | ||
|
|
||
| Returns an `EvalScope` with: | ||
| - `scope: Scope` - The underlying Effection scope for inspecting context | ||
| - `eval<T>(op: () => Operation<T>): Operation<Result<T>>` - Evaluate an operation | ||
|
|
||
| ### `box<T>(content: () => Operation<T>): Operation<Result<T>>` | ||
|
|
||
| Execute an operation and capture its result (success or error) as a `Result<T>`. | ||
|
|
||
| ### `unbox<T>(result: Result<T>): T` | ||
|
|
||
| Extract the value from a `Result<T>`, throwing if it's an error. | ||
|
|
||
| ## Use Cases | ||
|
|
||
| - **Testing**: Evaluate operations and inspect context/state without teardown | ||
| - **Resource retention**: Keep resources alive across multiple evaluations | ||
| - **Error boundaries**: Safely execute operations that might fail |
This file contains hidden or 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,42 @@ | ||
| import { describe, it } from "@effectionx/bdd"; | ||
| import { Err, Ok } from "effection"; | ||
| import { expect } from "expect"; | ||
| import { box, unbox } from "./mod.ts"; | ||
|
|
||
| describe("box", () => { | ||
| it("returns Ok for successful operations", function* () { | ||
| const result = yield* box(function* () { | ||
| return 42; | ||
| }); | ||
|
|
||
| expect(result.ok).toBe(true); | ||
| if (result.ok) { | ||
| expect(result.value).toBe(42); | ||
| } | ||
| }); | ||
|
|
||
| it("returns Err for failed operations", function* () { | ||
| const result = yield* box(function* () { | ||
| throw new Error("test error"); | ||
| }); | ||
|
|
||
| expect(result.ok).toBe(false); | ||
| if (!result.ok) { | ||
| expect(result.error.message).toBe("test error"); | ||
| } | ||
| }); | ||
| }); | ||
|
|
||
| describe("unbox", () => { | ||
| it("extracts value from Ok result", function* () { | ||
| const result = Ok("hello"); | ||
|
|
||
| expect(unbox(result)).toBe("hello"); | ||
| }); | ||
|
|
||
| it("throws error from Err result", function* () { | ||
| const result = Err(new Error("should throw")); | ||
|
|
||
| expect(() => unbox(result)).toThrow("should throw"); | ||
| }); | ||
| }); |
This file contains hidden or 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,57 @@ | ||
| import { Err, Ok, type Operation, type Result } from "effection"; | ||
|
|
||
| /** | ||
| * Execute an operation and capture its result (success or error) as a `Result<T>`. | ||
| * | ||
| * This is useful when you want to handle errors as values rather than exceptions. | ||
| * | ||
| * @param content - A function returning the operation to execute | ||
| * @returns An operation that yields `Ok(value)` on success or `Err(error)` on failure | ||
| * | ||
| * @example | ||
| * ```ts | ||
| * const result = yield* box(function*() { | ||
| * return yield* someOperation(); | ||
| * }); | ||
| * | ||
| * if (result.ok) { | ||
| * console.log("Success:", result.value); | ||
| * } else { | ||
| * console.log("Error:", result.error); | ||
| * } | ||
| * ``` | ||
| */ | ||
| export function box<T>(content: () => Operation<T>): Operation<Result<T>> { | ||
| return { | ||
| *[Symbol.iterator]() { | ||
| try { | ||
| return Ok(yield* content()); | ||
| } catch (error) { | ||
| return Err(error as Error); | ||
| } | ||
| }, | ||
| }; | ||
| } | ||
|
|
||
| /** | ||
| * Extract the value from a `Result<T>`, throwing if it's an error. | ||
| * | ||
| * @param result - The result to unbox | ||
| * @returns The success value | ||
| * @throws The error if the result is an `Err` | ||
| * | ||
| * @example | ||
| * ```ts | ||
| * const result = yield* box(function*() { | ||
| * return "hello"; | ||
| * }); | ||
| * | ||
| * const value = unbox(result); // "hello" | ||
| * ``` | ||
| */ | ||
| export function unbox<T>(result: Result<T>): T { | ||
| if (result.ok) { | ||
| return result.value; | ||
| } | ||
| throw result.error; | ||
| } | ||
This file contains hidden or 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,75 @@ | ||
| import { describe, it } from "@effectionx/bdd"; | ||
| import { createContext } from "effection"; | ||
| import { expect } from "expect"; | ||
| import { unbox, useEvalScope } from "./mod.ts"; | ||
|
|
||
| describe("useEvalScope", () => { | ||
| it("can evaluate operations in a separate scope", function* () { | ||
| const context = createContext<string>("test-context"); | ||
|
|
||
| const evalScope = yield* useEvalScope(); | ||
|
|
||
| // Context not set yet | ||
| expect(evalScope.scope.get(context)).toBeUndefined(); | ||
|
|
||
| // Evaluate an operation that sets context | ||
| const result = yield* evalScope.eval(function* () { | ||
| yield* context.set("Hello World!"); | ||
| return "done"; | ||
| }); | ||
|
|
||
| // Context is now visible via scope | ||
| expect(evalScope.scope.get(context)).toBe("Hello World!"); | ||
| expect(unbox(result)).toBe("done"); | ||
| }); | ||
|
|
||
| it("captures errors as Result.Err", function* () { | ||
| const evalScope = yield* useEvalScope(); | ||
|
|
||
| const result = yield* evalScope.eval(function* () { | ||
| throw new Error("boom"); | ||
| }); | ||
|
|
||
| expect(result.ok).toBe(false); | ||
| if (!result.ok) { | ||
| expect(result.error.message).toBe("boom"); | ||
| } | ||
| }); | ||
|
|
||
| it("can evaluate multiple operations in sequence", function* () { | ||
| const counter = createContext<number>("counter"); | ||
|
|
||
| const evalScope = yield* useEvalScope(); | ||
|
|
||
| yield* evalScope.eval(function* () { | ||
| yield* counter.set(1); | ||
| }); | ||
|
|
||
| yield* evalScope.eval(function* () { | ||
| const current = evalScope.scope.get(counter) ?? 0; | ||
| yield* counter.set(current + 1); | ||
| }); | ||
|
|
||
| expect(evalScope.scope.get(counter)).toBe(2); | ||
| }); | ||
|
|
||
| it("child scope can see parent context but setting creates own value", function* () { | ||
| const context = createContext<string>("inherited"); | ||
|
|
||
| // Set context in parent scope | ||
| yield* context.set("parent value"); | ||
|
|
||
| const evalScope = yield* useEvalScope(); | ||
|
|
||
| // Child scope CAN see parent's context (Effection context inheritance) | ||
| expect(evalScope.scope.get(context)).toBe("parent value"); | ||
|
|
||
| // Set in child scope - this creates a new value in the child | ||
| yield* evalScope.eval(function* () { | ||
| yield* context.set("child value"); | ||
| }); | ||
|
|
||
| // Child now has its own value | ||
| expect(evalScope.scope.get(context)).toBe("child value"); | ||
| }); | ||
| }); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.