@anchorlib/svelte 1.0.0-beta.9 → 1.0.0
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/client/index.d.ts +6 -0
- package/dist/client/index.js +52 -0
- package/dist/core/index.d.ts +1 -0
- package/dist/core/index.js +1 -0
- package/dist/index.d.ts +3 -307
- package/dist/index.js +4 -189
- package/dist/server/index.d.ts +1 -0
- package/dist/server/index.js +14 -0
- package/dist/storage/index.d.ts +2 -61
- package/dist/storage/index.js +2 -65
- package/dist/workflow/index.d.ts +1 -0
- package/dist/workflow/index.js +1 -0
- package/package.json +43 -27
- package/readme.md +7 -16
- package/dist/index.js.map +0 -1
- package/dist/storage/index.js.map +0 -1
- package/dist/types-CsfltB4A.d.ts +0 -16
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { anchor, createObserver, isBrowser, onGlobalCleanup, setCleanUpHandler, setTracker } from "@anchorlib/core";
|
|
2
|
+
import { onDestroy } from "svelte";
|
|
3
|
+
import { createSubscriber } from "svelte/reactivity";
|
|
4
|
+
|
|
5
|
+
//#region src/client/index.ts
|
|
6
|
+
anchor.configure({ globalScopeWarning: false });
|
|
7
|
+
const TRACKER_REGISTRY = /* @__PURE__ */ new WeakMap();
|
|
8
|
+
let bindingInitialized = false;
|
|
9
|
+
if (!bindingInitialized && isBrowser()) {
|
|
10
|
+
bindingInitialized = true;
|
|
11
|
+
/**
|
|
12
|
+
* Sets up a tracker function that integrates Anchor's reactivity system with Svelte's reactivity.
|
|
13
|
+
* This tracker is responsible for creating observers that watch for changes in reactive objects
|
|
14
|
+
* and properly subscribing/unsubscribing to Svelte's reactivity system.
|
|
15
|
+
*
|
|
16
|
+
* @param init - The initial linkable object to track
|
|
17
|
+
* @param observers - The observers collection to use for tracking
|
|
18
|
+
* @param key - The specific key/property to track on the object
|
|
19
|
+
*/
|
|
20
|
+
setTracker((init, observers, key) => {
|
|
21
|
+
if (!TRACKER_REGISTRY.has(init)) {
|
|
22
|
+
let track;
|
|
23
|
+
const subscribe = createSubscriber((update) => {
|
|
24
|
+
const observer = createObserver(() => {
|
|
25
|
+
observer.reset();
|
|
26
|
+
update();
|
|
27
|
+
}, void 0, true);
|
|
28
|
+
track = observer.assign(init, observers);
|
|
29
|
+
return () => {
|
|
30
|
+
observer.destroy();
|
|
31
|
+
TRACKER_REGISTRY.delete(init);
|
|
32
|
+
};
|
|
33
|
+
});
|
|
34
|
+
const assign = (prop) => {
|
|
35
|
+
subscribe();
|
|
36
|
+
track?.(prop);
|
|
37
|
+
};
|
|
38
|
+
TRACKER_REGISTRY.set(init, assign);
|
|
39
|
+
}
|
|
40
|
+
TRACKER_REGISTRY.get(init)?.(key);
|
|
41
|
+
});
|
|
42
|
+
setCleanUpHandler((handler) => {
|
|
43
|
+
try {
|
|
44
|
+
return onDestroy(handler);
|
|
45
|
+
} catch (_error) {
|
|
46
|
+
return onGlobalCleanup(handler);
|
|
47
|
+
}
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
//#endregion
|
|
52
|
+
export { TRACKER_REGISTRY };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "@anchorlib/core";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "@anchorlib/core"
|
package/dist/index.d.ts
CHANGED
|
@@ -1,307 +1,3 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
export {
|
|
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 extends Linkable, 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
|
-
* Reactive reference alias for anchorRef.
|
|
43
|
-
* @type {{<T, S=LinkableSchema extends LinkableSchema>(init: T, options?: StateOptions<S>): VariableRef<T>, <S extends LinkableSchema, T extends ModelInput<S>>(init: T, schema?: S, options?: StateOptions): VariableRef<ModelOutput<S>>, <S extends LinkableSchema, T extends ModelInput<S>>(init: T, schema?: S, options?: (StateOptions & {immutable: true})): VariableRef<Immutable<ModelOutput<S>>>}}
|
|
44
|
-
*/
|
|
45
|
-
declare const reactiveRef: typeof anchorRef;
|
|
46
|
-
/**
|
|
47
|
-
* Creates a writable reference that maintains a sorted array state based on a comparison function.
|
|
48
|
-
*
|
|
49
|
-
* @template T - The type of elements in the array
|
|
50
|
-
* @template S - The schema type for array elements, extending ModelArray
|
|
51
|
-
* @param init - The initial array value for the reference
|
|
52
|
-
* @param compare - A function that defines the sort order of elements
|
|
53
|
-
* @param options - Optional state options for the reference
|
|
54
|
-
* @returns A VariableRef containing the sorted array
|
|
55
|
-
*/
|
|
56
|
-
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>;
|
|
57
|
-
/**
|
|
58
|
-
* Creates a writable reference that maintains a flat array state.
|
|
59
|
-
*
|
|
60
|
-
* @template T - The type of elements in the array
|
|
61
|
-
* @template S - The schema type for array elements, extending ModelArray
|
|
62
|
-
* @param init - The initial array value for the reference
|
|
63
|
-
* @param options - Optional state options for the reference
|
|
64
|
-
* @returns A VariableRef containing the flat array
|
|
65
|
-
*/
|
|
66
|
-
declare function flatRef<T extends unknown[], S extends ModelArray = ModelArray>(init: T, options?: StateOptions<S>): VariableRef<T>;
|
|
67
|
-
/**
|
|
68
|
-
* Creates a writable reference that mutates the underlying object.
|
|
69
|
-
*
|
|
70
|
-
* Unless you set the global options to `cloned: true`, you don't want to use this.
|
|
71
|
-
*
|
|
72
|
-
* @template T - The type of the initial value
|
|
73
|
-
* @template S - The schema type, extending LinkableSchema
|
|
74
|
-
* @param init - The initial value for the reference
|
|
75
|
-
* @param options - Optional state options for the reference
|
|
76
|
-
* @returns A VariableRef containing the raw value
|
|
77
|
-
*/
|
|
78
|
-
declare function rawRef<T extends Linkable, S extends LinkableSchema = LinkableSchema>(init: T, options?: StateOptions<S>): VariableRef<T>;
|
|
79
|
-
|
|
80
|
-
/**
|
|
81
|
-
* Creates a derived store from a state or a writable reference.
|
|
82
|
-
*
|
|
83
|
-
* @template T - The type of the input state
|
|
84
|
-
* @template R - The type of the transformed output
|
|
85
|
-
* @param state - The input state or writable reference
|
|
86
|
-
* @returns A readable store containing the state value
|
|
87
|
-
*/
|
|
88
|
-
declare function derivedRef<T>(state: T | VariableRef<T>): Readable<T>;
|
|
89
|
-
/**
|
|
90
|
-
* Creates a derived store from a state or a writable reference with transformation.
|
|
91
|
-
*
|
|
92
|
-
* @template T - The type of the input state
|
|
93
|
-
* @template R - The type of the transformed output
|
|
94
|
-
* @param state - The input state or writable reference
|
|
95
|
-
* @param transform - A function that transforms the current state value
|
|
96
|
-
* @returns A readable store containing the transformed value
|
|
97
|
-
*/
|
|
98
|
-
declare function derivedRef<T, R>(state: T | VariableRef<T>, transform: (current: T) => R): Readable<R>;
|
|
99
|
-
|
|
100
|
-
/**
|
|
101
|
-
* Creates a readable Svelte store that manages the state of a fetch request.
|
|
102
|
-
* This overload is for GET or DELETE requests, which typically do not have a request body.
|
|
103
|
-
*
|
|
104
|
-
* @template R The type of the data expected in the response.
|
|
105
|
-
* @param init The initial value for the fetch state.
|
|
106
|
-
* @param options The options for the fetch request, including the URL and method.
|
|
107
|
-
* @returns A `ReadableRef` containing the `FetchState` of the request.
|
|
108
|
-
*/
|
|
109
|
-
declare function fetchRef<R>(init: R, options: FetchOptions & {
|
|
110
|
-
method: 'GET' | 'DELETE';
|
|
111
|
-
}): ConstantRef<FetchState<R>>;
|
|
112
|
-
/**
|
|
113
|
-
* Creates a readable Svelte store that manages the state of a fetch request.
|
|
114
|
-
* This overload is for POST, PUT, or PATCH requests, which typically include a request body.
|
|
115
|
-
*
|
|
116
|
-
* @template R The type of the data expected in the response.
|
|
117
|
-
* @template P The type of the request body.
|
|
118
|
-
* @param init The initial value for the fetch state.
|
|
119
|
-
* @param options The options for the fetch request, including the URL, method, and body.
|
|
120
|
-
* @returns A `ReadableRef` containing the `FetchState` of the request.
|
|
121
|
-
*/
|
|
122
|
-
declare function fetchRef<R, P>(init: R, options: FetchOptions & {
|
|
123
|
-
method: 'POST' | 'PUT' | 'PATCH';
|
|
124
|
-
body: P;
|
|
125
|
-
}): ConstantRef<FetchState<R>>;
|
|
126
|
-
/**
|
|
127
|
-
* Creates a readable Svelte store that manages the state of a streaming request.
|
|
128
|
-
* This overload is for GET or DELETE requests, which typically do not have a request body.
|
|
129
|
-
*
|
|
130
|
-
* @template R The type of the data expected in the response.
|
|
131
|
-
* @param init The initial value for the fetch state.
|
|
132
|
-
* @param options The options for the stream request, including the URL and method.
|
|
133
|
-
* @returns A `ReadableRef` containing the `FetchState` of the request.
|
|
134
|
-
*/
|
|
135
|
-
declare function streamRef<R>(init: R, options: StreamOptions<R> & {
|
|
136
|
-
method: 'GET' | 'DELETE';
|
|
137
|
-
}): ConstantRef<FetchState<R>>;
|
|
138
|
-
/**
|
|
139
|
-
* Creates a readable Svelte store that manages the state of a streaming request.
|
|
140
|
-
* This overload is for POST, PUT, or PATCH requests, which typically include a request body.
|
|
141
|
-
*
|
|
142
|
-
* @template R The type of the data expected in the response.
|
|
143
|
-
* @template P The type of the request body.
|
|
144
|
-
* @param init The initial value for the fetch state.
|
|
145
|
-
* @param options The options for the stream request, including the URL, method, and body.
|
|
146
|
-
* @returns A `ReadableRef` containing the `FetchState` of the request.
|
|
147
|
-
*/
|
|
148
|
-
declare function streamRef<R, P>(init: R, options: StreamOptions<R> & {
|
|
149
|
-
method: 'POST' | 'PUT' | 'PATCH';
|
|
150
|
-
body: P;
|
|
151
|
-
}): ConstantRef<FetchState<R>>;
|
|
152
|
-
|
|
153
|
-
/**
|
|
154
|
-
* Creates a readable Svelte store that reflects the history state of a given Anchor state.
|
|
155
|
-
* @param state The initial Anchor state.
|
|
156
|
-
* @param options Optional history options.
|
|
157
|
-
* @returns A readable Svelte store containing the history state.
|
|
158
|
-
*/
|
|
159
|
-
declare function historyRef<T extends State>(state: T, options?: HistoryOptions): ConstantRef<HistoryState>;
|
|
160
|
-
|
|
161
|
-
/**
|
|
162
|
-
* Creates an immutable ref from a state object.
|
|
163
|
-
*
|
|
164
|
-
* @template T The type of the state object.
|
|
165
|
-
* @template S The type of the linkable schema.
|
|
166
|
-
* @param init The initial state object.
|
|
167
|
-
* @param options Optional state options.
|
|
168
|
-
* @returns A VariableRef containing the immutable state.
|
|
169
|
-
*/
|
|
170
|
-
declare function immutableRef<T extends State, S extends LinkableSchema = LinkableSchema>(init: T, options?: StateOptions<S>): VariableRef<Immutable<T>>;
|
|
171
|
-
/**
|
|
172
|
-
* Creates an immutable ref from a model input and a schema.
|
|
173
|
-
*
|
|
174
|
-
* @template S The type of the linkable schema.
|
|
175
|
-
* @template T The type of the model input.
|
|
176
|
-
* @param init The initial model input.
|
|
177
|
-
* @param schema The linkable schema.
|
|
178
|
-
* @param options Optional base state options.
|
|
179
|
-
* @returns A VariableRef containing the immutable output of the schema.
|
|
180
|
-
*/
|
|
181
|
-
declare function immutableRef<S extends LinkableSchema, T extends ModelInput<S>>(init: T, schema: S, options?: StateBaseOptions): VariableRef<ImmutableOutput<S>>;
|
|
182
|
-
/**
|
|
183
|
-
* Creates a writable ref from a state object.
|
|
184
|
-
*
|
|
185
|
-
* @template T The type of the state object.
|
|
186
|
-
* @param state The initial state object.
|
|
187
|
-
* @returns A ConstantRef containing the mutable state.
|
|
188
|
-
*/
|
|
189
|
-
declare function writableRef<T extends State>(state: T): ConstantRef<Mutable<T>>;
|
|
190
|
-
/**
|
|
191
|
-
* Creates a writable ref from a state object and a list of contracts.
|
|
192
|
-
*
|
|
193
|
-
* @template T The type of the state object.
|
|
194
|
-
* @template K The type of the mutation keys.
|
|
195
|
-
* @param state The initial state object.
|
|
196
|
-
* @param contracts A list of mutation keys.
|
|
197
|
-
* @returns A ConstantRef containing the mutable part of the state.
|
|
198
|
-
*/
|
|
199
|
-
declare function writableRef<T extends State, K extends MutationKey<T>[]>(state: T, contracts: K): VariableRef<MutablePart<T, K>>;
|
|
200
|
-
|
|
201
|
-
/**
|
|
202
|
-
* Creates a model reference with mutable state.
|
|
203
|
-
*
|
|
204
|
-
* @template S - The linkable schema type
|
|
205
|
-
* @template T - The model input type that extends the schema
|
|
206
|
-
* @param schema - The schema to use for the model
|
|
207
|
-
* @param init - The initial value for the model
|
|
208
|
-
* @param options - Optional state configuration
|
|
209
|
-
* @returns A variable reference containing the model output
|
|
210
|
-
*/
|
|
211
|
-
declare function modelRef<S extends LinkableSchema, T extends ModelInput<S>>(schema: S, init: T, options?: StateBaseOptions): VariableRef<ModelOutput<S>>;
|
|
212
|
-
/**
|
|
213
|
-
* Creates a model reference with immutable state.
|
|
214
|
-
*
|
|
215
|
-
* @template S - The linkable schema type
|
|
216
|
-
* @template T - The model input type that extends the schema
|
|
217
|
-
* @param schema - The schema to use for the model
|
|
218
|
-
* @param init - The initial value for the model
|
|
219
|
-
* @param options - State configuration with immutable flag set to true
|
|
220
|
-
* @returns A variable reference containing the immutable output
|
|
221
|
-
*/
|
|
222
|
-
declare function modelRef<S extends LinkableSchema, T extends ModelInput<S>>(schema: S, init: T, options: StateBaseOptions & {
|
|
223
|
-
immutable: true;
|
|
224
|
-
}): VariableRef<ImmutableOutput<S>>;
|
|
225
|
-
/**
|
|
226
|
-
* Creates a constant reference that maps exceptions for a given state object or array.
|
|
227
|
-
*
|
|
228
|
-
* @template T - The type of the input state, must be an object-like or array type
|
|
229
|
-
* @param state - The input state object or array to create exception mappings for
|
|
230
|
-
* @returns A ConstantRef containing the StateExceptionMap for the provided state
|
|
231
|
-
*/
|
|
232
|
-
declare function exceptionRef<T extends ObjLike | Array<unknown>>(state: T | VariableRef<T>): ConstantRef<StateExceptionMap<T>>;
|
|
233
|
-
|
|
234
|
-
type Props = {
|
|
235
|
-
[key: string]: KeyLike | State;
|
|
236
|
-
};
|
|
237
|
-
type PropsRef<T extends Props> = {
|
|
238
|
-
[K in keyof T]: T[K] extends State ? ConstantRef<T[K]> : T[K];
|
|
239
|
-
};
|
|
240
|
-
/**
|
|
241
|
-
* Creates a reactive reference object from the provided props.
|
|
242
|
-
* For each property in the input props:
|
|
243
|
-
* - If the value is a State object, it will be converted to a derived ref using derivedRef
|
|
244
|
-
* - Otherwise, the value will be kept as is
|
|
245
|
-
*
|
|
246
|
-
* @template T - The type of props extending Props
|
|
247
|
-
* @param {T} props - The input props object containing KeyLike or State values
|
|
248
|
-
* @returns {PropsRef<T>} A new object with State values converted to Refs
|
|
249
|
-
*/
|
|
250
|
-
declare function propsRef<T extends Props>(props: T): PropsRef<T>;
|
|
251
|
-
|
|
252
|
-
/**
|
|
253
|
-
* Creates a Svelte readable store that observes a reactive function and updates its subscribers
|
|
254
|
-
* when the observed value changes. The function automatically handles observer lifecycle
|
|
255
|
-
* and cleanup using Svelte's onDestroy hook.
|
|
256
|
-
*
|
|
257
|
-
* @template R - The type of the observed value
|
|
258
|
-
* @param observe - A function that returns the value to be observed
|
|
259
|
-
* @returns A Svelte readable store containing the observed value
|
|
260
|
-
*/
|
|
261
|
-
declare function observedRef<R>(observe: () => R): Readable<R>;
|
|
262
|
-
|
|
263
|
-
declare const REF_REGISTRY: WeakMap<ConstantRef<unknown>, StateRef<unknown>>;
|
|
264
|
-
/**
|
|
265
|
-
* Creates a readable reference that can be subscribed to for reactive updates.
|
|
266
|
-
* This function initializes a reactive reference with a given initial value and provides
|
|
267
|
-
* mechanisms for subscribing to changes and publishing updates to subscribers.
|
|
268
|
-
*
|
|
269
|
-
* @template T The type of the value being referenced
|
|
270
|
-
* @param init - The initial value for the reference
|
|
271
|
-
* @returns A readable reference object with subscribe and publish capabilities
|
|
272
|
-
*/
|
|
273
|
-
declare function variableRef<T>(init: T): VariableRef<T>;
|
|
274
|
-
/**
|
|
275
|
-
* Creates a constant (read-only) reference that can be subscribed to for reactive updates.
|
|
276
|
-
* This function initializes a reactive reference with a given initial value that cannot be modified
|
|
277
|
-
* after creation.
|
|
278
|
-
*
|
|
279
|
-
* @template T The type of the value being referenced
|
|
280
|
-
* @param init - The initial value for the reference
|
|
281
|
-
* @param constant - If true, the reference will be read-only and cannot be updated.
|
|
282
|
-
* @returns A constant reference object with subscribe capability but no write access
|
|
283
|
-
*/
|
|
284
|
-
declare function variableRef<T>(init: T, constant: true): ConstantRef<T>;
|
|
285
|
-
/**
|
|
286
|
-
* Creates a constant (read-only) reference that can be subscribed to for reactive updates.
|
|
287
|
-
* This function initializes a reactive reference with a given initial value that cannot be modified
|
|
288
|
-
* after creation. It's useful for values that should remain constant throughout the component lifecycle
|
|
289
|
-
* but still need to be reactively tracked.
|
|
290
|
-
*
|
|
291
|
-
* @template T The type of the value being referenced
|
|
292
|
-
* @param init - The initial value for the constant reference
|
|
293
|
-
* @returns A constant reference object with subscribe capability but no write access
|
|
294
|
-
*/
|
|
295
|
-
declare function constantRef<T>(init: T): ConstantRef<T>;
|
|
296
|
-
/**
|
|
297
|
-
* Checks if a given value is a writable reference.
|
|
298
|
-
* This function uses the REF_REGISTRY to determine if the provided value
|
|
299
|
-
* is a registered writable reference.
|
|
300
|
-
*
|
|
301
|
-
* @template T The type of the value that the reference holds
|
|
302
|
-
* @param ref - The value to check
|
|
303
|
-
* @returns True if the value is a writable reference, false otherwise
|
|
304
|
-
*/
|
|
305
|
-
declare function isRef<T>(ref: unknown): ref is VariableRef<T>;
|
|
306
|
-
|
|
307
|
-
export { ConstantRef, type Props, type PropsRef, REF_REGISTRY, StateRef, VariableRef, anchorRef, constantRef, derivedRef, exceptionRef, fetchRef, flatRef, historyRef, immutableRef, isRef, modelRef, observedRef, orderedRef, propsRef, rawRef, reactiveRef, streamRef, variableRef, writableRef };
|
|
1
|
+
import { ASYNC_STATUS, AnchorSettings, AsyncHandler, AsyncOptions, AsyncState, BatchHandler, CookieEntry, CookieJar, CookieOptions, Debouncer, DerivedRef, EffectHandler, FetchOptions, FetchState, FetchStatus, Future, 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, awaited, cookies, createContext, createLifecycle, debouncer, decodeCookies, derived, effect, encodeCookies, exception, fetchState, form, getContext, getCookieJar, history, immutable, isImmutableRef, isMutableRef, isValueRef, microbatch, microloop, micropush, microtask, model, mutable, ordered, query, setContext, setCookieContext, setReactive, shortId, snapshot, streamState, stringify, subscribe, undoable, untrack, withIsolation, withScope, writable } from "@anchorlib/core";
|
|
2
|
+
import { WORKFLOW_STATUS, WORKFLOW_STORE, plan } from "@anchorlib/core/workflow";
|
|
3
|
+
export { ASYNC_STATUS, type AnchorSettings, type AsyncHandler, type AsyncOptions, type AsyncState, type BatchHandler, type CookieEntry, CookieJar, type CookieOptions, type Debouncer, DerivedRef, type EffectHandler, type FetchOptions, type FetchState, FetchStatus, type Future, 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, type PushHandler, type State, type StateChange, type StateException, type StateExceptionHandler, type StateObserver, type StateOptions, type StateSubscriber, type StateUnsubscribe, type TaskHandler, WORKFLOW_STATUS, WORKFLOW_STORE, type Writable, anchor, awaited, cookies, createContext, createLifecycle, debouncer, decodeCookies, derived, effect, encodeCookies, exception, fetchState, form, getContext, getCookieJar, history, immutable, isImmutableRef, isMutableRef, isValueRef, microbatch, microloop, micropush, microtask, model, mutable, ordered, plan, query, setContext, setCookieContext, setReactive, shortId, snapshot, streamState, stringify, subscribe, undoable, untrack, withIsolation, withScope, writable };
|
package/dist/index.js
CHANGED
|
@@ -1,190 +1,5 @@
|
|
|
1
|
-
import
|
|
2
|
-
import {
|
|
3
|
-
import
|
|
1
|
+
import "./client/index.js";
|
|
2
|
+
import { ASYNC_STATUS, CookieJar, DerivedRef, FetchStatus, ImmutableRef, MutableRef, anchor, awaited, cookies, createContext, createLifecycle, debouncer, decodeCookies, derived, effect, encodeCookies, exception, fetchState, form, getContext, getCookieJar, history, immutable, isImmutableRef, isMutableRef, isValueRef, microbatch, microloop, micropush, microtask, model, mutable, ordered, query, setContext, setCookieContext, setReactive, shortId, snapshot, streamState, stringify, subscribe, undoable, untrack, withIsolation, withScope, writable } from "@anchorlib/core";
|
|
3
|
+
import { WORKFLOW_STATUS, WORKFLOW_STORE, plan } from "@anchorlib/core/workflow";
|
|
4
4
|
|
|
5
|
-
|
|
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 = subscribe.resolve(stateRef);
|
|
11
|
-
let leaveState = void 0;
|
|
12
|
-
const set = (value) => {
|
|
13
|
-
if (constant === true || value === stateRef.value) return;
|
|
14
|
-
stateRef.value = value;
|
|
15
|
-
};
|
|
16
|
-
const subscribe$1 = (handler) => {
|
|
17
|
-
if (!leaveState) {
|
|
18
|
-
leaveState = controller.subscribe.all((_, event) => {
|
|
19
|
-
if (event.type !== "init" && !event.error) {
|
|
20
|
-
publish();
|
|
21
|
-
}
|
|
22
|
-
});
|
|
23
|
-
}
|
|
24
|
-
handler(stateRef.value);
|
|
25
|
-
subscribers.add(handler);
|
|
26
|
-
return () => {
|
|
27
|
-
subscribers.delete(handler);
|
|
28
|
-
if (!subscribers.size) {
|
|
29
|
-
leaveState?.();
|
|
30
|
-
leaveState = void 0;
|
|
31
|
-
}
|
|
32
|
-
};
|
|
33
|
-
};
|
|
34
|
-
const publish = () => {
|
|
35
|
-
for (const subscriber of subscribers) {
|
|
36
|
-
subscriber(stateRef.value);
|
|
37
|
-
}
|
|
38
|
-
};
|
|
39
|
-
onDestroy(() => {
|
|
40
|
-
subscribers.clear();
|
|
41
|
-
leaveState?.();
|
|
42
|
-
leaveState = void 0;
|
|
43
|
-
REF_REGISTRY.delete(ref);
|
|
44
|
-
anchor.destroy(stateRef);
|
|
45
|
-
});
|
|
46
|
-
const ref = {
|
|
47
|
-
get value() {
|
|
48
|
-
return stateRef.value;
|
|
49
|
-
},
|
|
50
|
-
set value(value) {
|
|
51
|
-
set(value);
|
|
52
|
-
}
|
|
53
|
-
};
|
|
54
|
-
Object.defineProperties(ref, {
|
|
55
|
-
publish: {
|
|
56
|
-
value: publish
|
|
57
|
-
},
|
|
58
|
-
subscribe: {
|
|
59
|
-
value: subscribe$1
|
|
60
|
-
},
|
|
61
|
-
set: {
|
|
62
|
-
value: set
|
|
63
|
-
}
|
|
64
|
-
});
|
|
65
|
-
REF_REGISTRY.set(ref, stateRef);
|
|
66
|
-
return ref;
|
|
67
|
-
}
|
|
68
|
-
function constantRef(init) {
|
|
69
|
-
return variableRef(init, true);
|
|
70
|
-
}
|
|
71
|
-
function isRef(ref) {
|
|
72
|
-
return REF_REGISTRY.has(ref);
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
// src/anchor.ts
|
|
76
|
-
function anchorRef(init, schemaOptions, options) {
|
|
77
|
-
const state = anchor(init, schemaOptions, options);
|
|
78
|
-
return variableRef(state);
|
|
79
|
-
}
|
|
80
|
-
var reactiveRef = anchorRef;
|
|
81
|
-
function orderedRef(init, compare, options) {
|
|
82
|
-
const state = anchor.ordered(init, compare, options);
|
|
83
|
-
return variableRef(state);
|
|
84
|
-
}
|
|
85
|
-
function flatRef(init, options) {
|
|
86
|
-
const state = anchor.flat(init, options);
|
|
87
|
-
return variableRef(state);
|
|
88
|
-
}
|
|
89
|
-
function rawRef(init, options) {
|
|
90
|
-
const state = anchor.raw(init, options);
|
|
91
|
-
return variableRef(state);
|
|
92
|
-
}
|
|
93
|
-
function derivedRef(state, transform) {
|
|
94
|
-
let target = state;
|
|
95
|
-
if (isRef(state)) {
|
|
96
|
-
target = REF_REGISTRY.get(state);
|
|
97
|
-
}
|
|
98
|
-
const subscribe$1 = (handler) => {
|
|
99
|
-
return subscribe(target, (current) => {
|
|
100
|
-
if (isRef(state)) {
|
|
101
|
-
current = state.value;
|
|
102
|
-
} else {
|
|
103
|
-
current = state;
|
|
104
|
-
}
|
|
105
|
-
const value = typeof transform === "function" ? transform(current) : current;
|
|
106
|
-
handler(value);
|
|
107
|
-
});
|
|
108
|
-
};
|
|
109
|
-
return { subscribe: subscribe$1, set: () => {
|
|
110
|
-
} };
|
|
111
|
-
}
|
|
112
|
-
function fetchRef(init, options) {
|
|
113
|
-
const state = fetchState(init, options);
|
|
114
|
-
return constantRef(state);
|
|
115
|
-
}
|
|
116
|
-
function streamRef(init, options) {
|
|
117
|
-
const state = streamState(init, options);
|
|
118
|
-
return constantRef(state);
|
|
119
|
-
}
|
|
120
|
-
function historyRef(state, options) {
|
|
121
|
-
const historyState = history(state, options);
|
|
122
|
-
return constantRef(historyState);
|
|
123
|
-
}
|
|
124
|
-
function immutableRef(init, schemaOptions, options) {
|
|
125
|
-
const state = anchor.immutable(init, schemaOptions, options);
|
|
126
|
-
return variableRef(state);
|
|
127
|
-
}
|
|
128
|
-
function writableRef(state, contracts) {
|
|
129
|
-
const writableState = anchor.writable(state, contracts);
|
|
130
|
-
return variableRef(writableState);
|
|
131
|
-
}
|
|
132
|
-
function modelRef(schema, init, options) {
|
|
133
|
-
const state = anchor(init, schema, options);
|
|
134
|
-
return variableRef(state);
|
|
135
|
-
}
|
|
136
|
-
function exceptionRef(state) {
|
|
137
|
-
if (isRef(state)) {
|
|
138
|
-
state = REF_REGISTRY.get(state).value;
|
|
139
|
-
captureStack.violation.general(
|
|
140
|
-
"VariableRef passing detected:",
|
|
141
|
-
"Attempted to capture exception on a VariableRef.",
|
|
142
|
-
new Error("Unexpected VariableRef passing"),
|
|
143
|
-
[
|
|
144
|
-
`While it works, it won't update when the variable value itself changed.`,
|
|
145
|
-
`We always recommend passing the state directly instead of passing the VariableRef.`
|
|
146
|
-
],
|
|
147
|
-
exceptionRef
|
|
148
|
-
);
|
|
149
|
-
}
|
|
150
|
-
const exception = anchor.catch(state);
|
|
151
|
-
return constantRef(exception);
|
|
152
|
-
}
|
|
153
|
-
function propsRef(props) {
|
|
154
|
-
const ref = {};
|
|
155
|
-
for (const [key, value] of Object.entries(props)) {
|
|
156
|
-
if (anchor.has(value)) {
|
|
157
|
-
ref[key] = constantRef(value);
|
|
158
|
-
} else {
|
|
159
|
-
ref[key] = value;
|
|
160
|
-
}
|
|
161
|
-
}
|
|
162
|
-
return ref;
|
|
163
|
-
}
|
|
164
|
-
function observedRef(observe) {
|
|
165
|
-
const subscribers = /* @__PURE__ */ new Set();
|
|
166
|
-
const observer = createObserver(() => {
|
|
167
|
-
update();
|
|
168
|
-
});
|
|
169
|
-
let current = observer.run(observe);
|
|
170
|
-
const update = () => {
|
|
171
|
-
current = observer.run(observe);
|
|
172
|
-
subscribers.forEach((handler) => handler(current));
|
|
173
|
-
};
|
|
174
|
-
const subscribe = (handler) => {
|
|
175
|
-
handler(current);
|
|
176
|
-
subscribers.add(handler);
|
|
177
|
-
return () => {
|
|
178
|
-
subscribers.delete(handler);
|
|
179
|
-
};
|
|
180
|
-
};
|
|
181
|
-
onDestroy(() => {
|
|
182
|
-
observer.destroy();
|
|
183
|
-
});
|
|
184
|
-
return { subscribe, set: () => {
|
|
185
|
-
} };
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
export { REF_REGISTRY, anchorRef, constantRef, derivedRef, exceptionRef, fetchRef, flatRef, historyRef, immutableRef, isRef, modelRef, observedRef, orderedRef, propsRef, rawRef, reactiveRef, streamRef, variableRef, writableRef };
|
|
189
|
-
//# sourceMappingURL=index.js.map
|
|
190
|
-
//# sourceMappingURL=index.js.map
|
|
5
|
+
export { ASYNC_STATUS, CookieJar, DerivedRef, FetchStatus, ImmutableRef, MutableRef, WORKFLOW_STATUS, WORKFLOW_STORE, anchor, awaited, cookies, createContext, createLifecycle, debouncer, decodeCookies, derived, effect, encodeCookies, exception, fetchState, form, getContext, getCookieJar, history, immutable, isImmutableRef, isMutableRef, isValueRef, microbatch, microloop, micropush, microtask, model, mutable, ordered, plan, query, setContext, setCookieContext, setReactive, shortId, snapshot, streamState, stringify, subscribe, undoable, untrack, withIsolation, withScope, writable };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { };
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { isBrowser, setAsyncScope, setReactive } from "@anchorlib/core";
|
|
2
|
+
import { AsyncLocalStorage } from "node:async_hooks";
|
|
3
|
+
|
|
4
|
+
//#region src/server/index.ts
|
|
5
|
+
var AnchorASL = class extends AsyncLocalStorage {
|
|
6
|
+
store = /* @__PURE__ */ new Map();
|
|
7
|
+
getStore() {
|
|
8
|
+
return super.getStore() ?? this.store;
|
|
9
|
+
}
|
|
10
|
+
};
|
|
11
|
+
setAsyncScope(new AnchorASL());
|
|
12
|
+
if (!isBrowser()) setReactive(false);
|
|
13
|
+
|
|
14
|
+
//#endregion
|
package/dist/storage/index.d.ts
CHANGED
|
@@ -1,61 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
import { ObjLike, LinkableSchema, StateOptions } from '@anchorlib/core';
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Creates a reactive reference to a key-value store state.
|
|
7
|
-
*
|
|
8
|
-
* This function initializes a key-value store with the given name and initial value,
|
|
9
|
-
* and automatically cleans up the store subscription when the component is destroyed.
|
|
10
|
-
*
|
|
11
|
-
* @template T - The type of the stored value, must extend Storable
|
|
12
|
-
* @param name - The unique identifier for the key-value store
|
|
13
|
-
* @param init - The initial value for the store
|
|
14
|
-
* @returns A reactive reference to the key-value store state
|
|
15
|
-
*/
|
|
16
|
-
declare function kvRef<T extends Storable>(name: string, init: T): ConstantRef<KVState<T>>;
|
|
17
|
-
|
|
18
|
-
/**
|
|
19
|
-
* Creates a persistent reactive reference using the provided name, initial value, and options.
|
|
20
|
-
* The persistentRef is tied to the browser's local storage, meaning its value will persist
|
|
21
|
-
* across page reloads and browser sessions until explicitly cleared.
|
|
22
|
-
*
|
|
23
|
-
* @template T - The type of the initial value, must extend object-like structure.
|
|
24
|
-
* @template S - The schema type for linkable validation, defaults to LinkableSchema.
|
|
25
|
-
* @param name - A unique string identifier for the local storage key.
|
|
26
|
-
* @param init - The initial value to be stored in local storage.
|
|
27
|
-
* @param options - Optional configuration for state behavior and validation schema.
|
|
28
|
-
* @returns A WritableRef<T> that provides reactive access and modification capabilities.
|
|
29
|
-
*/
|
|
30
|
-
declare function persistentRef<T extends ObjLike, S extends LinkableSchema = LinkableSchema>(name: string, init: T, options?: StateOptions<S>): ConstantRef<T>;
|
|
31
|
-
|
|
32
|
-
/**
|
|
33
|
-
* Creates a session-scoped reactive reference using the provided name, initial value, and options.
|
|
34
|
-
* The sessionRef is tied to the browser's session storage, meaning its value will persist
|
|
35
|
-
* across page reloads but not after the session ends (e.g., tab/window closed).
|
|
36
|
-
*
|
|
37
|
-
* @template T - The type of the initial value, must extend object-like structure.
|
|
38
|
-
* @template S - The schema type for linkable validation, defaults to LinkableSchema.
|
|
39
|
-
* @param name - A unique string identifier for the session storage key.
|
|
40
|
-
* @param init - The initial value to be stored in session storage.
|
|
41
|
-
* @param options - Optional configuration for state behavior and validation schema.
|
|
42
|
-
* @returns A WritableRef<T> that provides reactive access and modification capabilities.
|
|
43
|
-
*/
|
|
44
|
-
declare function sessionRef<T extends ObjLike, S extends LinkableSchema = LinkableSchema>(name: string, init: T, options?: StateOptions<S>): ConstantRef<T>;
|
|
45
|
-
|
|
46
|
-
interface TableRef<T extends Rec, R extends Row<T> = Row<T>> {
|
|
47
|
-
get(id: string): ConstantRef<RowState<R>>;
|
|
48
|
-
add(payload: T): ConstantRef<RowState<R>>;
|
|
49
|
-
remove(id: string): ConstantRef<RowState<R>>;
|
|
50
|
-
list(filter?: IDBKeyRange | FilterFn<R>, limit?: number, direction?: IDBCursorDirection): ConstantRef<RowListState<R>>;
|
|
51
|
-
listByIndex(name: keyof R, filter?: IDBKeyRange | FilterFn<R>, limit?: number, direction?: IDBCursorDirection): ConstantRef<RowListState<R>>;
|
|
52
|
-
seed<T extends R[]>(seeds: T): this;
|
|
53
|
-
table(): ReactiveTable<T>;
|
|
54
|
-
}
|
|
55
|
-
type InferRef<T> = T extends TableRef<Rec, infer R> ? ConstantRef<R> : never;
|
|
56
|
-
type InferListRef<T> = T extends TableRef<Rec, infer R> ? ConstantRef<R[]> : never;
|
|
57
|
-
|
|
58
|
-
declare function createTableRef<T extends ReactiveTable<Rec>>(table: T): TableRef<InferRec<T>>;
|
|
59
|
-
declare function createTableRef<T extends Rec, R extends Row<T> = Row<T>>(name: string, version?: number, indexes?: (keyof R)[], remIndexes?: (keyof R)[], dbName?: string): TableRef<T, R>;
|
|
60
|
-
|
|
61
|
-
export { type InferListRef, type InferRef, type TableRef, createTableRef, kvRef, persistentRef, sessionRef };
|
|
1
|
+
export * from "@anchorlib/storage";
|
|
2
|
+
export * from "@anchorlib/storage/db";
|
package/dist/storage/index.js
CHANGED
|
@@ -1,66 +1,3 @@
|
|
|
1
|
-
|
|
2
|
-
import { constantRef } from '../index.js';
|
|
3
|
-
import { onDestroy } from 'svelte';
|
|
4
|
-
import { persistent, session } from '@anchorlib/storage';
|
|
1
|
+
export * from "@anchorlib/storage"
|
|
5
2
|
|
|
6
|
-
|
|
7
|
-
function kvRef(name, init) {
|
|
8
|
-
const state = kv(name, init);
|
|
9
|
-
onDestroy(() => {
|
|
10
|
-
kv.leave(state);
|
|
11
|
-
});
|
|
12
|
-
return constantRef(state);
|
|
13
|
-
}
|
|
14
|
-
function persistentRef(name, init, options) {
|
|
15
|
-
const state = persistent(name, init, options);
|
|
16
|
-
return constantRef(state);
|
|
17
|
-
}
|
|
18
|
-
function sessionRef(name, init, options) {
|
|
19
|
-
const state = session(name, init, options);
|
|
20
|
-
return constantRef(state);
|
|
21
|
-
}
|
|
22
|
-
function createTableRef(tableName, version = 1, indexes, remIndexes, dbName = tableName) {
|
|
23
|
-
if (typeof tableName === "string") {
|
|
24
|
-
tableName = createTable(tableName, version, indexes, remIndexes, dbName);
|
|
25
|
-
}
|
|
26
|
-
const tableRef = tableName;
|
|
27
|
-
return {
|
|
28
|
-
get(id) {
|
|
29
|
-
const state = tableRef.get(id);
|
|
30
|
-
onDestroy(() => {
|
|
31
|
-
tableRef.leave(id);
|
|
32
|
-
});
|
|
33
|
-
return constantRef(state);
|
|
34
|
-
},
|
|
35
|
-
add(payload) {
|
|
36
|
-
const state = tableRef.add(payload);
|
|
37
|
-
onDestroy(() => {
|
|
38
|
-
tableRef.leave(state.data.id);
|
|
39
|
-
});
|
|
40
|
-
return constantRef(state);
|
|
41
|
-
},
|
|
42
|
-
list(filter, limit, direction) {
|
|
43
|
-
const state = tableRef.list(filter, limit, direction);
|
|
44
|
-
return constantRef(state);
|
|
45
|
-
},
|
|
46
|
-
listByIndex(name, filter, limit, direction) {
|
|
47
|
-
const state = tableRef.listByIndex(name, filter, limit, direction);
|
|
48
|
-
return constantRef(state);
|
|
49
|
-
},
|
|
50
|
-
remove(id) {
|
|
51
|
-
const state = tableRef.remove(id);
|
|
52
|
-
return constantRef(state);
|
|
53
|
-
},
|
|
54
|
-
seed(seeds) {
|
|
55
|
-
tableRef.seed(seeds);
|
|
56
|
-
return this;
|
|
57
|
-
},
|
|
58
|
-
table() {
|
|
59
|
-
return tableRef;
|
|
60
|
-
}
|
|
61
|
-
};
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
export { createTableRef, kvRef, persistentRef, sessionRef };
|
|
65
|
-
//# sourceMappingURL=index.js.map
|
|
66
|
-
//# sourceMappingURL=index.js.map
|
|
3
|
+
export * from "@anchorlib/storage/db"
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "@anchorlib/core/workflow";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "@anchorlib/core/workflow"
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@anchorlib/svelte",
|
|
3
|
-
"version": "1.0.0
|
|
3
|
+
"version": "1.0.0",
|
|
4
4
|
"description": "Svelte bindings for Anchor reactive state management",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"state",
|
|
@@ -10,14 +10,14 @@
|
|
|
10
10
|
"svelte"
|
|
11
11
|
],
|
|
12
12
|
"author": "Nanang Mahdaen El Agung <mahdaen@gmail.com>",
|
|
13
|
-
"homepage": "https://github.com/beerush-id/
|
|
13
|
+
"homepage": "https://github.com/beerush-id/airstack",
|
|
14
14
|
"license": "MIT",
|
|
15
15
|
"repository": {
|
|
16
16
|
"type": "git",
|
|
17
|
-
"url": "git+https://github.com/beerush-id/
|
|
17
|
+
"url": "git+https://github.com/beerush-id/airstack.git"
|
|
18
18
|
},
|
|
19
19
|
"bugs": {
|
|
20
|
-
"url": "https://github.com/beerush-id/
|
|
20
|
+
"url": "https://github.com/beerush-id/airstack/issues"
|
|
21
21
|
},
|
|
22
22
|
"type": "module",
|
|
23
23
|
"module": "./dist/index.js",
|
|
@@ -29,10 +29,29 @@
|
|
|
29
29
|
"svelte": "./dist/index.js",
|
|
30
30
|
"import": "./dist/index.js"
|
|
31
31
|
},
|
|
32
|
+
"./client": {
|
|
33
|
+
"types": "./dist/client/index.d.ts",
|
|
34
|
+
"svelte": "./dist/client/index.js",
|
|
35
|
+
"import": "./dist/client/index.js"
|
|
36
|
+
},
|
|
37
|
+
"./reactive": {
|
|
38
|
+
"types": "./dist/client/index.d.ts",
|
|
39
|
+
"svelte": "./dist/client/index.js",
|
|
40
|
+
"import": "./dist/client/index.js"
|
|
41
|
+
},
|
|
42
|
+
"./core": {
|
|
43
|
+
"types": "./dist/core/index.d.ts",
|
|
44
|
+
"svelte": "./dist/core/index.js",
|
|
45
|
+
"import": "./dist/core/index.js"
|
|
46
|
+
},
|
|
32
47
|
"./storage": {
|
|
33
48
|
"types": "./dist/storage/index.d.ts",
|
|
34
49
|
"svelte": "./dist/storage/index.js",
|
|
35
50
|
"import": "./dist/storage/index.js"
|
|
51
|
+
},
|
|
52
|
+
"./workflow": {
|
|
53
|
+
"types": "./dist/workflow/index.d.ts",
|
|
54
|
+
"import": "./dist/workflow/index.js"
|
|
36
55
|
}
|
|
37
56
|
},
|
|
38
57
|
"directories": {
|
|
@@ -45,38 +64,35 @@
|
|
|
45
64
|
"access": "public"
|
|
46
65
|
},
|
|
47
66
|
"dependencies": {
|
|
48
|
-
"@anchorlib/core": "^1.0.0
|
|
49
|
-
"@anchorlib/storage": "^1.0.0
|
|
67
|
+
"@anchorlib/core": "^1.0.0",
|
|
68
|
+
"@anchorlib/storage": "^1.0.0"
|
|
50
69
|
},
|
|
51
70
|
"devDependencies": {
|
|
52
|
-
"@
|
|
53
|
-
"@
|
|
54
|
-
"@
|
|
55
|
-
"@
|
|
56
|
-
"@
|
|
57
|
-
"@
|
|
58
|
-
"@
|
|
71
|
+
"@biomejs/biome": "2.3.3",
|
|
72
|
+
"@sveltejs/vite-plugin-svelte": "^5.0.0",
|
|
73
|
+
"@testing-library/svelte": "5.2.8",
|
|
74
|
+
"@testing-library/user-event": "14.6.1",
|
|
75
|
+
"@types/bun": "1.3.1",
|
|
76
|
+
"@types/react": "^19.1.0",
|
|
77
|
+
"@vitest/coverage-v8": "^3.2.4",
|
|
59
78
|
"@vitest/ui": "^3.2.4",
|
|
60
|
-
"
|
|
61
|
-
"
|
|
62
|
-
"
|
|
63
|
-
"
|
|
64
|
-
"
|
|
65
|
-
"rimraf": "^6.0.1",
|
|
66
|
-
"tsup": "^8.4.0",
|
|
67
|
-
"typescript-eslint": "^8.27.0",
|
|
79
|
+
"jsdom": "27.0.1",
|
|
80
|
+
"publint": "0.3.15",
|
|
81
|
+
"rimraf": "6.0.1",
|
|
82
|
+
"tsdown": "0.15.9",
|
|
83
|
+
"vite": "8.0.10",
|
|
68
84
|
"vitest": "^3.2.4"
|
|
69
85
|
},
|
|
70
86
|
"peerDependencies": {
|
|
71
|
-
"svelte": "^5.
|
|
72
|
-
"typescript": "^5.
|
|
87
|
+
"svelte": "^5.0.0",
|
|
88
|
+
"typescript": "^5.9.3"
|
|
73
89
|
},
|
|
74
90
|
"scripts": {
|
|
75
|
-
"dev": "rimraf dist &&
|
|
91
|
+
"dev": "rimraf dist && tsdown --watch ./src",
|
|
76
92
|
"clean": "rimraf dist",
|
|
77
|
-
"build": "rimraf dist &&
|
|
78
|
-
"prepublish": "rimraf dist &&
|
|
79
|
-
"format": "
|
|
93
|
+
"build": "rimraf dist && tsdown && publint",
|
|
94
|
+
"prepublish": "rimraf dist && tsdown && publint",
|
|
95
|
+
"format": "biome format --write",
|
|
80
96
|
"test": "rimraf coverage && vitest --run",
|
|
81
97
|
"test:preview": "rimraf coverage && vitest --run && vite preview --outDir coverage"
|
|
82
98
|
},
|
package/readme.md
CHANGED
|
@@ -1,15 +1,6 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Anchor Svelte Library
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
## Features
|
|
6
|
-
|
|
7
|
-
- **Reactive State Management** - Directly mutate state without needing setState patterns
|
|
8
|
-
- **Automatic Cleanup** - Automatic subscription cleanup using Svelte's onDestroy lifecycle
|
|
9
|
-
- **Svelte 5 Runes Integration** - Works with Svelte 5's runes system
|
|
10
|
-
- **Framework Optimized** - Designed specifically for Svelte's reactivity model
|
|
11
|
-
- **Schema Validation** - Built-in Zod schema validation support
|
|
12
|
-
- **Type Safety** - Full TypeScript support with comprehensive type definitions
|
|
3
|
+
This is the official Anchor library for Svelte. It provides a set of tools to manage state in your Svelte applications, based on the principles of the Anchor framework.
|
|
13
4
|
|
|
14
5
|
## Installation
|
|
15
6
|
|
|
@@ -19,7 +10,7 @@ npm install @anchorlib/svelte
|
|
|
19
10
|
|
|
20
11
|
## Documentation
|
|
21
12
|
|
|
22
|
-
For full documentation, visit [Anchor for Svelte](https://
|
|
13
|
+
For full documentation, visit [Anchor for Svelte](https://airlib.dev/svelte/introduction.html)
|
|
23
14
|
|
|
24
15
|
## Quick Start
|
|
25
16
|
|
|
@@ -38,10 +29,10 @@ For full documentation, visit [Anchor for Svelte](https://anchorlib.dev/docs/sve
|
|
|
38
29
|
</script>
|
|
39
30
|
|
|
40
31
|
<div>
|
|
41
|
-
<h1>Hello {
|
|
42
|
-
<p>Count: {
|
|
43
|
-
<button onclick={() =>
|
|
44
|
-
<button onclick={() =>
|
|
32
|
+
<h1>Hello {state.user.name}</h1>
|
|
33
|
+
<p>Count: {state.count}</p>
|
|
34
|
+
<button onclick={() => state.count++}>Increment</button>
|
|
35
|
+
<button onclick={() => state.user.name = 'Jane Doe'}>Change Name</button>
|
|
45
36
|
</div>
|
|
46
37
|
```
|
|
47
38
|
|
package/dist/index.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/ref.ts","../src/anchor.ts","../src/derive.ts","../src/fetch.ts","../src/history.ts","../src/immutable.ts","../src/model.ts","../src/prop.ts","../src/observable.ts"],"names":["derive","subscribe","anchor","onDestroy"],"mappings":";;;;;AAIO,IAAM,YAAA,uBAAmB,OAAA;AAmCzB,SAAS,WAAA,CAAe,MAAS,QAAA,EAAoB;AAC1D,EAAA,MAAM,WAAA,uBAAkB,GAAA,EAAsB;AAC9C,EAAA,MAAM,QAAA,GAAW,OAAO,EAAE,KAAA,EAAO,MAAK,EAAG,EAAE,SAAA,EAAW,IAAA,EAAM,CAAA;AAC5D,EAAA,MAAM,UAAA,GAAaA,SAAA,CAAO,OAAA,CAAQ,QAAQ,CAAA;AAE1C,EAAA,IAAI,UAAA,GAAwD,MAAA;AAE5D,EAAA,MAAM,GAAA,GAAM,CAAC,KAAA,KAAa;AAExB,IAAA,IAAI,QAAA,KAAa,IAAA,IAAQ,KAAA,KAAU,QAAA,CAAS,KAAA,EAAO;AAEnD,IAAA,QAAA,CAAS,KAAA,GAAQ,KAAA;AAAA,EACnB,CAAA;AAEA,EAAA,MAAMC,WAAA,GAAY,CAAC,OAAA,KAA8B;AAC/C,IAAA,IAAI,CAAC,UAAA,EAAY;AACf,MAAA,UAAA,GAAa,UAAA,CAAW,SAAA,CAAU,GAAA,CAAI,CAAC,GAAG,KAAA,KAAU;AA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AAM,WAAA,uBAAkB,GAAA,EAAsB;AAC9C,EAAA,MAAM,QAAA,GAAW,eAAe,MAAM;AACpC,IAAA,MAAA,EAAO;AAAA,EACT,CAAC,CAAA;AAED,EAAA,IAAI,OAAA,GAAU,QAAA,CAAS,GAAA,CAAI,OAAO,CAAA;AAElC,EAAA,MAAM,SAAS,MAAM;AACnB,IAAA,OAAA,GAAU,QAAA,CAAS,IAAI,OAAO,CAAA;AAC9B,IAAA,WAAA,CAAY,OAAA,CAAQ,CAAC,OAAA,KAAY,OAAA,CAAQ,OAAO,CAAC,CAAA;AAAA,EACnD,CAAA;AAEA,EAAA,MAAM,SAAA,GAAY,CAAC,OAAA,KAA8B;AAC/C,IAAA,OAAA,CAAQ,OAAO,CAAA;AACf,IAAA,WAAA,CAAY,IAAI,OAAO,CAAA;AAEvB,IAAA,OAAO,MAAM;AACX,MAAA,WAAA,CAAY,OAAO,OAAO,CAAA;AAAA,IAC5B,CAAA;AAAA,EACF,CAAA;AAEA,EAAAC,UAAU,MAAM;AACd,IAAA,QAAA,CAAS,OAAA,EAAQ;AAAA,EACnB,CAAC,CAAA;AAED,EAAA,OAAO,EAAE,SAAA,EAAW,GAAA,EAAK,MAAM;AAAA,EAAC,CAAA,EAAE;AACpC","file":"index.js","sourcesContent":["import type { ConstantRef, RefSubscriber, StateRef, VariableRef } from './types.js';\nimport { anchor, type StateController, subscribe as derive } from '@anchorlib/core';\nimport { onDestroy } from 'svelte';\n\nexport const REF_REGISTRY = new WeakMap<ConstantRef<unknown>, StateRef<unknown>>();\n\n/**\n * Creates a readable reference that can be subscribed to for reactive updates.\n * This function initializes a reactive reference with a given initial value and provides\n * mechanisms for subscribing to changes and publishing updates to subscribers.\n *\n * @template T The type of the value being referenced\n * @param init - The initial value for the reference\n * @returns A readable reference object with subscribe and publish capabilities\n */\nexport function variableRef<T>(init: T): VariableRef<T>;\n\n/**\n * Creates a constant (read-only) reference that can be subscribed to for reactive updates.\n * This function initializes a reactive reference with a given initial value that cannot be modified\n * after creation.\n *\n * @template T The type of the value being referenced\n * @param init - The initial value for the reference\n * @param constant - If true, the reference will be read-only and cannot be updated.\n * @returns A constant reference object with subscribe capability but no write access\n */\nexport function variableRef<T>(init: T, constant: true): ConstantRef<T>;\n\n/**\n * Creates a readable reference that can be subscribed to for reactive updates.\n * This function initializes a reactive reference with a given initial value and provides\n * mechanisms for subscribing to changes and publishing updates to subscribers.\n *\n * @template T The type of the value being referenced\n * @param init - The initial value for the reference\n * @param constant - If true, the reference will be read-only and cannot be updated.\n * @returns A readable reference object with subscribe and publish capabilities\n */\nexport function variableRef<T>(init: T, constant?: boolean) {\n const subscribers = new Set<RefSubscriber<T>>();\n const stateRef = anchor({ value: init }, { recursive: true });\n const controller = derive.resolve(stateRef) as StateController;\n\n let leaveState: ((destroy?: boolean) => void) | undefined = undefined;\n\n const set = (value: T) => {\n // Ignore if the value is the same.\n if (constant === true || value === stateRef.value) return;\n\n stateRef.value = value;\n };\n\n const subscribe = (handler: RefSubscriber<T>) => {\n if (!leaveState) {\n leaveState = controller.subscribe.all((_, event) => {\n if (event.type !== 'init' && !event.error) {\n publish();\n }\n });\n }\n\n handler(stateRef.value);\n subscribers.add(handler);\n\n return () => {\n subscribers.delete(handler);\n\n if (!subscribers.size) {\n leaveState?.();\n leaveState = undefined;\n }\n };\n };\n\n const publish = () => {\n for (const subscriber of subscribers) {\n subscriber(stateRef.value);\n }\n };\n\n onDestroy(() => {\n // Clear the subscribers.\n subscribers.clear();\n // Leave the ref state.\n leaveState?.();\n leaveState = undefined;\n\n // Remove the ref from the registry.\n REF_REGISTRY.delete(ref as ConstantRef<unknown>);\n\n // Destroy the ref state.\n anchor.destroy(stateRef);\n });\n\n const ref = {\n get value() {\n return stateRef.value;\n },\n set value(value: T) {\n set(value);\n },\n } as never;\n\n Object.defineProperties(ref, {\n publish: {\n value: publish,\n },\n subscribe: {\n value: subscribe,\n },\n set: {\n value: set,\n },\n });\n\n REF_REGISTRY.set(ref as ConstantRef<unknown>, stateRef);\n\n return ref as ConstantRef<T>;\n}\n\n/**\n * Creates a constant (read-only) reference that can be subscribed to for reactive updates.\n * This function initializes a reactive reference with a given initial value that cannot be modified\n * after creation. It's useful for values that should remain constant throughout the component lifecycle\n * but still need to be reactively tracked.\n *\n * @template T The type of the value being referenced\n * @param init - The initial value for the constant reference\n * @returns A constant reference object with subscribe capability but no write access\n */\nexport function constantRef<T>(init: T): ConstantRef<T> {\n return variableRef(init, true);\n}\n\n/**\n * Checks if a given value is a writable reference.\n * This function uses the REF_REGISTRY to determine if the provided value\n * is a registered writable reference.\n *\n * @template T The type of the value that the reference holds\n * @param ref - The value to check\n * @returns True if the value is a writable reference, false otherwise\n */\nexport function isRef<T>(ref: unknown): ref is VariableRef<T> {\n return REF_REGISTRY.has(ref as VariableRef<unknown>);\n}\n","import {\n anchor,\n type Immutable,\n type Linkable,\n type LinkableSchema,\n type ModelArray,\n type ModelInput,\n type ModelOutput,\n type StateOptions,\n} from '@anchorlib/core';\nimport type { VariableRef } from './types.js';\nimport { variableRef } from './ref.js';\n\n/**\n * Creates a writable reference that can be used to manage state with Anchor.\n * This overload is used when no schema is provided, or when using a LinkableSchema with StateOptions.\n *\n * @template T - The type of the initial value\n * @template S - The schema type, extending LinkableSchema\n * @param init - The initial value for the reference\n * @param options - Optional state options for the reference\n * @returns A WritableRef containing the initial value\n */\nexport function anchorRef<T extends Linkable, S extends LinkableSchema = LinkableSchema>(\n init: T,\n options?: StateOptions<S>\n): VariableRef<T>;\n\n/**\n * Creates a writable reference with a defined schema for validation and type inference.\n *\n * @template S - The schema type, extending LinkableSchema\n * @template T - The type of the initial value, must extend ModelInput of the schema\n * @param init - The initial value for the reference\n * @param schema - The schema to validate and type the reference\n * @param options - Optional state options for the reference\n * @returns A WritableRef containing the output model based on the schema\n */\nexport function anchorRef<S extends LinkableSchema, T extends ModelInput<S>>(\n init: T,\n schema?: S,\n options?: StateOptions\n): VariableRef<ModelOutput<S>>;\n\n/**\n * Creates an immutable writable reference with a defined schema.\n *\n * @template S - The schema type, extending LinkableSchema\n * @template T - The type of the initial value, must extend ModelInput of the schema\n * @param init - The initial value for the reference\n * @param schema - The schema to validate and type the reference\n * @param options - State options with immutable flag set to true\n * @returns A WritableRef containing an immutable output model based on the schema\n */\nexport function anchorRef<S extends LinkableSchema, T extends ModelInput<S>>(\n init: T,\n schema?: S,\n options?: StateOptions & { immutable: true }\n): VariableRef<Immutable<ModelOutput<S>>>;\n\n/**\n * Creates a writable reference for state management with optional schema validation.\n *\n * @template T - The type of the initial value\n * @template S - The schema type or options\n * @param init - The initial value for the reference\n * @param schemaOptions - Either a schema or state options\n * @param options - Additional state options when schema is provided\n * @returns A WritableRef containing the managed state\n */\nexport function anchorRef<T extends Linkable, S extends LinkableSchema = LinkableSchema>(\n init: T,\n schemaOptions?: S | StateOptions,\n options?: StateOptions\n): VariableRef<T | ModelOutput<S> | Immutable<ModelOutput<S>>> {\n const state = anchor<S, ModelInput<S>>(init as ModelInput<S>, schemaOptions as S, options);\n return variableRef(state);\n}\n\n/**\n * Reactive reference alias for anchorRef.\n * @type {{<T, S=LinkableSchema extends LinkableSchema>(init: T, options?: StateOptions<S>): VariableRef<T>, <S extends LinkableSchema, T extends ModelInput<S>>(init: T, schema?: S, options?: StateOptions): VariableRef<ModelOutput<S>>, <S extends LinkableSchema, T extends ModelInput<S>>(init: T, schema?: S, options?: (StateOptions & {immutable: true})): VariableRef<Immutable<ModelOutput<S>>>}}\n */\nexport const reactiveRef = anchorRef;\n\n/**\n * Creates a writable reference that maintains a sorted array state based on a comparison function.\n *\n * @template T - The type of elements in the array\n * @template S - The schema type for array elements, extending ModelArray\n * @param init - The initial array value for the reference\n * @param compare - A function that defines the sort order of elements\n * @param options - Optional state options for the reference\n * @returns A VariableRef containing the sorted array\n */\nexport function orderedRef<T extends unknown[], S extends ModelArray = ModelArray>(\n init: T,\n compare: (a: T[number], b: T[number]) => number,\n options?: StateOptions<S>\n): VariableRef<T> {\n const state = anchor.ordered(init, compare, options);\n return variableRef(state);\n}\n\n/**\n * Creates a writable reference that maintains a flat array state.\n *\n * @template T - The type of elements in the array\n * @template S - The schema type for array elements, extending ModelArray\n * @param init - The initial array value for the reference\n * @param options - Optional state options for the reference\n * @returns A VariableRef containing the flat array\n */\nexport function flatRef<T extends unknown[], S extends ModelArray = ModelArray>(\n init: T,\n options?: StateOptions<S>\n): VariableRef<T> {\n const state = anchor.flat(init, options);\n return variableRef(state);\n}\n\n/**\n * Creates a writable reference that mutates the underlying object.\n *\n * Unless you set the global options to `cloned: true`, you don't want to use this.\n *\n * @template T - The type of the initial value\n * @template S - The schema type, extending LinkableSchema\n * @param init - The initial value for the reference\n * @param options - Optional state options for the reference\n * @returns A VariableRef containing the raw value\n */\nexport function rawRef<T extends Linkable, S extends LinkableSchema = LinkableSchema>(\n init: T,\n options?: StateOptions<S>\n): VariableRef<T> {\n const state = anchor.raw(init, options);\n return variableRef(state);\n}\n","import { subscribe as derive } from '@anchorlib/core';\nimport type { Readable } from 'svelte/store';\nimport type { StateRef, VariableRef } from './types.js';\nimport { isRef, REF_REGISTRY } from './ref.js';\n\n/**\n * Creates a derived store from a state or a writable reference.\n *\n * @template T - The type of the input state\n * @template R - The type of the transformed output\n * @param state - The input state or writable reference\n * @returns A readable store containing the state value\n */\nexport function derivedRef<T>(state: T | VariableRef<T>): Readable<T>;\n\n/**\n * Creates a derived store from a state or a writable reference with transformation.\n *\n * @template T - The type of the input state\n * @template R - The type of the transformed output\n * @param state - The input state or writable reference\n * @param transform - A function that transforms the current state value\n * @returns A readable store containing the transformed value\n */\nexport function derivedRef<T, R>(state: T | VariableRef<T>, transform: (current: T) => R): Readable<R>;\n\n/**\n * Creates a derived store from a state or a writable reference with optional transformation.\n *\n * @template T - The type of the input state\n * @template R - The type of the transformed output\n * @param state - The input state or writable reference\n * @param transform - An optional function that transforms the current state value\n * @returns A readable store containing the state value or transformed value\n */\nexport function derivedRef<T, R>(state: T | VariableRef<T>, transform?: (current: T) => R): Readable<T | R> {\n let target = state;\n\n if (isRef(state)) {\n target = REF_REGISTRY.get(state as VariableRef<unknown>) as T;\n }\n\n const subscribe = (handler: (output: T | R) => void) => {\n return derive(target, (current) => {\n if (isRef(state)) {\n current = (state as StateRef<T>).value;\n } else {\n current = state;\n }\n\n const value = typeof transform === 'function' ? transform(current as T) : current;\n handler(value as T);\n });\n };\n\n return { subscribe, set: () => {} } as Readable<T | R>;\n}\n","import { type FetchOptions, fetchState, type FetchState, type StreamOptions, streamState } from '@anchorlib/core';\nimport type { ConstantRef } from './types.js';\nimport { constantRef } from './ref.js';\n\n/**\n * Creates a readable Svelte store that manages the state of a fetch request.\n * This overload is for GET or DELETE requests, which typically do not have a request body.\n *\n * @template R The type of the data expected in the response.\n * @param init The initial value for the fetch state.\n * @param options The options for the fetch request, including the URL and method.\n * @returns A `ReadableRef` containing the `FetchState` of the request.\n */\nexport function fetchRef<R>(init: R, options: FetchOptions & { method: 'GET' | 'DELETE' }): ConstantRef<FetchState<R>>;\n\n/**\n * Creates a readable Svelte store that manages the state of a fetch request.\n * This overload is for POST, PUT, or PATCH requests, which typically include a request body.\n *\n * @template R The type of the data expected in the response.\n * @template P The type of the request body.\n * @param init The initial value for the fetch state.\n * @param options The options for the fetch request, including the URL, method, and body.\n * @returns A `ReadableRef` containing the `FetchState` of the request.\n */\nexport function fetchRef<R, P>(\n init: R,\n options: FetchOptions & { method: 'POST' | 'PUT' | 'PATCH'; body: P }\n): ConstantRef<FetchState<R>>;\n/** @internal */\nexport function fetchRef<R>(init: R, options: FetchOptions): ConstantRef<FetchState<R>> {\n const state = fetchState(init, options);\n return constantRef(state);\n}\n\n/**\n * Creates a readable Svelte store that manages the state of a streaming request.\n * This overload is for GET or DELETE requests, which typically do not have a request body.\n *\n * @template R The type of the data expected in the response.\n * @param init The initial value for the fetch state.\n * @param options The options for the stream request, including the URL and method.\n * @returns A `ReadableRef` containing the `FetchState` of the request.\n */\nexport function streamRef<R>(\n init: R,\n options: StreamOptions<R> & { method: 'GET' | 'DELETE' }\n): ConstantRef<FetchState<R>>;\n\n/**\n * Creates a readable Svelte store that manages the state of a streaming request.\n * This overload is for POST, PUT, or PATCH requests, which typically include a request body.\n *\n * @template R The type of the data expected in the response.\n * @template P The type of the request body.\n * @param init The initial value for the fetch state.\n * @param options The options for the stream request, including the URL, method, and body.\n * @returns A `ReadableRef` containing the `FetchState` of the request.\n */\nexport function streamRef<R, P>(\n init: R,\n options: StreamOptions<R> & { method: 'POST' | 'PUT' | 'PATCH'; body: P }\n): ConstantRef<FetchState<R>>;\n\n/** @internal */\nexport function streamRef<R>(init: R, options: StreamOptions<R>): ConstantRef<FetchState<R>> {\n const state = streamState(init, options);\n return constantRef(state);\n}\n","import type { HistoryOptions, HistoryState, State } from '@anchorlib/core';\nimport { history } from '@anchorlib/core';\nimport type { ConstantRef } from './types.js';\nimport { constantRef } from './ref.js';\n\n/**\n * Creates a readable Svelte store that reflects the history state of a given Anchor state.\n * @param state The initial Anchor state.\n * @param options Optional history options.\n * @returns A readable Svelte store containing the history state.\n */\nexport function historyRef<T extends State>(state: T, options?: HistoryOptions): ConstantRef<HistoryState> {\n const historyState = history(state, options);\n return constantRef(historyState);\n}\n","import {\n anchor,\n type Immutable,\n type ImmutableOutput,\n type LinkableSchema,\n type ModelInput,\n type Mutable,\n type MutablePart,\n type MutationKey,\n type State,\n type StateBaseOptions,\n type StateOptions,\n} from '@anchorlib/core';\nimport { variableRef } from './ref.js';\nimport type { ConstantRef, VariableRef } from './types.js';\n\n/**\n * Creates an immutable ref from a state object.\n *\n * @template T The type of the state object.\n * @template S The type of the linkable schema.\n * @param init The initial state object.\n * @param options Optional state options.\n * @returns A VariableRef containing the immutable state.\n */\nexport function immutableRef<T extends State, S extends LinkableSchema = LinkableSchema>(\n init: T,\n options?: StateOptions<S>\n): VariableRef<Immutable<T>>;\n\n/**\n * Creates an immutable ref from a model input and a schema.\n *\n * @template S The type of the linkable schema.\n * @template T The type of the model input.\n * @param init The initial model input.\n * @param schema The linkable schema.\n * @param options Optional base state options.\n * @returns A VariableRef containing the immutable output of the schema.\n */\nexport function immutableRef<S extends LinkableSchema, T extends ModelInput<S>>(\n init: T,\n schema: S,\n options?: StateBaseOptions\n): VariableRef<ImmutableOutput<S>>;\n\n/** Implementation of `immutableRef` overloads. */\nexport function immutableRef<T extends State, S extends LinkableSchema = LinkableSchema>(\n init: T,\n schemaOptions?: S | StateOptions,\n options?: StateOptions<S>\n): VariableRef<Immutable<T>> {\n const state = anchor.immutable(init as never, schemaOptions as never, options);\n return variableRef(state) as VariableRef<Immutable<T>>;\n}\n\n/**\n * Creates a writable ref from a state object.\n *\n * @template T The type of the state object.\n * @param state The initial state object.\n * @returns A ConstantRef containing the mutable state.\n */\nexport function writableRef<T extends State>(state: T): ConstantRef<Mutable<T>>;\n\n/**\n * Creates a writable ref from a state object and a list of contracts.\n *\n * @template T The type of the state object.\n * @template K The type of the mutation keys.\n * @param state The initial state object.\n * @param contracts A list of mutation keys.\n * @returns A ConstantRef containing the mutable part of the state.\n */\nexport function writableRef<T extends State, K extends MutationKey<T>[]>(\n state: T,\n contracts: K\n): VariableRef<MutablePart<T, K>>;\n\n/** Implementation of `writableRef` overloads. */\nexport function writableRef<T extends State, K extends MutationKey<T>[]>(state: T, contracts?: K): ConstantRef<T> {\n const writableState = anchor.writable(state, contracts);\n return variableRef(writableState) as ConstantRef<T>;\n}\n","import {\n anchor,\n captureStack,\n type ImmutableOutput,\n type LinkableSchema,\n type ModelInput,\n type ModelOutput,\n type ObjLike,\n type StateBaseOptions,\n type StateExceptionMap,\n} from '@anchorlib/core';\nimport type { ConstantRef, VariableRef } from './types.js';\nimport { constantRef, isRef, REF_REGISTRY, variableRef } from './ref.js';\n\n/**\n * Creates a model reference with mutable state.\n *\n * @template S - The linkable schema type\n * @template T - The model input type that extends the schema\n * @param schema - The schema to use for the model\n * @param init - The initial value for the model\n * @param options - Optional state configuration\n * @returns A variable reference containing the model output\n */\nexport function modelRef<S extends LinkableSchema, T extends ModelInput<S>>(\n schema: S,\n init: T,\n options?: StateBaseOptions\n): VariableRef<ModelOutput<S>>;\n\n/**\n * Creates a model reference with immutable state.\n *\n * @template S - The linkable schema type\n * @template T - The model input type that extends the schema\n * @param schema - The schema to use for the model\n * @param init - The initial value for the model\n * @param options - State configuration with immutable flag set to true\n * @returns A variable reference containing the immutable output\n */\nexport function modelRef<S extends LinkableSchema, T extends ModelInput<S>>(\n schema: S,\n init: T,\n options: StateBaseOptions & { immutable: true }\n): VariableRef<ImmutableOutput<S>>;\n\nexport function modelRef<S extends LinkableSchema, T extends ModelInput<S>>(\n schema: S,\n init: T,\n options?: StateBaseOptions\n) {\n const state = anchor(init, schema, options);\n return variableRef(state);\n}\n\n/**\n * Creates a constant reference that maps exceptions for a given state object or array.\n *\n * @template T - The type of the input state, must be an object-like or array type\n * @param state - The input state object or array to create exception mappings for\n * @returns A ConstantRef containing the StateExceptionMap for the provided state\n */\nexport function exceptionRef<T extends ObjLike | Array<unknown>>(\n state: T | VariableRef<T>\n): ConstantRef<StateExceptionMap<T>> {\n if (isRef(state)) {\n state = REF_REGISTRY.get(state as VariableRef<unknown>)!.value as T;\n\n captureStack.violation.general(\n 'VariableRef passing detected:',\n 'Attempted to capture exception on a VariableRef.',\n new Error('Unexpected VariableRef passing'),\n [\n `While it works, it won't update when the variable value itself changed.`,\n `We always recommend passing the state directly instead of passing the VariableRef.`,\n ],\n exceptionRef\n );\n }\n\n const exception = anchor.catch(state);\n return constantRef(exception);\n}\n","import { anchor, type KeyLike, type State } from '@anchorlib/core';\nimport { constantRef } from './ref.js';\nimport type { ConstantRef } from './types.js';\n\nexport type Props = {\n [key: string]: KeyLike | State;\n};\n\nexport type PropsRef<T extends Props> = {\n [K in keyof T]: T[K] extends State ? ConstantRef<T[K]> : T[K];\n};\n\n/**\n * Creates a reactive reference object from the provided props.\n * For each property in the input props:\n * - If the value is a State object, it will be converted to a derived ref using derivedRef\n * - Otherwise, the value will be kept as is\n *\n * @template T - The type of props extending Props\n * @param {T} props - The input props object containing KeyLike or State values\n * @returns {PropsRef<T>} A new object with State values converted to Refs\n */\nexport function propsRef<T extends Props>(props: T): PropsRef<T> {\n const ref = {} as Props;\n\n for (const [key, value] of Object.entries(props)) {\n if (anchor.has(value as State)) {\n ref[key] = constantRef(value as State) as ConstantRef<T[keyof T]>;\n } else {\n ref[key] = value as T[keyof T];\n }\n }\n\n return ref as PropsRef<T>;\n}\n","import { type Readable } from 'svelte/store';\nimport { createObserver } from '@anchorlib/core';\nimport type { RefSubscriber } from './types.js';\nimport { onDestroy } from 'svelte';\n\n/**\n * Creates a Svelte readable store that observes a reactive function and updates its subscribers\n * when the observed value changes. The function automatically handles observer lifecycle\n * and cleanup using Svelte's onDestroy hook.\n *\n * @template R - The type of the observed value\n * @param observe - A function that returns the value to be observed\n * @returns A Svelte readable store containing the observed value\n */\nexport function observedRef<R>(observe: () => R): Readable<R> {\n const subscribers = new Set<RefSubscriber<R>>();\n const observer = createObserver(() => {\n update();\n });\n\n let current = observer.run(observe);\n\n const update = () => {\n current = observer.run(observe);\n subscribers.forEach((handler) => handler(current));\n };\n\n const subscribe = (handler: RefSubscriber<R>) => {\n handler(current);\n subscribers.add(handler);\n\n return () => {\n subscribers.delete(handler);\n };\n };\n\n onDestroy(() => {\n observer.destroy();\n });\n\n return { subscribe, set: () => {} } as Readable<R>;\n}\n"]}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/storage/kv.ts","../../src/storage/persistent.ts","../../src/storage/session.ts","../../src/storage/table.ts"],"names":["constantRef","onDestroy"],"mappings":";;;;;;AAgBO,SAAS,KAAA,CAA0B,MAAc,IAAA,EAAkC;AACxF,EAAA,MAAM,KAAA,GAAQ,EAAA,CAAG,IAAA,EAAM,IAAI,CAAA;AAE3B,EAAA,SAAA,CAAU,MAAM;AACd,IAAA,EAAA,CAAG,MAAM,KAAK,CAAA;AAAA,EAChB,CAAC,CAAA;AAED,EAAA,OAAO,YAAY,KAAK,CAAA;AAC1B;ACP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,MAAM,KAAA,GAAQ,QAAA,CAAS,IAAA,CAAK,MAAA,EAAQ,OAAO,SAAS,CAAA;AACpD,MAAA,OAAOA,YAAY,KAAK,CAAA;AAAA,IAC1B,CAAA;AAAA,IACA,WAAA,CAAY,IAAA,EAAe,MAAA,EAAoC,KAAA,EAAgB,SAAA,EAAgC;AAC7G,MAAA,MAAM,QAAQ,QAAA,CAAS,WAAA,CAAY,IAAA,EAAM,MAAA,EAAQ,OAAO,SAAS,CAAA;AACjE,MAAA,OAAOA,YAAY,KAAK,CAAA;AAAA,IAC1B,CAAA;AAAA,IACA,OAAO,EAAA,EAAY;AACjB,MAAA,MAAM,KAAA,GAAQ,QAAA,CAAS,MAAA,CAAO,EAAE,CAAA;AAChC,MAAA,OAAOA,YAAY,KAAK,CAAA;AAAA,IAC1B,CAAA;AAAA,IACA,KAAK,KAAA,EAAY;AACf,MAAA,QAAA,CAAS,KAAK,KAAK,CAAA;AACnB,MAAA,OAAO,IAAA;AAAA,IACT,CAAA;AAAA,IACA,KAAA,GAAQ;AACN,MAAA,OAAO,QAAA;AAAA,IACT;AAAA,GACF;AACF","file":"index.js","sourcesContent":["import { kv, type KVState, type Storable } from '@anchorlib/storage/db';\nimport type { ConstantRef } from '@base/index.js';\nimport { constantRef } from '@base/index.js';\nimport { onDestroy } from 'svelte';\n\n/**\n * Creates a reactive reference to a key-value store state.\n *\n * This function initializes a key-value store with the given name and initial value,\n * and automatically cleans up the store subscription when the component is destroyed.\n *\n * @template T - The type of the stored value, must extend Storable\n * @param name - The unique identifier for the key-value store\n * @param init - The initial value for the store\n * @returns A reactive reference to the key-value store state\n */\nexport function kvRef<T extends Storable>(name: string, init: T): ConstantRef<KVState<T>> {\n const state = kv(name, init);\n\n onDestroy(() => {\n kv.leave(state);\n });\n\n return constantRef(state);\n}\n","import type { LinkableSchema, ObjLike, StateOptions } from '@anchorlib/core';\nimport type { ConstantRef } from '@base/index.js';\nimport { constantRef } from '@base/index.js';\nimport { persistent } from '@anchorlib/storage';\n\n/**\n * Creates a persistent reactive reference using the provided name, initial value, and options.\n * The persistentRef is tied to the browser's local storage, meaning its value will persist\n * across page reloads and browser sessions until explicitly cleared.\n *\n * @template T - The type of the initial value, must extend object-like structure.\n * @template S - The schema type for linkable validation, defaults to LinkableSchema.\n * @param name - A unique string identifier for the local storage key.\n * @param init - The initial value to be stored in local storage.\n * @param options - Optional configuration for state behavior and validation schema.\n * @returns A WritableRef<T> that provides reactive access and modification capabilities.\n */\nexport function persistentRef<T extends ObjLike, S extends LinkableSchema = LinkableSchema>(\n name: string,\n init: T,\n options?: StateOptions<S>\n): ConstantRef<T> {\n const state = persistent(name, init, options);\n return constantRef<T>(state);\n}\n","import type { LinkableSchema, ObjLike, StateOptions } from '@anchorlib/core';\nimport type { ConstantRef } from '@base/index.js';\nimport { constantRef } from '@base/index.js';\nimport { session } from '@anchorlib/storage';\n\n/**\n * Creates a session-scoped reactive reference using the provided name, initial value, and options.\n * The sessionRef is tied to the browser's session storage, meaning its value will persist\n * across page reloads but not after the session ends (e.g., tab/window closed).\n *\n * @template T - The type of the initial value, must extend object-like structure.\n * @template S - The schema type for linkable validation, defaults to LinkableSchema.\n * @param name - A unique string identifier for the session storage key.\n * @param init - The initial value to be stored in session storage.\n * @param options - Optional configuration for state behavior and validation schema.\n * @returns A WritableRef<T> that provides reactive access and modification capabilities.\n */\nexport function sessionRef<T extends ObjLike, S extends LinkableSchema = LinkableSchema>(\n name: string,\n init: T,\n options?: StateOptions<S>\n): ConstantRef<T> {\n const state = session(name, init, options);\n return constantRef<T>(state);\n}\n","import {\n createTable,\n type FilterFn,\n type InferRec,\n type ReactiveTable,\n type Rec,\n type Row,\n} from '@anchorlib/storage/db';\nimport { onDestroy } from 'svelte';\nimport { constantRef } from '@base/index.js';\nimport type { TableRef } from './types.js';\n\nexport function createTableRef<T extends ReactiveTable<Rec>>(table: T): TableRef<InferRec<T>>;\nexport function createTableRef<T extends Rec, R extends Row<T> = Row<T>>(\n name: string,\n version?: number,\n indexes?: (keyof R)[],\n remIndexes?: (keyof R)[],\n dbName?: string\n): TableRef<T, R>;\nexport function createTableRef<T extends Rec, R extends Row<T> = Row<T>>(\n tableName: string | ReactiveTable<T>,\n version = 1,\n indexes?: (keyof R)[],\n remIndexes?: (keyof R)[],\n dbName = tableName as string\n): TableRef<T, R> {\n if (typeof tableName === 'string') {\n tableName = createTable<T, R>(tableName, version, indexes, remIndexes, dbName);\n }\n\n const tableRef = tableName as ReactiveTable<T, R>;\n\n return {\n get(id: string) {\n const state = tableRef.get(id);\n\n onDestroy(() => {\n tableRef.leave(id);\n });\n\n return constantRef(state);\n },\n add(payload: T) {\n const state = tableRef.add(payload);\n\n onDestroy(() => {\n tableRef.leave(state.data.id);\n });\n\n return constantRef(state);\n },\n list(filter?: IDBKeyRange | FilterFn<R>, limit?: number, direction?: IDBCursorDirection) {\n const state = tableRef.list(filter, limit, direction);\n return constantRef(state);\n },\n listByIndex(name: keyof R, filter?: IDBKeyRange | FilterFn<R>, limit?: number, direction?: IDBCursorDirection) {\n const state = tableRef.listByIndex(name, filter, limit, direction);\n return constantRef(state);\n },\n remove(id: string) {\n const state = tableRef.remove(id);\n return constantRef(state);\n },\n seed(seeds: R[]) {\n tableRef.seed(seeds);\n return this;\n },\n table() {\n return tableRef;\n },\n };\n}\n"]}
|
package/dist/types-CsfltB4A.d.ts
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
type StateRef<T> = {
|
|
2
|
-
value: T;
|
|
3
|
-
};
|
|
4
|
-
type ConstantRef<T> = {
|
|
5
|
-
get value(): T;
|
|
6
|
-
publish(): void;
|
|
7
|
-
subscribe(fn: RefSubscriber<T>): RefUnsubscribe;
|
|
8
|
-
};
|
|
9
|
-
type VariableRef<T> = ConstantRef<T> & {
|
|
10
|
-
set(value: T): void;
|
|
11
|
-
set value(value: T);
|
|
12
|
-
};
|
|
13
|
-
type RefSubscriber<T> = (current: T) => void;
|
|
14
|
-
type RefUnsubscribe = () => void;
|
|
15
|
-
|
|
16
|
-
export type { ConstantRef as C, RefSubscriber as R, StateRef as S, VariableRef as V, RefUnsubscribe as a };
|