@deijose/nix-js 2.2.0 → 2.2.1
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/plugins.d.ts +16 -0
- package/dist/lib/nix/store.d.ts +11 -5
- package/dist/lib/store.cjs +1 -1
- package/dist/lib/store.js +1 -1
- package/package.json +1 -1
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { type NixPlugin, type Store } from "./store";
|
|
2
|
+
export declare function persistPlugin<T extends Record<string, unknown>>(storageKey: string, opts?: {
|
|
3
|
+
/** Keys a excluir del storage (ej: tokens de sesión efímeros). */
|
|
4
|
+
exclude?: Array<keyof T>;
|
|
5
|
+
/** Serialización custom. Por defecto JSON.stringify. */
|
|
6
|
+
serialize?: (state: T) => string;
|
|
7
|
+
deserialize?: (raw: string) => Partial<T>;
|
|
8
|
+
}): NixPlugin<T>;
|
|
9
|
+
export declare function loggerPlugin<T extends Record<string, unknown>>(opts?: {
|
|
10
|
+
collapsed?: boolean;
|
|
11
|
+
filter?: (diff: Partial<T>) => boolean;
|
|
12
|
+
}): NixPlugin<T>;
|
|
13
|
+
type GuardFn<T extends Record<string, unknown>> = (next: Partial<T>, current: T) => Partial<T> | void;
|
|
14
|
+
export declare function guardPlugin<T extends Record<string, unknown>>(guards: GuardFn<T>[]): NixPlugin<T>;
|
|
15
|
+
export declare function bridgePlugin<TA extends Record<string, unknown>, TB extends Record<string, unknown>>(sourceStore: Store<TB>, sync: (sourceState: TB, targetStore: Store<TA>) => void): NixPlugin<TA>;
|
|
16
|
+
export {};
|
package/dist/lib/nix/store.d.ts
CHANGED
|
@@ -2,10 +2,10 @@ import { Signal, type WatchOptions } from "./reactivity";
|
|
|
2
2
|
export type StoreSignals<T extends Record<string, unknown>> = {
|
|
3
3
|
readonly [K in keyof T]: Signal<T[K]>;
|
|
4
4
|
};
|
|
5
|
-
export
|
|
6
|
-
readonly
|
|
7
|
-
|
|
8
|
-
}
|
|
5
|
+
export declare class ReadonlySignal<T> extends Signal<T> {
|
|
6
|
+
private readonly label;
|
|
7
|
+
constructor(source: Signal<T>, label?: string);
|
|
8
|
+
}
|
|
9
9
|
export type StoreGetters<G extends Record<string, Signal<unknown>>> = {
|
|
10
10
|
readonly [K in keyof G]: ReadonlySignal<G[K] extends Signal<infer V> ? V : never>;
|
|
11
11
|
};
|
|
@@ -48,7 +48,13 @@ export type Store<T extends Record<string, unknown>, A extends Record<string, un
|
|
|
48
48
|
*/
|
|
49
49
|
export type NixPlugin<T extends Record<string, unknown>, A extends Record<string, unknown> = Record<never, never>, G extends Record<string, Signal<unknown>> = Record<never, never>> = (store: Store<T, A, G>) => (() => void) | void;
|
|
50
50
|
export interface CreateStoreOptions<T extends Record<string, unknown>, A extends Record<string, unknown> = Record<never, never>, G extends Record<string, Signal<unknown>> = Record<never, never>> {
|
|
51
|
+
/** Display name for the store. Used in error messages and devtools. */
|
|
51
52
|
name?: string;
|
|
53
|
+
/** Factory that receives the raw signals and returns action methods. */
|
|
54
|
+
actions?: (signals: StoreSignals<T>) => A;
|
|
55
|
+
/** Factory that receives the raw signals and returns computed getters. */
|
|
56
|
+
getters?: (signals: StoreSignals<T>) => G;
|
|
57
|
+
/** Plugins to extend the store. Each receives the assembled store. */
|
|
52
58
|
plugins?: NixPlugin<T, A, G>[];
|
|
53
59
|
}
|
|
54
|
-
export declare function createStore<T extends Record<string, unknown>, A extends Record<string, unknown> = Record<never, never>, G extends Record<string, Signal<unknown>> = Record<never, never>>(initialState: T,
|
|
60
|
+
export declare function createStore<T extends Record<string, unknown>, A extends Record<string, unknown> = 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=new Set(["$id","$state","$stateSignal","$reset","$patch","$watch","$dispose"]);function
|
|
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={}){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,b=d,$=e.computed(()=>{let e={};for(let t of u)e[t]=d[t].value;return e}),h=a($,`store "${l}".$stateSignal`);try{g=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),b,{$reset:function(){e.batch(()=>{for(let e of u)d[e].value=g[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($,t,r)}});Object.defineProperty(p,"$id",{value:l,writable:!1,enumerable:!1,configurable:!1}),Object.defineProperty(p,"$state",{get:()=>$.value,enumerable:!0,configurable:!1}),Object.defineProperty(p,"$stateSignal",{value:h,writable:!1,enumerable:!1,configurable:!1});let y=new Set([...u,...Array.from(n)]);if(s){let e=s(b);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(b);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=[()=>$.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=new Set(["$id","$state","$stateSignal","$reset","$patch","$watch","$dispose"]);function
|
|
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={}){let{name:u="store",actions:d,getters:$,plugins:g=[]}=f,h=Object.keys(a),b={};for(let e of h)s(e),b[e]=r(a[e]);let p,w=b,y=n(()=>{let e={};for(let t of h)e[t]=b[t].value;return e}),S=l(y,`store "${u}".$stateSignal`);try{p=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 h)b[e].value=p[e]})},$patch:function(e){t(()=>{for(let t of Object.keys(e))Object.prototype.hasOwnProperty.call(b,t)&&(b[t].value=e[t])})},$watch:function(e,t){return i(y,e,t)}});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([...h,...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 g)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