@storix-js/vuex 0.1.0 → 0.1.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/README.md ADDED
@@ -0,0 +1,24 @@
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
+ });
22
+ ```
23
+
24
+ Use `applyVuexPatchMutation()` inside your Vuex mutation to apply the incoming patch payload.
package/package.json CHANGED
@@ -1,10 +1,17 @@
1
1
  {
2
2
  "name": "@storix-js/vuex",
3
- "version": "0.1.0",
3
+ "version": "0.1.1",
4
4
  "type": "module",
5
5
  "main": "./dist/index.cjs",
6
6
  "module": "./dist/index.js",
7
7
  "types": "./dist/index.d.ts",
8
+ "files": [
9
+ "dist",
10
+ "README.md"
11
+ ],
12
+ "publishConfig": {
13
+ "access": "public"
14
+ },
8
15
  "exports": {
9
16
  ".": {
10
17
  "types": "./dist/index.d.ts",
@@ -19,6 +26,6 @@
19
26
  "typecheck": "tsc -p tsconfig.json --noEmit"
20
27
  },
21
28
  "dependencies": {
22
- "@storix-js/core": "workspace:*"
29
+ "@storix-js/core": "^0.1.1"
23
30
  }
24
31
  }
package/src/index.ts DELETED
@@ -1,91 +0,0 @@
1
- import {
2
- createExternalStoreBridge,
3
- isolateSnapshot,
4
- type ExternalStoreBridgeHandle,
5
- type StoreInstance
6
- } from '@storix-js/core';
7
-
8
- type AnyObject = Record<string, unknown>;
9
-
10
- export interface VuexLikeStore<TState extends AnyObject> {
11
- state: TState;
12
- subscribe(listener: (mutation: unknown, state: TState) => void): () => void;
13
- commit(type: string, payload?: unknown): void;
14
- }
15
-
16
- export interface CreateVuexBridgeOptions<TVuexState extends AnyObject> {
17
- store: StoreInstance;
18
- vuex: VuexLikeStore<TVuexState>;
19
- mutationType?: string;
20
- projectVuexToStorix?: (state: TVuexState) => Record<string, unknown>;
21
- projectStorixToVuex?: (snapshot: Record<string, unknown>) => Record<string, unknown>;
22
- equals?: (current: unknown, next: unknown, key: string) => boolean;
23
- clone?: <T>(value: T) => T;
24
- immutable?: boolean;
25
- externalMutationOrigin?: 'local' | 'remote';
26
- }
27
-
28
- const isObjectLike = (value: unknown): value is AnyObject => {
29
- return value !== null && typeof value === 'object';
30
- };
31
-
32
- export function deepFreeze<T>(value: T): T {
33
- if (!isObjectLike(value)) return value;
34
-
35
- const seen = new WeakSet<object>();
36
-
37
- const freezeRecursively = (candidate: unknown): void => {
38
- if (!isObjectLike(candidate)) return;
39
-
40
- const objectCandidate = candidate as object;
41
- if (seen.has(objectCandidate)) return;
42
- seen.add(objectCandidate);
43
-
44
- const keys = Reflect.ownKeys(objectCandidate);
45
- for (const key of keys) {
46
- const nested = Reflect.get(objectCandidate, key);
47
- freezeRecursively(nested);
48
- }
49
-
50
- Object.freeze(objectCandidate);
51
- };
52
-
53
- freezeRecursively(value);
54
- return value;
55
- }
56
-
57
- export function applyVuexPatchMutation<TState extends AnyObject>(state: TState, patch: Record<string, unknown>) {
58
- for (const [key, value] of Object.entries(patch)) {
59
- (state as AnyObject)[key] = value;
60
- }
61
- }
62
-
63
- export function createVuexBridge<TVuexState extends AnyObject>(
64
- options: CreateVuexBridgeOptions<TVuexState>
65
- ): ExternalStoreBridgeHandle {
66
- const mutationType = options.mutationType ?? 'storix/APPLY_PATCH';
67
- const immutable = options.immutable ?? true;
68
- const baseClone = options.clone ?? isolateSnapshot;
69
-
70
- const clone = <T>(value: T): T => {
71
- const next = baseClone(value);
72
- if (!immutable) return next;
73
- return deepFreeze(next);
74
- };
75
-
76
- return createExternalStoreBridge({
77
- store: options.store,
78
- getExternalState: () => options.vuex.state,
79
- subscribeExternal: (listener) => {
80
- return options.vuex.subscribe(() => listener());
81
- },
82
- applyExternalPatch: (patch) => {
83
- options.vuex.commit(mutationType, clone(patch));
84
- },
85
- projectExternalToStore: options.projectVuexToStorix,
86
- projectStoreToExternal: options.projectStorixToVuex,
87
- equals: options.equals,
88
- clone,
89
- externalMutationOrigin: options.externalMutationOrigin ?? 'local'
90
- });
91
- }
@@ -1,8 +0,0 @@
1
- {
2
- "extends": "./tsconfig.json",
3
- "compilerOptions": {
4
- "rootDir": "src",
5
- "outDir": "dist",
6
- "paths": {}
7
- }
8
- }
package/tsconfig.json DELETED
@@ -1,4 +0,0 @@
1
- {
2
- "extends": "../../tsconfig.base.json",
3
- "include": ["src"]
4
- }
package/vite.config.ts DELETED
@@ -1,15 +0,0 @@
1
- import { defineConfig } from 'vite';
2
-
3
- export default defineConfig({
4
- build: {
5
- lib: {
6
- entry: 'src/index.ts',
7
- name: 'StorixVuex',
8
- fileName: 'index',
9
- formats: ['es', 'cjs']
10
- }
11
- },
12
- test: {
13
- environment: 'node'
14
- }
15
- });