Skip to content

Commit 282ed4f

Browse files
committed
allow using an atom for store options
1 parent 564d073 commit 282ed4f

File tree

3 files changed

+49
-15
lines changed

3 files changed

+49
-15
lines changed

.changeset/jolly-plums-throw.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@effect-atom/atom-livestore": patch
3+
---
4+
5+
allow using an atom for store options

docs/atom-livestore/AtomLivestore.ts.md

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ Added in v1.0.0
1616
- [Tag](#tag)
1717
- [Models](#models)
1818
- [AtomLiveStore (interface)](#atomlivestore-interface)
19+
- [Options (type alias)](#options-type-alias)
1920

2021
---
2122

@@ -28,7 +29,7 @@ Added in v1.0.0
2829
```ts
2930
export declare const Tag: <Self>() => <const Id extends string, S extends LiveStoreSchema, Context = {}>(
3031
id: Id,
31-
options: CreateStoreOptions<S, Context> & { readonly otelOptions?: Partial<OtelOptions> | undefined }
32+
options: Options<Id, S, Context> | Atom.Atom<Options<Id, S, Context>>
3233
) => AtomLiveStore<Self, Id, S, Context>
3334
```
3435
@@ -45,7 +46,7 @@ export interface AtomLiveStore<Self, Id extends string, S extends LiveStoreSchem
4546
extends Context.Tag<Self, Store<S, Context>> {
4647
new (_: never): Context.TagClassShape<Id, Store<S, Context>>
4748

48-
readonly layer: Layer.Layer<Self>
49+
readonly layer: Atom.Atom<Layer.Layer<Self>>
4950
readonly runtime: Atom.AtomRuntime<Self>
5051

5152
/**
@@ -80,3 +81,18 @@ export interface AtomLiveStore<Self, Id extends string, S extends LiveStoreSchem
8081
```
8182

8283
Added in v1.0.0
84+
85+
## Options (type alias)
86+
87+
**Signature**
88+
89+
```ts
90+
export type Options<const Id extends string, S extends LiveStoreSchema, Context = {}> = CreateStoreOptions<
91+
S,
92+
Context
93+
> & {
94+
readonly otelOptions?: Partial<OtelOptions> | undefined
95+
}
96+
```
97+
98+
Added in v1.0.0

packages/atom-livestore/src/AtomLivestore.ts

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export interface AtomLiveStore<Self, Id extends string, S extends LiveStoreSchem
2323
_: never
2424
): Context.TagClassShape<Id, Store<S, Context>>
2525

26-
readonly layer: Layer.Layer<Self>
26+
readonly layer: Atom.Atom<Layer.Layer<Self>>
2727
readonly runtime: Atom.AtomRuntime<Self>
2828

2929
/**
@@ -62,30 +62,43 @@ declare global {
6262
}
6363
}
6464

65+
/**
66+
* @since 1.0.0
67+
* @category Models
68+
*/
69+
export type Options<const Id extends string, S extends LiveStoreSchema, Context = {}> =
70+
& CreateStoreOptions<S, Context>
71+
& {
72+
readonly otelOptions?: Partial<OtelOptions> | undefined
73+
}
74+
6575
/**
6676
* @since 1.0.0
6777
* @category Constructors
6878
*/
6979
export const Tag = <Self>() =>
7080
<const Id extends string, S extends LiveStoreSchema, Context = {}>(
7181
id: Id,
72-
options: CreateStoreOptions<S, Context> & {
73-
readonly otelOptions?: Partial<OtelOptions> | undefined
74-
}
82+
options: Options<Id, S, Context> | Atom.Atom<Options<Id, S, Context>>
7583
): AtomLiveStore<Self, Id, S, Context> => {
7684
const self: Mutable<AtomLiveStore<Self, Id, S, Context>> = Context.Tag(id)<Self, Store<S, Context>>() as any
7785

78-
self.layer = Layer.scoped(
79-
self,
80-
createStore(options).pipe(
81-
provideOtel({
82-
parentSpanContext: options?.otelOptions?.rootSpanContext,
83-
otelTracer: options?.otelOptions?.tracer
84-
}),
85-
Effect.orDie
86+
const layerFromOptions = (opts: Options<Id, S, Context>) =>
87+
Layer.scoped(
88+
self,
89+
createStore(opts).pipe(
90+
provideOtel({
91+
parentSpanContext: opts?.otelOptions?.rootSpanContext,
92+
otelTracer: opts?.otelOptions?.tracer
93+
}),
94+
Effect.orDie
95+
)
8696
)
97+
98+
self.runtime = Atom.runtime(
99+
Atom.isAtom(options) ? (get) => layerFromOptions(get(options)) : layerFromOptions(options)
87100
)
88-
self.runtime = Atom.runtime(self.layer)
101+
self.layer = self.runtime.layer
89102
self.store = self.runtime.atom(Effect.contextWith(Context.get(self)) as any)
90103
self.storeUnsafe = Atom.readable((get) => {
91104
const result = get(self.store)

0 commit comments

Comments
 (0)