import { Behavior } from "./behavior.js"; import { Extent } from "./extent.js"; import { Graph } from "./graph.js"; import { GraphEvent, LinkType, Transient } from "./common.js"; /** Anything a behavior can demand: a resource, `resource.order`, or `state.trace`. */ export interface Demandable { resource: Resource; type: LinkType; } /** * A node in the graph that behaviors demand and supply. A plain Resource has no value and is * used for ordering; {@link State} and {@link Moment} carry values. Create them with * `extent.resource()`, `extent.state()` and `extent.moment()`. */ export declare class Resource implements Demandable { /** The field name on its extent (set when the extent is added) or the name passed at creation. */ debugName: string | null; /** The extent that owns this resource. */ extent: Extent; /** The graph of the owning extent. */ graph: Graph; /** The behaviors that demand this resource. */ subsequents: Set; /** The behavior that supplies this resource, or null if only actions update it. */ suppliedBy: Behavior | null; constructor(extent: Extent, name?: string); /** An ordering-only demand: the behavior runs after this resource's supplier and may read it, but does not run when it updates. Still an edge, so it can form cycles. */ get order(): Demandable; /** This resource (part of {@link Demandable}). */ get resource(): Resource; /** The link type of a plain demand (part of {@link Demandable}). */ get type(): LinkType; toString(): string; protected get isExtentRemoved(): boolean; /** Always false on a plain Resource; see State and Moment. */ get justUpdated(): boolean; /** Runs `callback` as a side effect at the end of any event in which this resource updated. Returns an unsubscribe function. */ subscribeToJustUpdated(callback: () => void): (() => void); protected notifyJustUpdatedSubscribers(): void; } /** * Something that happened in the current event, optionally with a payload of type T. It is * `justUpdated` for exactly one event and then resets. Use it for inputs, timer fires and * replies. Copy a payload into a {@link State} if it must outlast the event. */ export declare class Moment extends Resource implements Transient { private _happened; private _happenedValue; private _happenedWhen; /** True only during the event in which this moment was updated. Requires a demand or supply when read in a behavior. */ get justUpdated(): boolean; /** The payload during the event it was updated; undefined afterwards. */ get value(): T | undefined; /** The event in which this moment was last updated, or null if never. */ get event(): GraphEvent | null; toString(): string; /** True if this moment was updated in this event with a payload `==` to `value`. */ justUpdatedTo(value: T): boolean; /** Updates this moment in a new action. Use it from outside code, timers and callbacks. */ updateWithAction(value?: T | undefined, debugName?: string): void; /** Marks this moment as happened in this event. Only in an action (if no behavior supplies it) or in its supplying behavior. */ update(value?: T | undefined): void; /** Internal: called by the graph at the end of each event. */ clear(): void; } export type StateHistory = { value: T; event: GraphEvent; }; /** * A value that persists across events. It remembers the event in which it last changed, so a * behavior can ask whether it changed in this event (`justUpdated`) and what it was before * (`traceValue`). `update` compares with `===`. */ export declare class State extends Resource implements Transient { private currentState; private previousState; constructor(extent: Extent, initialState: T, name?: string); toString(): string; /** Updates this state in a new action. Use it from outside code, timers and callbacks. */ updateWithAction(newValue: T, debugName?: string): void; /** Sets the value, unless it is `===` to the current one (then nothing happens). Only in an action (if no behavior supplies it) or in its supplying behavior. */ update(newValue: T): void; /** Like `update`, but marks the state updated even if the value is `===` to the current one. */ updateForce(newValue: T): void; private _updateForce; /** Internal: called by the graph at the end of each event. */ clear(): void; /** The current value. Inside a behavior, requires a demand (plain or `.order`) or supply. */ get value(): T; /** The event in which the value last changed; sequence 0 for the initial value. */ get event(): GraphEvent; private get history(); /** A trace demand: lets a behavior read `traceValue` and `traceEvent` but not `value`. Not an edge, so it never orders or activates the behavior and cannot form a cycle. */ get trace(): Demandable; /** The value at the start of the current event (the current value outside events). Inside a behavior, requires a demand, supply, or `.trace` demand. */ get traceValue(): T; /** The event of the value returned by `traceValue`. */ get traceEvent(): GraphEvent; /** True if the value changed in the current event. */ get justUpdated(): boolean; /** True if the value changed in this event to `toState`. */ justUpdatedTo(toState: T): boolean; /** True if the value changed in this event from `fromState`. */ justUpdatedFrom(fromState: T): boolean; /** True if the value changed in this event from `fromState` to `toState`. */ justUpdatedToFrom(toState: T, fromState: T): boolean; }