@deijose/nix-js 2.2.2 → 2.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/lib/nix/store.d.ts +94 -29
- package/dist/lib/store.cjs +1 -1
- package/dist/lib/store.js +1 -1
- package/package.json +1 -1
package/dist/lib/nix/store.d.ts
CHANGED
|
@@ -1,60 +1,125 @@
|
|
|
1
1
|
import { Signal, type WatchOptions } from "./reactivity";
|
|
2
|
-
|
|
2
|
+
/**
|
|
3
|
+
* Maps a plain state object T into a record of Signals — one per key.
|
|
4
|
+
* This is what the actions/getters factories receive as their argument.
|
|
5
|
+
*
|
|
6
|
+
* Given: { count: number, name: string }
|
|
7
|
+
* Becomes: { count: Signal<number>, name: Signal<string> }
|
|
8
|
+
*/
|
|
9
|
+
export type StoreSignals<T extends object> = {
|
|
3
10
|
readonly [K in keyof T]: Signal<T[K]>;
|
|
4
11
|
};
|
|
12
|
+
/**
|
|
13
|
+
* A read-only Signal — extends Signal so it satisfies `instanceof Signal`
|
|
14
|
+
* checks (used by `watch()`), but throws on any mutation attempt.
|
|
15
|
+
*/
|
|
5
16
|
export declare class ReadonlySignal<T> extends Signal<T> {
|
|
6
17
|
private readonly label;
|
|
7
18
|
constructor(source: Signal<T>, label?: string);
|
|
8
19
|
}
|
|
20
|
+
/**
|
|
21
|
+
* Maps a getters factory result (a record of Signals) into a record of
|
|
22
|
+
* read-only Signals exposed on the store.
|
|
23
|
+
*
|
|
24
|
+
* The Omit pattern enforces `readonly value` at the type level — TypeScript
|
|
25
|
+
* blocks `getter.value = x` at compile time. The runtime ReadonlySignal class
|
|
26
|
+
* also throws on mutation as defense in depth.
|
|
27
|
+
*/
|
|
9
28
|
export type StoreGetters<G extends Record<string, Signal<unknown>>> = {
|
|
10
|
-
readonly [K in keyof G]: ReadonlySignal<G[K] extends Signal<infer V> ? V : never
|
|
29
|
+
readonly [K in keyof G]: Omit<ReadonlySignal<G[K] extends Signal<infer V> ? V : never>, "value"> & {
|
|
30
|
+
readonly value: G[K] extends Signal<infer V> ? V : never;
|
|
31
|
+
};
|
|
11
32
|
};
|
|
12
|
-
|
|
33
|
+
/**
|
|
34
|
+
* The full store type — combines reactive state signals, action methods,
|
|
35
|
+
* computed getters (as ReadonlySignals), and the framework-level $-prefixed API.
|
|
36
|
+
*/
|
|
37
|
+
export type Store<T extends object, A extends object = Record<never, never>, G extends Record<string, Signal<unknown>> = Record<never, never>> = StoreSignals<T> & A & StoreGetters<G> & {
|
|
13
38
|
readonly $id: string;
|
|
14
|
-
/**
|
|
39
|
+
/** Reactive snapshot — reading inside effect/computed creates a subscription to the whole state. */
|
|
15
40
|
readonly $state: T;
|
|
16
41
|
/**
|
|
17
|
-
*
|
|
18
|
-
*
|
|
19
|
-
*
|
|
20
|
-
|
|
42
|
+
* Passive snapshot — returns the current state values WITHOUT creating
|
|
43
|
+
* a reactive subscription. Use this in plugins, loggers, persistence,
|
|
44
|
+
* or anywhere you need a one-shot read.
|
|
45
|
+
*/
|
|
46
|
+
$snapshot(): T;
|
|
47
|
+
/**
|
|
48
|
+
* The computed Signal that backs $state. Plugins receive this to
|
|
49
|
+
* compose new reactive nodes on top.
|
|
21
50
|
*/
|
|
22
51
|
readonly $stateSignal: ReadonlySignal<T>;
|
|
23
52
|
/** Reset to initial values (batched). */
|
|
24
53
|
$reset(): void;
|
|
25
54
|
/** Partial update (batched). */
|
|
26
55
|
$patch(partial: Partial<T>): void;
|
|
27
|
-
/**
|
|
28
|
-
* Watches state changes. This is exactly watch() from reactivity.ts —
|
|
29
|
-
* no new primitive to learn.
|
|
30
|
-
*/
|
|
56
|
+
/** Watches state changes. Equivalent to `watch(store.$stateSignal, cb, opts)`. */
|
|
31
57
|
$watch(cb: (next: T, prev: T | undefined) => void, options?: WatchOptions): () => void;
|
|
32
58
|
/** Disposes the store and runs all plugin cleanups. */
|
|
33
59
|
$dispose(): void;
|
|
34
60
|
};
|
|
61
|
+
export type ActionsFactory<T extends object, A extends object> = (signals: StoreSignals<T>) => A;
|
|
62
|
+
export type GettersFactory<T extends object, G extends Record<string, Signal<unknown>>> = (signals: StoreSignals<T>) => G;
|
|
35
63
|
/**
|
|
36
64
|
* A NixPlugin is a function that receives the assembled store and
|
|
37
|
-
* optionally returns a cleanup function.
|
|
65
|
+
* optionally returns a cleanup function called on $dispose().
|
|
38
66
|
*
|
|
39
|
-
* There are NO lifecycle hooks.
|
|
67
|
+
* There are NO lifecycle hooks. Plugins extend the signal graph directly
|
|
68
|
+
* using the framework primitives:
|
|
40
69
|
*
|
|
41
70
|
* watch(store.$stateSignal, ...) — react to any state change
|
|
42
71
|
* computed(() => store.someSignal.value) — derive new nodes
|
|
43
|
-
* store
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
*
|
|
72
|
+
* store.$snapshot() — passive read for logging/persistence
|
|
73
|
+
*/
|
|
74
|
+
export type NixPlugin<T extends object, A extends object = Record<never, never>, G extends Record<string, Signal<unknown>> = Record<never, never>> = (store: Store<T, A, G>) => (() => void) | void;
|
|
75
|
+
/**
|
|
76
|
+
* Options object for createStore. NoInfer<T> on factory parameters ensures
|
|
77
|
+
* T is inferred ONLY from initialState, never from the factories — this is
|
|
78
|
+
* what restores the type inference that broke in v2.2.1.
|
|
48
79
|
*/
|
|
49
|
-
export type
|
|
50
|
-
|
|
51
|
-
/** Display name for the store. Used in error messages and devtools. */
|
|
80
|
+
export type CreateStoreOptions<T extends object, A extends object, G extends Record<string, Signal<unknown>>> = {
|
|
81
|
+
/** Display name. Used in error messages, devtools, and $id. */
|
|
52
82
|
name?: string;
|
|
53
|
-
/**
|
|
54
|
-
actions?: (signals: StoreSignals<T
|
|
55
|
-
/**
|
|
56
|
-
getters?: (signals: StoreSignals<T
|
|
83
|
+
/** Action factory — receives raw signals, returns methods exposed on the store. */
|
|
84
|
+
actions?: (signals: StoreSignals<NoInfer<T>>) => A;
|
|
85
|
+
/** Getter factory — receives raw signals, returns computed Signals exposed as ReadonlySignals. */
|
|
86
|
+
getters?: (signals: StoreSignals<NoInfer<T>>) => G;
|
|
57
87
|
/** Plugins to extend the store. Each receives the assembled store. */
|
|
58
|
-
plugins?: NixPlugin<T
|
|
59
|
-
}
|
|
60
|
-
|
|
88
|
+
plugins?: NixPlugin<NoInfer<T>, A, G>[];
|
|
89
|
+
};
|
|
90
|
+
/**
|
|
91
|
+
* Creates a reactive store with optional actions, getters, and plugins.
|
|
92
|
+
*
|
|
93
|
+
* @example Just state
|
|
94
|
+
* ```ts
|
|
95
|
+
* const counter = createStore({ count: 0 });
|
|
96
|
+
* counter.count.value++;
|
|
97
|
+
* ```
|
|
98
|
+
*
|
|
99
|
+
* @example State + actions
|
|
100
|
+
* ```ts
|
|
101
|
+
* const counter = createStore({ count: 0 }, {
|
|
102
|
+
* actions: (s) => ({ increment: () => s.count.value++ }),
|
|
103
|
+
* });
|
|
104
|
+
* counter.increment();
|
|
105
|
+
* ```
|
|
106
|
+
*
|
|
107
|
+
* @example State + getters (no actions)
|
|
108
|
+
* ```ts
|
|
109
|
+
* const inventory = createStore({ items: [] as Item[] }, {
|
|
110
|
+
* getters: (s) => ({ count: computed(() => s.items.value.length) }),
|
|
111
|
+
* });
|
|
112
|
+
* inventory.count.value;
|
|
113
|
+
* ```
|
|
114
|
+
*
|
|
115
|
+
* @example Full store
|
|
116
|
+
* ```ts
|
|
117
|
+
* const cart = createStore({ items: [] as Item[], discount: 0 }, {
|
|
118
|
+
* name: "cart",
|
|
119
|
+
* actions: (s) => ({ addItem: (i: Item) => { s.items.value = [...s.items.value, i] } }),
|
|
120
|
+
* getters: (s) => ({ total: computed(() => s.items.value.reduce((sum, i) => sum + i.price, 0) * (1 - s.discount.value)) }),
|
|
121
|
+
* plugins: [persistPlugin({ key: "cart" })],
|
|
122
|
+
* });
|
|
123
|
+
* ```
|
|
124
|
+
*/
|
|
125
|
+
export declare function createStore<T extends object, A extends object = Record<never, never>, G extends Record<string, Signal<unknown>> = Record<never, never>>(initialState: T, options?: CreateStoreOptions<T, A, G>): Store<T, A, G>;
|
package/dist/lib/store.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const e=require("./signals.cjs");var t=class extends e.Signal{label;constructor(e,t="ReadonlySignal"){super(e.peek()),this.label=t,Object.defineProperty(this,"value",{get:()=>e.value,set:()=>{throw Error(`[Nix] "${this.label}" is read-only.`)},configurable:!1}),this.update=()=>{throw Error(`[Nix] "${this.label}" is read-only.`)},this.dispose=()=>{throw Error(`[Nix] Cannot dispose "${this.label}" directly.`)}}},n=new Set(["$id","$state","$stateSignal","$reset","$patch","$watch","$dispose"]);function r(e){if("__proto__"===e||"constructor"===e||"prototype"===e)throw Error(`[Nix] Store key "${e}" is not allowed for security reasons.`);if(n.has(e))throw Error(`[Nix] Store key "${e}" is reserved.`)}function i(e,t){return!n.has(e)||(console.warn(`[Nix] Store ${t} "${e}" is reserved and will be ignored.`),!1)}function a(e,r){return new t(e,r)}function o(t,o
|
|
1
|
+
Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const e=require("./signals.cjs");var t=class extends e.Signal{label;constructor(e,t="ReadonlySignal"){super(e.peek()),this.label=t,Object.defineProperty(this,"value",{get:()=>e.value,set:()=>{throw Error(`[Nix] "${this.label}" is read-only.`)},configurable:!1}),this.update=()=>{throw Error(`[Nix] "${this.label}" is read-only.`)},this.dispose=()=>{throw Error(`[Nix] Cannot dispose "${this.label}" directly.`)}}},n=new Set(["$id","$state","$stateSignal","$snapshot","$reset","$patch","$watch","$dispose"]);function r(e){if("__proto__"===e||"constructor"===e||"prototype"===e)throw Error(`[Nix] Store key "${e}" is not allowed for security reasons.`);if(n.has(e))throw Error(`[Nix] Store key "${e}" is reserved.`)}function i(e,t){return!n.has(e)||(console.warn(`[Nix] Store ${t} "${e}" is reserved and will be ignored.`),!1)}function a(e,r){return new t(e,r)}function o(t,o){let{name:l="store",actions:s,getters:c,plugins:f=[]}=o??{},u=Object.keys(t),d={};for(let o of u)r(o),d[o]=e.signal(t[o]);let $,g=d,h=e.computed(()=>{let e={};for(let t of u)e[t]=d[t].value;return e}),b=a(h,`store "${l}".$stateSignal`);try{$=structuredClone(t)}catch(e){throw Error(`[Nix] Store "${l}" initialState contains non-serializable data (functions, DOM nodes, Symbols, or WeakRefs). Remove these before creating the store. Original error: ${e}`)}let p=Object.assign(Object.create(null),g,{$reset:function(){e.batch(()=>{for(let e of u)d[e].value=$[e]})},$patch:function(t){e.batch(()=>{for(let e of Object.keys(t))Object.prototype.hasOwnProperty.call(d,e)&&(d[e].value=t[e])})},$watch:function(t,r){return e.watch(h,t,r)},$snapshot:function(){let e={};for(let t of u)e[t]=d[t].peek();return e}});Object.defineProperty(p,"$id",{value:l,writable:!1,enumerable:!1,configurable:!1}),Object.defineProperty(p,"$state",{get:()=>h.value,enumerable:!0,configurable:!1}),Object.defineProperty(p,"$stateSignal",{value:b,writable:!1,enumerable:!1,configurable:!1});let y=new Set([...u,...Array.from(n)]);if(s){let e=s(g);for(let t of Object.keys(e))if(i(t,"action")){if(y.has(t)){console.warn(`[Nix] Store "${l}": action "${t}" collides with an existing signal or getter and will be ignored.`);continue}y.add(t),p[t]=e[t]}}if(c){let t=c(g);for(let r of Object.keys(t)){if(!i(r,"getter"))continue;if(y.has(r)){console.warn(`[Nix] Store "${l}": getter "${r}" collides with an existing signal or action and will be ignored.`);continue}let o=t[r];if(!(o instanceof e.Signal))throw TypeError(`[Nix] Store "${l}": getter "${r}" must return a Signal (wrap it with computed()). Got: ${typeof o}`);y.add(r),p[r]=a(o,`getter "${r}" in store "${l}"`)}}let w=[()=>h.dispose()];for(let e of f)try{let t=e(p);"function"==typeof t&&w.push(t)}catch(e){console.error(`[Nix] Plugin initialization failed for store "${l}":`,e)}return p.$dispose=()=>{for(let e of w)e()},p}exports.ReadonlySignal=t,exports.createStore=o;
|
package/dist/lib/store.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{Signal as e,batch as t,computed as n,signal as r,watch as i}from"./signals.js";var a=class extends e{label;constructor(e,t="ReadonlySignal"){super(e.peek()),this.label=t,Object.defineProperty(this,"value",{get:()=>e.value,set:()=>{throw Error(`[Nix] "${this.label}" is read-only.`)},configurable:!1}),this.update=()=>{throw Error(`[Nix] "${this.label}" is read-only.`)},this.dispose=()=>{throw Error(`[Nix] Cannot dispose "${this.label}" directly.`)}}},o=new Set(["$id","$state","$stateSignal","$reset","$patch","$watch","$dispose"]);function s(e){if("__proto__"===e||"constructor"===e||"prototype"===e)throw Error(`[Nix] Store key "${e}" is not allowed for security reasons.`);if(o.has(e))throw Error(`[Nix] Store key "${e}" is reserved.`)}function c(e,t){return!o.has(e)||(console.warn(`[Nix] Store ${t} "${e}" is reserved and will be ignored.`),!1)}function l(e,t){return new a(e,t)}function u(a,f
|
|
1
|
+
import{Signal as e,batch as t,computed as n,signal as r,watch as i}from"./signals.js";var a=class extends e{label;constructor(e,t="ReadonlySignal"){super(e.peek()),this.label=t,Object.defineProperty(this,"value",{get:()=>e.value,set:()=>{throw Error(`[Nix] "${this.label}" is read-only.`)},configurable:!1}),this.update=()=>{throw Error(`[Nix] "${this.label}" is read-only.`)},this.dispose=()=>{throw Error(`[Nix] Cannot dispose "${this.label}" directly.`)}}},o=new Set(["$id","$state","$stateSignal","$snapshot","$reset","$patch","$watch","$dispose"]);function s(e){if("__proto__"===e||"constructor"===e||"prototype"===e)throw Error(`[Nix] Store key "${e}" is not allowed for security reasons.`);if(o.has(e))throw Error(`[Nix] Store key "${e}" is reserved.`)}function c(e,t){return!o.has(e)||(console.warn(`[Nix] Store ${t} "${e}" is reserved and will be ignored.`),!1)}function l(e,t){return new a(e,t)}function u(a,f){let{name:u="store",actions:d,getters:$,plugins:h=[]}=f??{},g=Object.keys(a),p={};for(let e of g)s(e),p[e]=r(a[e]);let b,w=p,y=n(()=>{let e={};for(let t of g)e[t]=p[t].value;return e}),S=l(y,`store "${u}".$stateSignal`);try{b=structuredClone(a)}catch(e){throw Error(`[Nix] Store "${u}" initialState contains non-serializable data (functions, DOM nodes, Symbols, or WeakRefs). Remove these before creating the store. Original error: ${e}`)}let x=Object.assign(Object.create(null),w,{$reset:function(){t(()=>{for(let e of g)p[e].value=b[e]})},$patch:function(e){t(()=>{for(let t of Object.keys(e))Object.prototype.hasOwnProperty.call(p,t)&&(p[t].value=e[t])})},$watch:function(e,t){return i(y,e,t)},$snapshot:function(){let e={};for(let t of g)e[t]=p[t].peek();return e}});Object.defineProperty(x,"$id",{value:u,writable:!1,enumerable:!1,configurable:!1}),Object.defineProperty(x,"$state",{get:()=>y.value,enumerable:!0,configurable:!1}),Object.defineProperty(x,"$stateSignal",{value:S,writable:!1,enumerable:!1,configurable:!1});let O=new Set([...g,...Array.from(o)]);if(d){let e=d(w);for(let t of Object.keys(e))if(c(t,"action")){if(O.has(t)){console.warn(`[Nix] Store "${u}": action "${t}" collides with an existing signal or getter and will be ignored.`);continue}O.add(t),x[t]=e[t]}}if($){let t=$(w);for(let r of Object.keys(t)){if(!c(r,"getter"))continue;if(O.has(r)){console.warn(`[Nix] Store "${u}": getter "${r}" collides with an existing signal or action and will be ignored.`);continue}let o=t[r];if(!(o instanceof e))throw TypeError(`[Nix] Store "${u}": getter "${r}" must return a Signal (wrap it with computed()). Got: ${typeof o}`);O.add(r),x[r]=l(o,`getter "${r}" in store "${u}"`)}}let j=[()=>y.dispose()];for(let e of h)try{let t=e(x);"function"==typeof t&&j.push(t)}catch(e){console.error(`[Nix] Plugin initialization failed for store "${u}":`,e)}return x.$dispose=()=>{for(let e of j)e()},x}export{a as ReadonlySignal,u as createStore};
|
package/package.json
CHANGED