// ets_tracing: off import type * as Tp from "../../Collections/Immutable/Tuple/index.js" import type { Has, Tag } from "../../Has/index.js" import { tag } from "../../Has/index.js" import * as T from "../index.js" import * as L from "../Layer/index.js" import * as Ref from "../Ref/index.js" export interface State { //readonly serviceId: `@effect-ts/core/Effect/State<${TypeTag}>` readonly get: T.Effect readonly set: (s: S) => T.Effect readonly update: (f: (s: S) => S) => T.Effect readonly modify: (f: (s: S) => Tp.Tuple<[A, S]>) => T.Effect } export interface StateExternal { readonly Tag: Tag> readonly get: T.Effect>, never, S> readonly set: (s: S) => T.Effect>, never, void> readonly update: (f: (s: S) => S) => T.Effect>, never, void> readonly modify: ( f: (s: S) => Tp.Tuple<[A, S]> ) => T.Effect>, never, A> readonly runState: ( s: S ) => (self: T.Effect> & R, E, A>) => T.Effect readonly Live: (s: S) => L.Layer>> } export function makeState(initial: S): T.Effect> { return T.map_(Ref.makeRef(initial), (ref) => ({ get: Ref.get(ref), modify: (f) => Ref.modify_(ref, f), set: (s) => Ref.set_(ref, s), update: (f) => Ref.update_(ref, f) })) } export function State(S: PropertyKey): StateExternal { const Tag = tag>(S) const derived = T.deriveLifted(Tag)(["set", "update"], ["get"], []) return { Tag, modify: (f) => T.accessServiceM(Tag)((_) => _.modify(f)), runState: (s) => T.provideServiceM(Tag)(makeState(s)), Live: (s) => L.fromEffect(Tag)(makeState(s)), ...derived } }