@anchorlib/svelte 1.0.0-beta.2 → 1.0.0-beta.20

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.
package/dist/index.d.ts CHANGED
@@ -1,304 +1,12 @@
1
- import { LinkableSchema, StateOptions, ModelInput, ModelOutput, Immutable, ModelArray, Linkable, FetchOptions, FetchState, StreamOptions, State, HistoryOptions, HistoryState, StateBaseOptions, ImmutableOutput, Mutable, MutationKey, MutablePart, ObjLike, StateExceptionMap, KeyLike } from '@anchorlib/core';
2
- import { V as VariableRef, C as ConstantRef, S as StateRef } from './types-CsfltB4A.js';
3
- export { R as RefSubscriber, a as RefUnsubscribe } from './types-CsfltB4A.js';
4
- import { Readable } from 'svelte/store';
5
-
6
- /**
7
- * Creates a writable reference that can be used to manage state with Anchor.
8
- * This overload is used when no schema is provided, or when using a LinkableSchema with StateOptions.
9
- *
10
- * @template T - The type of the initial value
11
- * @template S - The schema type, extending LinkableSchema
12
- * @param init - The initial value for the reference
13
- * @param options - Optional state options for the reference
14
- * @returns A WritableRef containing the initial value
15
- */
16
- declare function anchorRef<T, S extends LinkableSchema = LinkableSchema>(init: T, options?: StateOptions<S>): VariableRef<T>;
17
- /**
18
- * Creates a writable reference with a defined schema for validation and type inference.
19
- *
20
- * @template S - The schema type, extending LinkableSchema
21
- * @template T - The type of the initial value, must extend ModelInput of the schema
22
- * @param init - The initial value for the reference
23
- * @param schema - The schema to validate and type the reference
24
- * @param options - Optional state options for the reference
25
- * @returns A WritableRef containing the output model based on the schema
26
- */
27
- declare function anchorRef<S extends LinkableSchema, T extends ModelInput<S>>(init: T, schema?: S, options?: StateOptions): VariableRef<ModelOutput<S>>;
28
- /**
29
- * Creates an immutable writable reference with a defined schema.
30
- *
31
- * @template S - The schema type, extending LinkableSchema
32
- * @template T - The type of the initial value, must extend ModelInput of the schema
33
- * @param init - The initial value for the reference
34
- * @param schema - The schema to validate and type the reference
35
- * @param options - State options with immutable flag set to true
36
- * @returns A WritableRef containing an immutable output model based on the schema
37
- */
38
- declare function anchorRef<S extends LinkableSchema, T extends ModelInput<S>>(init: T, schema?: S, options?: StateOptions & {
39
- immutable: true;
40
- }): VariableRef<Immutable<ModelOutput<S>>>;
41
- /**
42
- * Creates a writable reference that maintains a sorted array state based on a comparison function.
43
- *
44
- * @template T - The type of elements in the array
45
- * @template S - The schema type for array elements, extending ModelArray
46
- * @param init - The initial array value for the reference
47
- * @param compare - A function that defines the sort order of elements
48
- * @param options - Optional state options for the reference
49
- * @returns A VariableRef containing the sorted array
50
- */
51
- declare function orderedRef<T extends unknown[], S extends ModelArray = ModelArray>(init: T, compare: (a: T[number], b: T[number]) => number, options?: StateOptions<S>): VariableRef<T>;
52
- /**
53
- * Creates a writable reference that maintains a flat array state.
54
- *
55
- * @template T - The type of elements in the array
56
- * @template S - The schema type for array elements, extending ModelArray
57
- * @param init - The initial array value for the reference
58
- * @param options - Optional state options for the reference
59
- * @returns A VariableRef containing the flat array
60
- */
61
- declare function flatRef<T extends unknown[], S extends ModelArray = ModelArray>(init: T, options?: StateOptions<S>): VariableRef<T>;
62
- /**
63
- * Creates a writable reference that mutates the underlying object.
64
- *
65
- * Unless you set the global options to `cloned: true`, you don't want to use this.
66
- *
67
- * @template T - The type of the initial value
68
- * @template S - The schema type, extending LinkableSchema
69
- * @param init - The initial value for the reference
70
- * @param options - Optional state options for the reference
71
- * @returns A VariableRef containing the raw value
72
- */
73
- declare function rawRef<T extends Linkable, S extends LinkableSchema = LinkableSchema>(init: T, options?: StateOptions<S>): VariableRef<T>;
74
-
75
- /**
76
- * Creates a derived store from a state or a writable reference.
77
- *
78
- * @template T - The type of the input state
79
- * @template R - The type of the transformed output
80
- * @param state - The input state or writable reference
81
- * @param recursive - A boolean indicating whether to recursively derive the state
82
- * @returns A readable store containing the state value
83
- */
84
- declare function derivedRef<T>(state: T | VariableRef<T>, recursive?: boolean): Readable<T>;
85
- /**
86
- * Creates a derived store from a state or a writable reference with transformation.
87
- *
88
- * @template T - The type of the input state
89
- * @template R - The type of the transformed output
90
- * @param state - The input state or writable reference
91
- * @param transform - A function that transforms the current state value
92
- * @returns A readable store containing the transformed value
93
- */
94
- declare function derivedRef<T, R>(state: T | VariableRef<T>, transform: (current: T) => R): Readable<R>;
95
-
96
- /**
97
- * Creates a readable Svelte store that manages the state of a fetch request.
98
- * This overload is for GET or DELETE requests, which typically do not have a request body.
99
- *
100
- * @template R The type of the data expected in the response.
101
- * @param init The initial value for the fetch state.
102
- * @param options The options for the fetch request, including the URL and method.
103
- * @returns A `ReadableRef` containing the `FetchState` of the request.
104
- */
105
- declare function fetchRef<R>(init: R, options: FetchOptions & {
106
- method: 'GET' | 'DELETE';
107
- }): ConstantRef<FetchState<R>>;
108
- /**
109
- * Creates a readable Svelte store that manages the state of a fetch request.
110
- * This overload is for POST, PUT, or PATCH requests, which typically include a request body.
111
- *
112
- * @template R The type of the data expected in the response.
113
- * @template P The type of the request body.
114
- * @param init The initial value for the fetch state.
115
- * @param options The options for the fetch request, including the URL, method, and body.
116
- * @returns A `ReadableRef` containing the `FetchState` of the request.
117
- */
118
- declare function fetchRef<R, P>(init: R, options: FetchOptions & {
119
- method: 'POST' | 'PUT' | 'PATCH';
120
- body: P;
121
- }): ConstantRef<FetchState<R>>;
122
- /**
123
- * Creates a readable Svelte store that manages the state of a streaming request.
124
- * This overload is for GET or DELETE requests, which typically do not have a request body.
125
- *
126
- * @template R The type of the data expected in the response.
127
- * @param init The initial value for the fetch state.
128
- * @param options The options for the stream request, including the URL and method.
129
- * @returns A `ReadableRef` containing the `FetchState` of the request.
130
- */
131
- declare function streamRef<R>(init: R, options: StreamOptions<R> & {
132
- method: 'GET' | 'DELETE';
133
- }): ConstantRef<FetchState<R>>;
134
- /**
135
- * Creates a readable Svelte store that manages the state of a streaming request.
136
- * This overload is for POST, PUT, or PATCH requests, which typically include a request body.
137
- *
138
- * @template R The type of the data expected in the response.
139
- * @template P The type of the request body.
140
- * @param init The initial value for the fetch state.
141
- * @param options The options for the stream request, including the URL, method, and body.
142
- * @returns A `ReadableRef` containing the `FetchState` of the request.
143
- */
144
- declare function streamRef<R, P>(init: R, options: StreamOptions<R> & {
145
- method: 'POST' | 'PUT' | 'PATCH';
146
- body: P;
147
- }): ConstantRef<FetchState<R>>;
148
-
149
- /**
150
- * Creates a readable Svelte store that reflects the history state of a given Anchor state.
151
- * @param state The initial Anchor state.
152
- * @param options Optional history options.
153
- * @returns A readable Svelte store containing the history state.
154
- */
155
- declare function historyRef<T extends State>(state: T, options?: HistoryOptions): ConstantRef<HistoryState>;
156
-
157
- /**
158
- * Creates an immutable ref from a state object.
159
- *
160
- * @template T The type of the state object.
161
- * @template S The type of the linkable schema.
162
- * @param init The initial state object.
163
- * @param options Optional state options.
164
- * @returns A VariableRef containing the immutable state.
165
- */
166
- declare function immutableRef<T extends State, S extends LinkableSchema = LinkableSchema>(init: T, options?: StateOptions<S>): VariableRef<Immutable<T>>;
167
- /**
168
- * Creates an immutable ref from a model input and a schema.
169
- *
170
- * @template S The type of the linkable schema.
171
- * @template T The type of the model input.
172
- * @param init The initial model input.
173
- * @param schema The linkable schema.
174
- * @param options Optional base state options.
175
- * @returns A VariableRef containing the immutable output of the schema.
176
- */
177
- declare function immutableRef<S extends LinkableSchema, T extends ModelInput<S>>(init: T, schema: S, options?: StateBaseOptions): VariableRef<ImmutableOutput<S>>;
178
- /**
179
- * Creates a writable ref from a state object.
180
- *
181
- * @template T The type of the state object.
182
- * @param state The initial state object.
183
- * @returns A ConstantRef containing the mutable state.
184
- */
185
- declare function writableRef<T extends State>(state: T): ConstantRef<Mutable<T>>;
186
- /**
187
- * Creates a writable ref from a state object and a list of contracts.
188
- *
189
- * @template T The type of the state object.
190
- * @template K The type of the mutation keys.
191
- * @param state The initial state object.
192
- * @param contracts A list of mutation keys.
193
- * @returns A ConstantRef containing the mutable part of the state.
194
- */
195
- declare function writableRef<T extends State, K extends MutationKey<T>[]>(state: T, contracts: K): VariableRef<MutablePart<T, K>>;
196
-
197
- /**
198
- * Creates a model reference with mutable state.
199
- *
200
- * @template S - The linkable schema type
201
- * @template T - The model input type that extends the schema
202
- * @param schema - The schema to use for the model
203
- * @param init - The initial value for the model
204
- * @param options - Optional state configuration
205
- * @returns A variable reference containing the model output
206
- */
207
- declare function modelRef<S extends LinkableSchema, T extends ModelInput<S>>(schema: S, init: T, options?: StateBaseOptions): VariableRef<ModelOutput<S>>;
208
- /**
209
- * Creates a model reference with immutable state.
210
- *
211
- * @template S - The linkable schema type
212
- * @template T - The model input type that extends the schema
213
- * @param schema - The schema to use for the model
214
- * @param init - The initial value for the model
215
- * @param options - State configuration with immutable flag set to true
216
- * @returns A variable reference containing the immutable output
217
- */
218
- declare function modelRef<S extends LinkableSchema, T extends ModelInput<S>>(schema: S, init: T, options: StateBaseOptions & {
219
- immutable: true;
220
- }): VariableRef<ImmutableOutput<S>>;
221
- /**
222
- * Creates a constant reference that maps exceptions for a given state object or array.
223
- *
224
- * @template T - The type of the input state, must be an object-like or array type
225
- * @param state - The input state object or array to create exception mappings for
226
- * @returns A ConstantRef containing the StateExceptionMap for the provided state
227
- */
228
- declare function exceptionRef<T extends ObjLike | Array<unknown>>(state: T | VariableRef<T>): ConstantRef<StateExceptionMap<T>>;
229
-
230
- type Props = {
231
- [key: string]: KeyLike | State;
232
- };
233
- type PropsRef<T extends Props> = {
234
- [K in keyof T]: T[K] extends State ? ConstantRef<T[K]> : T[K];
235
- };
236
- /**
237
- * Creates a reactive reference object from the provided props.
238
- * For each property in the input props:
239
- * - If the value is a State object, it will be converted to a derived ref using derivedRef
240
- * - Otherwise, the value will be kept as is
241
- *
242
- * @template T - The type of props extending Props
243
- * @param {T} props - The input props object containing KeyLike or State values
244
- * @returns {PropsRef<T>} A new object with State values converted to Refs
245
- */
246
- declare function propsRef<T extends Props>(props: T): PropsRef<T>;
247
-
248
- /**
249
- * Creates a Svelte readable store that observes a reactive function and updates its subscribers
250
- * when the observed value changes. The function automatically handles observer lifecycle
251
- * and cleanup using Svelte's onDestroy hook.
252
- *
253
- * @template R - The type of the observed value
254
- * @param observe - A function that returns the value to be observed
255
- * @returns A Svelte readable store containing the observed value
256
- */
257
- declare function observedRef<R>(observe: () => R): Readable<R>;
258
-
259
- declare const REF_REGISTRY: WeakMap<ConstantRef<unknown>, StateRef<unknown>>;
260
- /**
261
- * Creates a readable reference that can be subscribed to for reactive updates.
262
- * This function initializes a reactive reference with a given initial value and provides
263
- * mechanisms for subscribing to changes and publishing updates to subscribers.
264
- *
265
- * @template T The type of the value being referenced
266
- * @param init - The initial value for the reference
267
- * @param updater
268
- * @returns A readable reference object with subscribe and publish capabilities
269
- */
270
- declare function variableRef<T>(init: T, updater?: (value: T) => T): VariableRef<T>;
271
- /**
272
- * Creates a constant (read-only) reference that can be subscribed to for reactive updates.
273
- * This function initializes a reactive reference with a given initial value that cannot be modified
274
- * after creation.
275
- *
276
- * @template T The type of the value being referenced
277
- * @param init - The initial value for the reference
278
- * @param constant - If true, the reference will be read-only and cannot be updated.
279
- * @returns A constant reference object with subscribe capability but no write access
280
- */
281
- declare function variableRef<T>(init: T, constant: true): ConstantRef<T>;
282
- /**
283
- * Creates a constant (read-only) reference that can be subscribed to for reactive updates.
284
- * This function initializes a reactive reference with a given initial value that cannot be modified
285
- * after creation. It's useful for values that should remain constant throughout the component lifecycle
286
- * but still need to be reactively tracked.
287
- *
288
- * @template T The type of the value being referenced
289
- * @param init - The initial value for the constant reference
290
- * @returns A constant reference object with subscribe capability but no write access
291
- */
292
- declare function constantRef<T>(init: T): ConstantRef<T>;
293
- /**
294
- * Checks if a given value is a writable reference.
295
- * This function uses the REF_REGISTRY to determine if the provided value
296
- * is a registered writable reference.
297
- *
298
- * @template T The type of the value that the reference holds
299
- * @param ref - The value to check
300
- * @returns True if the value is a writable reference, false otherwise
301
- */
302
- declare function isRef<T>(ref: unknown): ref is VariableRef<T>;
303
-
304
- export { ConstantRef, type Props, type PropsRef, REF_REGISTRY, StateRef, VariableRef, anchorRef, constantRef, derivedRef, exceptionRef, fetchRef, flatRef, historyRef, immutableRef, isRef, modelRef, observedRef, orderedRef, propsRef, rawRef, streamRef, variableRef, writableRef };
1
+ import { anchorRef, flatRef, orderedRef, rawRef, reactiveRef } from "./anchor.js";
2
+ import { ConstantRef, StateRef, VariableRef } from "./types.js";
3
+ import { derivedRef } from "./derive.js";
4
+ import { fetchRef, streamRef } from "./fetch.js";
5
+ import { historyRef } from "./history.js";
6
+ import { immutableRef, writableRef } from "./immutable.js";
7
+ import { exceptionRef, modelRef } from "./model.js";
8
+ import { observedRef } from "./observable.js";
9
+ import { Props, PropsRef, propsRef } from "./prop.js";
10
+ import { REF_REGISTRY, constantRef, isRef, variableRef } from "./ref.js";
11
+ import { AnchorSettings, BatchHandler, Context, ContextProvider, DerivedRef, EffectHandler, FetchOptions, FetchState, FetchStatus, HistoryOptions, HistoryState, Immutable, ImmutableOutput, ImmutableRef, MicroBatch, MicroLooper, MicroPusher, MicroTask, ModelError, ModelInput, ModelObject, Mutable, MutablePart, MutableRef, PushHandler, State, StateChange, StateException, StateExceptionHandler, StateObserver, StateOptions, StateSubscriber, StateUnsubscribe, TaskHandler, Writable, anchor, derived, effect, exception, fetchState, form, getContext, history, immutable, isImmutableRef, isMutableRef, isValueRef, microbatch, microloop, micropush, microtask, model, mutable, ordered, setContext, shortId, streamState, stringify, subscribe, undoable, writable } from "@anchorlib/core";
12
+ export { type AnchorSettings, type BatchHandler, ConstantRef, type Context, type ContextProvider, DerivedRef, type EffectHandler, type FetchOptions, type FetchState, FetchStatus, type HistoryOptions, type HistoryState, type Immutable, type ImmutableOutput, ImmutableRef, type MicroBatch, type MicroLooper, type MicroPusher, type MicroTask, type ModelError, type ModelInput, type ModelObject, type Mutable, type MutablePart, MutableRef, Props, PropsRef, type PushHandler, REF_REGISTRY, type State, type StateChange, type StateException, type StateExceptionHandler, type StateObserver, type StateOptions, StateRef, type StateSubscriber, type StateUnsubscribe, type TaskHandler, VariableRef, type Writable, anchor, anchorRef, constantRef, derived, derivedRef, effect, exception, exceptionRef, fetchRef, fetchState, flatRef, form, getContext, history, historyRef, immutable, immutableRef, isImmutableRef, isMutableRef, isRef, isValueRef, microbatch, microloop, micropush, microtask, model, modelRef, mutable, observedRef, ordered, orderedRef, propsRef, rawRef, reactiveRef, setContext, shortId, streamRef, streamState, stringify, subscribe, undoable, variableRef, writable, writableRef };
package/dist/index.js CHANGED
@@ -1,207 +1,13 @@
1
- import { anchor, derive, linkable, fetchState, streamState, history, captureStack, createObserver } from '@anchorlib/core';
2
- import { onDestroy } from 'svelte';
3
- import 'svelte/store';
1
+ import { anchorRef, flatRef, orderedRef, rawRef, reactiveRef } from "./anchor.js";
2
+ import { REF_REGISTRY, constantRef, isRef, variableRef } from "./ref.js";
3
+ import { derivedRef } from "./derive.js";
4
+ import { fetchRef, streamRef } from "./fetch.js";
5
+ import { historyRef } from "./history.js";
6
+ import { immutableRef, writableRef } from "./immutable.js";
7
+ import "./reactive.js";
8
+ import { exceptionRef, modelRef } from "./model.js";
9
+ import { observedRef } from "./observable.js";
10
+ import { propsRef } from "./prop.js";
11
+ import { DerivedRef, FetchStatus, ImmutableRef, MutableRef, anchor, derived, effect, exception, fetchState, form, getContext, history, immutable, isImmutableRef, isMutableRef, isValueRef, microbatch, microloop, micropush, microtask, model, mutable, ordered, setContext, shortId, streamState, stringify, subscribe, undoable, writable } from "@anchorlib/core";
4
12
 
5
- // src/anchor.ts
6
- var REF_REGISTRY = /* @__PURE__ */ new WeakMap();
7
- function variableRef(init, constant) {
8
- const subscribers = /* @__PURE__ */ new Set();
9
- const stateRef = anchor({ value: init }, { recursive: true });
10
- const controller = derive.resolve(stateRef);
11
- let leaveState = void 0;
12
- const set = (value) => {
13
- if (constant === true || value === stateRef.value) return;
14
- if (typeof constant === "function") {
15
- stateRef.value = constant(value);
16
- return;
17
- }
18
- if (!linkable(value)) {
19
- stateRef.value = value;
20
- return;
21
- }
22
- const { schema, configs } = anchor.has(stateRef.value) ? derive.resolve(stateRef.value)?.meta ?? {} : {};
23
- stateRef.value = anchor(value, schema ?? configs, configs);
24
- };
25
- const subscribe = (handler) => {
26
- if (!anchor.has(stateRef)) {
27
- handler(stateRef.value);
28
- return () => {
29
- };
30
- }
31
- if (!leaveState) {
32
- leaveState = controller.subscribe.all((_, event) => {
33
- if (event.type !== "init" && !event.error) {
34
- publish();
35
- }
36
- });
37
- }
38
- handler(stateRef.value);
39
- subscribers.add(handler);
40
- return () => {
41
- subscribers.delete(handler);
42
- if (!subscribers.size) {
43
- leaveState?.();
44
- leaveState = void 0;
45
- }
46
- };
47
- };
48
- const publish = () => {
49
- for (const subscriber of subscribers) {
50
- subscriber(stateRef.value);
51
- }
52
- };
53
- onDestroy(() => {
54
- subscribers.clear();
55
- leaveState?.();
56
- leaveState = void 0;
57
- REF_REGISTRY.delete(ref);
58
- anchor.destroy(stateRef);
59
- });
60
- const ref = {
61
- get value() {
62
- return stateRef.value;
63
- },
64
- set value(value) {
65
- set(value);
66
- }
67
- };
68
- Object.defineProperties(ref, {
69
- publish: {
70
- value: publish
71
- },
72
- subscribe: {
73
- value: subscribe
74
- },
75
- set: {
76
- value: set
77
- }
78
- });
79
- REF_REGISTRY.set(ref, stateRef);
80
- return ref;
81
- }
82
- function constantRef(init) {
83
- return variableRef(init, true);
84
- }
85
- function isRef(ref) {
86
- return REF_REGISTRY.has(ref);
87
- }
88
-
89
- // src/anchor.ts
90
- function anchorRef(init, schemaOptions, options) {
91
- const state = linkable(init) ? anchor(init, schemaOptions, options) : init;
92
- return variableRef(state);
93
- }
94
- function orderedRef(init, compare, options) {
95
- const state = anchor.ordered(init, compare, options);
96
- return variableRef(state);
97
- }
98
- function flatRef(init, options) {
99
- const state = anchor.flat(init, options);
100
- return variableRef(state);
101
- }
102
- function rawRef(init, options) {
103
- const state = anchor.raw(init, options);
104
- return variableRef(state);
105
- }
106
- function derivedRef(state, transformRecursive) {
107
- let target = state;
108
- if (isRef(state)) {
109
- target = REF_REGISTRY.get(state);
110
- }
111
- const subscribe = (handler) => {
112
- return derive(
113
- target,
114
- (current) => {
115
- if (isRef(state)) {
116
- current = state.value;
117
- } else {
118
- current = state;
119
- }
120
- const value = typeof transformRecursive === "function" ? transformRecursive(current) : current;
121
- handler(value);
122
- },
123
- typeof transformRecursive === "boolean" ? transformRecursive : void 0
124
- );
125
- };
126
- return { subscribe, set: () => {
127
- } };
128
- }
129
- function fetchRef(init, options) {
130
- const state = fetchState(init, options);
131
- return constantRef(state);
132
- }
133
- function streamRef(init, options) {
134
- const state = streamState(init, options);
135
- return constantRef(state);
136
- }
137
- function historyRef(state, options) {
138
- const historyState = history(state, options);
139
- return constantRef(historyState);
140
- }
141
- function immutableRef(init, schemaOptions, options) {
142
- const state = anchor.immutable(init, schemaOptions, options);
143
- return variableRef(state);
144
- }
145
- function writableRef(state, contracts) {
146
- const writableState = anchor.writable(state, contracts);
147
- return variableRef(writableState);
148
- }
149
- function modelRef(schema, init, options) {
150
- const state = anchor(init, schema, options);
151
- return variableRef(state);
152
- }
153
- function exceptionRef(state) {
154
- if (isRef(state)) {
155
- state = REF_REGISTRY.get(state).value;
156
- captureStack.violation.general(
157
- "VariableRef passing detected:",
158
- "Attempted to capture exception on a VariableRef.",
159
- new Error("Unexpected VariableRef passing"),
160
- [
161
- `While it works, it won't update when the variable value itself changed.`,
162
- `We always recommend passing the state directly instead of passing the VariableRef.`
163
- ],
164
- exceptionRef
165
- );
166
- }
167
- const exception = anchor.catch(state);
168
- return constantRef(exception);
169
- }
170
- function propsRef(props) {
171
- const ref = {};
172
- for (const [key, value] of Object.entries(props)) {
173
- if (anchor.has(value)) {
174
- ref[key] = constantRef(value);
175
- } else {
176
- ref[key] = value;
177
- }
178
- }
179
- return ref;
180
- }
181
- function observedRef(observe) {
182
- const subscribers = /* @__PURE__ */ new Set();
183
- const observer = createObserver(() => {
184
- update();
185
- });
186
- let current = observer.run(observe);
187
- const update = () => {
188
- current = observer.run(observe);
189
- subscribers.forEach((handler) => handler(current));
190
- };
191
- const subscribe = (handler) => {
192
- handler(current);
193
- subscribers.add(handler);
194
- return () => {
195
- subscribers.delete(handler);
196
- };
197
- };
198
- onDestroy(() => {
199
- observer.destroy();
200
- });
201
- return { subscribe, set: () => {
202
- } };
203
- }
204
-
205
- export { REF_REGISTRY, anchorRef, constantRef, derivedRef, exceptionRef, fetchRef, flatRef, historyRef, immutableRef, isRef, modelRef, observedRef, orderedRef, propsRef, rawRef, streamRef, variableRef, writableRef };
206
- //# sourceMappingURL=index.js.map
207
- //# sourceMappingURL=index.js.map
13
+ export { DerivedRef, FetchStatus, ImmutableRef, MutableRef, REF_REGISTRY, anchor, anchorRef, constantRef, derived, derivedRef, effect, exception, exceptionRef, fetchRef, fetchState, flatRef, form, getContext, history, historyRef, immutable, immutableRef, isImmutableRef, isMutableRef, isRef, isValueRef, microbatch, microloop, micropush, microtask, model, modelRef, mutable, observedRef, ordered, orderedRef, propsRef, rawRef, reactiveRef, setContext, shortId, streamRef, streamState, stringify, subscribe, undoable, variableRef, writable, writableRef };
@@ -0,0 +1,41 @@
1
+ import { ImmutableOutput, LinkableSchema, ModelInput, ModelOutput, ObjLike, StateBaseOptions, StateExceptionMap } from "@anchorlib/core";
2
+
3
+ //#region src/model.d.ts
4
+
5
+ /**
6
+ * @deprecated Use 'model()' instead.
7
+ * Creates a model with mutable state.
8
+ *
9
+ * @template S - The linkable schema type
10
+ * @template T - The model input type that extends the schema
11
+ * @param schema - The schema to use for the model
12
+ * @param init - The initial value for the model
13
+ * @param options - Optional state configuration
14
+ * @returns A model output.
15
+ */
16
+ declare function modelRef<S extends LinkableSchema, T extends ModelInput<S>>(schema: S, init: T, options?: StateBaseOptions): ModelOutput<S>;
17
+ /**
18
+ * @deprecated Use 'model()' instead.
19
+ * Creates a model with immutable state.
20
+ *
21
+ * @template S - The linkable schema type
22
+ * @template T - The model input type that extends the schema
23
+ * @param schema - The schema to use for the model
24
+ * @param init - The initial value for the model
25
+ * @param options - State configuration with immutable flag set to true
26
+ * @returns An immutable model output.
27
+ */
28
+ declare function modelRef<S extends LinkableSchema, T extends ModelInput<S>>(schema: S, init: T, options: StateBaseOptions & {
29
+ immutable: true;
30
+ }): ImmutableOutput<S>;
31
+ /**
32
+ * @deprecated Use 'exception()' instead.
33
+ * Creates a state that maps exceptions for a given state object or array.
34
+ *
35
+ * @template T - The type of the input state, must be an object-like or array type
36
+ * @param state - The input state object or array to create exception mappings for
37
+ * @returns A StateExceptionMap for the provided state.
38
+ */
39
+ declare function exceptionRef<T extends ObjLike | Array<unknown>>(state: T): StateExceptionMap<T>;
40
+ //#endregion
41
+ export { exceptionRef, modelRef };
package/dist/model.js ADDED
@@ -0,0 +1,20 @@
1
+ import { anchor } from "@anchorlib/core";
2
+
3
+ //#region src/model.ts
4
+ function modelRef(schema, init, options) {
5
+ return anchor(init, schema, options);
6
+ }
7
+ /**
8
+ * @deprecated Use 'exception()' instead.
9
+ * Creates a state that maps exceptions for a given state object or array.
10
+ *
11
+ * @template T - The type of the input state, must be an object-like or array type
12
+ * @param state - The input state object or array to create exception mappings for
13
+ * @returns A StateExceptionMap for the provided state.
14
+ */
15
+ function exceptionRef(state) {
16
+ return anchor.catch(state);
17
+ }
18
+
19
+ //#endregion
20
+ export { exceptionRef, modelRef };
@@ -0,0 +1,17 @@
1
+ import { ConstantRef } from "./types.js";
2
+
3
+ //#region src/observable.d.ts
4
+
5
+ /**
6
+ * @deprecated use `effect()` instead.
7
+ * Creates a read-only reference that observes a reactive function and updates its value
8
+ * when the observed value changes. The function automatically handles observer lifecycle
9
+ * and cleanup using Svelte's onDestroy hook.
10
+ *
11
+ * @template R - The type of the observed value
12
+ * @param observe - A function that returns the value to be observed
13
+ * @returns A read-only reference containing the observed value
14
+ */
15
+ declare function observedRef<R>(observe: () => R): ConstantRef<R>;
16
+ //#endregion
17
+ export { observedRef };
@@ -0,0 +1,36 @@
1
+ import { REF_REGISTRY } from "./ref.js";
2
+ import { anchor, createObserver } from "@anchorlib/core";
3
+ import { onDestroy } from "svelte";
4
+
5
+ //#region src/observable.ts
6
+ /**
7
+ * @deprecated use `effect()` instead.
8
+ * Creates a read-only reference that observes a reactive function and updates its value
9
+ * when the observed value changes. The function automatically handles observer lifecycle
10
+ * and cleanup using Svelte's onDestroy hook.
11
+ *
12
+ * @template R - The type of the observed value
13
+ * @param observe - A function that returns the value to be observed
14
+ * @returns A read-only reference containing the observed value
15
+ */
16
+ function observedRef(observe) {
17
+ const observer = createObserver(() => {
18
+ update();
19
+ });
20
+ const valueRef = anchor({ value: observer.run(observe) }, { recursive: false });
21
+ const stateRef = { get value() {
22
+ return valueRef.value;
23
+ } };
24
+ REF_REGISTRY.set(stateRef, valueRef);
25
+ const update = () => {
26
+ valueRef.value = observer.run(observe);
27
+ };
28
+ onDestroy(() => {
29
+ observer.destroy();
30
+ REF_REGISTRY.delete(stateRef);
31
+ });
32
+ return stateRef;
33
+ }
34
+
35
+ //#endregion
36
+ export { observedRef };
package/dist/prop.d.ts ADDED
@@ -0,0 +1,21 @@
1
+ import { ConstantRef } from "./types.js";
2
+ import { KeyLike, State } from "@anchorlib/core";
3
+
4
+ //#region src/prop.d.ts
5
+ type Props = {
6
+ [key: string]: KeyLike | State;
7
+ };
8
+ type PropsRef<T extends Props> = { [K in keyof T]: T[K] extends State ? ConstantRef<T[K]> : T[K] };
9
+ /**
10
+ * Creates a reactive state object from the provided props.
11
+ * For each property in the input props:
12
+ * - If the value is a State object, it will be converted to a derived state
13
+ * - Otherwise, the value will be kept as is
14
+ * @deprecated
15
+ * @template T - The type of props extending Props
16
+ * @param {T} props - The input props object containing KeyLike or State values
17
+ * @returns {PropsRef<T>} A new object with State values converted to reactive states
18
+ */
19
+ declare function propsRef<T extends Props>(props: T): T;
20
+ //#endregion
21
+ export { Props, PropsRef, propsRef };