@storix-js/vuex 0.1.0 → 0.1.2
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/README.md +46 -0
- package/dist/index.cjs +1 -1
- package/dist/index.d.ts +23 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +440 -374
- package/package.json +9 -2
- package/src/index.ts +0 -91
- package/tsconfig.build.json +0 -8
- package/tsconfig.json +0 -4
- package/vite.config.ts +0 -15
package/README.md
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
# @storix-js/vuex
|
|
2
|
+
|
|
3
|
+
Vuex bridge helpers for Storix.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @storix-js/core @storix-js/vuex
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
```ts
|
|
14
|
+
import { createVuexBridge, applyVuexPatchMutation } from '@storix-js/vuex';
|
|
15
|
+
|
|
16
|
+
const bridge = createVuexBridge({
|
|
17
|
+
store: useAppStore(),
|
|
18
|
+
vuex: vuexStore,
|
|
19
|
+
mutationType: 'storix/APPLY_PATCH',
|
|
20
|
+
immutable: true,
|
|
21
|
+
outboundRoutes: {
|
|
22
|
+
todos: {
|
|
23
|
+
mode: 'dispatch',
|
|
24
|
+
type: 'syncTodosFromStorix',
|
|
25
|
+
mapPayload: ({ storixSnapshot }) => storixSnapshot.todos
|
|
26
|
+
},
|
|
27
|
+
selectedId: {
|
|
28
|
+
mode: 'commit',
|
|
29
|
+
type: 'SET_SELECTED_ID',
|
|
30
|
+
mapPayload: ({ storixSnapshot }) => storixSnapshot.selectedId
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
});
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
## v1 routing support
|
|
37
|
+
|
|
38
|
+
`createVuexBridge()` supports a minimal v1 routing model for Storix -> Vuex writes:
|
|
39
|
+
|
|
40
|
+
- default patch mode via `mutationType`
|
|
41
|
+
- per-key outbound routes with `patch`, `commit`, `dispatch`, or `custom`
|
|
42
|
+
- optional bridge-echo suppression via `suppressBridgeEcho`
|
|
43
|
+
|
|
44
|
+
This keeps simple integrations patch-based while allowing special keys to flow through Vuex business logic.
|
|
45
|
+
|
|
46
|
+
Use `applyVuexPatchMutation()` inside your Vuex mutation to apply the incoming patch payload.
|
package/dist/index.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});var vt=Symbol.for("preact-signals");function D(){if($>1)$--;else{for(var e,t=!1;z!==void 0;){var n=z;for(z=void 0,Z++;n!==void 0;){var o=n.o;if(n.o=void 0,n.f&=-3,!(8&n.f)&&st(n))try{n.c()}catch(c){t||(e=c,t=!0)}n=o}}if(Z=0,$--,t)throw e}}function L(e){if($>0)return e();$++;try{return e()}finally{D()}}var l=void 0;function rt(e){var t=l;l=void 0;try{return e()}finally{l=t}}var z=void 0,$=0,Z=0,q=0;function ot(e){if(l!==void 0){var t=e.n;if(t===void 0||t.t!==l)return t={i:0,S:e,p:l.s,n:void 0,t:l,e:void 0,x:void 0,r:t},l.s!==void 0&&(l.s.n=t),l.s=t,e.n=t,32&l.f&&e.S(t),t;if(t.i===-1)return t.i=0,t.n!==void 0&&(t.n.p=t.p,t.p!==void 0&&(t.p.n=t.n),t.p=l.s,t.n=void 0,l.s.n=t,l.s=t),t}}function b(e,t){this.v=e,this.i=0,this.n=void 0,this.t=void 0,this.W=t==null?void 0:t.watched,this.Z=t==null?void 0:t.unwatched,this.name=t==null?void 0:t.name}b.prototype.brand=vt;b.prototype.h=function(){return!0};b.prototype.S=function(e){var t=this,n=this.t;n!==e&&e.e===void 0&&(e.x=n,this.t=e,n!==void 0?n.e=e:rt(function(){var o;(o=t.W)==null||o.call(t)}))};b.prototype.U=function(e){var t=this;if(this.t!==void 0){var n=e.e,o=e.x;n!==void 0&&(n.x=o,e.e=void 0),o!==void 0&&(o.e=n,e.x=void 0),e===this.t&&(this.t=o,o===void 0&&rt(function(){var c;(c=t.Z)==null||c.call(t)}))}};b.prototype.subscribe=function(e){var t=this;return pt(function(){var n=t.value,o=l;l=void 0;try{e(n)}finally{l=o}},{name:"sub"})};b.prototype.valueOf=function(){return this.value};b.prototype.toString=function(){return this.value+""};b.prototype.toJSON=function(){return this.value};b.prototype.peek=function(){var e=l;l=void 0;try{return this.value}finally{l=e}};Object.defineProperty(b.prototype,"value",{get:function(){var e=ot(this);return e!==void 0&&(e.i=this.i),this.v},set:function(e){if(e!==this.v){if(Z>100)throw new Error("Cycle detected");this.v=e,this.i++,q++,$++;try{for(var t=this.t;t!==void 0;t=t.x)t.t.N()}finally{D()}}}});function dt(e,t){return new b(e,t)}function st(e){for(var t=e.s;t!==void 0;t=t.n)if(t.S.i!==t.i||!t.S.h()||t.S.i!==t.i)return!0;return!1}function at(e){for(var t=e.s;t!==void 0;t=t.n){var n=t.S.n;if(n!==void 0&&(t.r=n),t.S.n=t,t.i=-1,t.n===void 0){e.s=t;break}}}function ft(e){for(var t=e.s,n=void 0;t!==void 0;){var o=t.p;t.i===-1?(t.S.U(t),o!==void 0&&(o.n=t.n),t.n!==void 0&&(t.n.p=o)):n=t,t.S.n=t.r,t.r!==void 0&&(t.r=void 0),t=o}e.s=n}function N(e,t){b.call(this,void 0),this.x=e,this.s=void 0,this.g=q-1,this.f=4,this.W=t==null?void 0:t.watched,this.Z=t==null?void 0:t.unwatched,this.name=t==null?void 0:t.name}N.prototype=new b;N.prototype.h=function(){if(this.f&=-3,1&this.f)return!1;if((36&this.f)==32||(this.f&=-5,this.g===q))return!0;if(this.g=q,this.f|=1,this.i>0&&!st(this))return this.f&=-2,!0;var e=l;try{at(this),l=this;var t=this.x();(16&this.f||this.v!==t||this.i===0)&&(this.v=t,this.f&=-17,this.i++)}catch(n){this.v=n,this.f|=16,this.i++}return l=e,ft(this),this.f&=-2,!0};N.prototype.S=function(e){if(this.t===void 0){this.f|=36;for(var t=this.s;t!==void 0;t=t.n)t.S.S(t)}b.prototype.S.call(this,e)};N.prototype.U=function(e){if(this.t!==void 0&&(b.prototype.U.call(this,e),this.t===void 0)){this.f&=-33;for(var t=this.s;t!==void 0;t=t.n)t.S.U(t)}};N.prototype.N=function(){if(!(2&this.f)){this.f|=6;for(var e=this.t;e!==void 0;e=e.x)e.t.N()}};Object.defineProperty(N.prototype,"value",{get:function(){if(1&this.f)throw new Error("Cycle detected");var e=ot(this);if(this.h(),e!==void 0&&(e.i=this.i),16&this.f)throw this.v;return this.v}});function tt(e,t){return new N(e,t)}function ut(e){var t=e.u;if(e.u=void 0,typeof t=="function"){$++;var n=l;l=void 0;try{t()}catch(o){throw e.f&=-2,e.f|=8,K(e),o}finally{l=n,D()}}}function K(e){for(var t=e.s;t!==void 0;t=t.n)t.S.U(t);e.x=void 0,e.s=void 0,ut(e)}function yt(e){if(l!==this)throw new Error("Out-of-order effect");ft(this),l=e,this.f&=-2,8&this.f&&K(this),D()}function A(e,t){this.x=e,this.u=void 0,this.s=void 0,this.o=void 0,this.f=32,this.name=t==null?void 0:t.name}A.prototype.c=function(){var e=this.S();try{if(8&this.f||this.x===void 0)return;var t=this.x();typeof t=="function"&&(this.u=t)}finally{e()}};A.prototype.S=function(){if(1&this.f)throw new Error("Cycle detected");this.f|=1,this.f&=-9,ut(this),at(this),$++;var e=l;return l=this,yt.bind(this,e)};A.prototype.N=function(){2&this.f||(this.f|=2,this.o=z,z=this)};A.prototype.d=function(){this.f|=8,1&this.f||K(this)};A.prototype.dispose=function(){this.d()};function pt(e,t){var n=new A(e,t);try{n.c()}catch(c){throw n.d(),c}var o=n.d.bind(n);return o[Symbol.dispose]=o,o}const et=e=>e!==null&&typeof e=="object"&&!Array.isArray(e);function H(e,t){if(Object.is(e,t))return!0;if(e instanceof Date&&t instanceof Date)return e.getTime()===t.getTime();if(Array.isArray(e)&&Array.isArray(t)){if(e.length!==t.length)return!1;for(let n=0;n<e.length;n+=1)if(!H(e[n],t[n]))return!1;return!0}if(et(e)&&et(t)){const n=Object.keys(e),o=Object.keys(t);if(n.length!==o.length)return!1;for(const c of n)if(!(c in t)||!H(e[c],t[c]))return!1;return!0}return!1}function Y(e){if(typeof structuredClone=="function")return structuredClone(e);try{return JSON.parse(JSON.stringify(e))}catch{return e}}function nt(e,t,n=Object.is,o=Y){const c={};for(const[h,d]of Object.entries(t)){const S=e[h];n(S,d,h)||(c[h]=o(d))}return c}function gt(e){const t=e.projectExternalToStore??(y=>y),n=e.projectStoreToExternal??(y=>y),o=e.equals??H,c=e.clone??Y,h=e.externalMutationOrigin??"local";let d=!1,S=!1,E=!1;const C=y=>Object.keys(y).length>0,R=()=>{if(d||E)return;const y=c(t(e.getExternalState())),U=e.store.$snapshot(),j=nt(U,y,o,c);if(C(j)){S=!0;try{e.store.$patch(j,h)}finally{S=!1}}},m=()=>{if(d||S)return;const y=c(n(e.store.$snapshot())),U=c(t(e.getExternalState())),j=nt(U,y,o,c);if(C(j)){E=!0;try{e.applyExternalPatch(j)}finally{E=!1}}},F=e.subscribeExternal(R),p=e.store.$subscribe(()=>{m()});return R(),{dispose(){d||(d=!0,F(),p())},syncFromExternal:R,syncFromStore:m}}function bt(e={}){const t=(e.namespace??"default").trim()||"default",n=e.plugins??[],o=new Map;function c(h,d){return function(){var S;const E=`${t}:${h}`,C=o.get(h);if(C)return C;const R=d.state(),m=d.modules??{},F={...R};for(const[r,i]of Object.entries(m))F[r]=i.state();const p=dt(F),y=new Map,U=new Map,j=new Map,T=new Map,O=new Map,W=new Set,V=new Set(Object.keys(R)),J=new Set(Object.keys(d.getters??{})),B=new Set(Object.keys(d.actions??{})),k=r=>p.value[r]??{},lt=(r,i)=>{p.value={...p.value,[r]:i}},ht=new Proxy({},{get(r,i){var a,f;if(y.has(i))return(a=y.get(i))==null?void 0:a.value;if(O.has(i))return(f=O.get(i))==null?void 0:f.value;if(V.has(i))return p.value[i];const v=T.get(i);return v?k(v.moduleName)[v.key]:p.value[i]}});for(const[r,i]of Object.entries(d.getters??{}))y.set(r,tt(()=>i(p.value,ht)));const _=(r,i)=>{var a;for(const f of W)f(r);for(const f of n)(a=f.onMutation)==null||a.call(f,i,r)},G=(r,i,a,f)=>{const v=p.value[r];Object.is(v,i)||(p.value={...p.value,[r]:i},_({namespace:t,scopeId:E,storeId:h,key:r,oldValue:v,newValue:i,origin:a},f))},I=(r,i,a,f,v)=>{const g=k(r),s=g[i];Object.is(s,a)||(lt(r,{...g,[i]:a}),_({namespace:t,scopeId:E,storeId:h,key:`${r}.${i}`,oldValue:s,newValue:a,origin:f},v))},M={$id:h,$namespace:t,$scopeId:E,$subscribe(r){return W.add(r),()=>W.delete(r)},$dispose(){var r;W.clear();for(const i of n)(r=i.onDispose)==null||r.call(i,x);o.delete(h)},$patch(r,i="local"){L(()=>{for(const[a,f]of Object.entries(r)){const v=T.get(a);if(v){I(v.moduleName,v.key,f,i,x);continue}if(a.includes(".")){const[g,s]=a.split(".",2);if(m[g]){I(g,s,f,i,x);continue}}if(m[a]&&f&&typeof f=="object"&&!Array.isArray(f)){for(const[g,s]of Object.entries(f))I(a,g,s,i,x);continue}G(a,f,i,x)}})},$snapshot(){return{...p.value}}};for(const[r,i]of Object.entries(m)){const a=new Map,f=new Map,v=new Proxy({},{get(s,u){var w,P,Q;if(a.has(u))return(w=a.get(u))==null?void 0:w.value;if(y.has(u))return(P=y.get(u))==null?void 0:P.value;if(O.has(u))return(Q=O.get(u))==null?void 0:Q.value;const X=k(r);if(u in X)return X[u];if(V.has(u))return p.value[u]}});for(const[s,u]of Object.entries(i.getters??{}))a.set(s,tt(()=>u(k(r),p.value,v)));let g;for(const[s,u]of Object.entries(i.actions??{}))f.set(s,(...w)=>L(()=>u.apply(g,w)));g=new Proxy({},{get(s,u){var w;if(f.has(u))return f.get(u);if(a.has(u))return(w=a.get(u))==null?void 0:w.value;const P=k(r);return u in P?P[u]:x[u]},set(s,u,w){if(f.has(u)||a.has(u))return!1;const P=k(r);return u in P?I(r,u,w,"local",x):x[u]=w,!0}}),j.set(r,g),U.set(r,a);for(const s of Object.keys(k(r)))!(V.has(s)||J.has(s)||B.has(s)||Reflect.has(M,s)||m[s]!==void 0)&&!T.has(s)&&T.set(s,{moduleName:r,key:s});for(const[s,u]of a.entries())!(V.has(s)||J.has(s)||B.has(s)||Reflect.has(M,s)||m[s]!==void 0)&&!O.has(s)&&O.set(s,u);for(const[s,u]of f.entries())!(V.has(s)||J.has(s)||B.has(s)||Reflect.has(M,s)||m[s]!==void 0)&&!Reflect.has(M,s)&&(M[s]=u)}const x=new Proxy(M,{get(r,i,a){var f,v;if(Reflect.has(r,i))return Reflect.get(r,i,a);if(j.has(i))return j.get(i);if(y.has(i))return(f=y.get(i))==null?void 0:f.value;if(O.has(i))return(v=O.get(i))==null?void 0:v.value;if(V.has(i))return p.value[i];const g=T.get(i);return g?k(g.moduleName)[g.key]:p.value[i]},set(r,i,a,f){if(Reflect.has(r,i))return Reflect.set(r,i,a,f);const v=T.get(i);return v?(I(v.moduleName,v.key,a,"local",f),!0):(G(i,a,"local",f),!0)}});for(const[r,i]of Object.entries(d.actions??{}))M[r]=(...a)=>L(()=>i.apply(x,a));for(const r of n)(S=r.onInit)==null||S.call(r,x);return o.set(h,x),x}}return{defineStore:c,use(h){n.push(h)}}}bt();const it=e=>e!==null&&typeof e=="object";function ct(e){if(!it(e))return e;const t=new WeakSet,n=o=>{if(!it(o))return;const c=o;if(t.has(c))return;t.add(c);const h=Reflect.ownKeys(c);for(const d of h){const S=Reflect.get(c,d);n(S)}Object.freeze(c)};return n(e),e}function xt(e,t){for(const[n,o]of Object.entries(t))e[n]=o}function St(e){const t=e.mutationType??"storix/APPLY_PATCH",n=e.immutable??!0,o=e.clone??Y,c=h=>{const d=o(h);return n?ct(d):d};return gt({store:e.store,getExternalState:()=>e.vuex.state,subscribeExternal:h=>e.vuex.subscribe(()=>h()),applyExternalPatch:h=>{e.vuex.commit(t,c(h))},projectExternalToStore:e.projectVuexToStorix,projectStoreToExternal:e.projectStorixToVuex,equals:e.equals,clone:c,externalMutationOrigin:e.externalMutationOrigin??"local"})}exports.applyVuexPatchMutation=xt;exports.createVuexBridge=St;exports.deepFreeze=ct;
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});var pt=Symbol.for("preact-signals");function H(){if(E>1)E--;else{for(var t,e=!1;J!==void 0;){var r=J;for(J=void 0,G++;r!==void 0;){var s=r.o;if(r.o=void 0,r.f&=-3,!(8&r.f)&&ct(r))try{r.c()}catch(h){e||(t=h,e=!0)}r=s}}if(G=0,E--,e)throw t}}function Y(t){if(E>0)return t();E++;try{return t()}finally{H()}}var l=void 0;function at(t){var e=l;l=void 0;try{return t()}finally{l=e}}var J=void 0,E=0,G=0,Z=0;function ut(t){if(l!==void 0){var e=t.n;if(e===void 0||e.t!==l)return e={i:0,S:t,p:l.s,n:void 0,t:l,e:void 0,x:void 0,r:e},l.s!==void 0&&(l.s.n=e),l.s=e,t.n=e,32&l.f&&t.S(e),e;if(e.i===-1)return e.i=0,e.n!==void 0&&(e.n.p=e.p,e.p!==void 0&&(e.p.n=e.n),e.p=l.s,e.n=void 0,l.s.n=e,l.s=e),e}}function S(t,e){this.v=t,this.i=0,this.n=void 0,this.t=void 0,this.W=e==null?void 0:e.watched,this.Z=e==null?void 0:e.unwatched,this.name=e==null?void 0:e.name}S.prototype.brand=pt;S.prototype.h=function(){return!0};S.prototype.S=function(t){var e=this,r=this.t;r!==t&&t.e===void 0&&(t.x=r,this.t=t,r!==void 0?r.e=t:at(function(){var s;(s=e.W)==null||s.call(e)}))};S.prototype.U=function(t){var e=this;if(this.t!==void 0){var r=t.e,s=t.x;r!==void 0&&(r.x=s,t.e=void 0),s!==void 0&&(s.e=r,t.x=void 0),t===this.t&&(this.t=s,s===void 0&&at(function(){var h;(h=e.Z)==null||h.call(e)}))}};S.prototype.subscribe=function(t){var e=this;return bt(function(){var r=e.value,s=l;l=void 0;try{t(r)}finally{l=s}},{name:"sub"})};S.prototype.valueOf=function(){return this.value};S.prototype.toString=function(){return this.value+""};S.prototype.toJSON=function(){return this.value};S.prototype.peek=function(){var t=l;l=void 0;try{return this.value}finally{l=t}};Object.defineProperty(S.prototype,"value",{get:function(){var t=ut(this);return t!==void 0&&(t.i=this.i),this.v},set:function(t){if(t!==this.v){if(G>100)throw new Error("Cycle detected");this.v=t,this.i++,Z++,E++;try{for(var e=this.t;e!==void 0;e=e.x)e.t.N()}finally{H()}}}});function yt(t,e){return new S(t,e)}function ct(t){for(var e=t.s;e!==void 0;e=e.n)if(e.S.i!==e.i||!e.S.h()||e.S.i!==e.i)return!0;return!1}function ft(t){for(var e=t.s;e!==void 0;e=e.n){var r=e.S.n;if(r!==void 0&&(e.r=r),e.S.n=e,e.i=-1,e.n===void 0){t.s=e;break}}}function lt(t){for(var e=t.s,r=void 0;e!==void 0;){var s=e.p;e.i===-1?(e.S.U(e),s!==void 0&&(s.n=e.n),e.n!==void 0&&(e.n.p=s)):r=e,e.S.n=e.r,e.r!==void 0&&(e.r=void 0),e=s}t.s=r}function B(t,e){S.call(this,void 0),this.x=t,this.s=void 0,this.g=Z-1,this.f=4,this.W=e==null?void 0:e.watched,this.Z=e==null?void 0:e.unwatched,this.name=e==null?void 0:e.name}B.prototype=new S;B.prototype.h=function(){if(this.f&=-3,1&this.f)return!1;if((36&this.f)==32||(this.f&=-5,this.g===Z))return!0;if(this.g=Z,this.f|=1,this.i>0&&!ct(this))return this.f&=-2,!0;var t=l;try{ft(this),l=this;var e=this.x();(16&this.f||this.v!==e||this.i===0)&&(this.v=e,this.f&=-17,this.i++)}catch(r){this.v=r,this.f|=16,this.i++}return l=t,lt(this),this.f&=-2,!0};B.prototype.S=function(t){if(this.t===void 0){this.f|=36;for(var e=this.s;e!==void 0;e=e.n)e.S.S(e)}S.prototype.S.call(this,t)};B.prototype.U=function(t){if(this.t!==void 0&&(S.prototype.U.call(this,t),this.t===void 0)){this.f&=-33;for(var e=this.s;e!==void 0;e=e.n)e.S.U(e)}};B.prototype.N=function(){if(!(2&this.f)){this.f|=6;for(var t=this.t;t!==void 0;t=t.x)t.t.N()}};Object.defineProperty(B.prototype,"value",{get:function(){if(1&this.f)throw new Error("Cycle detected");var t=ut(this);if(this.h(),t!==void 0&&(t.i=this.i),16&this.f)throw this.v;return this.v}});function rt(t,e){return new B(t,e)}function ht(t){var e=t.u;if(t.u=void 0,typeof e=="function"){E++;var r=l;l=void 0;try{e()}catch(s){throw t.f&=-2,t.f|=8,X(t),s}finally{l=r,H()}}}function X(t){for(var e=t.s;e!==void 0;e=e.n)e.S.U(e);t.x=void 0,t.s=void 0,ht(t)}function gt(t){if(l!==this)throw new Error("Out-of-order effect");lt(this),l=t,this.f&=-2,8&this.f&&X(this),H()}function D(t,e){this.x=t,this.u=void 0,this.s=void 0,this.o=void 0,this.f=32,this.name=e==null?void 0:e.name}D.prototype.c=function(){var t=this.S();try{if(8&this.f||this.x===void 0)return;var e=this.x();typeof e=="function"&&(this.u=e)}finally{t()}};D.prototype.S=function(){if(1&this.f)throw new Error("Cycle detected");this.f|=1,this.f&=-9,ht(this),ft(this),E++;var t=l;return l=this,gt.bind(this,t)};D.prototype.N=function(){2&this.f||(this.f|=2,this.o=J,J=this)};D.prototype.d=function(){this.f|=8,1&this.f||X(this)};D.prototype.dispose=function(){this.d()};function bt(t,e){var r=new D(t,e);try{r.c()}catch(h){throw r.d(),h}var s=r.d.bind(r);return s[Symbol.dispose]=s,s}const nt=t=>t!==null&&typeof t=="object"&&!Array.isArray(t);function Q(t,e){if(Object.is(t,e))return!0;if(t instanceof Date&&e instanceof Date)return t.getTime()===e.getTime();if(Array.isArray(t)&&Array.isArray(e)){if(t.length!==e.length)return!1;for(let r=0;r<t.length;r+=1)if(!Q(t[r],e[r]))return!1;return!0}if(nt(t)&&nt(e)){const r=Object.keys(t),s=Object.keys(e);if(r.length!==s.length)return!1;for(const h of r)if(!(h in e)||!Q(t[h],e[h]))return!1;return!0}return!1}function dt(t){if(typeof structuredClone=="function")return structuredClone(t);try{return JSON.parse(JSON.stringify(t))}catch{return t}}function it(t,e,r=Object.is,s=dt){const h={};for(const[y,w]of Object.entries(e)){const T=t[y];r(T,w,y)||(h[y]=s(w))}return h}function xt(t={}){const e=(t.namespace??"default").trim()||"default",r=t.plugins??[],s=new Map;function h(y,w){return function(){var T;const C=`${e}:${y}`,_=s.get(y);if(_)return _;const A=w.state(),j=w.modules??{},U={...A};for(const[i,n]of Object.entries(j))U[i]=n.state();const g=yt(U),m=new Map,F=new Map,q=new Map,$=new Map,P=new Map,z=new Set,N=new Set(Object.keys(A)),L=new Set(Object.keys(w.getters??{})),W=new Set(Object.keys(w.actions??{})),o=i=>g.value[i]??{},d=(i,n)=>{g.value={...g.value,[i]:n}},p=new Proxy({},{get(i,n){var u,c;if(m.has(n))return(u=m.get(n))==null?void 0:u.value;if(P.has(n))return(c=P.get(n))==null?void 0:c.value;if(N.has(n))return g.value[n];const v=$.get(n);return v?o(v.moduleName)[v.key]:g.value[n]}});for(const[i,n]of Object.entries(w.getters??{}))m.set(i,rt(()=>n(g.value,p)));const b=(i,n)=>{var u;for(const c of z)c(i);for(const c of r)(u=c.onMutation)==null||u.call(c,n,i)},O=(i,n,u,c)=>{const v=g.value[i];Object.is(v,n)||(g.value={...g.value,[i]:n},b({namespace:e,scopeId:C,storeId:y,key:i,oldValue:v,newValue:n,origin:u},c))},M=(i,n,u,c,v)=>{const x=o(i),a=x[n];Object.is(a,u)||(d(i,{...x,[n]:u}),b({namespace:e,scopeId:C,storeId:y,key:`${i}.${n}`,oldValue:a,newValue:u,origin:c},v))},V={$id:y,$namespace:e,$scopeId:C,$subscribe(i){return z.add(i),()=>z.delete(i)},$dispose(){var i;z.clear();for(const n of r)(i=n.onDispose)==null||i.call(n,k);s.delete(y)},$patch(i,n="local"){Y(()=>{for(const[u,c]of Object.entries(i)){const v=$.get(u);if(v){M(v.moduleName,v.key,c,n,k);continue}if(u.includes(".")){const[x,a]=u.split(".",2);if(j[x]){M(x,a,c,n,k);continue}}if(j[u]&&c&&typeof c=="object"&&!Array.isArray(c)){for(const[x,a]of Object.entries(c))M(u,x,a,n,k);continue}O(u,c,n,k)}})},$snapshot(){return{...g.value}}};for(const[i,n]of Object.entries(j)){const u=new Map,c=new Map,v=new Proxy({},{get(a,f){var R,I,tt;if(u.has(f))return(R=u.get(f))==null?void 0:R.value;if(m.has(f))return(I=m.get(f))==null?void 0:I.value;if(P.has(f))return(tt=P.get(f))==null?void 0:tt.value;const et=o(i);if(f in et)return et[f];if(N.has(f))return g.value[f]}});for(const[a,f]of Object.entries(n.getters??{}))u.set(a,rt(()=>f(o(i),g.value,v)));let x;for(const[a,f]of Object.entries(n.actions??{}))c.set(a,(...R)=>Y(()=>f.apply(x,R)));x=new Proxy({},{get(a,f){var R;if(c.has(f))return c.get(f);if(u.has(f))return(R=u.get(f))==null?void 0:R.value;const I=o(i);return f in I?I[f]:k[f]},set(a,f,R){if(c.has(f)||u.has(f))return!1;const I=o(i);return f in I?M(i,f,R,"local",k):k[f]=R,!0}}),q.set(i,x),F.set(i,u);for(const a of Object.keys(o(i)))!(N.has(a)||L.has(a)||W.has(a)||Reflect.has(V,a)||j[a]!==void 0)&&!$.has(a)&&$.set(a,{moduleName:i,key:a});for(const[a,f]of u.entries())!(N.has(a)||L.has(a)||W.has(a)||Reflect.has(V,a)||j[a]!==void 0)&&!P.has(a)&&P.set(a,f);for(const[a,f]of c.entries())!(N.has(a)||L.has(a)||W.has(a)||Reflect.has(V,a)||j[a]!==void 0)&&!Reflect.has(V,a)&&(V[a]=f)}const k=new Proxy(V,{get(i,n,u){var c,v;if(Reflect.has(i,n))return Reflect.get(i,n,u);if(q.has(n))return q.get(n);if(m.has(n))return(c=m.get(n))==null?void 0:c.value;if(P.has(n))return(v=P.get(n))==null?void 0:v.value;if(N.has(n))return g.value[n];const x=$.get(n);return x?o(x.moduleName)[x.key]:g.value[n]},set(i,n,u,c){if(Reflect.has(i,n))return Reflect.set(i,n,u,c);const v=$.get(n);return v?(M(v.moduleName,v.key,u,"local",c),!0):(O(n,u,"local",c),!0)}});for(const[i,n]of Object.entries(w.actions??{}))V[i]=(...u)=>Y(()=>n.apply(k,u));for(const i of r)(T=i.onInit)==null||T.call(i,k);return s.set(y,k),k}}return{defineStore:h,use(y){r.push(y)}}}xt();const K=t=>t!==null&&typeof t=="object";function vt(t){if(!K(t))return t;const e=new WeakSet,r=s=>{if(!K(s))return;const h=s;if(e.has(h))return;e.add(h);const y=Reflect.ownKeys(h);for(const w of y){const T=Reflect.get(h,w);r(T)}Object.freeze(h)};return r(t),t}function St(t,e){for(const[r,s]of Object.entries(e))t[r]=s}const ot=t=>Object.keys(t).length>0,st=t=>t?Array.isArray(t)?t:[t]:[],wt=t=>{const e=[t];if(t.includes(".")){const r=t.split(".",1)[0];r&&r!==t&&e.push(r)}return e},mt=(t,e)=>{if(!e||!K(t))return t;try{Object.defineProperty(t,e,{value:!0,enumerable:!1,configurable:!0})}catch{return t}return t};function jt(t){const e=t.mutationType??"storix/APPLY_PATCH",r=t.immutable??!0,s=t.clone??dt,h=t.equals??Q,y=t.projectVuexToStorix??(o=>o),w=t.projectStorixToVuex??(o=>o),T=t.suppressBridgeEcho??!1,C=t.bridgeOriginKey??"__storixBridge",_=t.outboundDefault??{mode:"patch",type:e},A=o=>{const d=s(o);return r?vt(d):d};let j=!1,U=!1,g=!1;const m=new Map,F=o=>{if(T)for(const d of o){const p=m.get(d)??0;m.set(d,p+1)}},q=o=>{if(!T||!K(o))return!1;const d=Reflect.get(o,"payload");if(C&&K(d)&&Reflect.get(d,C))return!0;const p=Reflect.get(o,"type");if(typeof p!="string")return!1;const b=m.get(p)??0;return b<=0?!1:(b===1?m.delete(p):m.set(p,b-1),!0)},$=()=>{if(j||g)return;const o=A(y(t.vuex.state)),d=t.store.$snapshot(),p=it(d,o,h,A);if(ot(p)){U=!0;try{t.store.$patch(p,t.externalMutationOrigin??"local")}finally{U=!1}}},P=o=>{const p=wt(o.storixMutation.key).flatMap(b=>{var O;return st((O=t.outboundRoutes)==null?void 0:O[b])});return p.push(...st(_)),p.find(b=>b.when?b.when(o):!0)},z=(o,d)=>{var M;if(o.mode==="custom"){(M=o.handler)==null||M.call(o,d),F(o.suppressMutationTypes??[]);return}const p=(o.mode==="patch",d.projectedPatch),b=mt(A(o.mapPayload?o.mapPayload(d):p),C);if(o.mode==="dispatch"){if(typeof t.vuex.dispatch!="function")throw new Error('Vuex bridge route mode "dispatch" requires vuex.dispatch to be available.');if(!o.type)throw new Error('Vuex bridge dispatch route requires a "type".');t.vuex.dispatch(o.type,b),F(o.suppressMutationTypes??[]);return}const O=o.type??e;t.vuex.commit(O,b),F(o.suppressMutationTypes??[O])},N=o=>{if(j||U)return;const d=t.store.$snapshot(),p=A(w(d)),b=A(y(t.vuex.state)),O=it(b,p,h,A);if(!ot(O))return;const M={storixMutation:o??{namespace:t.store.$namespace,scopeId:t.store.$scopeId,storeId:t.store.$id,key:Object.keys(O)[0]??"*",oldValue:void 0,newValue:void 0,origin:"local"},storixSnapshot:d,vuexState:t.vuex.state,projectedPatch:O,projectedVuexState:p,origin:"storix"},V=P(M);if(V){g=!0;try{z(V,M)}finally{g=!1}}},L=t.vuex.subscribe(o=>{q(o)||$()}),W=t.store.$subscribe(o=>{N(o)});return $(),{dispose(){j||(j=!0,L(),W())},syncFromExternal:$,syncFromStore(){N()}}}exports.applyVuexPatchMutation=St;exports.createVuexBridge=jt;exports.deepFreeze=vt;
|
package/dist/index.d.ts
CHANGED
|
@@ -1,9 +1,27 @@
|
|
|
1
|
-
import { type ExternalStoreBridgeHandle, type StoreInstance } from '@storix-js/core';
|
|
1
|
+
import { type ExternalStoreBridgeHandle, type MutationPayload, type StoreInstance } from '@storix-js/core';
|
|
2
2
|
type AnyObject = Record<string, unknown>;
|
|
3
3
|
export interface VuexLikeStore<TState extends AnyObject> {
|
|
4
4
|
state: TState;
|
|
5
5
|
subscribe(listener: (mutation: unknown, state: TState) => void): () => void;
|
|
6
6
|
commit(type: string, payload?: unknown): void;
|
|
7
|
+
dispatch?(type: string, payload?: unknown): unknown;
|
|
8
|
+
}
|
|
9
|
+
export type VuexOutboundMode = 'patch' | 'commit' | 'dispatch' | 'custom';
|
|
10
|
+
export interface VuexRouteContext<TVuexState extends AnyObject> {
|
|
11
|
+
storixMutation: MutationPayload;
|
|
12
|
+
storixSnapshot: Record<string, unknown>;
|
|
13
|
+
vuexState: TVuexState;
|
|
14
|
+
projectedPatch: Record<string, unknown>;
|
|
15
|
+
projectedVuexState: Record<string, unknown>;
|
|
16
|
+
origin: 'storix' | 'vuex';
|
|
17
|
+
}
|
|
18
|
+
export interface VuexOutboundRoute<TVuexState extends AnyObject> {
|
|
19
|
+
mode: VuexOutboundMode;
|
|
20
|
+
type?: string;
|
|
21
|
+
when?: (context: VuexRouteContext<TVuexState>) => boolean;
|
|
22
|
+
mapPayload?: (context: VuexRouteContext<TVuexState>) => unknown;
|
|
23
|
+
handler?: (context: VuexRouteContext<TVuexState>) => void | Promise<void>;
|
|
24
|
+
suppressMutationTypes?: string[];
|
|
7
25
|
}
|
|
8
26
|
export interface CreateVuexBridgeOptions<TVuexState extends AnyObject> {
|
|
9
27
|
store: StoreInstance;
|
|
@@ -15,6 +33,10 @@ export interface CreateVuexBridgeOptions<TVuexState extends AnyObject> {
|
|
|
15
33
|
clone?: <T>(value: T) => T;
|
|
16
34
|
immutable?: boolean;
|
|
17
35
|
externalMutationOrigin?: 'local' | 'remote';
|
|
36
|
+
outboundDefault?: VuexOutboundRoute<TVuexState>;
|
|
37
|
+
outboundRoutes?: Record<string, VuexOutboundRoute<TVuexState> | Array<VuexOutboundRoute<TVuexState>>>;
|
|
38
|
+
suppressBridgeEcho?: boolean;
|
|
39
|
+
bridgeOriginKey?: string;
|
|
18
40
|
}
|
|
19
41
|
export declare function deepFreeze<T>(value: T): T;
|
|
20
42
|
export declare function applyVuexPatchMutation<TState extends AnyObject>(state: TState, patch: Record<string, unknown>): void;
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"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"}
|