@anchorlib/svelte 1.0.0-beta.10
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 +305 -0
- package/dist/index.js +140 -0
- package/dist/index.js.map +1 -0
- package/dist/reactive.d.ts +5 -0
- package/dist/reactive.js +34 -0
- package/dist/reactive.js.map +1 -0
- package/dist/storage/index.d.ts +60 -0
- package/dist/storage/index.js +68 -0
- package/dist/storage/index.js.map +1 -0
- package/package.json +91 -0
- package/readme.md +41 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,305 @@
|
|
|
1
|
+
import { Linkable, LinkableSchema, StateOptions, ModelInput, ModelOutput, Immutable, ModelArray, FetchOptions, FetchState, StreamOptions, State, HistoryOptions, HistoryState, StateBaseOptions, ImmutableOutput, Mutable, MutationKey, MutablePart, ObjLike, StateExceptionMap, KeyLike } from '@anchorlib/core';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Creates a reactive state that can be used to manage state with Anchor.
|
|
5
|
+
* This overload is used when no schema is provided, or when using a LinkableSchema with StateOptions.
|
|
6
|
+
*
|
|
7
|
+
* @template T - The type of the initial value
|
|
8
|
+
* @template S - The schema type, extending LinkableSchema
|
|
9
|
+
* @param init - The initial value for the state
|
|
10
|
+
* @param options - Optional state options for the state
|
|
11
|
+
* @returns A reactive state containing the initial value
|
|
12
|
+
*/
|
|
13
|
+
declare function anchorRef<T extends Linkable, S extends LinkableSchema = LinkableSchema>(init: T, options?: StateOptions<S>): T;
|
|
14
|
+
/**
|
|
15
|
+
* Creates a reactive state with a defined schema for validation and type inference.
|
|
16
|
+
*
|
|
17
|
+
* @template S - The schema type, extending LinkableSchema
|
|
18
|
+
* @template T - The type of the initial value, must extend ModelInput of the schema
|
|
19
|
+
* @param init - The initial value for the state
|
|
20
|
+
* @param schema - The schema to validate and type the state
|
|
21
|
+
* @param options - Optional state options for the state
|
|
22
|
+
* @returns A reactive state containing the output model based on the schema
|
|
23
|
+
*/
|
|
24
|
+
declare function anchorRef<S extends LinkableSchema, T extends ModelInput<S>>(init: T, schema?: S, options?: StateOptions): ModelOutput<S>;
|
|
25
|
+
/**
|
|
26
|
+
* Creates an immutable reactive state with a defined schema.
|
|
27
|
+
*
|
|
28
|
+
* @template S - The schema type, extending LinkableSchema
|
|
29
|
+
* @template T - The type of the initial value, must extend ModelInput of the schema
|
|
30
|
+
* @param init - The initial value for the state
|
|
31
|
+
* @param schema - The schema to validate and type the state
|
|
32
|
+
* @param options - State options with immutable flag set to true
|
|
33
|
+
* @returns A reactive state containing an immutable output model based on the schema
|
|
34
|
+
*/
|
|
35
|
+
declare function anchorRef<S extends LinkableSchema, T extends ModelInput<S>>(init: T, schema?: S, options?: StateOptions & {
|
|
36
|
+
immutable: true;
|
|
37
|
+
}): Immutable<ModelOutput<S>>;
|
|
38
|
+
/**
|
|
39
|
+
* Reactive state alias for anchorRef.
|
|
40
|
+
* @type {{<T, S=LinkableSchema extends LinkableSchema>(init: T, options?: StateOptions<S>): T, <S extends LinkableSchema, T extends ModelInput<S>>(init: T, schema?: S, options?: StateOptions): ModelOutput<S>, <S extends LinkableSchema, T extends ModelInput<S>>(init: T, schema?: S, options?: (StateOptions & {immutable: true})): Immutable<ModelOutput<S>>}}
|
|
41
|
+
*/
|
|
42
|
+
declare const reactiveRef: typeof anchorRef;
|
|
43
|
+
/**
|
|
44
|
+
* Creates a reactive state that maintains a sorted array state based on a comparison function.
|
|
45
|
+
*
|
|
46
|
+
* @template T - The type of elements in the array
|
|
47
|
+
* @template S - The schema type for array elements, extending ModelArray
|
|
48
|
+
* @param init - The initial array value for the state
|
|
49
|
+
* @param compare - A function that defines the sort order of elements
|
|
50
|
+
* @param options - Optional state options for the state
|
|
51
|
+
* @returns A reactive state containing the sorted array
|
|
52
|
+
*/
|
|
53
|
+
declare function orderedRef<T extends unknown[], S extends ModelArray = ModelArray>(init: T, compare: (a: T[number], b: T[number]) => number, options?: StateOptions<S>): T;
|
|
54
|
+
/**
|
|
55
|
+
* Creates a reactive state that maintains a flat array state.
|
|
56
|
+
*
|
|
57
|
+
* @template T - The type of elements in the array
|
|
58
|
+
* @template S - The schema type for array elements, extending ModelArray
|
|
59
|
+
* @param init - The initial array value for the state
|
|
60
|
+
* @param options - Optional state options for the state
|
|
61
|
+
* @returns A reactive state containing the flat array
|
|
62
|
+
*/
|
|
63
|
+
declare function flatRef<T extends unknown[], S extends ModelArray = ModelArray>(init: T, options?: StateOptions<S>): T;
|
|
64
|
+
/**
|
|
65
|
+
* Creates a reactive state that mutates the underlying object.
|
|
66
|
+
*
|
|
67
|
+
* Unless you set the global options to `cloned: true`, you don't want to use this.
|
|
68
|
+
*
|
|
69
|
+
* @template T - The type of the initial value
|
|
70
|
+
* @template S - The schema type, extending LinkableSchema
|
|
71
|
+
* @param init - The initial value for the state
|
|
72
|
+
* @param options - Optional state options for the state
|
|
73
|
+
* @returns A reactive state containing the raw value
|
|
74
|
+
*/
|
|
75
|
+
declare function rawRef<T extends Linkable, S extends LinkableSchema = LinkableSchema>(init: T, options?: StateOptions<S>): T;
|
|
76
|
+
|
|
77
|
+
type StateRef<T> = {
|
|
78
|
+
value: T;
|
|
79
|
+
};
|
|
80
|
+
type ConstantRef<T> = {
|
|
81
|
+
get value(): T;
|
|
82
|
+
};
|
|
83
|
+
type VariableRef<T> = ConstantRef<T> & {
|
|
84
|
+
set value(value: T);
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Creates a derived state from a source state with an optional transformation.
|
|
89
|
+
*
|
|
90
|
+
* @template T - The type of the input state
|
|
91
|
+
* @template R - The type of the transformed output
|
|
92
|
+
* @param state - The source state
|
|
93
|
+
* @param derive - A function that transforms the current state value
|
|
94
|
+
* @returns A read-only reference containing the derived state value
|
|
95
|
+
*/
|
|
96
|
+
declare function derivedRef<T, R>(state: T, derive: (current: T) => R): ConstantRef<R>;
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Creates a reactive state that manages the state of a fetch request.
|
|
100
|
+
* This overload is for GET or DELETE requests, which typically do not have a request body.
|
|
101
|
+
*
|
|
102
|
+
* @template R The type of the data expected in the response.
|
|
103
|
+
* @param init The initial value for the fetch state.
|
|
104
|
+
* @param options The options for the fetch request, including the URL and method.
|
|
105
|
+
* @returns A `FetchState` object representing the state of the request.
|
|
106
|
+
*/
|
|
107
|
+
declare function fetchRef<R>(init: R, options: FetchOptions & {
|
|
108
|
+
method: 'GET' | 'DELETE';
|
|
109
|
+
}): FetchState<R>;
|
|
110
|
+
/**
|
|
111
|
+
* Creates a readable Svelte store that manages the state of a fetch request.
|
|
112
|
+
* This overload is for POST, PUT, or PATCH requests, which typically include a request body.
|
|
113
|
+
*
|
|
114
|
+
* @template R The type of the data expected in the response.
|
|
115
|
+
* @template P The type of the request body.
|
|
116
|
+
* @param init The initial value for the fetch state.
|
|
117
|
+
* @param options The options for the fetch request, including the URL, method, and body.
|
|
118
|
+
* @returns A `FetchState` object representing the state of the request.
|
|
119
|
+
*/
|
|
120
|
+
declare function fetchRef<R, P>(init: R, options: FetchOptions & {
|
|
121
|
+
method: 'POST' | 'PUT' | 'PATCH';
|
|
122
|
+
body: P;
|
|
123
|
+
}): FetchState<R>;
|
|
124
|
+
/**
|
|
125
|
+
* Creates a reactive state that manages the state of a streaming request.
|
|
126
|
+
* This overload is for GET or DELETE requests, which typically do not have a request body.
|
|
127
|
+
*
|
|
128
|
+
* @template R The type of the data expected in the response.
|
|
129
|
+
* @param init The initial value for the fetch state.
|
|
130
|
+
* @param options The options for the stream request, including the URL and method.
|
|
131
|
+
* @returns A `FetchState` object representing the state of the request.
|
|
132
|
+
*/
|
|
133
|
+
declare function streamRef<R>(init: R, options: StreamOptions<R> & {
|
|
134
|
+
method: 'GET' | 'DELETE';
|
|
135
|
+
}): FetchState<R>;
|
|
136
|
+
/**
|
|
137
|
+
* Creates a reactive state that manages the state of a streaming request.
|
|
138
|
+
* This overload is for POST, PUT, or PATCH requests, which typically include a request body.
|
|
139
|
+
*
|
|
140
|
+
* @template R The type of the data expected in the response.
|
|
141
|
+
* @template P The type of the request body.
|
|
142
|
+
* @param init The initial value for the fetch state.
|
|
143
|
+
* @param options The options for the stream request, including the URL, method, and body.
|
|
144
|
+
* @returns A `FetchState` object representing the state of the request.
|
|
145
|
+
*/
|
|
146
|
+
declare function streamRef<R, P>(init: R, options: StreamOptions<R> & {
|
|
147
|
+
method: 'POST' | 'PUT' | 'PATCH';
|
|
148
|
+
body: P;
|
|
149
|
+
}): FetchState<R>;
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* Creates a reactive state that reflects the history state of a given Anchor state.
|
|
153
|
+
* @param state The initial Anchor state.
|
|
154
|
+
* @param options Optional history options.
|
|
155
|
+
* @returns A `HistoryState` object representing the state of the history.
|
|
156
|
+
*/
|
|
157
|
+
declare function historyRef<T extends State>(state: T, options?: HistoryOptions): HistoryState;
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* Creates an immutable state from a state object.
|
|
161
|
+
*
|
|
162
|
+
* @template T The type of the state object.
|
|
163
|
+
* @template S The type of the linkable schema.
|
|
164
|
+
* @param init The initial state object.
|
|
165
|
+
* @param options Optional state options.
|
|
166
|
+
* @returns An immutable state.
|
|
167
|
+
*/
|
|
168
|
+
declare function immutableRef<T extends State, S extends LinkableSchema = LinkableSchema>(init: T, options?: StateOptions<S>): Immutable<T>;
|
|
169
|
+
/**
|
|
170
|
+
* Creates an immutable state from a model input and a schema.
|
|
171
|
+
*
|
|
172
|
+
* @template S The type of the linkable schema.
|
|
173
|
+
* @template T The type of the model input.
|
|
174
|
+
* @param init The initial model input.
|
|
175
|
+
* @param schema The linkable schema.
|
|
176
|
+
* @param options Optional base state options.
|
|
177
|
+
* @returns An immutable state.
|
|
178
|
+
*/
|
|
179
|
+
declare function immutableRef<S extends LinkableSchema, T extends ModelInput<S>>(init: T, schema: S, options?: StateBaseOptions): ImmutableOutput<S>;
|
|
180
|
+
/**
|
|
181
|
+
* Creates a writable state from a state object.
|
|
182
|
+
*
|
|
183
|
+
* @template T The type of the state object.
|
|
184
|
+
* @param state The initial state object.
|
|
185
|
+
* @returns A mutable state.
|
|
186
|
+
*/
|
|
187
|
+
declare function writableRef<T extends State>(state: T): Mutable<T>;
|
|
188
|
+
/**
|
|
189
|
+
* Creates a writable state from a state object and a list of contracts.
|
|
190
|
+
*
|
|
191
|
+
* @template T The type of the state object.
|
|
192
|
+
* @template K The type of the mutation keys.
|
|
193
|
+
* @param state The initial state object.
|
|
194
|
+
* @param contracts A list of mutation keys.
|
|
195
|
+
* @returns A mutable part of the state.
|
|
196
|
+
*/
|
|
197
|
+
declare function writableRef<T extends State, K extends MutationKey<T>[]>(state: T, contracts: K): MutablePart<T, K>;
|
|
198
|
+
|
|
199
|
+
/**
|
|
200
|
+
* Creates a model with mutable state.
|
|
201
|
+
*
|
|
202
|
+
* @template S - The linkable schema type
|
|
203
|
+
* @template T - The model input type that extends the schema
|
|
204
|
+
* @param schema - The schema to use for the model
|
|
205
|
+
* @param init - The initial value for the model
|
|
206
|
+
* @param options - Optional state configuration
|
|
207
|
+
* @returns A model output.
|
|
208
|
+
*/
|
|
209
|
+
declare function modelRef<S extends LinkableSchema, T extends ModelInput<S>>(schema: S, init: T, options?: StateBaseOptions): ModelOutput<S>;
|
|
210
|
+
/**
|
|
211
|
+
* Creates a model with immutable state.
|
|
212
|
+
*
|
|
213
|
+
* @template S - The linkable schema type
|
|
214
|
+
* @template T - The model input type that extends the schema
|
|
215
|
+
* @param schema - The schema to use for the model
|
|
216
|
+
* @param init - The initial value for the model
|
|
217
|
+
* @param options - State configuration with immutable flag set to true
|
|
218
|
+
* @returns An immutable model output.
|
|
219
|
+
*/
|
|
220
|
+
declare function modelRef<S extends LinkableSchema, T extends ModelInput<S>>(schema: S, init: T, options: StateBaseOptions & {
|
|
221
|
+
immutable: true;
|
|
222
|
+
}): ImmutableOutput<S>;
|
|
223
|
+
/**
|
|
224
|
+
* Creates a state that maps exceptions for a given state object or array.
|
|
225
|
+
*
|
|
226
|
+
* @template T - The type of the input state, must be an object-like or array type
|
|
227
|
+
* @param state - The input state object or array to create exception mappings for
|
|
228
|
+
* @returns A StateExceptionMap for the provided state.
|
|
229
|
+
*/
|
|
230
|
+
declare function exceptionRef<T extends ObjLike | Array<unknown>>(state: T): StateExceptionMap<T>;
|
|
231
|
+
|
|
232
|
+
type Props = {
|
|
233
|
+
[key: string]: KeyLike | State;
|
|
234
|
+
};
|
|
235
|
+
type PropsRef<T extends Props> = {
|
|
236
|
+
[K in keyof T]: T[K] extends State ? ConstantRef<T[K]> : T[K];
|
|
237
|
+
};
|
|
238
|
+
/**
|
|
239
|
+
* Creates a reactive state object from the provided props.
|
|
240
|
+
* For each property in the input props:
|
|
241
|
+
* - If the value is a State object, it will be converted to a derived state
|
|
242
|
+
* - Otherwise, the value will be kept as is
|
|
243
|
+
* @deprecated
|
|
244
|
+
* @template T - The type of props extending Props
|
|
245
|
+
* @param {T} props - The input props object containing KeyLike or State values
|
|
246
|
+
* @returns {PropsRef<T>} A new object with State values converted to reactive states
|
|
247
|
+
*/
|
|
248
|
+
declare function propsRef<T extends Props>(props: T): T;
|
|
249
|
+
|
|
250
|
+
/**
|
|
251
|
+
* Creates a read-only reference that observes a reactive function and updates its value
|
|
252
|
+
* when the observed value changes. The function automatically handles observer lifecycle
|
|
253
|
+
* and cleanup using Svelte's onDestroy hook.
|
|
254
|
+
*
|
|
255
|
+
* @template R - The type of the observed value
|
|
256
|
+
* @param observe - A function that returns the value to be observed
|
|
257
|
+
* @returns A read-only reference containing the observed value
|
|
258
|
+
*/
|
|
259
|
+
declare function observedRef<R>(observe: () => R): ConstantRef<R>;
|
|
260
|
+
|
|
261
|
+
declare const REF_REGISTRY: WeakMap<ConstantRef<unknown>, StateRef<unknown>>;
|
|
262
|
+
/**
|
|
263
|
+
* Creates a readable reference that can be subscribed to for reactive updates.
|
|
264
|
+
* This function initializes a reactive reference with a given initial value and provides
|
|
265
|
+
* mechanisms for subscribing to changes and publishing updates to subscribers.
|
|
266
|
+
*
|
|
267
|
+
* @template T The type of the value being referenced
|
|
268
|
+
* @param init - The initial value for the reference
|
|
269
|
+
* @returns A readable reference object with subscribe and publish capabilities
|
|
270
|
+
*/
|
|
271
|
+
declare function variableRef<T>(init: T): VariableRef<T>;
|
|
272
|
+
/**
|
|
273
|
+
* Creates a constant (read-only) reference that can be subscribed to for reactive updates.
|
|
274
|
+
* This function initializes a reactive reference with a given initial value that cannot be modified
|
|
275
|
+
* after creation.
|
|
276
|
+
*
|
|
277
|
+
* @template T The type of the value being referenced
|
|
278
|
+
* @param init - The initial value for the reference
|
|
279
|
+
* @param constant - If true, the reference will be read-only and cannot be updated.
|
|
280
|
+
* @returns A constant reference object with subscribe capability but no write access
|
|
281
|
+
*/
|
|
282
|
+
declare function variableRef<T>(init: T, constant: true): ConstantRef<T>;
|
|
283
|
+
/**
|
|
284
|
+
* Creates a constant (read-only) reference that can be subscribed to for reactive updates.
|
|
285
|
+
* This function initializes a reactive reference with a given initial value that cannot be modified
|
|
286
|
+
* after creation. It's useful for values that should remain constant throughout the component lifecycle
|
|
287
|
+
* but still need to be reactively tracked.
|
|
288
|
+
*
|
|
289
|
+
* @template T The type of the value being referenced
|
|
290
|
+
* @param init - The initial value for the constant reference
|
|
291
|
+
* @returns A constant reference object with subscribe capability but no write access
|
|
292
|
+
*/
|
|
293
|
+
declare function constantRef<T>(init: T): ConstantRef<T>;
|
|
294
|
+
/**
|
|
295
|
+
* Checks if a given value is a writable reference.
|
|
296
|
+
* This function uses the REF_REGISTRY to determine if the provided value
|
|
297
|
+
* is a registered writable reference.
|
|
298
|
+
*
|
|
299
|
+
* @template T The type of the value that the reference holds
|
|
300
|
+
* @param ref - The value to check
|
|
301
|
+
* @returns True if the value is a writable reference, false otherwise
|
|
302
|
+
*/
|
|
303
|
+
declare function isRef<T>(ref: unknown): ref is VariableRef<T>;
|
|
304
|
+
|
|
305
|
+
export { type ConstantRef, type Props, type PropsRef, REF_REGISTRY, type StateRef, type VariableRef, anchorRef, constantRef, derivedRef, exceptionRef, fetchRef, flatRef, historyRef, immutableRef, isRef, modelRef, observedRef, orderedRef, propsRef, rawRef, reactiveRef, streamRef, variableRef, writableRef };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
import { setTracker, createObserver, anchor, subscribe, fetchState, streamState, history } from '@anchorlib/core';
|
|
2
|
+
import { createSubscriber } from 'svelte/reactivity';
|
|
3
|
+
import { onDestroy } from 'svelte';
|
|
4
|
+
|
|
5
|
+
// src/reactive.ts
|
|
6
|
+
var TRACKER_REGISTRY = /* @__PURE__ */ new WeakMap();
|
|
7
|
+
var bindingInitialized = false;
|
|
8
|
+
if (!bindingInitialized && typeof window !== "undefined") {
|
|
9
|
+
bindingInitialized = true;
|
|
10
|
+
setTracker((init, observers, key) => {
|
|
11
|
+
if (!TRACKER_REGISTRY.has(init)) {
|
|
12
|
+
let track = void 0;
|
|
13
|
+
const subscribe2 = createSubscriber((update) => {
|
|
14
|
+
const observer = createObserver(() => {
|
|
15
|
+
update();
|
|
16
|
+
});
|
|
17
|
+
track = observer.assign(init, observers);
|
|
18
|
+
return () => {
|
|
19
|
+
observer.destroy();
|
|
20
|
+
TRACKER_REGISTRY.delete(init);
|
|
21
|
+
};
|
|
22
|
+
});
|
|
23
|
+
const assign = (prop) => {
|
|
24
|
+
subscribe2();
|
|
25
|
+
track?.(prop);
|
|
26
|
+
};
|
|
27
|
+
TRACKER_REGISTRY.set(init, assign);
|
|
28
|
+
}
|
|
29
|
+
TRACKER_REGISTRY.get(init)?.(key);
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
function anchorRef(init, schemaOptions, options) {
|
|
33
|
+
return anchor(init, schemaOptions, options);
|
|
34
|
+
}
|
|
35
|
+
var reactiveRef = anchorRef;
|
|
36
|
+
function orderedRef(init, compare, options) {
|
|
37
|
+
return anchor.ordered(init, compare, options);
|
|
38
|
+
}
|
|
39
|
+
function flatRef(init, options) {
|
|
40
|
+
return anchor.flat(init, options);
|
|
41
|
+
}
|
|
42
|
+
function rawRef(init, options) {
|
|
43
|
+
return anchor.raw(init, options);
|
|
44
|
+
}
|
|
45
|
+
var REF_REGISTRY = /* @__PURE__ */ new WeakMap();
|
|
46
|
+
function variableRef(init, constant) {
|
|
47
|
+
const valueRef = anchor({ value: init }, { recursive: true });
|
|
48
|
+
const set = (value) => {
|
|
49
|
+
if (constant === true || value === valueRef.value) return;
|
|
50
|
+
valueRef.value = value;
|
|
51
|
+
};
|
|
52
|
+
onDestroy(() => {
|
|
53
|
+
REF_REGISTRY.delete(stateRef);
|
|
54
|
+
anchor.destroy(valueRef);
|
|
55
|
+
});
|
|
56
|
+
const stateRef = {
|
|
57
|
+
get value() {
|
|
58
|
+
return valueRef.value;
|
|
59
|
+
},
|
|
60
|
+
set value(value) {
|
|
61
|
+
set(value);
|
|
62
|
+
}
|
|
63
|
+
};
|
|
64
|
+
REF_REGISTRY.set(stateRef, valueRef);
|
|
65
|
+
return stateRef;
|
|
66
|
+
}
|
|
67
|
+
function constantRef(init) {
|
|
68
|
+
return variableRef(init, true);
|
|
69
|
+
}
|
|
70
|
+
function isRef(ref) {
|
|
71
|
+
return REF_REGISTRY.has(ref);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
// src/derive.ts
|
|
75
|
+
function derivedRef(state, derive) {
|
|
76
|
+
const valueRef = anchor({}, { recursive: false });
|
|
77
|
+
const stateRef = {
|
|
78
|
+
get value() {
|
|
79
|
+
return valueRef.value;
|
|
80
|
+
}
|
|
81
|
+
};
|
|
82
|
+
REF_REGISTRY.set(stateRef, valueRef);
|
|
83
|
+
const unsubscribe = subscribe(state, (current) => {
|
|
84
|
+
valueRef.value = derive(current);
|
|
85
|
+
});
|
|
86
|
+
onDestroy(() => {
|
|
87
|
+
anchor.destroy(valueRef);
|
|
88
|
+
unsubscribe();
|
|
89
|
+
REF_REGISTRY.delete(stateRef);
|
|
90
|
+
});
|
|
91
|
+
return stateRef;
|
|
92
|
+
}
|
|
93
|
+
function fetchRef(init, options) {
|
|
94
|
+
return fetchState(init, options);
|
|
95
|
+
}
|
|
96
|
+
function streamRef(init, options) {
|
|
97
|
+
return streamState(init, options);
|
|
98
|
+
}
|
|
99
|
+
function historyRef(state, options) {
|
|
100
|
+
return history(state, options);
|
|
101
|
+
}
|
|
102
|
+
function immutableRef(init, schemaOptions, options) {
|
|
103
|
+
return anchor.immutable(init, schemaOptions, options);
|
|
104
|
+
}
|
|
105
|
+
function writableRef(state, contracts) {
|
|
106
|
+
return anchor.writable(state, contracts);
|
|
107
|
+
}
|
|
108
|
+
function modelRef(schema, init, options) {
|
|
109
|
+
return anchor(init, schema, options);
|
|
110
|
+
}
|
|
111
|
+
function exceptionRef(state) {
|
|
112
|
+
return anchor.catch(state);
|
|
113
|
+
}
|
|
114
|
+
function propsRef(props) {
|
|
115
|
+
return props;
|
|
116
|
+
}
|
|
117
|
+
function observedRef(observe) {
|
|
118
|
+
const observer = createObserver(() => {
|
|
119
|
+
update();
|
|
120
|
+
});
|
|
121
|
+
const valueRef = anchor({ value: observer.run(observe) }, { recursive: false });
|
|
122
|
+
const stateRef = {
|
|
123
|
+
get value() {
|
|
124
|
+
return valueRef.value;
|
|
125
|
+
}
|
|
126
|
+
};
|
|
127
|
+
REF_REGISTRY.set(stateRef, valueRef);
|
|
128
|
+
const update = () => {
|
|
129
|
+
valueRef.value = observer.run(observe);
|
|
130
|
+
};
|
|
131
|
+
onDestroy(() => {
|
|
132
|
+
observer.destroy();
|
|
133
|
+
REF_REGISTRY.delete(stateRef);
|
|
134
|
+
});
|
|
135
|
+
return stateRef;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
export { REF_REGISTRY, anchorRef, constantRef, derivedRef, exceptionRef, fetchRef, flatRef, historyRef, immutableRef, isRef, modelRef, observedRef, orderedRef, propsRef, rawRef, reactiveRef, streamRef, variableRef, writableRef };
|
|
139
|
+
//# sourceMappingURL=index.js.map
|
|
140
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/reactive.ts","../src/anchor.ts","../src/ref.ts","../src/derive.ts","../src/fetch.ts","../src/history.ts","../src/immutable.ts","../src/model.ts","../src/prop.ts","../src/observable.ts"],"names":["subscribe","anchor","onDestroy","createObserver"],"mappings":";;;;;AAGO,IAAM,gBAAA,uBAAuB,OAAA,EAA2C;AAE/E,IAAI,kBAAA,GAAqB,KAAA;AAEzB,IAAI,CAAC,kBAAA,IAAsB,OAAO,MAAA,KAAW,WAAA,EAAa;AACxD,EAAA,kBAAA,GAAqB,IAAA;AAWrB,EAAA,UAAA,CAAW,CAAC,IAAA,EAAM,SAAA,EAAW,GAAA,KAAQ;AAEnC,IAAA,IAAI,CAAC,gBAAA,CAAiB,GAAA,CAAI,IAAI,CAAA,EAAG;AAC/B,MAAA,IAAI,KAAA,GAA+C,MAAA;AAGnD,MAAA,MAAMA,UAAAA,GAAY,gBAAA,CAAiB,CAAC,MAAA,KAAW;AAE7C,QAAA,MAAM,QAAA,GAAW,eAAe,MAAM;AACpC,UAAA,MAAA,EAAO;AAAA,QACT,CAAC,CAAA;AAGD,QAAA,KAAA,GAAQ,QAAA,CAAS,MAAA,CAAO,IAAA,EAAM,SAAS,CAAA;AAGvC,QAAA,OAAO,MAAM;AACX,UAAA,QAAA,CAAS,OAAA,EAAQ;AACjB,UAAA,gBAAA,CAAiB,OAAO,IAAI,CAAA;AAAA,QAC9B,CAAA;AAAA,MACF,CAAC,CAAA;AAGD,MAAA,MAAM,MAAA,GAAS,CAAC,IAAA,KAAkB;AAEhC,QAAAA,UAAAA,EAAU;AAEV,QAAA,KAAA,GAAQ,IAAI,CAAA;AAAA,MACd,CAAA;AAGA,MAAA,gBAAA,CAAiB,GAAA,CAAI,MAAM,MAAM,CAAA;AAAA,IACnC;AAGA,IAAA,gBAAA,CAAiB,GAAA,CAAI,IAAI,CAAA,GAAI,GAAG,CAAA;AAAA,EAClC,C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AC,CAAA;AAED,EAAA,OAAO,QAAA;AACT","file":"index.js","sourcesContent":["import { createObserver, type KeyLike, type Linkable, setTracker } from '@anchorlib/core';\nimport { createSubscriber } from 'svelte/reactivity';\n\nexport const TRACKER_REGISTRY = new WeakMap<Linkable, (prop: KeyLike) => void>();\n\nlet bindingInitialized = false;\n\nif (!bindingInitialized && typeof window !== 'undefined') {\n bindingInitialized = true;\n\n /**\n * Sets up a tracker function that integrates Anchor's reactivity system with Svelte's reactivity.\n * This tracker is responsible for creating observers that watch for changes in reactive objects\n * and properly subscribing/unsubscribing to Svelte's reactivity system.\n *\n * @param init - The initial linkable object to track\n * @param observers - The observers collection to use for tracking\n * @param key - The specific key/property to track on the object\n */\n setTracker((init, observers, key) => {\n // Only initialize the tracking setup once per object\n if (!TRACKER_REGISTRY.has(init)) {\n let track: ((prop: KeyLike) => void) | undefined = undefined;\n\n // Create a Svelte subscriber that manages the lifecycle of our observer\n const subscribe = createSubscriber((update) => {\n // Create an Anchor observer that will trigger the Svelte update when changes occur\n const observer = createObserver(() => {\n update();\n });\n\n // Assign the observer to track changes on the init object and its observers\n track = observer.assign(init, observers);\n\n // Return cleanup function to destroy observer and remove from registry\n return () => {\n observer.destroy();\n TRACKER_REGISTRY.delete(init);\n };\n });\n\n // Function to assign tracking to a specific key/property\n const assign = (prop: KeyLike) => {\n // Activate the subscription\n subscribe();\n // Track the specific property\n track?.(prop);\n };\n\n // Store the assign function in the registry for this object\n TRACKER_REGISTRY.set(init, assign);\n }\n\n // Execute the tracking function for the specific key\n TRACKER_REGISTRY.get(init)?.(key);\n });\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';\n\n/**\n * Creates a reactive state 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 state\n * @param options - Optional state options for the state\n * @returns A reactive state containing the initial value\n */\nexport function anchorRef<T extends Linkable, S extends LinkableSchema = LinkableSchema>(\n init: T,\n options?: StateOptions<S>\n): T;\n\n/**\n * Creates a reactive state 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 state\n * @param schema - The schema to validate and type the state\n * @param options - Optional state options for the state\n * @returns A reactive state 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): ModelOutput<S>;\n\n/**\n * Creates an immutable reactive state 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 state\n * @param schema - The schema to validate and type the state\n * @param options - State options with immutable flag set to true\n * @returns A reactive state 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): Immutable<ModelOutput<S>>;\n\n/**\n * Creates a reactive state 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 state\n * @param schemaOptions - Either a schema or state options\n * @param options - Additional state options when schema is provided\n * @returns A reactive state 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): T | ModelOutput<S> | Immutable<ModelOutput<S>> {\n return anchor<S, ModelInput<S>>(init as ModelInput<S>, schemaOptions as S, options);\n}\n\n/**\n * Reactive state alias for anchorRef.\n * @type {{<T, S=LinkableSchema extends LinkableSchema>(init: T, options?: StateOptions<S>): T, <S extends LinkableSchema, T extends ModelInput<S>>(init: T, schema?: S, options?: StateOptions): ModelOutput<S>, <S extends LinkableSchema, T extends ModelInput<S>>(init: T, schema?: S, options?: (StateOptions & {immutable: true})): Immutable<ModelOutput<S>>}}\n */\nexport const reactiveRef = anchorRef;\n\n/**\n * Creates a reactive state 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 state\n * @param compare - A function that defines the sort order of elements\n * @param options - Optional state options for the state\n * @returns A reactive state 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): T {\n return anchor.ordered(init, compare, options);\n}\n\n/**\n * Creates a reactive state 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 state\n * @param options - Optional state options for the state\n * @returns A reactive state containing the flat array\n */\nexport function flatRef<T extends unknown[], S extends ModelArray = ModelArray>(init: T, options?: StateOptions<S>): T {\n return anchor.flat(init, options);\n}\n\n/**\n * Creates a reactive state 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 state\n * @param options - Optional state options for the state\n * @returns A reactive state containing the raw value\n */\nexport function rawRef<T extends Linkable, S extends LinkableSchema = LinkableSchema>(\n init: T,\n options?: StateOptions<S>\n): T {\n return anchor.raw(init, options);\n}\n","import type { ConstantRef, StateRef, VariableRef } from './types.js';\nimport { anchor } 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 valueRef = anchor({ value: init }, { recursive: true });\n\n const set = (value: T) => {\n // Ignore if the value is the same.\n if (constant === true || value === valueRef.value) return;\n\n valueRef.value = value;\n };\n\n onDestroy(() => {\n // Remove the ref from the registry.\n REF_REGISTRY.delete(stateRef as ConstantRef<unknown>);\n\n // Destroy the ref state.\n anchor.destroy(valueRef);\n });\n\n const stateRef = {\n get value() {\n return valueRef.value;\n },\n set value(value: T) {\n set(value);\n },\n } as never;\n\n REF_REGISTRY.set(stateRef as ConstantRef<unknown>, valueRef);\n\n return stateRef 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 { anchor, subscribe } from '@anchorlib/core';\nimport type { ConstantRef, StateRef } from './types.js';\nimport { onDestroy } from 'svelte';\nimport { REF_REGISTRY } from './ref.js';\n\n/**\n * Creates a derived state from a source state with an 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 source state\n * @param derive - A function that transforms the current state value\n * @returns A read-only reference containing the derived state value\n */\nexport function derivedRef<T, R>(state: T, derive: (current: T) => R): ConstantRef<R> {\n const valueRef = anchor({}, { recursive: false }) as StateRef<R>;\n const stateRef = {\n get value() {\n return valueRef.value;\n },\n };\n REF_REGISTRY.set(stateRef, valueRef);\n\n const unsubscribe = subscribe(state, (current) => {\n valueRef.value = derive(current);\n });\n\n onDestroy(() => {\n anchor.destroy(valueRef);\n unsubscribe();\n REF_REGISTRY.delete(stateRef);\n });\n\n return stateRef as ConstantRef<R>;\n}\n","import { type FetchOptions, fetchState, type FetchState, type StreamOptions, streamState } from '@anchorlib/core';\n\n/**\n * Creates a reactive state 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 `FetchState` object representing the state of the request.\n */\nexport function fetchRef<R>(init: R, options: FetchOptions & { method: 'GET' | 'DELETE' }): 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 `FetchState` object representing the state of the request.\n */\nexport function fetchRef<R, P>(\n init: R,\n options: FetchOptions & { method: 'POST' | 'PUT' | 'PATCH'; body: P }\n): FetchState<R>;\n\nexport function fetchRef<R>(init: R, options: FetchOptions): FetchState<R> {\n return fetchState(init, options);\n}\n\n/**\n * Creates a reactive state 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 `FetchState` object representing the state of the request.\n */\nexport function streamRef<R>(init: R, options: StreamOptions<R> & { method: 'GET' | 'DELETE' }): FetchState<R>;\n\n/**\n * Creates a reactive state 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 `FetchState` object representing the state of the request.\n */\nexport function streamRef<R, P>(\n init: R,\n options: StreamOptions<R> & { method: 'POST' | 'PUT' | 'PATCH'; body: P }\n): FetchState<R>;\n\nexport function streamRef<R>(init: R, options: StreamOptions<R>): FetchState<R> {\n return streamState(init, options);\n}\n","import type { HistoryOptions, HistoryState, State } from '@anchorlib/core';\nimport { history } from '@anchorlib/core';\n\n/**\n * Creates a reactive state 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 `HistoryState` object representing the state of the history.\n */\nexport function historyRef<T extends State>(state: T, options?: HistoryOptions): HistoryState {\n return history(state, options);\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';\n\n/**\n * Creates an immutable state 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 An immutable state.\n */\nexport function immutableRef<T extends State, S extends LinkableSchema = LinkableSchema>(\n init: T,\n options?: StateOptions<S>\n): Immutable<T>;\n\n/**\n * Creates an immutable state 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 An immutable state.\n */\nexport function immutableRef<S extends LinkableSchema, T extends ModelInput<S>>(\n init: T,\n schema: S,\n options?: StateBaseOptions\n): 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): Immutable<T> {\n return anchor.immutable(init as never, schemaOptions as never, options);\n}\n\n/**\n * Creates a writable state from a state object.\n *\n * @template T The type of the state object.\n * @param state The initial state object.\n * @returns A mutable state.\n */\nexport function writableRef<T extends State>(state: T): Mutable<T>;\n\n/**\n * Creates a writable state 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 mutable part of the state.\n */\nexport function writableRef<T extends State, K extends MutationKey<T>[]>(state: T, contracts: K): MutablePart<T, K>;\n\n/** Implementation of `writableRef` overloads. */\nexport function writableRef<T extends State, K extends MutationKey<T>[]>(state: T, contracts?: K): T {\n return anchor.writable(state, contracts) as T;\n}\n","import {\n anchor,\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';\n\n/**\n * Creates a model 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 model output.\n */\nexport function modelRef<S extends LinkableSchema, T extends ModelInput<S>>(\n schema: S,\n init: T,\n options?: StateBaseOptions\n): ModelOutput<S>;\n\n/**\n * Creates a model 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 An immutable model output.\n */\nexport function modelRef<S extends LinkableSchema, T extends ModelInput<S>>(\n schema: S,\n init: T,\n options: StateBaseOptions & { immutable: true }\n): ImmutableOutput<S>;\n\nexport function modelRef<S extends LinkableSchema, T extends ModelInput<S>>(\n schema: S,\n init: T,\n options?: StateBaseOptions\n) {\n return anchor(init, schema, options);\n}\n\n/**\n * Creates a state 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 StateExceptionMap for the provided state.\n */\nexport function exceptionRef<T extends ObjLike | Array<unknown>>(state: T): StateExceptionMap<T> {\n return anchor.catch(state);\n}\n","import { type KeyLike, type State } from '@anchorlib/core';\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 state 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 state\n * - Otherwise, the value will be kept as is\n * @deprecated\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 reactive states\n */\nexport function propsRef<T extends Props>(props: T): T {\n return props as never;\n}\n","import { anchor, createObserver } from '@anchorlib/core';\nimport type { ConstantRef, StateRef } from './types.js';\nimport { onDestroy } from 'svelte';\nimport { REF_REGISTRY } from './ref.js';\n\n/**\n * Creates a read-only reference that observes a reactive function and updates its value\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 read-only reference containing the observed value\n */\nexport function observedRef<R>(observe: () => R): ConstantRef<R> {\n const observer = createObserver(() => {\n update();\n });\n\n const valueRef = anchor({ value: observer.run(observe) }, { recursive: false }) as StateRef<R>;\n const stateRef = {\n get value() {\n return valueRef.value;\n },\n } as ConstantRef<R>;\n\n REF_REGISTRY.set(stateRef, valueRef);\n\n const update = () => {\n valueRef.value = observer.run(observe);\n };\n\n onDestroy(() => {\n observer.destroy();\n REF_REGISTRY.delete(stateRef);\n });\n\n return stateRef;\n}\n"]}
|
package/dist/reactive.js
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { setTracker, createObserver } from '@anchorlib/core';
|
|
2
|
+
import { createSubscriber } from 'svelte/reactivity';
|
|
3
|
+
|
|
4
|
+
// src/reactive.ts
|
|
5
|
+
var TRACKER_REGISTRY = /* @__PURE__ */ new WeakMap();
|
|
6
|
+
var bindingInitialized = false;
|
|
7
|
+
if (!bindingInitialized && typeof window !== "undefined") {
|
|
8
|
+
bindingInitialized = true;
|
|
9
|
+
setTracker((init, observers, key) => {
|
|
10
|
+
if (!TRACKER_REGISTRY.has(init)) {
|
|
11
|
+
let track = void 0;
|
|
12
|
+
const subscribe = createSubscriber((update) => {
|
|
13
|
+
const observer = createObserver(() => {
|
|
14
|
+
update();
|
|
15
|
+
});
|
|
16
|
+
track = observer.assign(init, observers);
|
|
17
|
+
return () => {
|
|
18
|
+
observer.destroy();
|
|
19
|
+
TRACKER_REGISTRY.delete(init);
|
|
20
|
+
};
|
|
21
|
+
});
|
|
22
|
+
const assign = (prop) => {
|
|
23
|
+
subscribe();
|
|
24
|
+
track?.(prop);
|
|
25
|
+
};
|
|
26
|
+
TRACKER_REGISTRY.set(init, assign);
|
|
27
|
+
}
|
|
28
|
+
TRACKER_REGISTRY.get(init)?.(key);
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export { TRACKER_REGISTRY };
|
|
33
|
+
//# sourceMappingURL=reactive.js.map
|
|
34
|
+
//# sourceMappingURL=reactive.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/reactive.ts"],"names":[],"mappings":";;;;AAGO,IAAM,gBAAA,uBAAuB,OAAA;AAEpC,IAAI,kBAAA,GAAqB,KAAA;AAEzB,IAAI,CAAC,kBAAA,IAAsB,OAAO,MAAA,KAAW,WAAA,EAAa;AACxD,EAAA,kBAAA,GAAqB,IAAA;AAWrB,EAAA,UAAA,CAAW,CAAC,IAAA,EAAM,SAAA,EAAW,GAAA,KAAQ;AAEnC,IAAA,IAAI,CAAC,gBAAA,CAAiB,GAAA,CAAI,IAAI,CAAA,EAAG;AAC/B,MAAA,IAAI,KAAA,GAA+C,MAAA;AAGnD,MAAA,MAAM,SAAA,GAAY,gBAAA,CAAiB,CAAC,MAAA,KAAW;AAE7C,QAAA,MAAM,QAAA,GAAW,eAAe,MAAM;AACpC,UAAA,MAAA,EAAO;AAAA,QACT,CAAC,CAAA;AAGD,QAAA,KAAA,GAAQ,QAAA,CAAS,MAAA,CAAO,IAAA,EAAM,SAAS,CAAA;AAGvC,QAAA,OAAO,MAAM;AACX,UAAA,QAAA,CAAS,OAAA,EAAQ;AACjB,UAAA,gBAAA,CAAiB,OAAO,IAAI,CAAA;AAAA,QAC9B,CAAA;AAAA,MACF,CAAC,CAAA;AAGD,MAAA,MAAM,MAAA,GAAS,CAAC,IAAA,KAAkB;AAEhC,QAAA,SAAA,EAAU;AAEV,QAAA,KAAA,GAAQ,IAAI,CAAA;AAAA,MACd,CAAA;AAGA,MAAA,gBAAA,CAAiB,GAAA,CAAI,MAAM,MAAM,CAAA;AAAA,IACnC;AAGA,IAAA,gBAAA,CAAiB,GAAA,CAAI,IAAI,CAAA,GAAI,GAAG,CAAA;AAAA,EAClC,CAAC,CAAA;AACH","file":"reactive.js","sourcesContent":["import { createObserver, type KeyLike, type Linkable, setTracker } from '@anchorlib/core';\nimport { createSubscriber } from 'svelte/reactivity';\n\nexport const TRACKER_REGISTRY = new WeakMap<Linkable, (prop: KeyLike) => void>();\n\nlet bindingInitialized = false;\n\nif (!bindingInitialized && typeof window !== 'undefined') {\n bindingInitialized = true;\n\n /**\n * Sets up a tracker function that integrates Anchor's reactivity system with Svelte's reactivity.\n * This tracker is responsible for creating observers that watch for changes in reactive objects\n * and properly subscribing/unsubscribing to Svelte's reactivity system.\n *\n * @param init - The initial linkable object to track\n * @param observers - The observers collection to use for tracking\n * @param key - The specific key/property to track on the object\n */\n setTracker((init, observers, key) => {\n // Only initialize the tracking setup once per object\n if (!TRACKER_REGISTRY.has(init)) {\n let track: ((prop: KeyLike) => void) | undefined = undefined;\n\n // Create a Svelte subscriber that manages the lifecycle of our observer\n const subscribe = createSubscriber((update) => {\n // Create an Anchor observer that will trigger the Svelte update when changes occur\n const observer = createObserver(() => {\n update();\n });\n\n // Assign the observer to track changes on the init object and its observers\n track = observer.assign(init, observers);\n\n // Return cleanup function to destroy observer and remove from registry\n return () => {\n observer.destroy();\n TRACKER_REGISTRY.delete(init);\n };\n });\n\n // Function to assign tracking to a specific key/property\n const assign = (prop: KeyLike) => {\n // Activate the subscription\n subscribe();\n // Track the specific property\n track?.(prop);\n };\n\n // Store the assign function in the registry for this object\n TRACKER_REGISTRY.set(init, assign);\n }\n\n // Execute the tracking function for the specific key\n TRACKER_REGISTRY.get(init)?.(key);\n });\n}\n"]}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { Storable, KVState, Rec, Row, RowState, FilterFn, RowListState, ReactiveTable, InferRec } from '@anchorlib/storage/db';
|
|
2
|
+
import { ObjLike, LinkableSchema, StateOptions } from '@anchorlib/core';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Creates a reactive key-value store state.
|
|
6
|
+
*
|
|
7
|
+
* This function initializes a key-value store with the given name and initial value,
|
|
8
|
+
* and automatically cleans up the store subscription when the component is destroyed.
|
|
9
|
+
*
|
|
10
|
+
* @template T - The type of the stored value, must extend Storable
|
|
11
|
+
* @param name - The unique identifier for the key-value store
|
|
12
|
+
* @param init - The initial value for the store
|
|
13
|
+
* @returns A reactive key-value store state.
|
|
14
|
+
*/
|
|
15
|
+
declare function kvRef<T extends Storable>(name: string, init: T): KVState<T>;
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Creates a persistent reactive state using the provided name, initial value, and options.
|
|
19
|
+
* The persistentRef is tied to the browser's local storage, meaning its value will persist
|
|
20
|
+
* across page reloads and browser sessions until explicitly cleared.
|
|
21
|
+
*
|
|
22
|
+
* @template T - The type of the initial value, must extend object-like structure.
|
|
23
|
+
* @template S - The schema type for linkable validation, defaults to LinkableSchema.
|
|
24
|
+
* @param name - A unique string identifier for the local storage key.
|
|
25
|
+
* @param init - The initial value to be stored in local storage.
|
|
26
|
+
* @param options - Optional configuration for state behavior and validation schema.
|
|
27
|
+
* @returns A reactive state that provides reactive access and modification capabilities.
|
|
28
|
+
*/
|
|
29
|
+
declare function persistentRef<T extends ObjLike, S extends LinkableSchema = LinkableSchema>(name: string, init: T, options?: StateOptions<S>): T;
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Creates a session-scoped reactive state using the provided name, initial value, and options.
|
|
33
|
+
* The sessionRef is tied to the browser's session storage, meaning its value will persist
|
|
34
|
+
* across page reloads but not after the session ends (e.g., tab/window closed).
|
|
35
|
+
*
|
|
36
|
+
* @template T - The type of the initial value, must extend object-like structure.
|
|
37
|
+
* @template S - The schema type for linkable validation, defaults to LinkableSchema.
|
|
38
|
+
* @param name - A unique string identifier for the session storage key.
|
|
39
|
+
* @param init - The initial value to be stored in session storage.
|
|
40
|
+
* @param options - Optional configuration for state behavior and validation schema.
|
|
41
|
+
* @returns A reactive state that provides reactive access and modification capabilities.
|
|
42
|
+
*/
|
|
43
|
+
declare function sessionRef<T extends ObjLike, S extends LinkableSchema = LinkableSchema>(name: string, init: T, options?: StateOptions<S>): T;
|
|
44
|
+
|
|
45
|
+
interface TableRef<T extends Rec, R extends Row<T> = Row<T>> {
|
|
46
|
+
get(id: string): RowState<R>;
|
|
47
|
+
add(payload: T): RowState<R>;
|
|
48
|
+
remove(id: string): RowState<R>;
|
|
49
|
+
list(filter?: IDBKeyRange | FilterFn<R>, limit?: number, direction?: IDBCursorDirection): RowListState<R>;
|
|
50
|
+
listByIndex(name: keyof R, filter?: IDBKeyRange | FilterFn<R>, limit?: number, direction?: IDBCursorDirection): RowListState<R>;
|
|
51
|
+
seed<T extends R[]>(seeds: T): this;
|
|
52
|
+
table(): ReactiveTable<T>;
|
|
53
|
+
}
|
|
54
|
+
type InferRef<T> = T extends TableRef<Rec, infer R> ? R : never;
|
|
55
|
+
type InferListRef<T> = T extends TableRef<Rec, infer R> ? R[] : never;
|
|
56
|
+
|
|
57
|
+
declare function createTableRef<T extends ReactiveTable<Rec>>(table: T): TableRef<InferRec<T>>;
|
|
58
|
+
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>;
|
|
59
|
+
|
|
60
|
+
export { type InferListRef, type InferRef, type TableRef, createTableRef, kvRef, persistentRef, sessionRef };
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { kv, createTable } from '@anchorlib/storage/db';
|
|
2
|
+
import { onDestroy } from 'svelte';
|
|
3
|
+
import { persistent, session } from '@anchorlib/storage';
|
|
4
|
+
|
|
5
|
+
// src/storage/kv.ts
|
|
6
|
+
function kvRef(name, init) {
|
|
7
|
+
const state = kv(name, init);
|
|
8
|
+
onDestroy(() => {
|
|
9
|
+
kv.leave(state);
|
|
10
|
+
});
|
|
11
|
+
return state;
|
|
12
|
+
}
|
|
13
|
+
function persistentRef(name, init, options) {
|
|
14
|
+
const state = persistent(name, init, options);
|
|
15
|
+
onDestroy(() => {
|
|
16
|
+
persistent.leave(state);
|
|
17
|
+
});
|
|
18
|
+
return state;
|
|
19
|
+
}
|
|
20
|
+
function sessionRef(name, init, options) {
|
|
21
|
+
const state = session(name, init, options);
|
|
22
|
+
onDestroy(() => {
|
|
23
|
+
session.leave(state);
|
|
24
|
+
});
|
|
25
|
+
return state;
|
|
26
|
+
}
|
|
27
|
+
function createTableRef(tableName, version = 1, indexes, remIndexes, dbName = tableName) {
|
|
28
|
+
if (typeof tableName === "string") {
|
|
29
|
+
tableName = createTable(tableName, version, indexes, remIndexes, dbName);
|
|
30
|
+
}
|
|
31
|
+
const tableRef = tableName;
|
|
32
|
+
return {
|
|
33
|
+
get(id) {
|
|
34
|
+
const state = tableRef.get(id);
|
|
35
|
+
onDestroy(() => {
|
|
36
|
+
tableRef.leave(id);
|
|
37
|
+
});
|
|
38
|
+
return state;
|
|
39
|
+
},
|
|
40
|
+
add(payload) {
|
|
41
|
+
const state = tableRef.add(payload);
|
|
42
|
+
onDestroy(() => {
|
|
43
|
+
tableRef.leave(state.data.id);
|
|
44
|
+
});
|
|
45
|
+
return state;
|
|
46
|
+
},
|
|
47
|
+
list(filter, limit, direction) {
|
|
48
|
+
return tableRef.list(filter, limit, direction);
|
|
49
|
+
},
|
|
50
|
+
listByIndex(name, filter, limit, direction) {
|
|
51
|
+
return tableRef.listByIndex(name, filter, limit, direction);
|
|
52
|
+
},
|
|
53
|
+
remove(id) {
|
|
54
|
+
return tableRef.remove(id);
|
|
55
|
+
},
|
|
56
|
+
seed(seeds) {
|
|
57
|
+
tableRef.seed(seeds);
|
|
58
|
+
return this;
|
|
59
|
+
},
|
|
60
|
+
table() {
|
|
61
|
+
return tableRef;
|
|
62
|
+
}
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export { createTableRef, kvRef, persistentRef, sessionRef };
|
|
67
|
+
//# sourceMappingURL=index.js.map
|
|
68
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/storage/kv.ts","../../src/storage/persistent.ts","../../src/storage/session.ts","../../src/storage/table.ts"],"names":["onDestroy"],"mappings":";;;;;AAcO,SAAS,KAAA,CAA0B,MAAc,IAAA,EAAqB;AAC3E,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,KAAA;AACT;ACNO,SAAS,aAAA,CACd,IAAA,EACA,IAAA,EACA,OAAA,EACG;AACH,EAAA,MAAM,KAAA,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,KAAA,EAAgB,SAAA,EAAgC;AACvF,MAAA,OAAO,QAAA,CAAS,IAAA,CAAK,MAAA,EAAQ,KAAA,EAAO,SAAS,CAAA;AAAA,IAC/C,CAAA;AAAA,IACA,WAAA,CAAY,IAAA,EAAe,MAAA,EAAoC,KAAA,EAAgB,SAAA,EAAgC;AAC7G,MAAA,OAAO,QAAA,CAAS,WAAA,CAAY,IAAA,EAAM,MAAA,EAAQ,OAAO,SAAS,CAAA;AAAA,IAC5D,CAAA;AAAA,IACA,OAAO,EAAA,EAAY;AACjB,MAAA,OAAO,QAAA,CAAS,OAAO,EAAE,CAAA;AAAA,IAC3B,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 { onDestroy } from 'svelte';\n\n/**\n * Creates a reactive 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 key-value store state.\n */\nexport function kvRef<T extends Storable>(name: string, init: T): KVState<T> {\n const state = kv(name, init);\n\n onDestroy(() => {\n kv.leave(state);\n });\n\n return state;\n}\n","import type { LinkableSchema, ObjLike, StateOptions } from '@anchorlib/core';\nimport { persistent } from '@anchorlib/storage';\nimport { onDestroy } from 'svelte';\n\n/**\n * Creates a persistent reactive state 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 reactive state 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): T {\n const state = persistent(name, init, options);\n\n onDestroy(() => {\n persistent.leave(state);\n });\n\n return state;\n}\n","import type { LinkableSchema, ObjLike, StateOptions } from '@anchorlib/core';\nimport { session } from '@anchorlib/storage';\nimport { onDestroy } from 'svelte';\n\n/**\n * Creates a session-scoped reactive state 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 reactive state 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): T {\n const state = session(name, init, options);\n\n onDestroy(() => {\n session.leave(state);\n });\n\n return 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 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 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 state;\n },\n list(filter?: IDBKeyRange | FilterFn<R>, limit?: number, direction?: IDBCursorDirection) {\n return tableRef.list(filter, limit, direction);\n },\n listByIndex(name: keyof R, filter?: IDBKeyRange | FilterFn<R>, limit?: number, direction?: IDBCursorDirection) {\n return tableRef.listByIndex(name, filter, limit, direction);\n },\n remove(id: string) {\n return tableRef.remove(id);\n },\n seed(seeds: R[]) {\n tableRef.seed(seeds);\n return this;\n },\n table() {\n return tableRef;\n },\n };\n}\n"]}
|
package/package.json
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@anchorlib/svelte",
|
|
3
|
+
"version": "1.0.0-beta.10",
|
|
4
|
+
"description": "Svelte bindings for Anchor reactive state management",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"state",
|
|
7
|
+
"management",
|
|
8
|
+
"reactive",
|
|
9
|
+
"observable",
|
|
10
|
+
"svelte"
|
|
11
|
+
],
|
|
12
|
+
"author": "Nanang Mahdaen El Agung <mahdaen@gmail.com>",
|
|
13
|
+
"homepage": "https://github.com/beerush-id/anchor",
|
|
14
|
+
"license": "MIT",
|
|
15
|
+
"repository": {
|
|
16
|
+
"type": "git",
|
|
17
|
+
"url": "git+https://github.com/beerush-id/anchor.git"
|
|
18
|
+
},
|
|
19
|
+
"bugs": {
|
|
20
|
+
"url": "https://github.com/beerush-id/anchor/issues"
|
|
21
|
+
},
|
|
22
|
+
"type": "module",
|
|
23
|
+
"module": "./dist/index.js",
|
|
24
|
+
"svelte": "./dist/index.js",
|
|
25
|
+
"types": "./dist/index.d.ts",
|
|
26
|
+
"exports": {
|
|
27
|
+
".": {
|
|
28
|
+
"types": "./dist/index.d.ts",
|
|
29
|
+
"svelte": "./dist/index.js",
|
|
30
|
+
"import": "./dist/index.js"
|
|
31
|
+
},
|
|
32
|
+
"./reactive": {
|
|
33
|
+
"types": "./dist/reactive.d.ts",
|
|
34
|
+
"svelte": "./dist/reactive.js",
|
|
35
|
+
"import": "./dist/reactive.js"
|
|
36
|
+
},
|
|
37
|
+
"./storage": {
|
|
38
|
+
"types": "./dist/storage/index.d.ts",
|
|
39
|
+
"svelte": "./dist/storage/index.js",
|
|
40
|
+
"import": "./dist/storage/index.js"
|
|
41
|
+
}
|
|
42
|
+
},
|
|
43
|
+
"directories": {
|
|
44
|
+
"dist": "./dist"
|
|
45
|
+
},
|
|
46
|
+
"files": [
|
|
47
|
+
"dist"
|
|
48
|
+
],
|
|
49
|
+
"publishConfig": {
|
|
50
|
+
"access": "public"
|
|
51
|
+
},
|
|
52
|
+
"dependencies": {
|
|
53
|
+
"@anchorlib/core": "^1.0.0-beta.10",
|
|
54
|
+
"@anchorlib/storage": "^1.0.0-beta.10"
|
|
55
|
+
},
|
|
56
|
+
"devDependencies": {
|
|
57
|
+
"@eslint/css": "^0.5.0",
|
|
58
|
+
"@eslint/js": "^9.23.0",
|
|
59
|
+
"@eslint/markdown": "^6.3.0",
|
|
60
|
+
"@sveltejs/vite-plugin-svelte": "^6.2.1",
|
|
61
|
+
"@testing-library/svelte": "^5.2.8",
|
|
62
|
+
"@testing-library/user-event": "^14.6.1",
|
|
63
|
+
"@types/react": "^19.1.8",
|
|
64
|
+
"@vitest/ui": "^3.2.4",
|
|
65
|
+
"eslint": "^9.23.0",
|
|
66
|
+
"eslint-plugin-prettier": "^5.2.3",
|
|
67
|
+
"jsdom": "^27.0.0",
|
|
68
|
+
"prettier": "^3.5.3",
|
|
69
|
+
"publint": "^0.3.9",
|
|
70
|
+
"rimraf": "^6.0.1",
|
|
71
|
+
"tsup": "^8.4.0",
|
|
72
|
+
"typescript-eslint": "^8.27.0",
|
|
73
|
+
"vitest": "^3.2.4"
|
|
74
|
+
},
|
|
75
|
+
"peerDependencies": {
|
|
76
|
+
"svelte": "^5.36.16",
|
|
77
|
+
"typescript": "^5.8.2"
|
|
78
|
+
},
|
|
79
|
+
"scripts": {
|
|
80
|
+
"dev": "rimraf dist && tsup --watch",
|
|
81
|
+
"clean": "rimraf dist",
|
|
82
|
+
"build": "rimraf dist && tsup && publint",
|
|
83
|
+
"prepublish": "rimraf dist && tsup && publint",
|
|
84
|
+
"format": "prettier --write .",
|
|
85
|
+
"test": "rimraf coverage && vitest --run",
|
|
86
|
+
"test:preview": "rimraf coverage && vitest --run && vite preview --outDir coverage"
|
|
87
|
+
},
|
|
88
|
+
"optionalDependencies": {
|
|
89
|
+
"zod": "^4.1.5"
|
|
90
|
+
}
|
|
91
|
+
}
|
package/readme.md
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
# Anchor Svelte Library
|
|
2
|
+
|
|
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.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @anchorlib/svelte
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Documentation
|
|
12
|
+
|
|
13
|
+
For full documentation, visit [Anchor for Svelte](https://anchorlib.dev/docs/svelte/introduction.html)
|
|
14
|
+
|
|
15
|
+
## Quick Start
|
|
16
|
+
|
|
17
|
+
```svelte
|
|
18
|
+
<script>
|
|
19
|
+
import { anchorRef } from '@anchorlib/svelte';
|
|
20
|
+
|
|
21
|
+
// Create a reactive state object
|
|
22
|
+
const state = anchorRef({
|
|
23
|
+
count: 0,
|
|
24
|
+
user: {
|
|
25
|
+
name: 'John Doe',
|
|
26
|
+
email: 'john@example.com'
|
|
27
|
+
}
|
|
28
|
+
});
|
|
29
|
+
</script>
|
|
30
|
+
|
|
31
|
+
<div>
|
|
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>
|
|
36
|
+
</div>
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
## License
|
|
40
|
+
|
|
41
|
+
MIT
|