@systemfsoftware/effect-atom 0.5.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,1287 @@
1
+ import { L as Result } from "./Result-BHH-qTvm.js";
2
+ import * as Arr from "effect/Array";
3
+ import * as Cause from "effect/Cause";
4
+ import * as Context from "effect/Context";
5
+ import * as Duration from "effect/Duration";
6
+ import * as Effect from "effect/Effect";
7
+ import { LazyArg } from "effect/Function";
8
+ import * as Layer from "effect/Layer";
9
+ import * as Option from "effect/Option";
10
+ import { Scheduler } from "effect/Scheduler";
11
+ import * as Schema from "effect/Schema";
12
+ import * as Scope from "effect/Scope";
13
+ import * as Stream from "effect/Stream";
14
+ import * as SubscriptionRef from "effect/SubscriptionRef";
15
+ import * as KeyValueStore from "effect/unstable/persistence/KeyValueStore";
16
+ import * as Reactivity from "effect/unstable/reactivity/Reactivity";
17
+ import { Inspectable } from "effect/Inspectable";
18
+ import { Pipeable } from "effect/Pipeable";
19
+ import { ReadonlyRecord } from "effect/Record";
20
+ import { NoInfer } from "effect/Types";
21
+ //#region src/internal/Core.d.ts
22
+ /**
23
+ * Type-level identifier used to recognize `Atom` values.
24
+ *
25
+ * @category type IDs
26
+ * @since 4.0.0
27
+ */
28
+ type TypeId$1 = '~effect/reactivity/Atom';
29
+ /**
30
+ * Runtime identifier attached to `Atom` values and used by `isAtom`.
31
+ *
32
+ * @category type IDs
33
+ * @since 4.0.0
34
+ */
35
+ declare const TypeId$1: TypeId$1;
36
+ /**
37
+ * Type-level identifier used to recognize writable atoms.
38
+ *
39
+ * @category type IDs
40
+ * @since 4.0.0
41
+ */
42
+ type WritableTypeId = '~effect/reactivity/Atom/Writable';
43
+ /**
44
+ * Runtime identifier attached to writable atoms and used by `isWritable`.
45
+ *
46
+ * @category type IDs
47
+ * @since 4.0.0
48
+ */
49
+ declare const WritableTypeId: WritableTypeId;
50
+ /**
51
+ * Returns `true` when a value is an `Atom`.
52
+ *
53
+ * @category guards
54
+ * @since 4.0.0
55
+ */
56
+ declare const isAtom: (u: unknown) => u is Atom<unknown>;
57
+ /**
58
+ * Returns a copy of an atom with an idle time-to-live: finite durations dispose it after inactivity, while an infinite duration keeps it alive.
59
+ *
60
+ * @category combinators
61
+ * @since 4.0.0
62
+ */
63
+ declare const setIdleTTL: {
64
+ (duration: Duration.Input): <A extends Atom<unknown>>(self: A) => A;
65
+ <A extends Atom<unknown>>(self: A, duration: Duration.Input): A;
66
+ };
67
+ /**
68
+ * Returns `true` when an atom is writable.
69
+ *
70
+ * @category guards
71
+ * @since 4.0.0
72
+ */
73
+ declare const isWritable: <R, W>(atom: Atom<R>) => atom is Writable<R, W>;
74
+ /**
75
+ * Creates a read-only atom from a read function and an optional custom refresh registration callback.
76
+ *
77
+ * @category constructors
78
+ * @since 4.0.0
79
+ */
80
+ declare const readable: <A>(read: (get: AtomContext) => A, refresh?: (f: <A_1>(atom: Atom<A_1>) => void) => void) => Atom<A>;
81
+ /**
82
+ * Creates a writable atom from read and write functions, with an optional custom refresh registration callback.
83
+ *
84
+ * @category constructors
85
+ * @since 4.0.0
86
+ */
87
+ declare const writable: <R, W>(read: (get: AtomContext) => R, write: (ctx: WriteContext<R>, value: W) => void, refresh?: (f: <A>(atom: Atom<A>) => void) => void) => Writable<R, W>;
88
+ /**
89
+ * Creates a derived atom by reading another atom with a custom `AtomContext`
90
+ * function.
91
+ *
92
+ * **Details**
93
+ *
94
+ * If the source is writable, the derived atom keeps the source write input and
95
+ * forwards writes to the source. `initialValueTarget` controls which atom receives
96
+ * preloaded initial values for the derived atom.
97
+ *
98
+ * @category combinators
99
+ * @since 4.0.0
100
+ */
101
+ declare const transform: {
102
+ <R extends Atom<unknown>, B>(f: (get: AtomContext, atom: R) => B, options?: {
103
+ readonly initialValueTarget?: Atom<B> | undefined;
104
+ }): (self: R) => [R] extends [Writable<infer _, infer RW>] ? Writable<B, RW> : Atom<B>;
105
+ <R extends Atom<unknown>, B>(self: R, f: (get: AtomContext, atom: R) => B, options?: {
106
+ readonly initialValueTarget?: Atom<B> | undefined;
107
+ }): [R] extends [Writable<infer _, infer RW>] ? Writable<B, RW> : Atom<B>;
108
+ };
109
+ declare namespace Registry_d_exports {
110
+ export { AtomRegistry, Node, Registry, TypeId, getResult$1 as getResult, isAtomRegistry, layer, layerOptions, make$1 as make, mount$1 as mount, toStream$1 as toStream, toStreamResult$1 as toStreamResult };
111
+ }
112
+ /**
113
+ * The literal type used to identify `AtomRegistry` services and values.
114
+ *
115
+ * @category type IDs
116
+ * @since 4.0.0
117
+ */
118
+ type TypeId = '~effect-atom/atom/Registry';
119
+ /**
120
+ * The runtime type id used to identify `AtomRegistry` services and values.
121
+ *
122
+ * @category type IDs
123
+ * @since 4.0.0
124
+ */
125
+ declare const TypeId: TypeId;
126
+ /**
127
+ * Returns `true` when the value has the `AtomRegistry` type id.
128
+ *
129
+ * @category guards
130
+ * @since 4.0.0
131
+ */
132
+ declare const isAtomRegistry: (u: unknown) => u is Registry;
133
+ /**
134
+ * The runtime registry that stores atom nodes and coordinates reads, writes,
135
+ * refreshes, subscriptions, and disposal.
136
+ *
137
+ * **Details**
138
+ *
139
+ * It also manages scheduler configuration, serializable preloaded values, and node
140
+ * addition/removal callbacks.
141
+ *
142
+ * @category models
143
+ * @since 4.0.0
144
+ */
145
+ interface Registry {
146
+ readonly [TypeId]: TypeId;
147
+ readonly scheduler: Scheduler;
148
+ readonly schedulerAsync: Scheduler;
149
+ /**
150
+ * The clock and delayed-callback scheduler every time-dependent atom on this
151
+ * registry reads. Supplied to `make`, so one substitution drives idle-TTL
152
+ * eviction, `Atom.debounce` and `Atom.swr` staleness together.
153
+ */
154
+ readonly now: () => number;
155
+ readonly scheduleTimer: (f: () => void, delayMillis: number) => () => void;
156
+ readonly getNodes: () => ReadonlyMap<Atom<unknown> | string, Node<unknown>>;
157
+ readonly get: <A>(atom: Atom<A>) => A;
158
+ /**
159
+ * Returns the current value of an atom when its node has been initialized, without rebuilding a stale or uninitialized node.
160
+ *
161
+ * @since 4.0.0
162
+ */
163
+ readonly getRaw: <A>(atom: Atom<A>) => Option.Option<A>;
164
+ readonly mount: <A>(atom: Atom<A>) => () => void;
165
+ readonly refresh: <A>(atom: Atom<A>) => void;
166
+ readonly set: <R, W>(atom: Writable<R, W>, value: W) => void;
167
+ readonly setSerializable: (key: string, encoded: unknown) => void;
168
+ readonly setInitialValue: <A>(atom: Atom<A>, value: A) => void;
169
+ readonly modify: <R, W, A>(atom: Writable<R, W>, f: (_: R) => [returnValue: A, nextValue: W]) => A;
170
+ readonly update: <R, W>(atom: Writable<R, W>, f: (_: R) => W) => void;
171
+ readonly subscribe: <A>(atom: Atom<A>, f: (_: A) => void, options?: {
172
+ readonly immediate?: boolean;
173
+ }) => () => void;
174
+ readonly reset: () => void;
175
+ readonly dispose: () => void;
176
+ onNodeAdded?: ((node: Node<unknown>) => void) | undefined;
177
+ onNodeRemoved?: ((node: Node<unknown>) => void) | undefined;
178
+ }
179
+ /**
180
+ * A registry node for a single atom.
181
+ *
182
+ * **Details**
183
+ *
184
+ * Nodes expose the current value, parent and child dependency links, listener set,
185
+ * and current lifecycle state.
186
+ *
187
+ * @category models
188
+ * @since 4.0.0
189
+ */
190
+ interface Node<A> {
191
+ readonly atom: Atom<A>;
192
+ readonly value: () => A;
193
+ /**
194
+ * Read-only views of the registry's dependency graph. The registry mutates
195
+ * these sets internally; consumers can inspect them but must not coordinate
196
+ * through them.
197
+ */
198
+ readonly parents: ReadonlySet<Node<unknown>>;
199
+ readonly children: ReadonlySet<Node<unknown>>;
200
+ readonly listeners: ReadonlySet<() => void>;
201
+ currentState(): 'uninitialized' | 'stale' | 'valid' | 'removed';
202
+ }
203
+ /**
204
+ * Creates an `AtomRegistry`.
205
+ *
206
+ * **Details**
207
+ *
208
+ * Options can preload initial atom values, provide a custom task scheduler,
209
+ * configure timeout bucket resolution, and set a default idle time-to-live for
210
+ * unused atoms.
211
+ *
212
+ * @category constructors
213
+ * @since 4.0.0
214
+ */
215
+ declare const make$1: (options?: {
216
+ readonly initialValues?: Iterable<readonly [Atom<unknown>, unknown]> | undefined;
217
+ readonly scheduleTask?: ((f: () => void) => () => void) | undefined;
218
+ readonly timeoutResolution?: number | undefined;
219
+ readonly defaultIdleTTL?: number | undefined;
220
+ readonly now?: (() => number) | undefined;
221
+ readonly scheduleTimer?: ((f: () => void, delayMillis: number) => () => void) | undefined;
222
+ }) => Registry;
223
+ declare const AtomRegistry_base: Context.ServiceClass<AtomRegistry, "~effect-atom/atom/Registry", Registry>;
224
+ /**
225
+ * Service tag for the active atom runtime cache.
226
+ *
227
+ * **When to use**
228
+ *
229
+ * Use to access or provide the registry that stores atom values,
230
+ * dependencies, subscriptions, and disposal state for a reactive lifetime.
231
+ *
232
+ * @category services
233
+ * @since 4.0.0
234
+ */
235
+ declare class AtomRegistry extends AtomRegistry_base {}
236
+ /**
237
+ * Creates a layer that provides an `AtomRegistry` configured with the supplied
238
+ * options.
239
+ *
240
+ * **Details**
241
+ *
242
+ * The registry is disposed when the layer scope is finalized.
243
+ *
244
+ * @category layers
245
+ * @since 4.0.0
246
+ */
247
+ declare const layerOptions: (options?: {
248
+ readonly initialValues?: Iterable<readonly [Atom<unknown>, unknown]> | undefined;
249
+ readonly scheduleTask?: ((f: () => void) => () => void) | undefined;
250
+ readonly timeoutResolution?: number | undefined;
251
+ readonly defaultIdleTTL?: number | undefined;
252
+ readonly now?: (() => number) | undefined;
253
+ readonly scheduleTimer?: ((f: () => void, delayMillis: number) => () => void) | undefined;
254
+ }) => Layer.Layer<AtomRegistry>;
255
+ /**
256
+ * The default layer that provides a fresh `AtomRegistry`.
257
+ *
258
+ * @category layers
259
+ * @since 4.0.0
260
+ */
261
+ declare const layer: Layer.Layer<AtomRegistry>;
262
+ /**
263
+ * Converts an atom in this registry into a stream.
264
+ *
265
+ * **Details**
266
+ *
267
+ * The stream emits the current value immediately, emits subsequent changes, and
268
+ * unsubscribes from the registry when the stream scope closes.
269
+ *
270
+ * @category converting
271
+ * @since 4.0.0
272
+ */
273
+ declare const toStream$1: {
274
+ <A>(atom: Atom<A>): (self: Registry) => Stream.Stream<A>;
275
+ <A>(self: Registry, atom: Atom<A>): Stream.Stream<A>;
276
+ };
277
+ /**
278
+ * Converts an `AsyncResult` atom in this registry into a stream of successful
279
+ * values.
280
+ *
281
+ * **Details**
282
+ *
283
+ * Initial results are skipped, failures fail the stream with their cause, and
284
+ * duplicate stream values are dropped with `Stream.changes`.
285
+ *
286
+ * @category converting
287
+ * @since 4.0.0
288
+ */
289
+ declare const toStreamResult$1: {
290
+ <A, E>(atom: Atom<Result<A, E>>): (self: Registry) => Stream.Stream<A, E>;
291
+ <A, E>(self: Registry, atom: Atom<Result<A, E>>): Stream.Stream<A, E>;
292
+ };
293
+ /**
294
+ * Reads an `AsyncResult` atom from this registry as an effect.
295
+ *
296
+ * **Details**
297
+ *
298
+ * The effect waits for the result to leave `Initial`, and also waits through
299
+ * waiting results when `suspendOnWaiting` is enabled.
300
+ *
301
+ * @category converting
302
+ * @since 4.0.0
303
+ */
304
+ declare const getResult$1: {
305
+ <A, E>(atom: Atom<Result<A, E>>, options?: {
306
+ readonly suspendOnWaiting?: boolean | undefined;
307
+ }): (self: Registry) => Effect.Effect<A, E>;
308
+ <A, E>(self: Registry, atom: Atom<Result<A, E>>, options?: {
309
+ readonly suspendOnWaiting?: boolean | undefined;
310
+ }): Effect.Effect<A, E>;
311
+ };
312
+ /**
313
+ * Mounts an atom in this registry for the lifetime of the current scope.
314
+ *
315
+ * **Details**
316
+ *
317
+ * The atom is subscribed with a no-op listener and the subscription is released
318
+ * when the scope finalizer runs.
319
+ *
320
+ * @category converting
321
+ * @since 4.0.0
322
+ */
323
+ declare const mount$1: {
324
+ <A>(atom: Atom<A>): (self: Registry) => Effect.Effect<void, never, Scope.Scope>;
325
+ <A>(self: Registry, atom: Atom<A>): Effect.Effect<void, never, Scope.Scope>;
326
+ };
327
+ //#endregion
328
+ //#region src/Browser.d.ts
329
+ /**
330
+ * Creates a browser-only signal atom that increments when the document becomes visible.
331
+ *
332
+ * **Details**
333
+ *
334
+ * It listens for `visibilitychange` events on `window` and removes the listener
335
+ * when the atom is disposed.
336
+ *
337
+ * @category constants
338
+ * @since 4.0.0
339
+ */
340
+ declare const windowFocusSignal: Atom<number>;
341
+ /**
342
+ * Creates a combinator that refreshes an atom whenever the supplied signal atom
343
+ * changes.
344
+ *
345
+ * **Details**
346
+ *
347
+ * The derived atom also subscribes to the source atom so normal source updates are
348
+ * forwarded to its own value.
349
+ *
350
+ * @category constructors
351
+ * @since 4.0.0
352
+ */
353
+ declare const makeRefreshOnSignal: <S>(signal: Atom<S>) => <A extends Atom<unknown>>(self: A) => WithoutSerializable<A>;
354
+ /**
355
+ * Refreshes an atom whenever `windowFocusSignal` changes.
356
+ *
357
+ * **Details**
358
+ *
359
+ * This helper is browser-only because `windowFocusSignal` depends on `window` and
360
+ * `document.visibilityState`.
361
+ *
362
+ * @category combinators
363
+ * @since 4.0.0
364
+ */
365
+ declare const refreshOnWindowFocus: <A extends Atom<unknown>>(self: A) => WithoutSerializable<A>;
366
+ /**
367
+ * Creates an atom that reads and writes a URL search parameter.
368
+ *
369
+ * **Gotchas**
370
+ *
371
+ * If you pass a schema, it has to be synchronous and have no context.
372
+ *
373
+ * @category constructors
374
+ * @since 4.0.0
375
+ */
376
+ declare function searchParam<S extends Schema.ConstraintCodec<unknown, string> = never>(name: string, options?: {
377
+ readonly schema?: S | undefined;
378
+ }): Writable<[S] extends [never] ? string : Option.Option<S['Type']>>;
379
+ //#endregion
380
+ //#region src/Server.d.ts
381
+ /**
382
+ * The type id used to mark atoms with a server-side read override.
383
+ *
384
+ * @category type IDs
385
+ * @since 4.0.0
386
+ */
387
+ declare const ServerValueTypeId: '~effect-atom/atom/Atom/ServerValue';
388
+ /**
389
+ * Server-side read override attached to an atom by `withServerValue`.
390
+ *
391
+ * @category models
392
+ * @since 4.0.0
393
+ */
394
+ type ServerValue<A> = {
395
+ readonly [ServerValueTypeId]: (get: <A2>(atom: Atom<A2>) => A2) => A;
396
+ };
397
+ /**
398
+ * Sets the value of an Atom when read on the server.
399
+ *
400
+ * @category transforming
401
+ * @since 4.0.0
402
+ */
403
+ declare const withServerValue: {
404
+ <A extends Atom<unknown>>(read: (get: <A2>(atom: Atom<A2>) => A2) => Type<A>): (self: A) => A;
405
+ <A extends Atom<unknown>>(self: A, read: (get: <A2>(atom: Atom<A2>) => A2) => Type<A>): A;
406
+ };
407
+ /**
408
+ * Sets an `AsyncResult` atom's server-side value to
409
+ * `AsyncResult.initial(true)`.
410
+ *
411
+ * @category transforming
412
+ * @since 4.0.0
413
+ */
414
+ declare const withServerValueInitial: <A extends Atom<Result<unknown, unknown>>>(self: A) => A;
415
+ /**
416
+ * Reads an atom from a registry, using its server-side read override when one is
417
+ * present.
418
+ *
419
+ * **Details**
420
+ *
421
+ * Nested reads performed by the override are resolved against the same registry.
422
+ *
423
+ * @category getters
424
+ * @since 4.0.0
425
+ */
426
+ declare const getServerValue: {
427
+ (registry: Registry): <A>(self: Atom<A>) => A;
428
+ <A>(self: Atom<A>, registry: Registry): A;
429
+ };
430
+ declare namespace Atom_d_exports {
431
+ export { Atom, AtomContext, AtomResultFn, AtomRuntime, Failure, FnContext, Interrupt, PullResult, PullSuccess, RegistryRuntimeFactory, Reset, RuntimeFactory, Serializable, SerializableJson, SerializableTypeId, ServerValue, ServerValueTypeId, SharedRuntimeFactory, Success, Type, TypeId$1 as TypeId, WithoutSerializable, Writable, WritableTypeId, WriteContext, autoDispose, batch, context, debounce, family, fn, fnSync, get, getResult, getServerValue, initialValue, isAtom, isSerializable, isWritable, keepAlive, kvs, make, makeRead, makeRefreshOnSignal, map, mapResult, modify, mount, optimistic, optimisticFn, pull, readable, refresh, refreshOnWindowFocus, runtime, searchParam, serializable, set, setIdleTTL, setLazy, subscriptionRef, swr, toStream, toStreamResult, transform, update, windowFocusSignal, withEquality, withFallback, withLabel, withReactivity, withRefresh, withServerValue, withServerValueInitial, writable };
432
+ }
433
+ /**
434
+ * Reactive value read by an `AtomRegistry`, with metadata controlling caching, laziness, refresh behavior, and initial value targeting.
435
+ *
436
+ * @category models
437
+ * @since 4.0.0
438
+ */
439
+ interface Atom<A> extends Pipeable, Inspectable {
440
+ readonly [TypeId$1]: TypeId$1;
441
+ readonly keepAlive: boolean;
442
+ readonly lazy: boolean;
443
+ readonly read: (get: AtomContext) => A;
444
+ equals(value: A, next: A): boolean;
445
+ readonly refresh?: ((f: <A>(atom: Atom<A>) => void) => void) | undefined;
446
+ readonly label?: readonly [name: string, stack: string];
447
+ readonly idleTTL?: number;
448
+ readonly initialValueTarget?: Atom<A>;
449
+ }
450
+ /**
451
+ * Extracts the value type produced by an `Atom`.
452
+ *
453
+ * @category utility types
454
+ * @since 4.0.0
455
+ */
456
+ type Type<T extends Atom<unknown>> = T extends Atom<infer A> ? A : never;
457
+ /**
458
+ * Extracts the success value type from an atom whose value is an `AsyncResult`.
459
+ *
460
+ * @category utility types
461
+ * @since 4.0.0
462
+ */
463
+ type Success<T extends Atom<unknown>> = T extends Atom<Result<infer A, infer _>> ? A : never;
464
+ /**
465
+ * Extracts the item type from an atom whose value is a `PullResult`.
466
+ *
467
+ * @category utility types
468
+ * @since 4.0.0
469
+ */
470
+ type PullSuccess<T extends Atom<unknown>> = T extends Atom<PullResult<infer A, infer _>> ? A : never;
471
+ /**
472
+ * Extracts the failure error type from an atom whose value is an `AsyncResult`.
473
+ *
474
+ * @category utility types
475
+ * @since 4.0.0
476
+ */
477
+ type Failure<T extends Atom<unknown>> = T extends Atom<Result<infer _, infer E>> ? E : never;
478
+ /**
479
+ * Returns an atom type without serializable metadata, preserving `Writable` read and write types when the input atom is writable.
480
+ *
481
+ * @category utility types
482
+ * @since 4.0.0
483
+ */
484
+ type WithoutSerializable<T extends Atom<unknown>> = T extends Writable<infer R, infer W> ? Writable<R, W> : Atom<Type<T>>;
485
+ /**
486
+ * Atom that can also be written to, using a `WriteContext` and an input value to update reactive state.
487
+ *
488
+ * @category models
489
+ * @since 4.0.0
490
+ */
491
+ interface Writable<R, W = R> extends Atom<R> {
492
+ readonly [WritableTypeId]: WritableTypeId;
493
+ readonly write: (ctx: WriteContext<R>, value: W) => void;
494
+ }
495
+ /**
496
+ * Context passed to atom read functions for reading dependencies, awaiting `AsyncResult` or `Option` values, managing subscriptions and finalizers, refreshing atoms, and updating writable atoms.
497
+ *
498
+ * @category context
499
+ * @since 4.0.0
500
+ */
501
+ interface AtomContext {
502
+ <A>(atom: Atom<A>): A;
503
+ get<A>(this: AtomContext, atom: Atom<A>): A;
504
+ result<A, E>(this: AtomContext, atom: Atom<Result<A, E>>, options?: {
505
+ readonly suspendOnWaiting?: boolean | undefined;
506
+ }): Effect.Effect<A, E>;
507
+ resultOnce<A, E>(this: AtomContext, atom: Atom<Result<A, E>>, options?: {
508
+ readonly suspendOnWaiting?: boolean | undefined;
509
+ }): Effect.Effect<A, E>;
510
+ once<A>(this: AtomContext, atom: Atom<A>): A;
511
+ addFinalizer(this: AtomContext, f: () => void): void;
512
+ mount<A>(this: AtomContext, atom: Atom<A>): void;
513
+ refresh<A>(this: AtomContext, atom: Atom<A>): void;
514
+ refreshSelf(this: AtomContext): void;
515
+ self<A>(this: AtomContext): Option.Option<A>;
516
+ setSelf<A>(this: AtomContext, a: A): void;
517
+ set<R, W>(this: AtomContext, atom: Writable<R, W>, value: W): void;
518
+ setResult<A, E, W>(this: AtomContext, atom: Writable<Result<A, E>, W>, value: W): Effect.Effect<A, E>;
519
+ some<A>(this: AtomContext, atom: Atom<Option.Option<A>>): Effect.Effect<A>;
520
+ someOnce<A>(this: AtomContext, atom: Atom<Option.Option<A>>): Effect.Effect<A>;
521
+ stream<A>(this: AtomContext, atom: Atom<A>, options?: {
522
+ readonly withoutInitialValue?: boolean;
523
+ readonly bufferSize?: number;
524
+ }): Stream.Stream<A>;
525
+ streamResult<A, E>(this: AtomContext, atom: Atom<Result<A, E>>, options?: {
526
+ readonly withoutInitialValue?: boolean;
527
+ readonly bufferSize?: number;
528
+ }): Stream.Stream<A, E>;
529
+ subscribe<A>(this: AtomContext, atom: Atom<A>, f: (_: A) => void, options?: {
530
+ readonly immediate?: boolean;
531
+ }): void;
532
+ isFn?: boolean | undefined;
533
+ readonly registry: Registry;
534
+ }
535
+ /**
536
+ * Context passed to writable atom write functions for reading atoms, refreshing or setting the current atom, and writing to other writable atoms.
537
+ *
538
+ * @category context
539
+ * @since 4.0.0
540
+ */
541
+ interface WriteContext<A> {
542
+ get<T>(this: WriteContext<A>, atom: Atom<T>): T;
543
+ refreshSelf(this: WriteContext<A>): void;
544
+ setSelf(this: WriteContext<A>, a: A): void;
545
+ set<R, W>(this: WriteContext<A>, atom: Writable<R, W>, value: W): void;
546
+ }
547
+ /**
548
+ * Creates an atom from a synchronous value or read function, or from an `Effect` or `Stream` whose state is exposed as an `AsyncResult`; plain values create writable state atoms.
549
+ *
550
+ * @category constructors
551
+ * @since 4.0.0
552
+ */
553
+ declare function make<A, E>(create: (get: AtomContext) => Effect.Effect<A, E, Scope.Scope | AtomRegistry>, options?: {
554
+ readonly initialValue?: A | undefined;
555
+ readonly uninterruptible?: boolean | undefined;
556
+ }): Atom<Result<A, E>>;
557
+ declare function make<A, E>(effect: Effect.Effect<A, E, Scope.Scope | AtomRegistry>, options?: {
558
+ readonly initialValue?: A;
559
+ readonly uninterruptible?: boolean | undefined;
560
+ }): Atom<Result<A, E>>;
561
+ declare function make<A, E>(create: (get: AtomContext) => Stream.Stream<A, E, AtomRegistry>, options?: {
562
+ readonly initialValue?: A;
563
+ }): Atom<Result<A, E | Cause.NoSuchElementError>>;
564
+ declare function make<A, E>(stream: Stream.Stream<A, E, AtomRegistry>, options?: {
565
+ readonly initialValue?: A;
566
+ }): Atom<Result<A, E | Cause.NoSuchElementError>>;
567
+ declare function make<A>(create: (get: AtomContext) => A): Atom<A>;
568
+ declare function make<A>(initialValue: A): Writable<A>;
569
+ declare function makeRead<A, E>(effect: Effect.Effect<A, E, Scope.Scope | AtomRegistry>, options?: {
570
+ readonly initialValue?: A;
571
+ readonly uninterruptible?: boolean | undefined;
572
+ }): (get: AtomContext, services?: Context.Context<never>) => Result<A, E>;
573
+ declare function makeRead<A, E>(create: (get: AtomContext) => Effect.Effect<A, E, Scope.Scope | AtomRegistry>, options?: {
574
+ readonly initialValue?: A;
575
+ readonly uninterruptible?: boolean | undefined;
576
+ }): (get: AtomContext, services?: Context.Context<never>) => Result<A, E>;
577
+ declare function makeRead<A, E>(stream: Stream.Stream<A, E, AtomRegistry>, options?: {
578
+ readonly initialValue?: A;
579
+ readonly uninterruptible?: boolean | undefined;
580
+ }): (get: AtomContext, services?: Context.Context<never>) => Result<A, E | Cause.NoSuchElementError>;
581
+ declare function makeRead<A, E>(create: (get: AtomContext) => Stream.Stream<A, E, AtomRegistry>, options?: {
582
+ readonly initialValue?: A;
583
+ readonly uninterruptible?: boolean | undefined;
584
+ }): (get: AtomContext, services?: Context.Context<never>) => Result<A, E | Cause.NoSuchElementError>;
585
+ declare function makeRead<A>(create: (get: AtomContext) => A): (get: AtomContext, services?: Context.Context<never>) => A;
586
+ declare function makeRead<A>(initialValue: A): Writable<A>;
587
+ /**
588
+ * Atom that builds a `Context` from a `Layer` and exposes constructors for atoms, functions, pulls, and subscription refs that run with that context.
589
+ *
590
+ * @category models
591
+ * @since 4.0.0
592
+ */
593
+ interface AtomRuntime<R, ER = never> extends Atom<Result<Context.Context<R>, ER>> {
594
+ readonly factory: RuntimeFactory;
595
+ readonly layer: Atom<Layer.Layer<R, ER, unknown>>;
596
+ readonly atom: {
597
+ <A, E>(create: (get: AtomContext) => Effect.Effect<A, E, Scope.Scope | R | AtomRegistry | Reactivity.Reactivity>, options?: {
598
+ readonly initialValue?: A;
599
+ readonly uninterruptible?: boolean | undefined;
600
+ }): Atom<Result<A | Context.Context<R>, E | ER | Cause.NoSuchElementError>>;
601
+ <A, E>(effect: Effect.Effect<A, E, Scope.Scope | R | AtomRegistry | Reactivity.Reactivity>, options?: {
602
+ readonly initialValue?: A;
603
+ readonly uninterruptible?: boolean | undefined;
604
+ }): Atom<Result<A | Context.Context<R>, E | ER | Cause.NoSuchElementError>>;
605
+ <A, E>(create: (get: AtomContext) => Stream.Stream<A, E, AtomRegistry | Reactivity.Reactivity | R>, options?: {
606
+ readonly initialValue?: A;
607
+ }): Atom<Result<A | Context.Context<R>, E | ER | Cause.NoSuchElementError>>;
608
+ <A, E>(stream: Stream.Stream<A, E, AtomRegistry | Reactivity.Reactivity | R>, options?: {
609
+ readonly initialValue?: A;
610
+ }): Atom<Result<A | Context.Context<R>, E | ER | Cause.NoSuchElementError>>;
611
+ };
612
+ readonly fn: {
613
+ <Arg>(): {
614
+ <E, A>(fn: (arg: Arg, get: FnContext) => Effect.Effect<A, E, Scope.Scope | AtomRegistry | Reactivity.Reactivity | R>, options?: {
615
+ readonly initialValue?: A | undefined;
616
+ readonly reactivityKeys?: readonly unknown[] | ReadonlyRecord<string, readonly unknown[]> | undefined;
617
+ readonly concurrent?: boolean | undefined;
618
+ }): AtomResultFn<Arg, A, E | ER> | AtomResultFn<unknown, unknown, unknown>;
619
+ <E, A>(fn: (arg: Arg, get: FnContext) => Stream.Stream<A, E, AtomRegistry | Reactivity.Reactivity | R>, options?: {
620
+ readonly initialValue?: A | undefined;
621
+ readonly reactivityKeys?: readonly unknown[] | ReadonlyRecord<string, readonly unknown[]> | undefined;
622
+ readonly concurrent?: boolean | undefined;
623
+ }): AtomResultFn<Arg, A, E | ER | Cause.NoSuchElementError> | AtomResultFn<unknown, unknown, unknown>;
624
+ };
625
+ <E, A, Arg = void>(fn: (arg: Arg, get: FnContext) => Effect.Effect<A, E, Scope.Scope | AtomRegistry | Reactivity.Reactivity | R>, options?: {
626
+ readonly initialValue?: A | undefined;
627
+ readonly reactivityKeys?: readonly unknown[] | ReadonlyRecord<string, readonly unknown[]> | undefined;
628
+ readonly concurrent?: boolean | undefined;
629
+ }): AtomResultFn<Arg, A, E | ER> | AtomResultFn<unknown, unknown, unknown>;
630
+ <E, A, Arg = void>(fn: (arg: Arg, get: FnContext) => Stream.Stream<A, E, AtomRegistry | Reactivity.Reactivity | R>, options?: {
631
+ readonly initialValue?: A | undefined;
632
+ readonly reactivityKeys?: readonly unknown[] | ReadonlyRecord<string, readonly unknown[]> | undefined;
633
+ readonly concurrent?: boolean | undefined;
634
+ }): AtomResultFn<Arg, A, E | ER | Cause.NoSuchElementError> | AtomResultFn<unknown, unknown, unknown>;
635
+ };
636
+ readonly pull: <A, E>(create: ((get: AtomContext) => Stream.Stream<A, E, R | AtomRegistry | Reactivity.Reactivity>) | Stream.Stream<A, E, R | AtomRegistry | Reactivity.Reactivity>, options?: {
637
+ readonly disableAccumulation?: boolean;
638
+ readonly initialValue?: readonly A[];
639
+ }) => Writable<PullResult<A | Context.Context<R>, E | ER | Cause.NoSuchElementError>, void>;
640
+ readonly subscriptionRef: <A, E>(create: Effect.Effect<SubscriptionRef.SubscriptionRef<A>, E, Scope.Scope | R | AtomRegistry | Reactivity.Reactivity> | ((get: AtomContext) => Effect.Effect<SubscriptionRef.SubscriptionRef<A>, E, Scope.Scope | R | AtomRegistry | Reactivity.Reactivity>)) => Writable<Result<A | Context.Context<R>, E | ER | Cause.NoSuchElementError>, A>;
641
+ }
642
+ /**
643
+ * Factory for `AtomRuntime` values that share a set of global layers.
644
+ *
645
+ * @category models
646
+ * @since 4.0.0
647
+ */
648
+ interface RuntimeFactory {
649
+ <R, E>(create: Layer.Layer<R, E, unknown> | ((get: AtomContext) => Layer.Layer<R, E, unknown>)): AtomRuntime<R, E>;
650
+ readonly addGlobalLayer: <A, E>(layer: Layer.Layer<A, E, AtomRegistry | Reactivity.Reactivity>) => void;
651
+ /**
652
+ * Uses the `Reactivity` service from the runtime to refresh the atom whenever
653
+ * the keys change.
654
+ */
655
+ readonly withReactivity: (keys: readonly unknown[] | ReadonlyRecord<string, readonly unknown[]>) => <A extends Atom<unknown>>(atom: A) => A;
656
+ }
657
+ /**
658
+ * A `RuntimeFactory` backed by an atom whose memo map is scoped to each registry.
659
+ *
660
+ * @category models
661
+ * @since 4.0.0
662
+ */
663
+ interface RegistryRuntimeFactory extends RuntimeFactory {
664
+ readonly memoMap: Atom<Layer.MemoMap>;
665
+ }
666
+ /**
667
+ * A `RuntimeFactory` backed by a concrete memo map shared across registries.
668
+ *
669
+ * @category models
670
+ * @since 4.0.0
671
+ */
672
+ interface SharedRuntimeFactory extends RuntimeFactory {
673
+ readonly memoMap: Layer.MemoMap;
674
+ }
675
+ /**
676
+ * Creates a `RuntimeFactory` backed by a registry-scoped memo map by default,
677
+ * or by the supplied atom or concrete `Layer.MemoMap`.
678
+ *
679
+ * @category constructors
680
+ * @since 4.0.0
681
+ */
682
+ declare function context(): RegistryRuntimeFactory;
683
+ declare function context(options: {
684
+ readonly memoMap: Atom<Layer.MemoMap>;
685
+ }): RegistryRuntimeFactory;
686
+ declare function context(options: {
687
+ readonly memoMap: Layer.MemoMap;
688
+ }): SharedRuntimeFactory;
689
+ /**
690
+ * Default registry-scoped `RuntimeFactory`.
691
+ *
692
+ * @category context
693
+ * @since 4.0.0
694
+ */
695
+ declare const runtime: RegistryRuntimeFactory;
696
+ /**
697
+ * Returns `Rx.runtime.withReactivity` for refreshing an atom whenever the
698
+ * keys change in the `Reactivity` service.
699
+ *
700
+ * **When to use**
701
+ *
702
+ * Use to refresh an atom whenever one or more invalidation keys change in the
703
+ * default reactivity runtime.
704
+ *
705
+ * @category reactivity
706
+ * @since 4.0.0
707
+ */
708
+ declare const withReactivity: (keys: readonly unknown[] | ReadonlyRecord<string, readonly unknown[]>) => <A extends Atom<unknown>>(atom: A) => A;
709
+ /**
710
+ * Creates a writable atom backed by a `SubscriptionRef`, or by an effect that produces one, updating from ref changes and writing atom updates back to the ref.
711
+ *
712
+ * @category constructors
713
+ * @since 4.0.0
714
+ */
715
+ declare const subscriptionRef: {
716
+ <A>(ref: SubscriptionRef.SubscriptionRef<A> | ((get: AtomContext) => SubscriptionRef.SubscriptionRef<A>)): Writable<A> | Writable<unknown, unknown>;
717
+ <A, E>(effect: Effect.Effect<SubscriptionRef.SubscriptionRef<A>, E, Scope.Scope | AtomRegistry> | ((get: AtomContext) => Effect.Effect<SubscriptionRef.SubscriptionRef<A>, E, Scope.Scope | AtomRegistry>)): Writable<Result<A, E | Cause.NoSuchElementError>, A> | Writable<unknown, unknown>;
718
+ };
719
+ /**
720
+ * Context passed to `fn` and `fnSync` computations for reading atoms, awaiting results, registering finalizers, refreshing atoms, subscribing to changes, and writing updates.
721
+ *
722
+ * @category models
723
+ * @since 4.0.0
724
+ */
725
+ interface FnContext {
726
+ <A>(atom: Atom<A>): A;
727
+ result<A, E>(this: FnContext, atom: Atom<Result<A, E>>, options?: {
728
+ readonly suspendOnWaiting?: boolean | undefined;
729
+ }): Effect.Effect<A, E>;
730
+ addFinalizer(this: FnContext, f: () => void): void;
731
+ mount<A>(this: FnContext, atom: Atom<A>): void;
732
+ refresh<A>(this: FnContext, atom: Atom<A>): void;
733
+ self(this: FnContext): Option.Option<unknown>;
734
+ setSelf<A>(this: FnContext, a: A): void;
735
+ set<R, W>(this: FnContext, atom: Writable<R, W>, value: W): void;
736
+ setResult<A, E, W>(this: FnContext, atom: Writable<Result<A, E>, W>, value: W): Effect.Effect<A, E>;
737
+ some<A>(this: FnContext, atom: Atom<Option.Option<A>>): Effect.Effect<A>;
738
+ stream<A>(this: FnContext, atom: Atom<A>, options?: {
739
+ readonly withoutInitialValue?: boolean;
740
+ readonly bufferSize?: number;
741
+ }): Stream.Stream<A>;
742
+ streamResult<A, E>(this: FnContext, atom: Atom<Result<A, E>>, options?: {
743
+ readonly withoutInitialValue?: boolean;
744
+ readonly bufferSize?: number;
745
+ }): Stream.Stream<A, E>;
746
+ subscribe<A>(this: FnContext, atom: Atom<A>, f: (_: A) => void, options?: {
747
+ readonly immediate?: boolean;
748
+ }): void;
749
+ readonly registry: Registry;
750
+ }
751
+ /**
752
+ * Creates a writable atom for a synchronous function; writing an argument re-runs the function, returning `Option.none` before the first call unless an initial value is supplied.
753
+ *
754
+ * @category constructors
755
+ * @since 4.0.0
756
+ */
757
+ declare function fnSync<Arg>(): {
758
+ <A>(f: (arg: Arg, get: FnContext) => A): Writable<Option.Option<A>, Arg>;
759
+ <A>(f: (arg: Arg, get: FnContext) => A, options: {
760
+ readonly initialValue: A;
761
+ }): Writable<A, Arg>;
762
+ };
763
+ declare function fnSync<A, Arg = void>(f: (arg: Arg, get: FnContext) => A): Writable<Option.Option<A>, Arg>;
764
+ declare function fnSync<A, Arg = void>(f: (arg: Arg, get: FnContext) => A, options: {
765
+ readonly initialValue: A;
766
+ }): Writable<A, Arg>;
767
+ /**
768
+ * Writable async function atom whose value is an `AsyncResult` and whose writes accept function arguments plus `Reset` and `Interrupt` controls.
769
+ *
770
+ * @category models
771
+ * @since 4.0.0
772
+ */
773
+ interface AtomResultFn<Arg, A, E = never> extends Writable<Result<A, E>, Arg | Reset | Interrupt> {}
774
+ /**
775
+ * Defines the control symbol that can be written to an `AtomResultFn` to reset it to its initial state.
776
+ *
777
+ * **When to use**
778
+ *
779
+ * Use when you need an `AtomResultFn` write value that clears the current async
780
+ * result and returns it to the initial state.
781
+ *
782
+ * @category symbols
783
+ * @since 4.0.0
784
+ */
785
+ declare const Reset: unique symbol;
786
+ /**
787
+ * Type of the `Reset` control symbol accepted by `AtomResultFn` writes.
788
+ *
789
+ * @category symbols
790
+ * @since 4.0.0
791
+ */
792
+ type Reset = typeof Reset;
793
+ /**
794
+ * Defines the control symbol that can be written to an `AtomResultFn` to interrupt the current asynchronous computation.
795
+ *
796
+ * **When to use**
797
+ *
798
+ * Use when you need an `AtomResultFn` write value that interrupts the currently
799
+ * running async computation.
800
+ *
801
+ * @category symbols
802
+ * @since 4.0.0
803
+ */
804
+ declare const Interrupt: unique symbol;
805
+ /**
806
+ * Type of the `Interrupt` control symbol accepted by `AtomResultFn` writes.
807
+ *
808
+ * @category symbols
809
+ * @since 4.0.0
810
+ */
811
+ type Interrupt = typeof Interrupt;
812
+ /**
813
+ * Creates a writable atom for an `Effect` or `Stream` function; writing an argument starts the computation and exposes its state as an `AsyncResult`.
814
+ *
815
+ * @category constructors
816
+ * @since 4.0.0
817
+ */
818
+ declare function fn<Arg>(): <E, A>(fn: (arg: Arg, get: FnContext) => Effect.Effect<A, E, Scope.Scope | AtomRegistry>, options?: {
819
+ readonly initialValue?: A | undefined;
820
+ readonly concurrent?: boolean | undefined;
821
+ }) => AtomResultFn<Arg, A, E>;
822
+ declare function fn<E, A, Arg = void>(fn: (arg: Arg, get: FnContext) => Effect.Effect<A, E, Scope.Scope | AtomRegistry>, options?: {
823
+ readonly initialValue?: A | undefined;
824
+ readonly concurrent?: boolean | undefined;
825
+ }): AtomResultFn<Arg, A, E>;
826
+ declare function fn<Arg>(): <E, A>(fn: (arg: Arg, get: FnContext) => Stream.Stream<A, E, AtomRegistry>, options?: {
827
+ readonly initialValue?: A | undefined;
828
+ readonly concurrent?: boolean | undefined;
829
+ }) => AtomResultFn<Arg, A, E | Cause.NoSuchElementError>;
830
+ declare function fn<E, A, Arg = void>(fn: (arg: Arg, get: FnContext) => Stream.Stream<A, E, AtomRegistry>, options?: {
831
+ readonly initialValue?: A | undefined;
832
+ readonly concurrent?: boolean | undefined;
833
+ }): AtomResultFn<Arg, A, E | Cause.NoSuchElementError>;
834
+ /**
835
+ * `AsyncResult` produced by `pull`, containing a non-empty batch of pulled items and a `done` flag, or `NoSuchElementError` when the stream completes without items.
836
+ *
837
+ * @category models
838
+ * @since 4.0.0
839
+ */
840
+ type PullResult<A, E = never> = Result<{
841
+ readonly done: boolean;
842
+ readonly items: Arr.NonEmptyReadonlyArray<A>;
843
+ }, E | Cause.NoSuchElementError>;
844
+ /**
845
+ * Creates a writable atom that pulls an initial chunk from a stream and then pulls the next chunk whenever it is written to, accumulating items unless `disableAccumulation` is enabled.
846
+ *
847
+ * @category constructors
848
+ * @since 4.0.0
849
+ */
850
+ declare const pull: <A, E>(create: ((get: AtomContext) => Stream.Stream<A, E, AtomRegistry>) | Stream.Stream<A, E, AtomRegistry>, options?: {
851
+ readonly disableAccumulation?: boolean | undefined;
852
+ }) => Writable<PullResult<A, E>, void>;
853
+ /**
854
+ * Creates a memoized atom factory that returns the same object for the same argument, using weak references for cached values when the platform supports them.
855
+ *
856
+ * @category constructors
857
+ * @since 4.0.0
858
+ */
859
+ declare const family: <Arg, T extends object>(f: (arg: Arg) => T) => (arg: Arg) => T;
860
+ /**
861
+ * Uses a fallback `AsyncResult` atom while the primary atom is `Initial`, marking the fallback result as waiting until the primary atom produces a non-initial result.
862
+ *
863
+ * @category combinators
864
+ * @since 4.0.0
865
+ */
866
+ declare const withFallback: {
867
+ <E2, A2>(fallback: Atom<Result<A2, E2>>): <R extends Atom<Result<unknown, unknown>>>(self: R) => [R] extends [Writable<infer _, infer RW>] ? Writable<Result<unknown, unknown> | Result<A2, E2>, RW> : Atom<Result<unknown, unknown> | Result<A2, E2>>;
868
+ <R extends Atom<Result<unknown, unknown>>, A2, E2>(self: R, fallback: Atom<Result<A2, E2>>): [R] extends [Writable<infer _, infer RW>] ? Writable<Result<unknown, unknown> | Result<A2, E2>, RW> : Atom<Result<unknown, unknown> | Result<A2, E2>>;
869
+ };
870
+ /**
871
+ * Returns a copy of an atom that remains cached and mounted even when no subscribers are using it.
872
+ *
873
+ * @category combinators
874
+ * @since 4.0.0
875
+ */
876
+ declare const keepAlive: <A extends Atom<unknown>>(self: A) => A;
877
+ /**
878
+ * Allows a reactive value to be disposed of when it is not in use.
879
+ *
880
+ * **Details**
881
+ *
882
+ * Atoms have this behavior by default, so use this to undo `keepAlive` on a copied atom.
883
+ *
884
+ * @category combinators
885
+ * @since 4.0.0
886
+ */
887
+ declare const autoDispose: <A extends Atom<unknown>>(self: A) => A;
888
+ /**
889
+ * Sets whether an atom should be lazy.
890
+ *
891
+ * **Details**
892
+ *
893
+ * Lazy atoms defer recomputation while they have no active listeners or active
894
+ * non-lazy dependents, rebuilding the next time their value is observed.
895
+ *
896
+ * @category combinators
897
+ * @since 4.0.0
898
+ */
899
+ declare const setLazy: {
900
+ (lazy: boolean): <A extends Atom<unknown>>(self: A) => A;
901
+ <A extends Atom<unknown>>(self: A, lazy: boolean): A;
902
+ };
903
+ /**
904
+ * Returns a copy of an atom that uses a custom equality function to detect
905
+ * value changes.
906
+ *
907
+ * **Details**
908
+ *
909
+ * When an atom's value is rebuilt or written, the registry compares the new
910
+ * value against the current one to decide whether dependents and listeners
911
+ * should be notified. By default the comparison uses `Object.is`, so a
912
+ * structurally equal but referentially distinct value still triggers
913
+ * notifications. Providing an equality function lets the atom skip updates
914
+ * when the new value is equal to the current one.
915
+ *
916
+ * **Example** (Comparing values structurally)
917
+ *
918
+ * ```ts import.meta.vitest
919
+ * import { Atom } from "effect/unstable/reactivity"
920
+ *
921
+ * const point = Atom.make({ x: 0, y: 0 }).pipe(
922
+ * Atom.withEquality<{ x: number; y: number }>((a, b) => a.x === b.x && a.y === b.y)
923
+ * )
924
+ * point.equals({ x: 1, y: 2 }, { x: 1, y: 2 }) // => true
925
+ * ```
926
+ *
927
+ * @category combinators
928
+ * @since 4.0.0
929
+ */
930
+ declare const withEquality: {
931
+ <A>(equals: (value: A, next: A) => boolean): <T extends Atom<A>>(self: T) => T;
932
+ <T extends Atom<unknown>>(self: T, equals: (value: Type<T>, next: Type<T>) => boolean): T;
933
+ };
934
+ /**
935
+ * Attaches a diagnostic label to an atom.
936
+ *
937
+ * **Details**
938
+ *
939
+ * The label is used for inspection and debugging metadata and does not change the
940
+ * atom's read or write behavior.
941
+ *
942
+ * @category combinators
943
+ * @since 4.0.0
944
+ */
945
+ declare const withLabel: {
946
+ (name: string): <A extends Atom<unknown>>(self: A) => A;
947
+ <A extends Atom<unknown>>(self: A, name: string): A;
948
+ };
949
+ /**
950
+ * Pairs an atom with an initial value for registry initialization.
951
+ *
952
+ * **When to use**
953
+ *
954
+ * Use to preload an atom value when constructing or seeding a registry.
955
+ *
956
+ * **Details**
957
+ *
958
+ * The returned tuple can be supplied to `AtomRegistry` initial values so the atom
959
+ * starts with the provided value before it is first rebuilt.
960
+ *
961
+ * @category combinators
962
+ * @since 4.0.0
963
+ */
964
+ declare const initialValue: {
965
+ <A>(initialValue: A): (self: Atom<A>) => readonly [Atom<A>, A];
966
+ <A>(self: Atom<A>, initialValue: A): readonly [Atom<A>, A];
967
+ };
968
+ /**
969
+ * Maps the value of an atom by reading the source atom, applying the function,
970
+ *
971
+ * **Details**
972
+ *
973
+ * When the source atom is writable, the returned atom remains writable and keeps
974
+ * the source atom's write input type.
975
+ *
976
+ * @category combinators
977
+ * @since 4.0.0
978
+ */
979
+ declare const map: {
980
+ <R extends Atom<unknown>, B>(f: (_: Type<R>) => B): (self: R) => [R] extends [Writable<infer _, infer RW>] ? Writable<B, RW> : Atom<B>;
981
+ <R extends Atom<unknown>, B>(self: R, f: (_: Type<R>) => B): [R] extends [Writable<infer _, infer RW>] ? Writable<B, RW> : Atom<B>;
982
+ };
983
+ declare function mapResult<R extends Atom<Result<unknown, unknown>>, B>(f: (_: Result.Success<Type<R>>) => B): (self: R) => [R] extends [Writable<infer _, infer RW>] ? Writable<Result<B, Result.Failure<Type<R>>>, RW> : Atom<Result<B, Result.Failure<Type<R>>>>;
984
+ declare function mapResult<R extends Atom<Result<unknown, unknown>>, B>(self: R, f: (_: Result.Success<Type<R>>) => B): [R] extends [Writable<infer _, infer RW>] ? Writable<Result<B, Result.Failure<Type<R>>>, RW> : Atom<Result<B, Result.Failure<Type<R>>>>;
985
+ /**
986
+ * Creates an atom that publishes source changes only after the source has stopped
987
+ * changing for the specified duration.
988
+ *
989
+ * **Details**
990
+ *
991
+ * The current source value is used immediately, and any pending debounce timer is
992
+ * cleared when the derived atom is disposed.
993
+ *
994
+ * @category combinators
995
+ * @since 4.0.0
996
+ */
997
+ declare const debounce: {
998
+ (duration: Duration.Input): <A extends Atom<unknown>>(self: A) => WithoutSerializable<A>;
999
+ <A extends Atom<unknown>>(self: A, duration: Duration.Input): WithoutSerializable<A>;
1000
+ };
1001
+ /**
1002
+ * Creates a derived atom that reads the source and schedules a refresh after the
1003
+ * specified duration.
1004
+ *
1005
+ * **Details**
1006
+ *
1007
+ * The scheduled refresh is canceled when the derived atom's lifetime is disposed.
1008
+ *
1009
+ * @category combinators
1010
+ * @since 4.0.0
1011
+ */
1012
+ declare const withRefresh: {
1013
+ (duration: Duration.Input): <A extends Atom<unknown>>(self: A) => WithoutSerializable<A>;
1014
+ <A extends Atom<unknown>>(self: A, duration: Duration.Input): WithoutSerializable<A>;
1015
+ };
1016
+ /**
1017
+ * Adds stale-while-revalidate refresh behavior to an async result atom.
1018
+ *
1019
+ * **Details**
1020
+ *
1021
+ * Automatic revalidation during reads is skipped while the current value is
1022
+ * fresh within `staleTime`. Manual `refresh` calls remain forceful and always
1023
+ * forward to the wrapped atom. Use `revalidateOnMount` to control whether stale data should trigger a
1024
+ * background refresh on first mount. Use `revalidateOnFocus` to control
1025
+ * focus behavior. `true` respects `staleTime` and `"always"` forces refetch.
1026
+ *
1027
+ * @category combinators
1028
+ * @since 4.0.0
1029
+ */
1030
+ declare const swr: {
1031
+ (options: {
1032
+ readonly staleTime: Duration.Input;
1033
+ readonly revalidateOnMount?: boolean | undefined;
1034
+ readonly revalidateOnFocus?: boolean | 'always' | undefined;
1035
+ readonly focusSignal?: Atom<unknown> | undefined;
1036
+ }): <R extends Atom<Result<unknown, unknown>>>(self: R) => WithoutSerializable<R>;
1037
+ <R extends Atom<Result<unknown, unknown>>>(self: R, options: {
1038
+ readonly staleTime: Duration.Input;
1039
+ readonly revalidateOnMount?: boolean | undefined;
1040
+ readonly revalidateOnFocus?: boolean | 'always' | undefined;
1041
+ readonly focusSignal?: Atom<unknown> | undefined;
1042
+ }): WithoutSerializable<R>;
1043
+ };
1044
+ /**
1045
+ * Wraps an atom in a writable optimistic atom.
1046
+ *
1047
+ * **Details**
1048
+ *
1049
+ * Writes accept transition atoms containing `AsyncResult` values. Waiting
1050
+ * successes are shown optimistically while transitions run; when successful
1051
+ * transitions finish, the source atom is refreshed, and failures roll the value
1052
+ * back to the latest source value.
1053
+ *
1054
+ * @category constructors
1055
+ * @since 4.0.0
1056
+ */
1057
+ declare const optimistic: <A>(self: Atom<A>) => Writable<A, Atom<Result<A, unknown>>>;
1058
+ /**
1059
+ * Creates an `AtomResultFn` that applies an optimistic update before running the
1060
+ * underlying mutation.
1061
+ *
1062
+ * **Details**
1063
+ *
1064
+ * The reducer computes the provisional value from the current value and mutation
1065
+ * input. The wrapped function result then completes the transition or updates the
1066
+ * optimistic value through the provided setter callback.
1067
+ *
1068
+ * @category combinators
1069
+ * @since 4.0.0
1070
+ */
1071
+ declare const optimisticFn: {
1072
+ <A, W, XA, XE, OW = void>(options: {
1073
+ readonly reducer: (current: NoInfer<A>, update: OW) => NoInfer<W>;
1074
+ readonly fn: AtomResultFn<OW, XA, XE> | ((set: (result: NoInfer<W>) => void) => AtomResultFn<OW, XA, XE>);
1075
+ }): (self: Writable<A, Atom<Result<W, unknown>>>) => AtomResultFn<OW, XA, XE>;
1076
+ <A, W, XA, XE, OW = void>(self: Writable<A, Atom<Result<W, unknown>>>, options: {
1077
+ readonly reducer: (current: NoInfer<A>, update: OW) => NoInfer<W>;
1078
+ readonly fn: AtomResultFn<OW, XA, XE> | ((set: (result: NoInfer<W>) => void) => AtomResultFn<OW, XA, XE>);
1079
+ }): AtomResultFn<OW, XA, XE>;
1080
+ };
1081
+ /**
1082
+ * Runs synchronous atom updates as a batch.
1083
+ *
1084
+ * **Details**
1085
+ *
1086
+ * Stale nodes are rebuilt and listeners are notified after the callback completes,
1087
+ * so dependent updates observe the final batched state.
1088
+ *
1089
+ * @category batching
1090
+ * @since 4.0.0
1091
+ */
1092
+ declare const batch: (f: () => void) => void;
1093
+ /**
1094
+ * Creates a writable atom backed by a `KeyValueStore` entry.
1095
+ *
1096
+ * **Details**
1097
+ *
1098
+ * Values are encoded and decoded with the supplied schema. In sync mode the atom
1099
+ * exposes the decoded value and writes the default value when the key is missing;
1100
+ * in async mode it exposes an `AsyncResult` of the decoded value.
1101
+ *
1102
+ * **Gotchas**
1103
+ *
1104
+ * Error surfacing differs by mode. Async mode reports a failed store read as an
1105
+ * `AsyncResult.failure`; sync mode has no error channel on its bare value, so a
1106
+ * failed read renders the default value instead. Writes are optimistic in both
1107
+ * modes: the value is shown immediately and the store write is fired in the
1108
+ * background, so a write the store later refuses is not reflected in the atom.
1109
+ * Use async mode when store failures must be observable.
1110
+ *
1111
+ * @category constructors
1112
+ * @since 4.0.0
1113
+ */
1114
+ declare function kvs<S extends Schema.ConstraintCodec<unknown, unknown>, const Mode extends 'sync' | 'async' = never>(options: {
1115
+ readonly runtime: AtomRuntime<KeyValueStore.KeyValueStore, unknown>;
1116
+ readonly key: string;
1117
+ readonly schema: S;
1118
+ readonly defaultValue: LazyArg<S['Type']>;
1119
+ readonly mode?: Mode | undefined;
1120
+ }): Writable<'async' extends Mode ? Result<S['Type']> : S['Type'], S['Type']>;
1121
+ /**
1122
+ * Converts an atom into a stream using the `AtomRegistry` service.
1123
+ *
1124
+ * **Details**
1125
+ *
1126
+ * The stream emits the atom's current value immediately and then emits subsequent
1127
+ * changes until the stream scope is closed.
1128
+ *
1129
+ * @category converting
1130
+ * @since 4.0.0
1131
+ */
1132
+ declare const toStream: <A>(self: Atom<A>) => Stream.Stream<A, never, AtomRegistry>;
1133
+ /**
1134
+ * Converts an `AsyncResult` atom into a stream using the `AtomRegistry` service.
1135
+ *
1136
+ * **Details**
1137
+ *
1138
+ * Initial results are skipped, successes are emitted as stream values, and
1139
+ * failures fail the stream with the result cause.
1140
+ *
1141
+ * @category converting
1142
+ * @since 4.0.0
1143
+ */
1144
+ declare const toStreamResult: <A, E>(self: Atom<Result<A, E>>) => Stream.Stream<A, E, AtomRegistry>;
1145
+ /**
1146
+ * Reads an atom's current value from the `AtomRegistry` service.
1147
+ *
1148
+ * @category converting
1149
+ * @since 4.0.0
1150
+ */
1151
+ declare const get: <A>(self: Atom<A>) => Effect.Effect<A, never, AtomRegistry>;
1152
+ /**
1153
+ * Reads a writable atom, computes a return value and next write value, writes the
1154
+ * next value, and returns the computed result.
1155
+ *
1156
+ * @category converting
1157
+ * @since 4.0.0
1158
+ */
1159
+ declare const modify: {
1160
+ <R, W, A>(f: (_: R) => [returnValue: A, nextValue: W]): (self: Writable<R, W>) => Effect.Effect<A, never, AtomRegistry>;
1161
+ <R, W, A>(self: Writable<R, W>, f: (_: R) => [returnValue: A, nextValue: W]): Effect.Effect<A, never, AtomRegistry>;
1162
+ };
1163
+ /**
1164
+ * Writes a value to a writable atom through the `AtomRegistry` service.
1165
+ *
1166
+ * @category converting
1167
+ * @since 4.0.0
1168
+ */
1169
+ declare const set: {
1170
+ <W>(value: W): <R>(self: Writable<R, W>) => Effect.Effect<void, never, AtomRegistry>;
1171
+ <R, W>(self: Writable<R, W>, value: W): Effect.Effect<void, never, AtomRegistry>;
1172
+ };
1173
+ /**
1174
+ * Updates a writable atom by reading its current value from the registry and
1175
+ * writing the value returned by the update function.
1176
+ *
1177
+ * @category converting
1178
+ * @since 4.0.0
1179
+ */
1180
+ declare const update: {
1181
+ <R, W>(f: (_: R) => W): (self: Writable<R, W>) => Effect.Effect<void, never, AtomRegistry>;
1182
+ <R, W>(self: Writable<R, W>, f: (_: R) => W): Effect.Effect<void, never, AtomRegistry>;
1183
+ };
1184
+ /**
1185
+ * Reads an `AsyncResult` atom as an effect through the `AtomRegistry` service.
1186
+ *
1187
+ * **Details**
1188
+ *
1189
+ * The effect waits while the result is `Initial`, and also while it is waiting
1190
+ * when `suspendOnWaiting` is enabled. Successes succeed with the value and
1191
+ * failures fail with the result cause.
1192
+ *
1193
+ * @category converting
1194
+ * @since 4.0.0
1195
+ */
1196
+ declare const getResult: <A, E>(self: Atom<Result<A, E>>, options?: {
1197
+ readonly suspendOnWaiting?: boolean | undefined;
1198
+ }) => Effect.Effect<A, E, AtomRegistry>;
1199
+ /**
1200
+ * Runs a refresh request for an atom through the `AtomRegistry` service.
1201
+ *
1202
+ * **When to use**
1203
+ *
1204
+ * Use to invalidate and recompute an atom from an Effect that has access to the
1205
+ * active registry.
1206
+ *
1207
+ * @category converting
1208
+ * @since 4.0.0
1209
+ */
1210
+ declare const refresh: <A>(self: Atom<A>) => Effect.Effect<void, never, AtomRegistry>;
1211
+ /**
1212
+ * Mounts an atom in the `AtomRegistry` for the lifetime of the current scope.
1213
+ *
1214
+ * **Details**
1215
+ *
1216
+ * Mounting keeps the atom subscribed with a no-op listener until the scope
1217
+ * finalizer releases it.
1218
+ *
1219
+ * @category converting
1220
+ * @since 4.0.0
1221
+ */
1222
+ declare const mount: <A>(self: Atom<A>) => Effect.Effect<void, never, AtomRegistry | Scope.Scope>;
1223
+ /**
1224
+ * The type id used to mark atoms that carry serialization metadata.
1225
+ *
1226
+ * @category type IDs
1227
+ * @since 4.0.0
1228
+ */
1229
+ declare const SerializableTypeId: SerializableTypeId;
1230
+ /**
1231
+ * The literal type of the serializable atom marker.
1232
+ *
1233
+ * @category type IDs
1234
+ * @since 4.0.0
1235
+ */
1236
+ type SerializableTypeId = '~effect-atom/atom/Atom/Serializable';
1237
+ /**
1238
+ * Serialization metadata attached to an atom.
1239
+ *
1240
+ * **Details**
1241
+ *
1242
+ * The key identifies the atom in dehydrated state, and the encode/decode
1243
+ * functions convert between the atom value and the schema encoded value.
1244
+ *
1245
+ * @category models
1246
+ * @since 4.0.0
1247
+ */
1248
+ type SerializableJson = string | number | boolean | null | ReadonlyArray<SerializableJson> | {
1249
+ readonly [key: string]: SerializableJson;
1250
+ };
1251
+ interface Serializable<S extends Schema.Constraint> {
1252
+ readonly [SerializableTypeId]: {
1253
+ readonly key: string;
1254
+ readonly encode: (value: S['Type']) => SerializableJson;
1255
+ readonly decode: (value: SerializableJson) => S['Type'];
1256
+ };
1257
+ }
1258
+ /**
1259
+ * Returns `true` when an atom carries `Serializable` metadata.
1260
+ *
1261
+ * @category guards
1262
+ * @since 4.0.0
1263
+ */
1264
+ declare const isSerializable: (self: Atom<unknown>) => self is Atom<unknown> & Serializable<Schema.Unknown>;
1265
+ /**
1266
+ * Attaches serialization metadata to an atom using a schema and stable key.
1267
+ *
1268
+ * **Details**
1269
+ *
1270
+ * The schema is converted to a JSON codec for synchronous encode/decode, and the
1271
+ * key is also used as the atom label when the atom does not already have one.
1272
+ *
1273
+ * @category combinators
1274
+ * @since 4.0.0
1275
+ */
1276
+ declare const serializable: {
1277
+ <R extends Atom<unknown>, S extends Schema.Constraint>(options: {
1278
+ readonly key: string;
1279
+ readonly schema: S;
1280
+ }): (self: R) => R & Serializable<S>;
1281
+ <R extends Atom<unknown>, S extends Schema.Constraint>(self: R, options: {
1282
+ readonly key: string;
1283
+ readonly schema: S;
1284
+ }): R & Serializable<S>;
1285
+ };
1286
+ //#endregion
1287
+ export { toStreamResult as $, get as A, isAtom as At, modify as B, autoDispose as C, layerOptions as Ct, family as D, toStreamResult$1 as Dt, debounce as E, toStream$1 as Et, kvs as F, writable as Ft, refresh as G, optimistic as H, make as I, set as J, runtime as K, makeRead as L, initialValue as M, readable as Mt, isSerializable as N, setIdleTTL as Nt, fn as O, TypeId$1 as Ot, keepAlive as P, transform as Pt, toStream as Q, map as R, WriteContext as S, layer as St, context as T, mount$1 as Tt, optimisticFn as U, mount as V, pull as W, subscriptionRef as X, setLazy as Y, swr as Z, SharedRuntimeFactory as _, Registry as _t, Atom_d_exports as a, withRefresh as at, WithoutSerializable as b, getResult$1 as bt, Interrupt as c, getServerValue as ct, RegistryRuntimeFactory as d, makeRefreshOnSignal as dt, update as et, Reset as f, refreshOnWindowFocus as ft, SerializableTypeId as g, Node as gt, SerializableJson as h, AtomRegistry as ht, AtomRuntime as i, withReactivity as it, getResult as j, isWritable as jt, fnSync as k, WritableTypeId as kt, PullResult as l, withServerValue as lt, Serializable as m, windowFocusSignal as mt, AtomContext as n, withFallback as nt, Failure as o, ServerValue as ot, RuntimeFactory as p, searchParam as pt, serializable as q, AtomResultFn as r, withLabel as rt, FnContext as s, ServerValueTypeId as st, Atom as t, withEquality as tt, PullSuccess as u, withServerValueInitial as ut, Success as v, Registry_d_exports as vt, batch as w, make$1 as wt, Writable as x, isAtomRegistry as xt, Type as y, TypeId as yt, mapResult as z };