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

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@anchorlib/svelte",
3
- "version": "1.0.0-beta.2",
3
+ "version": "1.0.0-beta.20",
4
4
  "description": "Svelte bindings for Anchor reactive state management",
5
5
  "keywords": [
6
6
  "state",
@@ -29,6 +29,16 @@
29
29
  "svelte": "./dist/index.js",
30
30
  "import": "./dist/index.js"
31
31
  },
32
+ "./reactive": {
33
+ "types": "./dist/reactive.d.ts",
34
+ "svelte": "./dist/reactive.js",
35
+ "import": "./dist/reactive.js"
36
+ },
37
+ "./core": {
38
+ "types": "./dist/core/index.d.ts",
39
+ "svelte": "./dist/core/index.js",
40
+ "import": "./dist/core/index.js"
41
+ },
32
42
  "./storage": {
33
43
  "types": "./dist/storage/index.d.ts",
34
44
  "svelte": "./dist/storage/index.js",
@@ -45,34 +55,36 @@
45
55
  "access": "public"
46
56
  },
47
57
  "dependencies": {
48
- "@anchorlib/core": "^1.0.0-beta.2",
49
- "@anchorlib/storage": "^1.0.0-beta.2"
58
+ "@anchorlib/core": "^1.0.0-beta.20",
59
+ "@anchorlib/storage": "^1.0.0-beta.20"
50
60
  },
51
61
  "devDependencies": {
52
- "@eslint/css": "^0.5.0",
53
- "@eslint/js": "^9.23.0",
54
- "@eslint/markdown": "^6.3.0",
55
- "@types/react": "^19.1.8",
62
+ "@biomejs/biome": "2.3.3",
63
+ "@sveltejs/vite-plugin-svelte": "6.2.1",
64
+ "@testing-library/svelte": "5.2.8",
65
+ "@testing-library/user-event": "14.6.1",
66
+ "@types/react": "^19.1.0",
67
+ "@vitest/coverage-v8": "^3.2.4",
56
68
  "@vitest/ui": "^3.2.4",
57
- "eslint": "^9.23.0",
58
- "eslint-plugin-prettier": "^5.2.3",
59
- "prettier": "^3.5.3",
60
- "publint": "^0.3.9",
61
- "rimraf": "^6.0.1",
62
- "tsup": "^8.4.0",
63
- "typescript-eslint": "^8.27.0",
69
+ "jsdom": "27.0.1",
70
+ "publint": "0.3.15",
71
+ "rimraf": "6.0.1",
72
+ "tsdown": "0.15.9",
73
+ "vite": "7.1.12",
64
74
  "vitest": "^3.2.4"
65
75
  },
66
76
  "peerDependencies": {
67
- "svelte": "^5.36.16",
68
- "typescript": "^5.8.2"
77
+ "svelte": "^5.0.0",
78
+ "typescript": "^5.9.3"
69
79
  },
70
80
  "scripts": {
71
- "dev": "rimraf dist && tsup --watch",
81
+ "dev": "rimraf dist && tsdown --watch ./src",
72
82
  "clean": "rimraf dist",
73
- "build": "rimraf dist && tsup && publint",
74
- "prepublish": "rimraf dist && tsup && publint",
75
- "format": "prettier --write ."
83
+ "build": "rimraf dist && tsdown && publint",
84
+ "prepublish": "rimraf dist && tsdown && publint",
85
+ "format": "biome format --write",
86
+ "test": "rimraf coverage && vitest --run",
87
+ "test:preview": "rimraf coverage && vitest --run && vite preview --outDir coverage"
76
88
  },
77
89
  "optionalDependencies": {
78
90
  "zod": "^4.1.5"
package/readme.md CHANGED
@@ -1,15 +1,6 @@
1
- # @anchorlib/svelte
1
+ # Anchor Svelte Library
2
2
 
3
- Svelte integration for the Anchor reactive state management library.
4
-
5
- ## Features
6
-
7
- - **Reactive State Management** - Directly mutate state without needing setState patterns
8
- - **Automatic Cleanup** - Automatic subscription cleanup using Svelte's onDestroy lifecycle
9
- - **Svelte 5 Runes Integration** - Works with Svelte 5's runes system
10
- - **Framework Optimized** - Designed specifically for Svelte's reactivity model
11
- - **Schema Validation** - Built-in Zod schema validation support
12
- - **Type Safety** - Full TypeScript support with comprehensive type definitions
3
+ This is the official Anchor library for Svelte. It provides a set of tools to manage state in your Svelte applications, based on the principles of the Anchor framework.
13
4
 
14
5
  ## Installation
15
6
 
@@ -19,7 +10,7 @@ npm install @anchorlib/svelte
19
10
 
20
11
  ## Documentation
21
12
 
22
- For full documentation, visit [Anchor for Svelte](https://beerush-id.github.io/anchor/docs/svelte/introduction.html)
13
+ For full documentation, visit [Anchor for Svelte](https://anchorlib.dev/docs/svelte/introduction.html)
23
14
 
24
15
  ## Quick Start
25
16
 
@@ -38,10 +29,10 @@ For full documentation, visit [Anchor for Svelte](https://beerush-id.github.io/a
38
29
  </script>
39
30
 
40
31
  <div>
41
- <h1>Hello {$state.user.name}</h1>
42
- <p>Count: {$state.count}</p>
43
- <button onclick={() => $state.count++}>Increment</button>
44
- <button onclick={() => $state.user.name = 'Jane Doe'}>Change Name</button>
32
+ <h1>Hello {state.user.name}</h1>
33
+ <p>Count: {state.count}</p>
34
+ <button onclick={() => state.count++}>Increment</button>
35
+ <button onclick={() => state.user.name = 'Jane Doe'}>Change Name</button>
45
36
  </div>
46
37
  ```
47
38
 
package/dist/index.js.map DELETED
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../src/ref.ts","../src/anchor.ts","../src/derive.ts","../src/fetch.ts","../src/history.ts","../src/immutable.ts","../src/model.ts","../src/prop.ts","../src/observable.ts"],"names":["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,CAAC,KAAA,KAAa;AAExB,IAAA,IAAI,QAAA,KAAa,IAAA,IAAQ,KAAA,KAAU,QAAA,CAAS,KAAA,EAAO;AAEnD,IAAA,IAAI,OAAO,aAAa,UAAA,EAAY;AAClC,MAAA,QAAA,CAAS,KAAA,GAAS,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AAA;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 +0,0 @@
1
- {"version":3,"sources":["../../src/storage/kv.ts","../../src/storage/persistent.ts","../../src/storage/session.ts","../../src/storage/table.ts"],"names":["constantRef","onDestroy"],"mappings":";;;;;;AAgBO,SAAS,KAAA,CAA0B,MAAc,IAAA,EAAkC;AACxF,EAAA,MAAM,KAAA,GAAQ,EAAA,CAAG,IAAA,EAAM,IAAI,CAAA;AAE3B,EAAA,SAAA,CAAU,MAAM;AACd,IAAA,EAAA,CAAG,MAAM,KAAK,CAAA;AAAA,EAChB,CAAC,CAAA;AAED,EAAA,OAAO,YAAY,KAAK,CAAA;AAC1B;ACPO,SAAS,aAAA,CACd,IAAA,EACA,IAAA,EACA,OAAA,EACgB;AAChB,EAAA,MAAM,KAAA,GAAQ,UAAA,CAAW,IAAA,EAAM,IAAA,EAAM,OAAO,CAAA;AAC5C,EAAA,OAAOA,YAAe,KAAK,CAAA;AAC7B;ACPO,SAAS,UAAA,CACd,IAAA,EACA,IAAA,EACA,OAAA,EACgB;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AAA,CAAS,WAAA,CAAY,IAAA,EAAM,MAAA,EAAQ,OAAO,SAAS,CAAA;AACjE,MAAA,OAAOA,YAAY,KAAK,CAAA;AAAA,IAC1B,CAAA;AAAA,IACA,OAAO,EAAA,EAAY;AACjB,MAAA,MAAM,KAAA,GAAQ,QAAA,CAAS,MAAA,CAAO,EAAE,CAAA;AAChC,MAAA,OAAOA,YAAY,KAAK,CAAA;AAAA,IAC1B,CAAA;AAAA,IACA,KAAK,KAAA,EAAY;AACf,MAAA,QAAA,CAAS,KAAK,KAAK,CAAA;AACnB,MAAA,OAAO,IAAA;AAAA,IACT,CAAA;AAAA,IACA,KAAA,GAAQ;AACN,MAAA,OAAO,QAAA;AAAA,IACT;AAAA,GACF;AACF","file":"index.js","sourcesContent":["import { kv, type KVState, type Storable } from '@anchorlib/storage/db';\nimport type { ConstantRef } from '@base/index.js';\nimport { constantRef } from '@base/index.js';\nimport { onDestroy } from 'svelte';\n\n/**\n * Creates a reactive reference to a key-value store state.\n *\n * This function initializes a key-value store with the given name and initial value,\n * and automatically cleans up the store subscription when the component is destroyed.\n *\n * @template T - The type of the stored value, must extend Storable\n * @param name - The unique identifier for the key-value store\n * @param init - The initial value for the store\n * @returns A reactive reference to the key-value store state\n */\nexport function kvRef<T extends Storable>(name: string, init: T): ConstantRef<KVState<T>> {\n const state = kv(name, init);\n\n onDestroy(() => {\n kv.leave(state);\n });\n\n return constantRef(state);\n}\n","import type { LinkableSchema, ObjLike, StateOptions } from '@anchorlib/core';\nimport type { ConstantRef } from '@base/index.js';\nimport { constantRef } from '@base/index.js';\nimport { persistent } from '@anchorlib/storage';\n\n/**\n * Creates a persistent reactive reference using the provided name, initial value, and options.\n * The persistentRef is tied to the browser's local storage, meaning its value will persist\n * across page reloads and browser sessions until explicitly cleared.\n *\n * @template T - The type of the initial value, must extend object-like structure.\n * @template S - The schema type for linkable validation, defaults to LinkableSchema.\n * @param name - A unique string identifier for the local storage key.\n * @param init - The initial value to be stored in local storage.\n * @param options - Optional configuration for state behavior and validation schema.\n * @returns A WritableRef<T> that provides reactive access and modification capabilities.\n */\nexport function persistentRef<T extends ObjLike, S extends LinkableSchema = LinkableSchema>(\n name: string,\n init: T,\n options?: StateOptions<S>\n): ConstantRef<T> {\n const state = persistent(name, init, options);\n return constantRef<T>(state);\n}\n","import type { LinkableSchema, ObjLike, StateOptions } from '@anchorlib/core';\nimport type { ConstantRef } from '@base/index.js';\nimport { constantRef } from '@base/index.js';\nimport { session } from '@anchorlib/storage';\n\n/**\n * Creates a session-scoped reactive reference using the provided name, initial value, and options.\n * The sessionRef is tied to the browser's session storage, meaning its value will persist\n * across page reloads but not after the session ends (e.g., tab/window closed).\n *\n * @template T - The type of the initial value, must extend object-like structure.\n * @template S - The schema type for linkable validation, defaults to LinkableSchema.\n * @param name - A unique string identifier for the session storage key.\n * @param init - The initial value to be stored in session storage.\n * @param options - Optional configuration for state behavior and validation schema.\n * @returns A WritableRef<T> that provides reactive access and modification capabilities.\n */\nexport function sessionRef<T extends ObjLike, S extends LinkableSchema = LinkableSchema>(\n name: string,\n init: T,\n options?: StateOptions<S>\n): ConstantRef<T> {\n const state = session(name, init, options);\n return constantRef<T>(state);\n}\n","import {\n createTable,\n type FilterFn,\n type InferRec,\n type ReactiveTable,\n type Rec,\n type Row,\n} from '@anchorlib/storage/db';\nimport { onDestroy } from 'svelte';\nimport { constantRef } from '@base/index.js';\nimport type { TableRef } from './types.js';\n\nexport function createTableRef<T extends ReactiveTable<Rec>>(table: T): TableRef<InferRec<T>>;\nexport function createTableRef<T extends Rec, R extends Row<T> = Row<T>>(\n name: string,\n version?: number,\n indexes?: (keyof R)[],\n remIndexes?: (keyof R)[],\n dbName?: string\n): TableRef<T, R>;\nexport function createTableRef<T extends Rec, R extends Row<T> = Row<T>>(\n tableName: string | ReactiveTable<T>,\n version = 1,\n indexes?: (keyof R)[],\n remIndexes?: (keyof R)[],\n dbName = tableName as string\n): TableRef<T, R> {\n if (typeof tableName === 'string') {\n tableName = createTable<T, R>(tableName, version, indexes, remIndexes, dbName);\n }\n\n const tableRef = tableName as ReactiveTable<T, R>;\n\n return {\n get(id: string) {\n const state = tableRef.get(id);\n\n onDestroy(() => {\n tableRef.leave(id);\n });\n\n return constantRef(state);\n },\n add(payload: T) {\n const state = tableRef.add(payload);\n\n onDestroy(() => {\n tableRef.leave(state.data.id);\n });\n\n return constantRef(state);\n },\n list(filter?: IDBKeyRange | FilterFn<R>, limit?: number, direction?: IDBCursorDirection) {\n const state = tableRef.list(filter, limit, direction);\n return constantRef(state);\n },\n listByIndex(name: keyof R, filter?: IDBKeyRange | FilterFn<R>, limit?: number, direction?: IDBCursorDirection) {\n const state = tableRef.listByIndex(name, filter, limit, direction);\n return constantRef(state);\n },\n remove(id: string) {\n const state = tableRef.remove(id);\n return constantRef(state);\n },\n seed(seeds: R[]) {\n tableRef.seed(seeds);\n return this;\n },\n table() {\n return tableRef;\n },\n };\n}\n"]}
@@ -1,16 +0,0 @@
1
- type StateRef<T> = {
2
- value: T;
3
- };
4
- type ConstantRef<T> = {
5
- get value(): T;
6
- publish(): void;
7
- subscribe(fn: RefSubscriber<T>): RefUnsubscribe;
8
- };
9
- type VariableRef<T> = ConstantRef<T> & {
10
- set(value: T): void;
11
- set value(value: T);
12
- };
13
- type RefSubscriber<T> = (current: T) => void;
14
- type RefUnsubscribe = () => void;
15
-
16
- export type { ConstantRef as C, RefSubscriber as R, StateRef as S, VariableRef as V, RefUnsubscribe as a };