forked from Effect-TS/effect
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add Runtime.updateFiberRefs/setFiberRef/deleteFiberRef (Effect-TS#2149)
- Loading branch information
Showing
4 changed files
with
142 additions
and
0 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,20 @@ | ||
--- | ||
"effect": patch | ||
--- | ||
|
||
add Runtime.updateFiberRefs/setFiberRef/deleteFiberRef | ||
|
||
This change allows you to update fiber ref values inside a Runtime object. | ||
|
||
Example: | ||
|
||
```ts | ||
import { Effect, FiberRef, Runtime } from "effect"; | ||
|
||
const ref = FiberRef.unsafeMake(0); | ||
|
||
const updatedRuntime = Runtime.defaultRuntime.pipe(Runtime.setFiberRef(ref, 1)); | ||
|
||
// returns 1 | ||
const result = Runtime.runSync(updatedRuntime)(FiberRef.get(ref)); | ||
``` |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { Effect, FiberRef, Layer, Runtime } from "effect" | ||
import { assert, describe } from "vitest" | ||
import * as it from "./utils/extend.js" | ||
|
||
describe("Runtime", () => { | ||
it.effect("setFiberRef", () => | ||
Effect.gen(function*(_) { | ||
const ref = FiberRef.unsafeMake(0) | ||
const runtime = Runtime.defaultRuntime.pipe( | ||
Runtime.setFiberRef(ref, 1) | ||
) | ||
let result = Runtime.runSync(runtime)(FiberRef.get(ref)) | ||
assert.strictEqual(result, 1) | ||
|
||
result = yield* _(FiberRef.get(ref), Effect.provide(runtime)) | ||
assert.strictEqual(result, 1) | ||
})) | ||
|
||
it.scoped("deleteFiberRef", () => | ||
Effect.gen(function*(_) { | ||
const ref = FiberRef.unsafeMake({ value: 0 }) | ||
const runtime = yield* _(Layer.toRuntime(Layer.effectDiscard(FiberRef.set(ref, { value: 1 })))) | ||
|
||
let result = Runtime.runSync(runtime)(FiberRef.get(ref)) | ||
assert.deepStrictEqual(result, { value: 1 }) | ||
|
||
result = Runtime.runSync(Runtime.deleteFiberRef(runtime, ref))(FiberRef.get(ref)) | ||
assert.deepStrictEqual(result, { value: 0 }) | ||
})) | ||
}) |