// Context.ts
// Define fiber local storage for `Foo`.
const FooContext = Fiber.makeContext<Foo>()
// MyCode.ts
// fiber1 enters with the default Foo (which is `null`)
const fiber1 = yield* Effect.fork(Effect.gen(function* () {
const foo = yield* FooContext.get; // foo will be null
}))
// fiber2 enters with a given Foo...
const fiber2 = yield* Effect.fork(Effect.gen(function* () {
// ... which will be provided via the context accessor.
const foo = yield* FooContext.get; // foo will be a Foo based on 100
})).pipe(
Fiber.enterWith(FooContext, Foo.make(100))
)
// effect1 has a local 'override' so no matter where it runs (even inside one of the above
// forks), it will access that specific Foo.
const effect1 = yield* Effect.gen(function* () {
const foo = yield* FooContext.get; // foo will be a Foo based on 200
}).pipe(
Effect.withContext(FooContext, Foo.make(200))
// ..or
Effect.withContextScoped(FooContext, Foo.make(200))
)
// Context.ts
// Define fiber local storage for `Foo`.
const FooContext = Fiber.makeContext<Foo>()
// MyCode.ts
// fiber1 enters with the default Foo (which is `null`)
const fiber1 = yield* Effect.fork(Effect.gen(function* () {
const foo = yield* FooContext.get; // foo will be null
}))
// fiber2 enters with a given Foo...
const fiber2 = yield* Effect.fork(Effect.gen(function* () {
// ... which will be provided via the context accessor.
const foo = yield* FooContext.get; // foo will be a Foo based on 100
})).pipe(
Fiber.enterWith(FooContext, Foo.make(100))
)
// effect1 has a local 'override' so no matter where it runs (even inside one of the above
// forks), it will access that specific Foo.
const effect1 = yield* Effect.gen(function* () {
const foo = yield* FooContext.get; // foo will be a Foo based on 200
}).pipe(
Effect.withContext(FooContext, Foo.make(200))
// ..or
Effect.withContextScoped(FooContext, Foo.make(200))
)