@anchorlib/svelte 1.0.0-beta.5 → 1.0.0-beta.6
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 +6 -1
- package/dist/index.js +7 -6
- package/dist/index.js.map +1 -1
- package/package.json +3 -3
package/dist/index.d.ts
CHANGED
|
@@ -38,6 +38,11 @@ declare function anchorRef<S extends LinkableSchema, T extends ModelInput<S>>(in
|
|
|
38
38
|
declare function anchorRef<S extends LinkableSchema, T extends ModelInput<S>>(init: T, schema?: S, options?: StateOptions & {
|
|
39
39
|
immutable: true;
|
|
40
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;
|
|
41
46
|
/**
|
|
42
47
|
* Creates a writable reference that maintains a sorted array state based on a comparison function.
|
|
43
48
|
*
|
|
@@ -301,4 +306,4 @@ declare function constantRef<T>(init: T): ConstantRef<T>;
|
|
|
301
306
|
*/
|
|
302
307
|
declare function isRef<T>(ref: unknown): ref is VariableRef<T>;
|
|
303
308
|
|
|
304
|
-
export { ConstantRef, type Props, type PropsRef, REF_REGISTRY, StateRef, VariableRef, anchorRef, constantRef, derivedRef, exceptionRef, fetchRef, flatRef, historyRef, immutableRef, isRef, modelRef, observedRef, orderedRef, propsRef, rawRef, streamRef, variableRef, writableRef };
|
|
309
|
+
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 };
|
package/dist/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { anchor,
|
|
1
|
+
import { anchor, subscribe, linkable, fetchState, streamState, history, captureStack, createObserver, derive } from '@anchorlib/core';
|
|
2
2
|
import { onDestroy } from 'svelte';
|
|
3
3
|
import 'svelte/store';
|
|
4
4
|
|
|
@@ -7,7 +7,7 @@ var REF_REGISTRY = /* @__PURE__ */ new WeakMap();
|
|
|
7
7
|
function variableRef(init, constant) {
|
|
8
8
|
const subscribers = /* @__PURE__ */ new Set();
|
|
9
9
|
const stateRef = anchor({ value: init }, { recursive: true });
|
|
10
|
-
const controller =
|
|
10
|
+
const controller = subscribe.resolve(stateRef);
|
|
11
11
|
let leaveState = void 0;
|
|
12
12
|
const set = (value) => {
|
|
13
13
|
if (constant === true || value === stateRef.value) return;
|
|
@@ -19,10 +19,10 @@ function variableRef(init, constant) {
|
|
|
19
19
|
stateRef.value = value;
|
|
20
20
|
return;
|
|
21
21
|
}
|
|
22
|
-
const { schema, configs } = anchor.has(stateRef.value) ?
|
|
22
|
+
const { schema, configs } = anchor.has(stateRef.value) ? subscribe.resolve(stateRef.value)?.meta ?? {} : {};
|
|
23
23
|
stateRef.value = anchor(value, schema ?? configs, configs);
|
|
24
24
|
};
|
|
25
|
-
const subscribe = (handler) => {
|
|
25
|
+
const subscribe$1 = (handler) => {
|
|
26
26
|
if (!anchor.has(stateRef)) {
|
|
27
27
|
handler(stateRef.value);
|
|
28
28
|
return () => {
|
|
@@ -70,7 +70,7 @@ function variableRef(init, constant) {
|
|
|
70
70
|
value: publish
|
|
71
71
|
},
|
|
72
72
|
subscribe: {
|
|
73
|
-
value: subscribe
|
|
73
|
+
value: subscribe$1
|
|
74
74
|
},
|
|
75
75
|
set: {
|
|
76
76
|
value: set
|
|
@@ -91,6 +91,7 @@ function anchorRef(init, schemaOptions, options) {
|
|
|
91
91
|
const state = linkable(init) ? anchor(init, schemaOptions, options) : init;
|
|
92
92
|
return variableRef(state);
|
|
93
93
|
}
|
|
94
|
+
var reactiveRef = anchorRef;
|
|
94
95
|
function orderedRef(init, compare, options) {
|
|
95
96
|
const state = anchor.ordered(init, compare, options);
|
|
96
97
|
return variableRef(state);
|
|
@@ -202,6 +203,6 @@ function observedRef(observe) {
|
|
|
202
203
|
} };
|
|
203
204
|
}
|
|
204
205
|
|
|
205
|
-
export { REF_REGISTRY, anchorRef, constantRef, derivedRef, exceptionRef, fetchRef, flatRef, historyRef, immutableRef, isRef, modelRef, observedRef, orderedRef, propsRef, rawRef, streamRef, variableRef, writableRef };
|
|
206
|
+
export { REF_REGISTRY, anchorRef, constantRef, derivedRef, exceptionRef, fetchRef, flatRef, historyRef, immutableRef, isRef, modelRef, observedRef, orderedRef, propsRef, rawRef, reactiveRef, streamRef, variableRef, writableRef };
|
|
206
207
|
//# sourceMappingURL=index.js.map
|
|
207
208
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
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":["linkable","anchor","derive","onDestroy"],"mappings":";;;;;AAIO,IAAM,YAAA,uBAAmB,OAAA;AAoCzB,SAAS,WAAA,CAAe,MAAS,QAAA,EAAwC;AAC9E,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,GAAa,MAAA,CAAO,OAAA,CAAQ,QAAQ,CAAA;AAE1C,EAAA,IAAI,UAAA,GAAwD,MAAA;AAE5D,EAAA,MAAM,GAAA,GAAM,C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A,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,EAAAE,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, derive, type Linkable, linkable, type StateController } 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 * @param updater\n * @returns A readable reference object with subscribe and publish capabilities\n */\nexport function variableRef<T>(init: T, updater?: (value: T) => 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 | ((value: T) => T)) {\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 if (typeof constant === 'function') {\n stateRef.value = (constant as (value: T) => T)(value);\n return;\n }\n\n if (!linkable(value)) {\n stateRef.value = value;\n return;\n }\n\n const { schema, configs } = anchor.has(stateRef.value as Linkable)\n ? (derive.resolve(stateRef.value as Linkable)?.meta ?? {})\n : {};\n\n // Create a new state using the same options.\n stateRef.value = anchor(value as never, (schema ?? configs) as never, configs);\n };\n\n const subscribe = (handler: RefSubscriber<T>) => {\n if (!anchor.has(stateRef)) {\n handler(stateRef.value);\n return () => {};\n }\n\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 linkable,\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, 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 = linkable(init) ? anchor<S, ModelInput<S>>(init as ModelInput<S>, schemaOptions as S, options) : init;\n return variableRef(state);\n}\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 { 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 * @param recursive - A boolean indicating whether to recursively derive the state\n * @returns A readable store containing the state value\n */\nexport function derivedRef<T>(state: T | VariableRef<T>, recursive?: boolean): 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 transformRecursive - 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>(\n state: T | VariableRef<T>,\n transformRecursive?: ((current: T) => R) | boolean\n): 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(\n target,\n (current) => {\n if (isRef(state)) {\n current = (state as StateRef<T>).value;\n } else {\n current = state;\n }\n\n const value = typeof transformRecursive === 'function' ? transformRecursive(current as T) : current;\n handler(value as T);\n },\n typeof transformRecursive === 'boolean' ? transformRecursive : undefined\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
|
+
{"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","linkable","anchor","onDestroy"],"mappings":";;;;;AAIO,IAAM,YAAA,uBAAmB,OAAA;AAoCzB,SAAS,WAAA,CAAe,MAAS,QAAA,EAAwC;AAC9E,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,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AAM;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 Linkable, linkable, 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 * @param updater\n * @returns A readable reference object with subscribe and publish capabilities\n */\nexport function variableRef<T>(init: T, updater?: (value: T) => 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 | ((value: T) => T)) {\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 if (typeof constant === 'function') {\n stateRef.value = (constant as (value: T) => T)(value);\n return;\n }\n\n if (!linkable(value)) {\n stateRef.value = value;\n return;\n }\n\n const { schema, configs } = anchor.has(stateRef.value as Linkable)\n ? (derive.resolve(stateRef.value as Linkable)?.meta ?? {})\n : {};\n\n // Create a new state using the same options.\n stateRef.value = anchor(value as never, (schema ?? configs) as never, configs);\n };\n\n const subscribe = (handler: RefSubscriber<T>) => {\n if (!anchor.has(stateRef)) {\n handler(stateRef.value);\n return () => {};\n }\n\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 linkable,\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, 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 = linkable(init) ? anchor<S, ModelInput<S>>(init as ModelInput<S>, schemaOptions as S, options) : init;\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 { 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 * @param recursive - A boolean indicating whether to recursively derive the state\n * @returns A readable store containing the state value\n */\nexport function derivedRef<T>(state: T | VariableRef<T>, recursive?: boolean): 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 transformRecursive - 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>(\n state: T | VariableRef<T>,\n transformRecursive?: ((current: T) => R) | boolean\n): 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(\n target,\n (current) => {\n if (isRef(state)) {\n current = (state as StateRef<T>).value;\n } else {\n current = state;\n }\n\n const value = typeof transformRecursive === 'function' ? transformRecursive(current as T) : current;\n handler(value as T);\n },\n typeof transformRecursive === 'boolean' ? transformRecursive : undefined\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"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@anchorlib/svelte",
|
|
3
|
-
"version": "1.0.0-beta.
|
|
3
|
+
"version": "1.0.0-beta.6",
|
|
4
4
|
"description": "Svelte bindings for Anchor reactive state management",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"state",
|
|
@@ -45,8 +45,8 @@
|
|
|
45
45
|
"access": "public"
|
|
46
46
|
},
|
|
47
47
|
"dependencies": {
|
|
48
|
-
"@anchorlib/core": "^1.0.0-beta.
|
|
49
|
-
"@anchorlib/storage": "^1.0.0-beta.
|
|
48
|
+
"@anchorlib/core": "^1.0.0-beta.6",
|
|
49
|
+
"@anchorlib/storage": "^1.0.0-beta.6"
|
|
50
50
|
},
|
|
51
51
|
"devDependencies": {
|
|
52
52
|
"@eslint/css": "^0.5.0",
|