Skip to content

Commit 65b3912

Browse files
committed
Fix syntax error in box.ts and apply formatting
- Remove duplicate box() function definition - Apply Stateless Streams policy (use *[Symbol.iterator] pattern) - Fix formatting (tabs -> spaces)
1 parent d178dcc commit 65b3912

File tree

3 files changed

+160
-166
lines changed

3 files changed

+160
-166
lines changed

scope-eval/box.ts

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -21,22 +21,16 @@ import { Err, Ok, type Operation, type Result } from "effection";
2121
* }
2222
* ```
2323
*/
24-
export function* box<T>(content: () => Operation<T>): Operation<Result<T>> {
25-
try {
26-
return Ok(yield* content());
27-
} catch (error) {
28-
return Err(error as Error);
29-
}
3024
export function box<T>(content: () => Operation<T>): Operation<Result<T>> {
31-
return {
32-
*[Symbol.iterator]() {
33-
try {
34-
return Ok(yield* content());
35-
} catch (error) {
36-
return Err(error as Error);
37-
}
38-
}
39-
};
25+
return {
26+
*[Symbol.iterator]() {
27+
try {
28+
return Ok(yield* content());
29+
} catch (error) {
30+
return Err(error as Error);
31+
}
32+
},
33+
};
4034
}
4135

4236
/**
@@ -56,8 +50,8 @@ export function box<T>(content: () => Operation<T>): Operation<Result<T>> {
5650
* ```
5751
*/
5852
export function unbox<T>(result: Result<T>): T {
59-
if (result.ok) {
60-
return result.value;
61-
}
62-
throw result.error;
53+
if (result.ok) {
54+
return result.value;
55+
}
56+
throw result.error;
6357
}

scope-eval/eval-scope.test.ts

Lines changed: 83 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -4,114 +4,114 @@ import { describe, it } from "@effectionx/bdd";
44
import { useEvalScope, unbox, box } from "./mod.ts";
55

66
describe("box", () => {
7-
it("returns Ok for successful operations", function* () {
8-
const result = yield* box(function* () {
9-
return 42;
10-
});
11-
12-
expect(result.ok).toBe(true);
13-
if (result.ok) {
14-
expect(result.value).toBe(42);
15-
}
16-
});
17-
18-
it("returns Err for failed operations", function* () {
19-
const result = yield* box(function* () {
20-
throw new Error("test error");
21-
});
22-
23-
expect(result.ok).toBe(false);
24-
if (!result.ok) {
25-
expect(result.error.message).toBe("test error");
26-
}
27-
});
7+
it("returns Ok for successful operations", function* () {
8+
const result = yield* box(function* () {
9+
return 42;
10+
});
11+
12+
expect(result.ok).toBe(true);
13+
if (result.ok) {
14+
expect(result.value).toBe(42);
15+
}
16+
});
17+
18+
it("returns Err for failed operations", function* () {
19+
const result = yield* box(function* () {
20+
throw new Error("test error");
21+
});
22+
23+
expect(result.ok).toBe(false);
24+
if (!result.ok) {
25+
expect(result.error.message).toBe("test error");
26+
}
27+
});
2828
});
2929

3030
describe("unbox", () => {
31-
it("extracts value from Ok result", function* () {
32-
const result = yield* box(function* () {
33-
return "hello";
34-
});
31+
it("extracts value from Ok result", function* () {
32+
const result = yield* box(function* () {
33+
return "hello";
34+
});
3535

36-
expect(unbox(result)).toBe("hello");
37-
});
36+
expect(unbox(result)).toBe("hello");
37+
});
3838

39-
it("throws error from Err result", function* () {
40-
const result = yield* box(function* () {
41-
throw new Error("should throw");
42-
});
39+
it("throws error from Err result", function* () {
40+
const result = yield* box(function* () {
41+
throw new Error("should throw");
42+
});
4343

44-
expect(() => unbox(result)).toThrow("should throw");
45-
});
44+
expect(() => unbox(result)).toThrow("should throw");
45+
});
4646
});
4747

4848
describe("useEvalScope", () => {
49-
it("can evaluate operations in a separate scope", function* () {
50-
const context = createContext<string>("test-context");
49+
it("can evaluate operations in a separate scope", function* () {
50+
const context = createContext<string>("test-context");
5151

52-
const evalScope = yield* useEvalScope();
52+
const evalScope = yield* useEvalScope();
5353

54-
// Context not set yet
55-
expect(evalScope.scope.get(context)).toBeUndefined();
54+
// Context not set yet
55+
expect(evalScope.scope.get(context)).toBeUndefined();
5656

57-
// Evaluate an operation that sets context
58-
const result = yield* evalScope.eval(function* () {
59-
yield* context.set("Hello World!");
60-
return "done";
61-
});
57+
// Evaluate an operation that sets context
58+
const result = yield* evalScope.eval(function* () {
59+
yield* context.set("Hello World!");
60+
return "done";
61+
});
6262

63-
// Context is now visible via scope
64-
expect(evalScope.scope.get(context)).toBe("Hello World!");
65-
expect(unbox(result)).toBe("done");
66-
});
63+
// Context is now visible via scope
64+
expect(evalScope.scope.get(context)).toBe("Hello World!");
65+
expect(unbox(result)).toBe("done");
66+
});
6767

68-
it("captures errors as Result.Err", function* () {
69-
const evalScope = yield* useEvalScope();
68+
it("captures errors as Result.Err", function* () {
69+
const evalScope = yield* useEvalScope();
7070

71-
const result = yield* evalScope.eval(function* () {
72-
throw new Error("boom");
73-
});
71+
const result = yield* evalScope.eval(function* () {
72+
throw new Error("boom");
73+
});
7474

75-
expect(result.ok).toBe(false);
76-
if (!result.ok) {
77-
expect(result.error.message).toBe("boom");
78-
}
79-
});
75+
expect(result.ok).toBe(false);
76+
if (!result.ok) {
77+
expect(result.error.message).toBe("boom");
78+
}
79+
});
8080

81-
it("can evaluate multiple operations in sequence", function* () {
82-
const counter = createContext<number>("counter");
81+
it("can evaluate multiple operations in sequence", function* () {
82+
const counter = createContext<number>("counter");
8383

84-
const evalScope = yield* useEvalScope();
84+
const evalScope = yield* useEvalScope();
8585

86-
yield* evalScope.eval(function* () {
87-
yield* counter.set(1);
88-
});
86+
yield* evalScope.eval(function* () {
87+
yield* counter.set(1);
88+
});
8989

90-
yield* evalScope.eval(function* () {
91-
const current = evalScope.scope.get(counter) ?? 0;
92-
yield* counter.set(current + 1);
93-
});
90+
yield* evalScope.eval(function* () {
91+
const current = evalScope.scope.get(counter) ?? 0;
92+
yield* counter.set(current + 1);
93+
});
9494

95-
expect(evalScope.scope.get(counter)).toBe(2);
96-
});
95+
expect(evalScope.scope.get(counter)).toBe(2);
96+
});
9797

98-
it("child scope can see parent context but setting creates own value", function* () {
99-
const context = createContext<string>("inherited");
98+
it("child scope can see parent context but setting creates own value", function* () {
99+
const context = createContext<string>("inherited");
100100

101-
// Set context in parent scope
102-
yield* context.set("parent value");
101+
// Set context in parent scope
102+
yield* context.set("parent value");
103103

104-
const evalScope = yield* useEvalScope();
104+
const evalScope = yield* useEvalScope();
105105

106-
// Child scope CAN see parent's context (Effection context inheritance)
107-
expect(evalScope.scope.get(context)).toBe("parent value");
106+
// Child scope CAN see parent's context (Effection context inheritance)
107+
expect(evalScope.scope.get(context)).toBe("parent value");
108108

109-
// Set in child scope - this creates a new value in the child
110-
yield* evalScope.eval(function* () {
111-
yield* context.set("child value");
112-
});
109+
// Set in child scope - this creates a new value in the child
110+
yield* evalScope.eval(function* () {
111+
yield* context.set("child value");
112+
});
113113

114-
// Child now has its own value
115-
expect(evalScope.scope.get(context)).toBe("child value");
116-
});
114+
// Child now has its own value
115+
expect(evalScope.scope.get(context)).toBe("child value");
116+
});
117117
});

scope-eval/eval-scope.ts

Lines changed: 64 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import {
2-
type Operation,
3-
type Result,
4-
type Scope,
5-
type Stream,
6-
createChannel,
7-
spawn,
8-
useScope,
9-
withResolvers,
2+
type Operation,
3+
type Result,
4+
type Scope,
5+
type Stream,
6+
createChannel,
7+
spawn,
8+
useScope,
9+
withResolvers,
1010
} from "effection";
1111
import { box } from "./box.ts";
1212

@@ -19,27 +19,27 @@ import { box } from "./box.ts";
1919
* - Inspect the scope's state from outside
2020
*/
2121
export interface EvalScope {
22-
/**
23-
* The underlying Effection scope.
24-
* Use this to inspect context values set by evaluated operations.
25-
*/
26-
scope: Scope;
22+
/**
23+
* The underlying Effection scope.
24+
* Use this to inspect context values set by evaluated operations.
25+
*/
26+
scope: Scope;
2727

28-
/**
29-
* Evaluate an operation within this scope.
30-
*
31-
* The operation runs in the spawned scope, so any context values it sets
32-
* will be visible via `scope.get(context)`.
33-
*
34-
* @param op - A function returning the operation to evaluate
35-
* @returns The result of the operation (Ok or Err)
36-
*/
37-
eval<T>(op: () => Operation<T>): Operation<Result<T>>;
28+
/**
29+
* Evaluate an operation within this scope.
30+
*
31+
* The operation runs in the spawned scope, so any context values it sets
32+
* will be visible via `scope.get(context)`.
33+
*
34+
* @param op - A function returning the operation to evaluate
35+
* @returns The result of the operation (Ok or Err)
36+
*/
37+
eval<T>(op: () => Operation<T>): Operation<Result<T>>;
3838
}
3939

4040
interface CallEval {
41-
operation: () => Operation<unknown>;
42-
resolve: (result: Result<unknown>) => void;
41+
operation: () => Operation<unknown>;
42+
resolve: (result: Result<unknown>) => void;
4343
}
4444

4545
/**
@@ -73,46 +73,46 @@ interface CallEval {
7373
* ```
7474
*/
7575
export function* useEvalScope(): Operation<EvalScope> {
76-
const scopeResolver = withResolvers<Scope>();
77-
const readyResolver = withResolvers<void>();
78-
const operations = createChannel<CallEval, never>();
76+
const scopeResolver = withResolvers<Scope>();
77+
const readyResolver = withResolvers<void>();
78+
const operations = createChannel<CallEval, never>();
7979

80-
yield* spawn(function* () {
81-
scopeResolver.resolve(yield* useScope());
82-
83-
// Get subscription to the channel
84-
const subscription = yield* (operations as Stream<CallEval, never>);
85-
86-
// Signal that we're ready to receive
87-
readyResolver.resolve();
88-
89-
// Process operations as they come in
90-
while (true) {
91-
const next = yield* subscription.next();
92-
if (next.done) {
93-
break;
94-
}
95-
const call = next.value;
96-
const result = yield* box(call.operation);
97-
call.resolve(result);
98-
}
99-
});
80+
yield* spawn(function* () {
81+
scopeResolver.resolve(yield* useScope());
10082

101-
// Wait for the scope to be available
102-
const scope = yield* scopeResolver.operation;
103-
104-
// Wait for the spawned task to be ready to receive
105-
yield* readyResolver.operation;
83+
// Get subscription to the channel
84+
const subscription = yield* operations as Stream<CallEval, never>;
10685

107-
return {
108-
scope,
109-
*eval<T>(operation: () => Operation<T>): Operation<Result<T>> {
110-
const resolver = withResolvers<Result<T>>();
111-
yield* operations.send({
112-
resolve: resolver.resolve as (result: Result<unknown>) => void,
113-
operation: operation as () => Operation<unknown>,
114-
});
115-
return yield* resolver.operation;
116-
},
117-
};
86+
// Signal that we're ready to receive
87+
readyResolver.resolve();
88+
89+
// Process operations as they come in
90+
while (true) {
91+
const next = yield* subscription.next();
92+
if (next.done) {
93+
break;
94+
}
95+
const call = next.value;
96+
const result = yield* box(call.operation);
97+
call.resolve(result);
98+
}
99+
});
100+
101+
// Wait for the scope to be available
102+
const scope = yield* scopeResolver.operation;
103+
104+
// Wait for the spawned task to be ready to receive
105+
yield* readyResolver.operation;
106+
107+
return {
108+
scope,
109+
*eval<T>(operation: () => Operation<T>): Operation<Result<T>> {
110+
const resolver = withResolvers<Result<T>>();
111+
yield* operations.send({
112+
resolve: resolver.resolve as (result: Result<unknown>) => void,
113+
operation: operation as () => Operation<unknown>,
114+
});
115+
return yield* resolver.operation;
116+
},
117+
};
118118
}

0 commit comments

Comments
 (0)