@storix-js/vuex 0.2.1 → 0.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/README.md CHANGED
@@ -44,3 +44,5 @@ const bridge = createVuexBridge({
44
44
  This keeps simple integrations patch-based while allowing special keys to flow through Vuex business logic.
45
45
 
46
46
  Use `applyVuexPatchMutation()` inside your Vuex mutation to apply the incoming patch payload.
47
+
48
+ For Vue 2 rendering, combine this bridge with `@storix-js/vue2`; the rendering adapter and Vuex bridge have separate lifecycles and should each be disposed at their ownership boundary.
package/dist/index.cjs CHANGED
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const b=require("@storix-js/core"),d=e=>e!==null&&typeof e=="object";function R(e){if(!d(e))return e;const n=new WeakSet,o=i=>{if(!d(i))return;const a=i;if(n.has(a))return;n.add(a);const y=Reflect.ownKeys(a);for(const g of y){const h=Reflect.get(a,g);o(h)}Object.freeze(a)};return o(e),e}function q(e,n){for(const[o,i]of Object.entries(n))e[o]=i}const T=e=>Object.keys(e).length>0,k=e=>e?Array.isArray(e)?e:[e]:[],z=e=>{const n=[e];if(e.includes(".")){const o=e.split(".",1)[0];o&&o!==e&&n.push(o)}return n},A=(e,n)=>{if(!n||!d(e))return e;try{Object.defineProperty(e,n,{value:!0,enumerable:!1,configurable:!0})}catch{return e}return e};function C(e){const n=e.mutationType??"storix/APPLY_PATCH",o=e.immutable??!0,i=e.clone??b.isolateSnapshot,a=e.equals??b.deepEqual,y=e.projectVuexToStorix??(t=>t),g=e.projectStorixToVuex??(t=>t),h=e.suppressBridgeEcho??!1,S=e.bridgeOriginKey??"__storixBridge",w=e.outboundDefault??{mode:"patch",type:n},l=t=>{const r=i(t);return o?R(r):r};let x=!1,m=!1,j=!1;const f=new Map,P=t=>{if(h)for(const r of t){const s=f.get(r)??0;f.set(r,s+1)}},O=t=>{if(!h||!d(t))return!1;const r=Reflect.get(t,"payload");if(S&&d(r)&&Reflect.get(r,S))return!0;const s=Reflect.get(t,"type");if(typeof s!="string")return!1;const c=f.get(s)??0;return c<=0?!1:(c===1?f.delete(s):f.set(s,c-1),!0)},v=()=>{if(x||j)return;const t=l(y(e.vuex.state)),r=e.store.$snapshot(),s=b.createPatchFromSnapshot(r,t,a,l);if(T(s)){m=!0;try{e.store.$patch(s,e.externalMutationOrigin??"local")}finally{m=!1}}},E=t=>{const s=z(t.storixMutation.key).flatMap(c=>{var u;return k((u=e.outboundRoutes)==null?void 0:u[c])});return s.push(...k(w)),s.find(c=>c.when?c.when(t):!0)},B=(t,r)=>{var p;if(t.mode==="custom"){(p=t.handler)==null||p.call(t,r),P(t.suppressMutationTypes??[]);return}const s=r.projectedPatch,c=A(l(t.mapPayload?t.mapPayload(r):s),S);if(t.mode==="dispatch"){if(typeof e.vuex.dispatch!="function")throw new Error('Vuex bridge route mode "dispatch" requires vuex.dispatch to be available.');if(!t.type)throw new Error('Vuex bridge dispatch route requires a "type".');e.vuex.dispatch(t.type,c),P(t.suppressMutationTypes??[]);return}const u=t.type??n;e.vuex.commit(u,c),P(t.suppressMutationTypes??[u])},M=t=>{if(x||m)return;const r=e.store.$snapshot(),s=l(g(r)),c=l(y(e.vuex.state)),u=b.createPatchFromSnapshot(c,s,a,l);if(!T(u))return;const p={storixMutation:t??{namespace:e.store.$namespace,scopeId:e.store.$scopeId,storeId:e.store.$id,key:Object.keys(u)[0]??"*",oldValue:void 0,newValue:void 0,origin:"local"},storixSnapshot:r,vuexState:e.vuex.state,projectedPatch:u,projectedVuexState:s,origin:"storix"},V=E(p);if(V){j=!0;try{B(V,p)}finally{j=!1}}},F=e.vuex.subscribe(t=>{O(t)||v()}),$=e.store.$subscribe(t=>{M(t)});return v(),{dispose(){x||(x=!0,F(),$())},syncFromExternal:v,syncFromStore(){M()}}}exports.applyVuexPatchMutation=q;exports.createVuexBridge=C;exports.deepFreeze=R;
1
+ Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});let e=require("@storix-js/core");var t=e=>typeof e==`object`&&!!e;function n(e){if(!t(e))return e;let n=new WeakSet,r=e=>{if(!t(e))return;let i=e;if(n.has(i))return;n.add(i);let a=Reflect.ownKeys(i);for(let e of a){let t=Reflect.get(i,e);r(t)}Object.freeze(i)};return r(e),e}function r(e,t){for(let[n,r]of Object.entries(t))e[n]=r}var i=e=>Object.keys(e).length>0,a=e=>e?Array.isArray(e)?e:[e]:[],o=e=>{let t=[e];if(e.includes(`.`)){let n=e.split(`.`,1)[0];n&&n!==e&&t.push(n)}return t},s=(e,n)=>{if(!n||!t(e))return e;try{Object.defineProperty(e,n,{value:!0,enumerable:!1,configurable:!0})}catch{return e}return e};function c(r){let c=r.mutationType??`storix/APPLY_PATCH`,l=r.immutable??!0,u=r.clone??e.isolateSnapshot,d=r.equals??e.deepEqual,f=r.projectVuexToStorix??(e=>e),p=r.projectStorixToVuex??(e=>e),m=r.suppressBridgeEcho??!1,h=r.bridgeOriginKey??`__storixBridge`,g=r.outboundDefault??{mode:`patch`,type:c},_=e=>{let t=u(e);return l?n(t):t},v=!1,y=!1,b=!1,x=new Map,S=e=>{if(m)for(let t of e){let e=x.get(t)??0;x.set(t,e+1)}},C=e=>{if(!m||!t(e))return!1;let n=Reflect.get(e,`payload`);if(h&&t(n)&&Reflect.get(n,h))return!0;let r=Reflect.get(e,`type`);if(typeof r!=`string`)return!1;let i=x.get(r)??0;return i<=0?!1:(i===1?x.delete(r):x.set(r,i-1),!0)},w=()=>{if(v||b)return;let t=_(f(r.vuex.state)),n=r.store.$snapshot(),a=(0,e.createPatchFromSnapshot)(n,t,d,_);if(i(a)){y=!0;try{r.store.$patch(a,r.externalMutationOrigin??`local`)}finally{y=!1}}},T=e=>{let t=o(e.storixMutation.key).flatMap(e=>a(r.outboundRoutes?.[e]));return t.push(...a(g)),t.find(t=>!t.when||t.when(e))},E=(e,t)=>{if(e.mode===`custom`){e.handler?.(t),S(e.suppressMutationTypes??[]);return}let n=t.projectedPatch,i=s(_(e.mapPayload?e.mapPayload(t):n),h);if(e.mode===`dispatch`){if(typeof r.vuex.dispatch!=`function`)throw Error(`Vuex bridge route mode "dispatch" requires vuex.dispatch to be available.`);if(!e.type)throw Error(`Vuex bridge dispatch route requires a "type".`);r.vuex.dispatch(e.type,i),S(e.suppressMutationTypes??[]);return}let a=e.type??c;r.vuex.commit(a,i),S(e.suppressMutationTypes??[a])},D=t=>{if(v||y)return;let n=r.store.$snapshot(),a=_(p(n)),o=_(f(r.vuex.state)),s=(0,e.createPatchFromSnapshot)(o,a,d,_);if(!i(s))return;let c={storixMutation:t??{namespace:r.store.$namespace,scopeId:r.store.$scopeId,storeId:r.store.$id,key:Object.keys(s)[0]??`*`,oldValue:void 0,newValue:void 0,origin:`local`},storixSnapshot:n,vuexState:r.vuex.state,projectedPatch:s,projectedVuexState:a,origin:`storix`},l=T(c);if(l){b=!0;try{E(l,c)}finally{b=!1}}},O=r.vuex.subscribe(e=>{C(e)||w()}),k=r.store.$subscribe(e=>{D(e)});return w(),{dispose(){v||(v=!0,O(),k())},syncFromExternal:w,syncFromStore(){D()}}}exports.applyVuexPatchMutation=r,exports.createVuexBridge=c,exports.deepFreeze=n;
package/dist/index.js CHANGED
@@ -1,148 +1,135 @@
1
- import { isolateSnapshot as q, deepEqual as F, createPatchFromSnapshot as T } from "@storix-js/core";
2
- const d = (e) => e !== null && typeof e == "object";
3
- function z(e) {
4
- if (!d(e)) return e;
5
- const n = /* @__PURE__ */ new WeakSet(), o = (i) => {
6
- if (!d(i)) return;
7
- const a = i;
8
- if (n.has(a)) return;
9
- n.add(a);
10
- const y = Reflect.ownKeys(a);
11
- for (const b of y) {
12
- const h = Reflect.get(a, b);
13
- o(h);
14
- }
15
- Object.freeze(a);
16
- };
17
- return o(e), e;
1
+ import { createPatchFromSnapshot as e, deepEqual as t, isolateSnapshot as n } from "@storix-js/core";
2
+ //#region src/index.ts
3
+ var r = (e) => typeof e == "object" && !!e;
4
+ function i(e) {
5
+ if (!r(e)) return e;
6
+ let t = /* @__PURE__ */ new WeakSet(), n = (e) => {
7
+ if (!r(e)) return;
8
+ let i = e;
9
+ if (t.has(i)) return;
10
+ t.add(i);
11
+ let a = Reflect.ownKeys(i);
12
+ for (let e of a) {
13
+ let t = Reflect.get(i, e);
14
+ n(t);
15
+ }
16
+ Object.freeze(i);
17
+ };
18
+ return n(e), e;
18
19
  }
19
- function I(e, n) {
20
- for (const [o, i] of Object.entries(n))
21
- e[o] = i;
20
+ function a(e, t) {
21
+ for (let [n, r] of Object.entries(t)) e[n] = r;
22
22
  }
23
- const V = (e) => Object.keys(e).length > 0, k = (e) => e ? Array.isArray(e) ? e : [e] : [], A = (e) => {
24
- const n = [e];
25
- if (e.includes(".")) {
26
- const o = e.split(".", 1)[0];
27
- o && o !== e && n.push(o);
28
- }
29
- return n;
30
- }, C = (e, n) => {
31
- if (!n || !d(e)) return e;
32
- try {
33
- Object.defineProperty(e, n, {
34
- value: !0,
35
- enumerable: !1,
36
- configurable: !0
37
- });
38
- } catch {
39
- return e;
40
- }
41
- return e;
23
+ var o = (e) => Object.keys(e).length > 0, s = (e) => e ? Array.isArray(e) ? e : [e] : [], c = (e) => {
24
+ let t = [e];
25
+ if (e.includes(".")) {
26
+ let n = e.split(".", 1)[0];
27
+ n && n !== e && t.push(n);
28
+ }
29
+ return t;
30
+ }, l = (e, t) => {
31
+ if (!t || !r(e)) return e;
32
+ try {
33
+ Object.defineProperty(e, t, {
34
+ value: !0,
35
+ enumerable: !1,
36
+ configurable: !0
37
+ });
38
+ } catch {
39
+ return e;
40
+ }
41
+ return e;
42
42
  };
43
- function K(e) {
44
- const n = e.mutationType ?? "storix/APPLY_PATCH", o = e.immutable ?? !0, i = e.clone ?? q, a = e.equals ?? F, y = e.projectVuexToStorix ?? ((t) => t), b = e.projectStorixToVuex ?? ((t) => t), h = e.suppressBridgeEcho ?? !1, g = e.bridgeOriginKey ?? "__storixBridge", R = e.outboundDefault ?? {
45
- mode: "patch",
46
- type: n
47
- }, l = (t) => {
48
- const r = i(t);
49
- return o ? z(r) : r;
50
- };
51
- let x = !1, S = !1, m = !1;
52
- const f = /* @__PURE__ */ new Map(), j = (t) => {
53
- if (h)
54
- for (const r of t) {
55
- const s = f.get(r) ?? 0;
56
- f.set(r, s + 1);
57
- }
58
- }, w = (t) => {
59
- if (!h || !d(t)) return !1;
60
- const r = Reflect.get(t, "payload");
61
- if (g && d(r) && Reflect.get(r, g))
62
- return !0;
63
- const s = Reflect.get(t, "type");
64
- if (typeof s != "string") return !1;
65
- const c = f.get(s) ?? 0;
66
- return c <= 0 ? !1 : (c === 1 ? f.delete(s) : f.set(s, c - 1), !0);
67
- }, v = () => {
68
- if (x || m) return;
69
- const t = l(y(e.vuex.state)), r = e.store.$snapshot(), s = T(r, t, a, l);
70
- if (V(s)) {
71
- S = !0;
72
- try {
73
- e.store.$patch(s, e.externalMutationOrigin ?? "local");
74
- } finally {
75
- S = !1;
76
- }
77
- }
78
- }, E = (t) => {
79
- const s = A(t.storixMutation.key).flatMap((c) => {
80
- var u;
81
- return k((u = e.outboundRoutes) == null ? void 0 : u[c]);
82
- });
83
- return s.push(...k(R)), s.find((c) => c.when ? c.when(t) : !0);
84
- }, O = (t, r) => {
85
- var p;
86
- if (t.mode === "custom") {
87
- (p = t.handler) == null || p.call(t, r), j(t.suppressMutationTypes ?? []);
88
- return;
89
- }
90
- const s = r.projectedPatch, c = C(l(t.mapPayload ? t.mapPayload(r) : s), g);
91
- if (t.mode === "dispatch") {
92
- if (typeof e.vuex.dispatch != "function")
93
- throw new Error('Vuex bridge route mode "dispatch" requires vuex.dispatch to be available.');
94
- if (!t.type)
95
- throw new Error('Vuex bridge dispatch route requires a "type".');
96
- e.vuex.dispatch(t.type, c), j(t.suppressMutationTypes ?? []);
97
- return;
98
- }
99
- const u = t.type ?? n;
100
- e.vuex.commit(u, c), j(t.suppressMutationTypes ?? [u]);
101
- }, M = (t) => {
102
- if (x || S) return;
103
- const r = e.store.$snapshot(), s = l(b(r)), c = l(y(e.vuex.state)), u = T(c, s, a, l);
104
- if (!V(u)) return;
105
- const p = {
106
- storixMutation: t ?? {
107
- namespace: e.store.$namespace,
108
- scopeId: e.store.$scopeId,
109
- storeId: e.store.$id,
110
- key: Object.keys(u)[0] ?? "*",
111
- oldValue: void 0,
112
- newValue: void 0,
113
- origin: "local"
114
- },
115
- storixSnapshot: r,
116
- vuexState: e.vuex.state,
117
- projectedPatch: u,
118
- projectedVuexState: s,
119
- origin: "storix"
120
- }, P = E(p);
121
- if (P) {
122
- m = !0;
123
- try {
124
- O(P, p);
125
- } finally {
126
- m = !1;
127
- }
128
- }
129
- }, $ = e.vuex.subscribe((t) => {
130
- w(t) || v();
131
- }), B = e.store.$subscribe((t) => {
132
- M(t);
133
- });
134
- return v(), {
135
- dispose() {
136
- x || (x = !0, $(), B());
137
- },
138
- syncFromExternal: v,
139
- syncFromStore() {
140
- M();
141
- }
142
- };
43
+ function u(a) {
44
+ let u = a.mutationType ?? "storix/APPLY_PATCH", d = a.immutable ?? !0, f = a.clone ?? n, p = a.equals ?? t, m = a.projectVuexToStorix ?? ((e) => e), h = a.projectStorixToVuex ?? ((e) => e), g = a.suppressBridgeEcho ?? !1, _ = a.bridgeOriginKey ?? "__storixBridge", v = a.outboundDefault ?? {
45
+ mode: "patch",
46
+ type: u
47
+ }, y = (e) => {
48
+ let t = f(e);
49
+ return d ? i(t) : t;
50
+ }, b = !1, x = !1, S = !1, C = /* @__PURE__ */ new Map(), w = (e) => {
51
+ if (g) for (let t of e) {
52
+ let e = C.get(t) ?? 0;
53
+ C.set(t, e + 1);
54
+ }
55
+ }, T = (e) => {
56
+ if (!g || !r(e)) return !1;
57
+ let t = Reflect.get(e, "payload");
58
+ if (_ && r(t) && Reflect.get(t, _)) return !0;
59
+ let n = Reflect.get(e, "type");
60
+ if (typeof n != "string") return !1;
61
+ let i = C.get(n) ?? 0;
62
+ return i <= 0 ? !1 : (i === 1 ? C.delete(n) : C.set(n, i - 1), !0);
63
+ }, E = () => {
64
+ if (b || S) return;
65
+ let t = y(m(a.vuex.state)), n = a.store.$snapshot(), r = e(n, t, p, y);
66
+ if (o(r)) {
67
+ x = !0;
68
+ try {
69
+ a.store.$patch(r, a.externalMutationOrigin ?? "local");
70
+ } finally {
71
+ x = !1;
72
+ }
73
+ }
74
+ }, D = (e) => {
75
+ let t = c(e.storixMutation.key).flatMap((e) => s(a.outboundRoutes?.[e]));
76
+ return t.push(...s(v)), t.find((t) => !t.when || t.when(e));
77
+ }, O = (e, t) => {
78
+ if (e.mode === "custom") {
79
+ e.handler?.(t), w(e.suppressMutationTypes ?? []);
80
+ return;
81
+ }
82
+ let n = t.projectedPatch, r = l(y(e.mapPayload ? e.mapPayload(t) : n), _);
83
+ if (e.mode === "dispatch") {
84
+ if (typeof a.vuex.dispatch != "function") throw Error("Vuex bridge route mode \"dispatch\" requires vuex.dispatch to be available.");
85
+ if (!e.type) throw Error("Vuex bridge dispatch route requires a \"type\".");
86
+ a.vuex.dispatch(e.type, r), w(e.suppressMutationTypes ?? []);
87
+ return;
88
+ }
89
+ let i = e.type ?? u;
90
+ a.vuex.commit(i, r), w(e.suppressMutationTypes ?? [i]);
91
+ }, k = (t) => {
92
+ if (b || x) return;
93
+ let n = a.store.$snapshot(), r = y(h(n)), i = y(m(a.vuex.state)), s = e(i, r, p, y);
94
+ if (!o(s)) return;
95
+ let c = {
96
+ storixMutation: t ?? {
97
+ namespace: a.store.$namespace,
98
+ scopeId: a.store.$scopeId,
99
+ storeId: a.store.$id,
100
+ key: Object.keys(s)[0] ?? "*",
101
+ oldValue: void 0,
102
+ newValue: void 0,
103
+ origin: "local"
104
+ },
105
+ storixSnapshot: n,
106
+ vuexState: a.vuex.state,
107
+ projectedPatch: s,
108
+ projectedVuexState: r,
109
+ origin: "storix"
110
+ }, l = D(c);
111
+ if (l) {
112
+ S = !0;
113
+ try {
114
+ O(l, c);
115
+ } finally {
116
+ S = !1;
117
+ }
118
+ }
119
+ }, A = a.vuex.subscribe((e) => {
120
+ T(e) || E();
121
+ }), j = a.store.$subscribe((e) => {
122
+ k(e);
123
+ });
124
+ return E(), {
125
+ dispose() {
126
+ b || (b = !0, A(), j());
127
+ },
128
+ syncFromExternal: E,
129
+ syncFromStore() {
130
+ k();
131
+ }
132
+ };
143
133
  }
144
- export {
145
- I as applyVuexPatchMutation,
146
- K as createVuexBridge,
147
- z as deepFreeze
148
- };
134
+ //#endregion
135
+ export { a as applyVuexPatchMutation, u as createVuexBridge, i as deepFreeze };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@storix-js/vuex",
3
- "version": "0.2.1",
3
+ "version": "0.3.0",
4
4
  "type": "module",
5
5
  "main": "./dist/index.cjs",
6
6
  "module": "./dist/index.js",
@@ -19,13 +19,13 @@
19
19
  "require": "./dist/index.cjs"
20
20
  }
21
21
  },
22
+ "dependencies": {
23
+ "@storix-js/core": "0.3.0"
24
+ },
22
25
  "scripts": {
23
26
  "build": "vite build && tsc -p tsconfig.build.json --emitDeclarationOnly",
24
27
  "dev": "vite build --watch",
25
28
  "test": "vitest run --passWithNoTests",
26
29
  "typecheck": "tsc -p tsconfig.json --noEmit"
27
- },
28
- "dependencies": {
29
- "@storix-js/core": "workspace:*"
30
30
  }
31
- }
31
+ }