/**
* @since 1.0.0
*/
import * as Reactivity from "@effect/experimental/Reactivity";
import * as KeyValueStore from "@effect/platform/KeyValueStore";
import * as Arr from "effect/Array";
import { NoSuchElementException } from "effect/Cause";
import * as Cause from "effect/Cause";
import * as Duration from "effect/Duration";
import * as Effect from "effect/Effect";
import type { LazyArg } from "effect/Function";
import * as Inspectable from "effect/Inspectable";
import * as Layer from "effect/Layer";
import * as Option from "effect/Option";
import { type Pipeable } from "effect/Pipeable";
import type { ReadonlyRecord } from "effect/Record";
import * as Runtime from "effect/Runtime";
import * as Schema from "effect/Schema";
import * as Scope from "effect/Scope";
import * as Stream from "effect/Stream";
import * as Subscribable from "effect/Subscribable";
import * as SubscriptionRef from "effect/SubscriptionRef";
import type { NoInfer } from "effect/Types";
import * as Registry from "./Registry.js";
import { AtomRegistry as AtomRegistry } from "./Registry.js";
import * as Result from "./Result.js";
declare const TypeId: TypeId;
type TypeId = "~effect-atom/atom/Atom";
/**
* @since 1.0.0
* @category models
*/
export interface Atom extends Pipeable, Inspectable.Inspectable {
readonly [TypeId]: TypeId;
readonly keepAlive: boolean;
readonly lazy: boolean;
readonly read: (get: Context) => A;
readonly refresh?: (f: (atom: Atom) => void) => void;
readonly label?: readonly [name: string, stack: string];
readonly idleTTL?: number;
}
/**
* @since 1.0.0
* @category Guards
*/
export declare const isAtom: (u: unknown) => u is Atom;
/**
* @since 1.0.0
*/
export type Type> = T extends Atom ? A : never;
/**
* @since 1.0.0
*/
export type Success> = T extends Atom> ? A : never;
/**
* @since 1.0.0
*/
export type PullSuccess> = T extends Atom> ? A : never;
/**
* @since 1.0.0
*/
export type Failure> = T extends Atom> ? E : never;
/**
* @since 1.0.0
*/
export type WithoutSerializable> = T extends Writable ? Writable : Atom>;
type WritableTypeId = "~effect-atom/atom/Atom/Writable";
declare const WritableTypeId: WritableTypeId;
/**
* @since 1.0.0
* @category models
*/
export interface Writable extends Atom {
readonly [WritableTypeId]: WritableTypeId;
readonly write: (ctx: WriteContext, value: W) => void;
}
/**
* @since 1.0.0
* @category context
*/
export interface Context {
(atom: Atom): A;
get(this: Context, atom: Atom): A;
result(this: Context, atom: Atom>, options?: {
readonly suspendOnWaiting?: boolean | undefined;
}): Effect.Effect;
resultOnce(this: Context, atom: Atom>, options?: {
readonly suspendOnWaiting?: boolean | undefined;
}): Effect.Effect;
once(this: Context, atom: Atom): A;
addFinalizer(this: Context, f: () => void): void;
mount(this: Context, atom: Atom): void;
refresh(this: Context, atom: Atom): void;
refreshSelf(this: Context): void;
self(this: Context): Option.Option;
setSelf(this: Context, a: A): void;
set(this: Context, atom: Writable, value: W): void;
setResult(this: Context, atom: Writable, W>, value: W): Effect.Effect;
some(this: Context, atom: Atom>): Effect.Effect;
someOnce(this: Context, atom: Atom>): Effect.Effect;
stream(this: Context, atom: Atom, options?: {
readonly withoutInitialValue?: boolean;
readonly bufferSize?: number;
}): Stream.Stream;
streamResult(this: Context, atom: Atom>, options?: {
readonly withoutInitialValue?: boolean;
readonly bufferSize?: number;
}): Stream.Stream;
subscribe(this: Context, atom: Atom, f: (_: A) => void, options?: {
readonly immediate?: boolean;
}): void;
readonly registry: Registry.Registry;
}
/**
* @since 1.0.0
* @category context
*/
export interface WriteContext {
get(this: WriteContext, atom: Atom): T;
refreshSelf(this: WriteContext): void;
setSelf(this: WriteContext, a: A): void;
set(this: WriteContext, atom: Writable, value: W): void;
}
/**
* @since 1.0.0
* @category combinators
*/
export declare const setIdleTTL: {
(duration: Duration.DurationInput): >(self: A) => A;
>(self: A, duration: Duration.DurationInput): A;
};
/**
* @since 1.0.0
* @category refinements
*/
export declare const isWritable: (atom: Atom) => atom is Writable;
/**
* @since 1.0.0
* @category constructors
*/
export declare const readable: (read: (get: Context) => A, refresh?: (f: (atom: Atom) => void) => void) => Atom;
/**
* @since 1.0.0
* @category constructors
*/
export declare const writable: (read: (get: Context) => R, write: (ctx: WriteContext, value: W) => void, refresh?: (f: (atom: Atom) => void) => void) => Writable;
/**
* @since 1.0.0
* @category constructors
*/
export declare const make: {
(create: (get: Context) => Effect.Effect, options?: {
readonly initialValue?: A;
}): Atom>;
(effect: Effect.Effect, options?: {
readonly initialValue?: A;
}): Atom>;
(create: (get: Context) => Stream.Stream, options?: {
readonly initialValue?: A;
}): Atom>;
(stream: Stream.Stream, options?: {
readonly initialValue?: A;
}): Atom>;
(create: (get: Context) => A): Atom;
(initialValue: A): Writable;
};
/**
* @since 1.0.0
* @category models
*/
export interface AtomRuntime extends Atom, ER>> {
readonly factory: RuntimeFactory;
readonly layer: Atom>;
readonly atom: {
(create: (get: Context) => Effect.Effect, options?: {
readonly initialValue?: A;
}): Atom>;
(effect: Effect.Effect, options?: {
readonly initialValue?: A;
}): Atom>;
(create: (get: Context) => Stream.Stream, options?: {
readonly initialValue?: A;
}): Atom>;
(stream: Stream.Stream, options?: {
readonly initialValue?: A;
}): Atom>;
};
readonly fn: {
(): {
(fn: (arg: Arg, get: FnContext) => Effect.Effect, options?: {
readonly initialValue?: A | undefined;
readonly reactivityKeys?: ReadonlyArray | ReadonlyRecord> | undefined;
readonly concurrent?: boolean | undefined;
}): AtomResultFn;
(fn: (arg: Arg, get: FnContext) => Stream.Stream, options?: {
readonly initialValue?: A | undefined;
readonly reactivityKeys?: ReadonlyArray | ReadonlyRecord> | undefined;
readonly concurrent?: boolean | undefined;
}): AtomResultFn;
};
(fn: (arg: Arg, get: FnContext) => Effect.Effect, options?: {
readonly initialValue?: A | undefined;
readonly reactivityKeys?: ReadonlyArray | ReadonlyRecord> | undefined;
readonly concurrent?: boolean | undefined;
}): AtomResultFn;
(fn: (arg: Arg, get: FnContext) => Stream.Stream, options?: {
readonly initialValue?: A | undefined;
readonly reactivityKeys?: ReadonlyArray | ReadonlyRecord> | undefined;
readonly concurrent?: boolean | undefined;
}): AtomResultFn;
};
readonly pull: (create: ((get: Context) => Stream.Stream) | Stream.Stream, options?: {
readonly disableAccumulation?: boolean;
readonly initialValue?: ReadonlyArray;
}) => Writable, void>;
readonly subscriptionRef: (create: Effect.Effect, E, Scope.Scope | R | AtomRegistry | Reactivity.Reactivity> | ((get: Context) => Effect.Effect, E, Scope.Scope | R | AtomRegistry | Reactivity.Reactivity>)) => Writable, A>;
readonly subscribable: (create: Effect.Effect, E1, Scope.Scope | R | AtomRegistry | Reactivity.Reactivity> | ((get: Context) => Effect.Effect, E1, Scope.Scope | R | AtomRegistry | Reactivity.Reactivity>)) => Atom>;
}
/**
* @since 1.0.0
* @category models
*/
export interface RuntimeFactory {
(create: Layer.Layer | ((get: Context) => Layer.Layer)): AtomRuntime;
readonly memoMap: Layer.MemoMap;
readonly addGlobalLayer: (layer: Layer.Layer) => void;
/**
* Uses the `Reactivity` service from the runtime to refresh the atom whenever
* the keys change.
*/
readonly withReactivity: (keys: ReadonlyArray | ReadonlyRecord>) => >(atom: A) => A;
}
/**
* @since 1.0.0
* @category constructors
*/
export declare const context: (options: {
readonly memoMap: Layer.MemoMap;
}) => RuntimeFactory;
/**
* @since 1.0.0
* @category context
*/
export declare const defaultMemoMap: Layer.MemoMap;
/**
* @since 1.0.0
* @category context
*/
export declare const runtime: RuntimeFactory;
/**
* An alias to `Rx.runtime.withReactivity`, for refreshing an atom whenever the
* keys change in the `Reactivity` service.
*
* @since 1.0.0
* @category Reactivity
*/
export declare const withReactivity: (keys: ReadonlyArray | ReadonlyRecord>) => >(atom: A) => A;
/**
* @since 1.0.0
* @category constructors
*/
export declare const subscriptionRef: {
(ref: SubscriptionRef.SubscriptionRef | ((get: Context) => SubscriptionRef.SubscriptionRef)): Writable;
(effect: Effect.Effect, E, Scope.Scope | AtomRegistry> | ((get: Context) => Effect.Effect, E, Scope.Scope | AtomRegistry>)): Writable, A>;
};
/**
* @since 1.0.0
* @category constructors
*/
export declare const subscribable: {
(ref: Subscribable.Subscribable | ((get: Context) => Subscribable.Subscribable)): Atom;
(effect: Effect.Effect, E, Scope.Scope | AtomRegistry> | ((get: Context) => Effect.Effect, E, Scope.Scope | AtomRegistry>)): Atom>;
};
/**
* @since 1.0.0
* @category models
*/
export interface FnContext {
(atom: Atom): A;
result(this: FnContext, atom: Atom>, options?: {
readonly suspendOnWaiting?: boolean | undefined;
}): Effect.Effect;
addFinalizer(this: FnContext, f: () => void): void;
mount(this: FnContext, atom: Atom): void;
refresh(this: FnContext, atom: Atom): void;
self(this: FnContext): Option.Option;
setSelf(this: FnContext, a: A): void;
set(this: FnContext, atom: Writable, value: W): void;
setResult(this: FnContext, atom: Writable, W>, value: W): Effect.Effect;
some(this: FnContext, atom: Atom>): Effect.Effect;
stream(this: FnContext, atom: Atom, options?: {
readonly withoutInitialValue?: boolean;
readonly bufferSize?: number;
}): Stream.Stream;
streamResult(this: FnContext, atom: Atom>, options?: {
readonly withoutInitialValue?: boolean;
readonly bufferSize?: number;
}): Stream.Stream;
subscribe(this: FnContext, atom: Atom, f: (_: A) => void, options?: {
readonly immediate?: boolean;
}): void;
readonly registry: Registry.Registry;
}
/**
* @since 1.0.0
* @category constructors
*/
export declare const fnSync: {
(): {
(f: (arg: Arg, get: FnContext) => A): Writable, Arg>;
(f: (arg: Arg, get: FnContext) => A, options: {
readonly initialValue: A;
}): Writable;
};
(f: (arg: Arg, get: FnContext) => A): Writable, Arg>;
(f: (arg: Arg, get: FnContext) => A, options: {
readonly initialValue: A;
}): Writable;
};
/**
* @since 1.0.0
* @category models
*/
export interface AtomResultFn extends Writable, Arg | Reset | Interrupt> {
}
/**
* @since 1.0.0
* @category symbols
*/
export declare const Reset: unique symbol;
/**
* @since 1.0.0
* @category symbols
*/
export type Reset = typeof Reset;
/**
* @since 1.0.0
* @category symbols
*/
export declare const Interrupt: unique symbol;
/**
* @since 1.0.0
* @category symbols
*/
export type Interrupt = typeof Interrupt;
/**
* @since 1.0.0
* @category constructors
*/
export declare const fn: {
(): (fn: (arg: Arg, get: FnContext) => Effect.Effect, options?: {
readonly initialValue?: A | undefined;
readonly concurrent?: boolean | undefined;
}) => AtomResultFn;
(fn: (arg: Arg, get: FnContext) => Effect.Effect, options?: {
readonly initialValue?: A | undefined;
readonly concurrent?: boolean | undefined;
}): AtomResultFn;
(): (fn: (arg: Arg, get: FnContext) => Stream.Stream, options?: {
readonly initialValue?: A | undefined;
readonly concurrent?: boolean | undefined;
}) => AtomResultFn;
(fn: (arg: Arg, get: FnContext) => Stream.Stream, options?: {
readonly initialValue?: A | undefined;
readonly concurrent?: boolean | undefined;
}): AtomResultFn;
};
/**
* @since 1.0.0
* @category models
*/
export type PullResult = Result.Result<{
readonly done: boolean;
readonly items: Arr.NonEmptyArray;
}, E | Cause.NoSuchElementException>;
/**
* @since 1.0.0
* @category constructors
*/
export declare const pull: (create: ((get: Context) => Stream.Stream) | Stream.Stream, options?: {
readonly disableAccumulation?: boolean | undefined;
}) => Writable, void>;
/**
* @since 1.0.0
* @category constructors
*/
export declare const family: (f: (arg: Arg) => T) => (arg: Arg) => T;
/**
* @since 1.0.0
* @category combinators
*/
export declare const withFallback: {
(fallback: Atom>): >>(self: R) => [R] extends [Writable] ? Writable> | A2, Result.Result.Failure> | E2>, RW> : Atom> | A2, Result.Result.Failure> | E2>>;
>, A2, E2>(self: R, fallback: Atom>): [R] extends [Writable] ? Writable> | A2, Result.Result.Failure> | E2>, RW> : Atom> | A2, Result.Result.Failure> | E2>>;
};
/**
* @since 1.0.0
* @category combinators
*/
export declare const keepAlive: >(self: A) => A;
/**
* Reverts the `keepAlive` behavior of a reactive value, allowing it to be
* disposed of when not in use.
*
* Note that Atom's have this behavior by default.
*
* @since 1.0.0
* @category combinators
*/
export declare const autoDispose: >(self: A) => A;
/**
* @since 1.0.0
* @category combinators
*/
export declare const setLazy: {
(lazy: boolean): >(self: A) => A;
>(self: A, lazy: boolean): A;
};
/**
* @since 1.0.0
* @category combinators
*/
export declare const withLabel: {
(name: string): >(self: A) => A;
>(self: A, name: string): A;
};
/**
* @since 1.0.0
* @category combinators
*/
export declare const initialValue: {
(initialValue: A): (self: Atom) => readonly [Atom, A];
(self: Atom, initialValue: A): readonly [Atom, A];
};
/**
* @since 1.0.0
* @category combinators
*/
export declare const transform: {
, B>(f: (get: Context) => B): (self: R) => [R] extends [Writable] ? Writable : Atom;
, B>(self: R, f: (get: Context) => B): [R] extends [Writable] ? Writable : Atom;
};
/**
* @since 1.0.0
* @category combinators
*/
export declare const map: {
, B>(f: (_: Type) => B): (self: R) => [R] extends [Writable] ? Writable : Atom;
, B>(self: R, f: (_: Type) => B): [R] extends [Writable] ? Writable : Atom;
};
/**
* @since 1.0.0
* @category combinators
*/
export declare const mapResult: {
>, B>(f: (_: Result.Result.Success>) => B): (self: R) => [R] extends [Writable] ? Writable>>, RW> : Atom>>>;
>, B>(self: R, f: (_: Result.Result.Success>) => B): [R] extends [Writable] ? Writable>>, RW> : Atom>>>;
};
/**
* @since 1.0.0
* @category combinators
*/
export declare const debounce: {
(duration: Duration.DurationInput): >(self: A) => WithoutSerializable;
>(self: A, duration: Duration.DurationInput): WithoutSerializable;
};
/**
* @since 1.0.0
* @category Optimistic
*/
export declare const optimistic: (self: Atom) => Writable>>;
/**
* @since 1.0.0
* @category Optimistic
*/
export declare const optimisticFn: {
(options: {
readonly reducer: (current: NoInfer, update: OW) => NoInfer;
readonly fn: AtomResultFn | ((set: (result: NoInfer) => void) => AtomResultFn);
}): (self: Writable>>) => AtomResultFn;
(self: Writable>>, options: {
readonly reducer: (current: NoInfer, update: OW) => NoInfer;
readonly fn: AtomResultFn | ((set: (result: NoInfer) => void) => AtomResultFn);
}): AtomResultFn;
};
/**
* @since 1.0.0
* @category batching
*/
export declare const batch: (f: () => void) => void;
/**
* @since 1.0.0
* @category Focus
*/
export declare const windowFocusSignal: Atom;
/**
* @since 1.0.0
* @category Focus
*/
export declare const makeRefreshOnSignal: <_>(signal: Atom<_>) => >(self: A) => WithoutSerializable;
/**
* @since 1.0.0
* @category Focus
*/
export declare const refreshOnWindowFocus: >(self: A) => WithoutSerializable;
/**
* @since 1.0.0
* @category KeyValueStore
*/
export declare const kvs: (options: {
readonly runtime: AtomRuntime;
readonly key: string;
readonly schema: Schema.Schema;
readonly defaultValue: LazyArg;
}) => Writable;
/**
* Create an Atom that reads and writes a URL search parameter.
*
* Note: If you pass a schema, it has to be synchronous and have no context.
*
* @since 1.0.0
* @category URL search params
*/
export declare const searchParam: (name: string, options?: {
readonly schema?: Schema.Schema;
}) => Writable<[A] extends [never] ? string : Option.Option>;
/**
* @since 1.0.0
* @category Conversions
*/
export declare const toStream: (self: Atom) => Stream.Stream;
/**
* @since 1.0.0
* @category Conversions
*/
export declare const toStreamResult: (self: Atom>) => Stream.Stream;
/**
* @since 1.0.0
* @category Conversions
*/
export declare const get: (self: Atom) => Effect.Effect;
/**
* @since 1.0.0
* @category Conversions
*/
export declare const modify: {
(f: (_: R) => [returnValue: A, nextValue: W]): (self: Writable) => Effect.Effect;
(self: Writable, f: (_: R) => [returnValue: A, nextValue: W]): Effect.Effect;
};
/**
* @since 1.0.0
* @category Conversions
*/
export declare const set: {
(value: W): (self: Writable) => Effect.Effect;
(self: Writable, value: W): Effect.Effect;
};
/**
* @since 1.0.0
* @category Conversions
*/
export declare const update: {
(f: (_: R) => W): (self: Writable) => Effect.Effect;
(self: Writable, f: (_: R) => W): Effect.Effect;
};
/**
* @since 1.0.0
* @category Conversions
*/
export declare const getResult: (self: Atom>, options?: {
readonly suspendOnWaiting?: boolean | undefined;
}) => Effect.Effect;
/**
* @since 1.0.0
* @category Conversions
*/
export declare const refresh: (self: Atom) => Effect.Effect;
/**
* @since 1.0.0
* @category Serializable
*/
export declare const SerializableTypeId: SerializableTypeId;
/**
* @since 1.0.0
* @category Serializable
*/
export type SerializableTypeId = "~effect-atom/atom/Atom/Serializable";
/**
* @since 1.0.0
* @category Serializable
*/
export interface Serializable {
readonly [SerializableTypeId]: {
readonly key: string;
readonly encode: (value: S["Type"]) => S["Encoded"];
readonly decode: (value: S["Encoded"]) => S["Type"];
};
}
/**
* @since 1.0.0
* @category Serializable
*/
export declare const isSerializable: (self: Atom) => self is Atom & Serializable;
/**
* @since 1.0.0
* @category combinators
*/
export declare const serializable: {
, S extends Schema.Schema, any>>(options: {
readonly key: string;
readonly schema: S;
}): (self: R) => R & Serializable;
, S extends Schema.Schema, any>>(self: R, options: {
readonly key: string;
readonly schema: S;
}): R & Serializable;
};
/**
* @since 1.0.0
* @category ServerValue
*/
export declare const ServerValueTypeId: "~effect-atom/atom/Atom/ServerValue";
/**
* Overrides the value of an Atom when read on the server.
*
* @since 1.0.0
* @category ServerValue
*/
export declare const withServerValue: {