-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
89 additions
and
32 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,5 @@ | ||
--- | ||
"@effect-app/infra": minor | ||
--- | ||
|
||
context map container |
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,49 @@ | ||
import { ContextMap, makeContextMap } from "./service.js" | ||
|
||
// TODO: we have to create a new contextmap on every request. | ||
// we want to share one map during startup | ||
// but we want to make sure we don't re-use the startup map after startup | ||
// we can call another start after startup. but it would be even better if we could Die on accessing rootmap | ||
// we could also make the ContextMap optional, and when missing, issue a warning instead? | ||
|
||
/** | ||
* @tsplus companion ContextMapContainer.Ops | ||
*/ | ||
export abstract class ContextMapContainer extends TagClass<ContextMapContainer>() { | ||
abstract readonly get: Effect<never, never, ContextMap> | ||
abstract readonly start: Effect<never, never, void> | ||
static get get(): Effect<ContextMapContainer, never, ContextMap> { | ||
return ContextMapContainer.flatMap((_) => _.get) | ||
} | ||
static get getOption() { | ||
return Effect | ||
.contextWith((_: Context<never>) => Context.getOption(_, ContextMapContainer)) | ||
.flatMap((ctx) => | ||
ctx.isSome() | ||
? ctx.value.get.map(Option.some) | ||
: Effect(Option.none) | ||
) | ||
} | ||
} | ||
|
||
export class ContextMapContainerImpl extends ContextMapContainer { | ||
#ref: FiberRef<ContextMap> | ||
constructor() { | ||
super() | ||
this.#ref = FiberRef.unsafeMake<ContextMap>(makeContextMap()) | ||
} | ||
|
||
override get get() { | ||
return this.#ref.get | ||
} | ||
|
||
override start = ContextMap.Make.flatMap((_) => this.#ref.set(_)) | ||
} | ||
|
||
/** | ||
* @tsplus static ContextMapContainer.Ops live | ||
*/ | ||
export const live = Effect.sync(() => new ContextMapContainerImpl()).toLayer(ContextMapContainer) | ||
|
||
/** @tsplus static ContextMap.Ops Tag */ | ||
export const RCTag = Tag<ContextMap>() |
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