@coaction/vue 1.5.0 → 2.0.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 +2 -2
- package/dist/index.d.mts +38 -24
- package/dist/index.d.ts +38 -24
- package/dist/index.js +110 -138
- package/dist/index.mjs +103 -136
- package/package.json +30 -35
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# @coaction/vue
|
|
2
2
|
|
|
3
|
-

|
|
4
4
|
[](https://www.npmjs.com/package/@coaction/vue)
|
|
5
5
|

|
|
6
6
|
|
|
@@ -50,4 +50,4 @@ console.log(selectors.count.value);
|
|
|
50
50
|
|
|
51
51
|
## Documentation
|
|
52
52
|
|
|
53
|
-
You can find the documentation [here](https://github.com/
|
|
53
|
+
You can find the documentation [here](https://github.com/coactionjs/coaction).
|
package/dist/index.d.mts
CHANGED
|
@@ -1,34 +1,48 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
import { Asyncify, ClientStoreOptions, ISlices, Slice, SliceState, Store, StoreOptions } from "coaction";
|
|
2
|
+
import { ComputedRef } from "vue";
|
|
3
|
+
export * from "coaction";
|
|
4
4
|
|
|
5
|
+
//#region \0rolldown/runtime.js
|
|
6
|
+
//#endregion
|
|
7
|
+
//#region packages/coaction-vue/src/index.d.ts
|
|
5
8
|
type SelectorOptions = {
|
|
6
|
-
|
|
7
|
-
};
|
|
8
|
-
type AutoSelector<T> = {
|
|
9
|
-
[K in keyof T]: T[K] extends (...args: any[]) => any ? T[K] : T[K] extends readonly any[] ? ComputedRef<T[K]> : T[K] extends object ? AutoSelector<T[K]> : ComputedRef<T[K]>;
|
|
9
|
+
autoSelector?: boolean;
|
|
10
10
|
};
|
|
11
|
+
type LeafObject = Date | RegExp | Error | Promise<unknown> | ReadonlyMap<unknown, unknown> | ReadonlySet<unknown> | WeakMap<object, unknown> | WeakSet<object> | ArrayBuffer | DataView;
|
|
12
|
+
type AutoSelectorValue<T> = T extends ((...args: any[]) => any) ? T : T extends readonly any[] ? ComputedRef<T> : T extends LeafObject ? ComputedRef<T> : T extends object ? AutoSelector<T> : ComputedRef<T>;
|
|
13
|
+
type AutoSelector<T> = { [K in keyof T]: AutoSelectorValue<T[K]> };
|
|
11
14
|
type StoreReturn<T extends object> = Store<T> & {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
15
|
+
<P>(selector: (state: T) => P): ComputedRef<P>;
|
|
16
|
+
(options: {
|
|
17
|
+
autoSelector: true;
|
|
18
|
+
}): AutoSelector<T>;
|
|
19
|
+
(options?: SelectorOptions): T;
|
|
17
20
|
};
|
|
18
21
|
type StoreWithAsyncFunction<T extends object, D extends true | false = false> = Store<Asyncify<T, D>> & {
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
22
|
+
<P>(selector: (state: Asyncify<T, D>) => P): ComputedRef<P>;
|
|
23
|
+
(options: {
|
|
24
|
+
autoSelector: true;
|
|
25
|
+
}): AutoSelector<Asyncify<T, D>>;
|
|
26
|
+
(options?: SelectorOptions): Asyncify<T, D>;
|
|
27
|
+
};
|
|
28
|
+
type CreateState = ISlices | Record<PropertyKey, Slice<any>>;
|
|
29
|
+
type SingleStoreOptions<T extends CreateState> = StoreOptions<T> & {
|
|
30
|
+
sliceMode: 'single';
|
|
31
|
+
};
|
|
32
|
+
type SingleClientStoreOptions<T extends CreateState> = ClientStoreOptions<T> & {
|
|
33
|
+
sliceMode: 'single';
|
|
24
34
|
};
|
|
25
|
-
type CreateState = ISlices | Record<string, Slice<any>>;
|
|
26
35
|
type Creator = {
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
36
|
+
<T extends ISlices>(createState: T, options: SingleStoreOptions<T>): StoreReturn<T>;
|
|
37
|
+
<T extends Record<PropertyKey, Slice<any>>>(createState: T, options?: StoreOptions<T>): StoreReturn<SliceState<T>>;
|
|
38
|
+
<T extends ISlices>(createState: Slice<T> | T, options?: StoreOptions<T>): StoreReturn<T>;
|
|
39
|
+
<T extends ISlices>(createState: T, options: SingleClientStoreOptions<T>): StoreWithAsyncFunction<T>;
|
|
40
|
+
<T extends Record<PropertyKey, Slice<any>>>(createState: T, options?: ClientStoreOptions<T>): StoreWithAsyncFunction<SliceState<T>, true>;
|
|
41
|
+
<T extends ISlices>(createState: Slice<T> | T, options?: ClientStoreOptions<T>): StoreWithAsyncFunction<T>;
|
|
31
42
|
};
|
|
32
43
|
declare const create: Creator;
|
|
33
|
-
|
|
34
|
-
export {
|
|
44
|
+
declare namespace index_d_exports {
|
|
45
|
+
export { CreateState, Creator, StoreReturn, StoreWithAsyncFunction, create };
|
|
46
|
+
}
|
|
47
|
+
//#endregion
|
|
48
|
+
export { CreateState, Creator, StoreReturn, StoreWithAsyncFunction, create };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,34 +1,48 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
import { Asyncify, ClientStoreOptions, ISlices, Slice, SliceState, Store, StoreOptions } from "coaction";
|
|
2
|
+
import { ComputedRef } from "vue";
|
|
3
|
+
export * from "coaction";
|
|
4
4
|
|
|
5
|
+
//#region \0rolldown/runtime.js
|
|
6
|
+
//#endregion
|
|
7
|
+
//#region packages/coaction-vue/src/index.d.ts
|
|
5
8
|
type SelectorOptions = {
|
|
6
|
-
|
|
7
|
-
};
|
|
8
|
-
type AutoSelector<T> = {
|
|
9
|
-
[K in keyof T]: T[K] extends (...args: any[]) => any ? T[K] : T[K] extends readonly any[] ? ComputedRef<T[K]> : T[K] extends object ? AutoSelector<T[K]> : ComputedRef<T[K]>;
|
|
9
|
+
autoSelector?: boolean;
|
|
10
10
|
};
|
|
11
|
+
type LeafObject = Date | RegExp | Error | Promise<unknown> | ReadonlyMap<unknown, unknown> | ReadonlySet<unknown> | WeakMap<object, unknown> | WeakSet<object> | ArrayBuffer | DataView;
|
|
12
|
+
type AutoSelectorValue<T> = T extends ((...args: any[]) => any) ? T : T extends readonly any[] ? ComputedRef<T> : T extends LeafObject ? ComputedRef<T> : T extends object ? AutoSelector<T> : ComputedRef<T>;
|
|
13
|
+
type AutoSelector<T> = { [K in keyof T]: AutoSelectorValue<T[K]> };
|
|
11
14
|
type StoreReturn<T extends object> = Store<T> & {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
15
|
+
<P>(selector: (state: T) => P): ComputedRef<P>;
|
|
16
|
+
(options: {
|
|
17
|
+
autoSelector: true;
|
|
18
|
+
}): AutoSelector<T>;
|
|
19
|
+
(options?: SelectorOptions): T;
|
|
17
20
|
};
|
|
18
21
|
type StoreWithAsyncFunction<T extends object, D extends true | false = false> = Store<Asyncify<T, D>> & {
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
22
|
+
<P>(selector: (state: Asyncify<T, D>) => P): ComputedRef<P>;
|
|
23
|
+
(options: {
|
|
24
|
+
autoSelector: true;
|
|
25
|
+
}): AutoSelector<Asyncify<T, D>>;
|
|
26
|
+
(options?: SelectorOptions): Asyncify<T, D>;
|
|
27
|
+
};
|
|
28
|
+
type CreateState = ISlices | Record<PropertyKey, Slice<any>>;
|
|
29
|
+
type SingleStoreOptions<T extends CreateState> = StoreOptions<T> & {
|
|
30
|
+
sliceMode: 'single';
|
|
31
|
+
};
|
|
32
|
+
type SingleClientStoreOptions<T extends CreateState> = ClientStoreOptions<T> & {
|
|
33
|
+
sliceMode: 'single';
|
|
24
34
|
};
|
|
25
|
-
type CreateState = ISlices | Record<string, Slice<any>>;
|
|
26
35
|
type Creator = {
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
36
|
+
<T extends ISlices>(createState: T, options: SingleStoreOptions<T>): StoreReturn<T>;
|
|
37
|
+
<T extends Record<PropertyKey, Slice<any>>>(createState: T, options?: StoreOptions<T>): StoreReturn<SliceState<T>>;
|
|
38
|
+
<T extends ISlices>(createState: Slice<T> | T, options?: StoreOptions<T>): StoreReturn<T>;
|
|
39
|
+
<T extends ISlices>(createState: T, options: SingleClientStoreOptions<T>): StoreWithAsyncFunction<T>;
|
|
40
|
+
<T extends Record<PropertyKey, Slice<any>>>(createState: T, options?: ClientStoreOptions<T>): StoreWithAsyncFunction<SliceState<T>, true>;
|
|
41
|
+
<T extends ISlices>(createState: Slice<T> | T, options?: ClientStoreOptions<T>): StoreWithAsyncFunction<T>;
|
|
31
42
|
};
|
|
32
43
|
declare const create: Creator;
|
|
33
|
-
|
|
34
|
-
export {
|
|
44
|
+
declare namespace index_d_exports {
|
|
45
|
+
export { CreateState, Creator, StoreReturn, StoreWithAsyncFunction, create };
|
|
46
|
+
}
|
|
47
|
+
//#endregion
|
|
48
|
+
export { CreateState, Creator, StoreReturn, StoreWithAsyncFunction, create };
|
package/dist/index.js
CHANGED
|
@@ -1,143 +1,115 @@
|
|
|
1
|
-
"
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
//#endregion
|
|
3
|
+
let coaction = require("coaction");
|
|
4
|
+
let vue = require("vue");
|
|
5
|
+
//#region packages/coaction-vue/src/index.ts
|
|
6
|
+
const getOwnEnumerableKeys = (value) => Reflect.ownKeys(value).filter((key) => Object.prototype.propertyIsEnumerable.call(value, key));
|
|
7
|
+
const isPlainObject = (value) => {
|
|
8
|
+
const prototype = Object.getPrototypeOf(value);
|
|
9
|
+
return prototype === Object.prototype || prototype === null;
|
|
9
10
|
};
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
});
|
|
33
|
-
var import_coaction = require("coaction");
|
|
34
|
-
var import_vue = require("vue");
|
|
35
|
-
__reExport(src_exports, require("coaction"));
|
|
36
|
-
var createStateProxy = (store, version) => new Proxy({}, {
|
|
37
|
-
get(_, key) {
|
|
38
|
-
void version.value;
|
|
39
|
-
const state = store.getState();
|
|
40
|
-
const value = state[key];
|
|
41
|
-
if (typeof value === "function") {
|
|
42
|
-
return value.bind(store.getState());
|
|
43
|
-
}
|
|
44
|
-
return value;
|
|
45
|
-
},
|
|
46
|
-
has(_, key) {
|
|
47
|
-
void version.value;
|
|
48
|
-
return key in store.getState();
|
|
49
|
-
},
|
|
50
|
-
ownKeys() {
|
|
51
|
-
void version.value;
|
|
52
|
-
return Reflect.ownKeys(store.getState());
|
|
53
|
-
},
|
|
54
|
-
getOwnPropertyDescriptor(_, key) {
|
|
55
|
-
void version.value;
|
|
56
|
-
const descriptor = Object.getOwnPropertyDescriptor(store.getState(), key);
|
|
57
|
-
if (!descriptor) {
|
|
58
|
-
return void 0;
|
|
59
|
-
}
|
|
60
|
-
return {
|
|
61
|
-
...descriptor,
|
|
62
|
-
configurable: true
|
|
63
|
-
};
|
|
64
|
-
}
|
|
11
|
+
const createStateProxy = (store, version) => new Proxy({}, {
|
|
12
|
+
get(_, key) {
|
|
13
|
+
version.value;
|
|
14
|
+
return store.getState()[key];
|
|
15
|
+
},
|
|
16
|
+
has(_, key) {
|
|
17
|
+
version.value;
|
|
18
|
+
return key in store.getState();
|
|
19
|
+
},
|
|
20
|
+
ownKeys() {
|
|
21
|
+
version.value;
|
|
22
|
+
return Reflect.ownKeys(store.getState());
|
|
23
|
+
},
|
|
24
|
+
getOwnPropertyDescriptor(_, key) {
|
|
25
|
+
version.value;
|
|
26
|
+
const descriptor = Object.getOwnPropertyDescriptor(store.getState(), key);
|
|
27
|
+
if (!descriptor) return;
|
|
28
|
+
return {
|
|
29
|
+
...descriptor,
|
|
30
|
+
configurable: true
|
|
31
|
+
};
|
|
32
|
+
}
|
|
65
33
|
});
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
34
|
+
const createAutoSelector = (store, version) => {
|
|
35
|
+
const getPathValue = (path) => {
|
|
36
|
+
let current = store.getState();
|
|
37
|
+
for (const key of path) {
|
|
38
|
+
if (typeof current !== "object" && typeof current !== "function" || current === null) return;
|
|
39
|
+
current = current[key];
|
|
40
|
+
}
|
|
41
|
+
return current;
|
|
42
|
+
};
|
|
43
|
+
const createAction = (path) => ((...args) => {
|
|
44
|
+
const fn = getPathValue(path);
|
|
45
|
+
if (typeof fn !== "function") return;
|
|
46
|
+
const receiverPath = path.slice(0, -1);
|
|
47
|
+
const receiver = receiverPath.length ? getPathValue(receiverPath) : store.getState();
|
|
48
|
+
return fn.apply(receiver, args);
|
|
49
|
+
});
|
|
50
|
+
const createNode = (path, value, ancestors = []) => {
|
|
51
|
+
if (typeof value === "function") return createAction(path);
|
|
52
|
+
if (typeof value !== "object" || value === null || Array.isArray(value) || !isPlainObject(value)) return (0, vue.computed)(() => {
|
|
53
|
+
version.value;
|
|
54
|
+
return getPathValue(path);
|
|
55
|
+
});
|
|
56
|
+
if (ancestors.includes(value)) return (0, vue.computed)(() => {
|
|
57
|
+
version.value;
|
|
58
|
+
return getPathValue(path);
|
|
59
|
+
});
|
|
60
|
+
const node = {};
|
|
61
|
+
const nextAncestors = [...ancestors, value];
|
|
62
|
+
for (const key of getOwnEnumerableKeys(value)) node[key] = createNode([...path, key], value[key], nextAncestors);
|
|
63
|
+
return node;
|
|
64
|
+
};
|
|
65
|
+
const state = store.getState();
|
|
66
|
+
const autoSelector = {};
|
|
67
|
+
if (!store.isSliceStore) {
|
|
68
|
+
for (const key of getOwnEnumerableKeys(state)) autoSelector[key] = createNode([key], state[key]);
|
|
69
|
+
return autoSelector;
|
|
70
|
+
}
|
|
71
|
+
for (const sliceKey of getOwnEnumerableKeys(state)) {
|
|
72
|
+
const slice = state[sliceKey];
|
|
73
|
+
if (typeof slice !== "object" || slice === null) continue;
|
|
74
|
+
autoSelector[sliceKey] = createNode([sliceKey], slice);
|
|
75
|
+
}
|
|
76
|
+
return autoSelector;
|
|
106
77
|
};
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
});
|
|
135
|
-
return useStore;
|
|
78
|
+
const create = (createState, options) => {
|
|
79
|
+
const store = (0, coaction.create)(createState, options);
|
|
80
|
+
const version = (0, vue.ref)(0);
|
|
81
|
+
const unsubscribe = store.subscribe(() => {
|
|
82
|
+
version.value += 1;
|
|
83
|
+
});
|
|
84
|
+
const baseDestroy = store.destroy;
|
|
85
|
+
let destroyed = false;
|
|
86
|
+
store.destroy = () => {
|
|
87
|
+
if (destroyed) return;
|
|
88
|
+
destroyed = true;
|
|
89
|
+
unsubscribe();
|
|
90
|
+
baseDestroy();
|
|
91
|
+
};
|
|
92
|
+
const stateProxy = createStateProxy(store, version);
|
|
93
|
+
let autoSelector;
|
|
94
|
+
return (0, coaction.wrapStore)(store, (selector) => {
|
|
95
|
+
if (typeof selector === "function") return (0, vue.computed)(() => {
|
|
96
|
+
version.value;
|
|
97
|
+
return selector(store.getState());
|
|
98
|
+
});
|
|
99
|
+
if (selector?.autoSelector) {
|
|
100
|
+
if (!autoSelector) autoSelector = createAutoSelector(store, version);
|
|
101
|
+
return autoSelector;
|
|
102
|
+
}
|
|
103
|
+
return stateProxy;
|
|
104
|
+
});
|
|
136
105
|
};
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
106
|
+
//#endregion
|
|
107
|
+
exports.create = create;
|
|
108
|
+
Object.keys(coaction).forEach(function(k) {
|
|
109
|
+
if (k !== "default" && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {
|
|
110
|
+
enumerable: true,
|
|
111
|
+
get: function() {
|
|
112
|
+
return coaction[k];
|
|
113
|
+
}
|
|
114
|
+
});
|
|
143
115
|
});
|
package/dist/index.mjs
CHANGED
|
@@ -1,140 +1,107 @@
|
|
|
1
|
-
|
|
2
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
-
var __export = (target, all) => {
|
|
6
|
-
for (var name in all)
|
|
7
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
8
|
-
};
|
|
9
|
-
var __copyProps = (to, from, except, desc) => {
|
|
10
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
11
|
-
for (let key of __getOwnPropNames(from))
|
|
12
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
13
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
14
|
-
}
|
|
15
|
-
return to;
|
|
16
|
-
};
|
|
17
|
-
var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
|
|
18
|
-
|
|
19
|
-
// index.ts
|
|
20
|
-
var index_exports = {};
|
|
21
|
-
__export(index_exports, {
|
|
22
|
-
create: () => create
|
|
23
|
-
});
|
|
24
|
-
|
|
25
|
-
// src/index.ts
|
|
26
|
-
var src_exports = {};
|
|
27
|
-
__export(src_exports, {
|
|
28
|
-
create: () => create
|
|
29
|
-
});
|
|
30
|
-
__reExport(src_exports, coaction_star);
|
|
31
|
-
import { create as createVanilla, wrapStore } from "coaction";
|
|
1
|
+
import { create as create$1, wrapStore } from "coaction";
|
|
32
2
|
import { computed, ref } from "vue";
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
return value.bind(store.getState());
|
|
41
|
-
}
|
|
42
|
-
return value;
|
|
43
|
-
},
|
|
44
|
-
has(_, key) {
|
|
45
|
-
void version.value;
|
|
46
|
-
return key in store.getState();
|
|
47
|
-
},
|
|
48
|
-
ownKeys() {
|
|
49
|
-
void version.value;
|
|
50
|
-
return Reflect.ownKeys(store.getState());
|
|
51
|
-
},
|
|
52
|
-
getOwnPropertyDescriptor(_, key) {
|
|
53
|
-
void version.value;
|
|
54
|
-
const descriptor = Object.getOwnPropertyDescriptor(store.getState(), key);
|
|
55
|
-
if (!descriptor) {
|
|
56
|
-
return void 0;
|
|
57
|
-
}
|
|
58
|
-
return {
|
|
59
|
-
...descriptor,
|
|
60
|
-
configurable: true
|
|
61
|
-
};
|
|
62
|
-
}
|
|
63
|
-
});
|
|
64
|
-
var createAutoSelector = (store, version) => {
|
|
65
|
-
const state = store.getState();
|
|
66
|
-
if (!store.isSliceStore) {
|
|
67
|
-
const autoSelector2 = {};
|
|
68
|
-
const descriptors = Object.getOwnPropertyDescriptors(state);
|
|
69
|
-
for (const key of Object.keys(descriptors)) {
|
|
70
|
-
const descriptor = descriptors[key];
|
|
71
|
-
if (typeof descriptor.value === "function") {
|
|
72
|
-
autoSelector2[key] = descriptor.value.bind(state);
|
|
73
|
-
} else {
|
|
74
|
-
autoSelector2[key] = computed(() => {
|
|
75
|
-
void version.value;
|
|
76
|
-
return store.getState()[key];
|
|
77
|
-
});
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
return autoSelector2;
|
|
81
|
-
}
|
|
82
|
-
const autoSelector = {};
|
|
83
|
-
for (const sliceKey of Object.keys(state)) {
|
|
84
|
-
const slice = state[sliceKey];
|
|
85
|
-
if (typeof slice !== "object" || slice === null) {
|
|
86
|
-
continue;
|
|
87
|
-
}
|
|
88
|
-
const sliceAutoSelector = {};
|
|
89
|
-
const descriptors = Object.getOwnPropertyDescriptors(slice);
|
|
90
|
-
for (const key of Object.keys(descriptors)) {
|
|
91
|
-
const descriptor = descriptors[key];
|
|
92
|
-
if (typeof descriptor.value === "function") {
|
|
93
|
-
sliceAutoSelector[key] = descriptor.value.bind(slice);
|
|
94
|
-
} else {
|
|
95
|
-
sliceAutoSelector[key] = computed(() => {
|
|
96
|
-
void version.value;
|
|
97
|
-
return store.getState()[sliceKey][key];
|
|
98
|
-
});
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
|
-
autoSelector[sliceKey] = sliceAutoSelector;
|
|
102
|
-
}
|
|
103
|
-
return autoSelector;
|
|
3
|
+
export * from "coaction";
|
|
4
|
+
//#endregion
|
|
5
|
+
//#region packages/coaction-vue/src/index.ts
|
|
6
|
+
const getOwnEnumerableKeys = (value) => Reflect.ownKeys(value).filter((key) => Object.prototype.propertyIsEnumerable.call(value, key));
|
|
7
|
+
const isPlainObject = (value) => {
|
|
8
|
+
const prototype = Object.getPrototypeOf(value);
|
|
9
|
+
return prototype === Object.prototype || prototype === null;
|
|
104
10
|
};
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
11
|
+
const createStateProxy = (store, version) => new Proxy({}, {
|
|
12
|
+
get(_, key) {
|
|
13
|
+
version.value;
|
|
14
|
+
return store.getState()[key];
|
|
15
|
+
},
|
|
16
|
+
has(_, key) {
|
|
17
|
+
version.value;
|
|
18
|
+
return key in store.getState();
|
|
19
|
+
},
|
|
20
|
+
ownKeys() {
|
|
21
|
+
version.value;
|
|
22
|
+
return Reflect.ownKeys(store.getState());
|
|
23
|
+
},
|
|
24
|
+
getOwnPropertyDescriptor(_, key) {
|
|
25
|
+
version.value;
|
|
26
|
+
const descriptor = Object.getOwnPropertyDescriptor(store.getState(), key);
|
|
27
|
+
if (!descriptor) return;
|
|
28
|
+
return {
|
|
29
|
+
...descriptor,
|
|
30
|
+
configurable: true
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
});
|
|
34
|
+
const createAutoSelector = (store, version) => {
|
|
35
|
+
const getPathValue = (path) => {
|
|
36
|
+
let current = store.getState();
|
|
37
|
+
for (const key of path) {
|
|
38
|
+
if (typeof current !== "object" && typeof current !== "function" || current === null) return;
|
|
39
|
+
current = current[key];
|
|
40
|
+
}
|
|
41
|
+
return current;
|
|
42
|
+
};
|
|
43
|
+
const createAction = (path) => ((...args) => {
|
|
44
|
+
const fn = getPathValue(path);
|
|
45
|
+
if (typeof fn !== "function") return;
|
|
46
|
+
const receiverPath = path.slice(0, -1);
|
|
47
|
+
const receiver = receiverPath.length ? getPathValue(receiverPath) : store.getState();
|
|
48
|
+
return fn.apply(receiver, args);
|
|
49
|
+
});
|
|
50
|
+
const createNode = (path, value, ancestors = []) => {
|
|
51
|
+
if (typeof value === "function") return createAction(path);
|
|
52
|
+
if (typeof value !== "object" || value === null || Array.isArray(value) || !isPlainObject(value)) return computed(() => {
|
|
53
|
+
version.value;
|
|
54
|
+
return getPathValue(path);
|
|
55
|
+
});
|
|
56
|
+
if (ancestors.includes(value)) return computed(() => {
|
|
57
|
+
version.value;
|
|
58
|
+
return getPathValue(path);
|
|
59
|
+
});
|
|
60
|
+
const node = {};
|
|
61
|
+
const nextAncestors = [...ancestors, value];
|
|
62
|
+
for (const key of getOwnEnumerableKeys(value)) node[key] = createNode([...path, key], value[key], nextAncestors);
|
|
63
|
+
return node;
|
|
64
|
+
};
|
|
65
|
+
const state = store.getState();
|
|
66
|
+
const autoSelector = {};
|
|
67
|
+
if (!store.isSliceStore) {
|
|
68
|
+
for (const key of getOwnEnumerableKeys(state)) autoSelector[key] = createNode([key], state[key]);
|
|
69
|
+
return autoSelector;
|
|
70
|
+
}
|
|
71
|
+
for (const sliceKey of getOwnEnumerableKeys(state)) {
|
|
72
|
+
const slice = state[sliceKey];
|
|
73
|
+
if (typeof slice !== "object" || slice === null) continue;
|
|
74
|
+
autoSelector[sliceKey] = createNode([sliceKey], slice);
|
|
75
|
+
}
|
|
76
|
+
return autoSelector;
|
|
134
77
|
};
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
78
|
+
const create = (createState, options) => {
|
|
79
|
+
const store = create$1(createState, options);
|
|
80
|
+
const version = ref(0);
|
|
81
|
+
const unsubscribe = store.subscribe(() => {
|
|
82
|
+
version.value += 1;
|
|
83
|
+
});
|
|
84
|
+
const baseDestroy = store.destroy;
|
|
85
|
+
let destroyed = false;
|
|
86
|
+
store.destroy = () => {
|
|
87
|
+
if (destroyed) return;
|
|
88
|
+
destroyed = true;
|
|
89
|
+
unsubscribe();
|
|
90
|
+
baseDestroy();
|
|
91
|
+
};
|
|
92
|
+
const stateProxy = createStateProxy(store, version);
|
|
93
|
+
let autoSelector;
|
|
94
|
+
return wrapStore(store, (selector) => {
|
|
95
|
+
if (typeof selector === "function") return computed(() => {
|
|
96
|
+
version.value;
|
|
97
|
+
return selector(store.getState());
|
|
98
|
+
});
|
|
99
|
+
if (selector?.autoSelector) {
|
|
100
|
+
if (!autoSelector) autoSelector = createAutoSelector(store, version);
|
|
101
|
+
return autoSelector;
|
|
102
|
+
}
|
|
103
|
+
return stateProxy;
|
|
104
|
+
});
|
|
140
105
|
};
|
|
106
|
+
//#endregion
|
|
107
|
+
export { create };
|
package/package.json
CHANGED
|
@@ -1,21 +1,32 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@coaction/vue",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.0",
|
|
4
4
|
"description": "A Coaction integration tool for Vue",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"coaction",
|
|
7
|
-
"
|
|
7
|
+
"data-transport",
|
|
8
8
|
"multithreading",
|
|
9
9
|
"mutative",
|
|
10
|
-
"
|
|
10
|
+
"vue"
|
|
11
11
|
],
|
|
12
|
-
"
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
12
|
+
"homepage": "https://github.com/coactionjs/coaction/tree/main/packages/coaction-vue#readme",
|
|
13
|
+
"bugs": {
|
|
14
|
+
"url": "https://github.com/coactionjs/coaction/issues"
|
|
15
|
+
},
|
|
16
16
|
"license": "MIT",
|
|
17
|
+
"author": "unadlib",
|
|
18
|
+
"repository": {
|
|
19
|
+
"type": "git",
|
|
20
|
+
"url": "https://github.com/coactionjs/coaction.git",
|
|
21
|
+
"directory": "packages/coaction-vue"
|
|
22
|
+
},
|
|
23
|
+
"files": [
|
|
24
|
+
"dist"
|
|
25
|
+
],
|
|
26
|
+
"sideEffects": false,
|
|
17
27
|
"main": "dist/index.js",
|
|
18
28
|
"module": "dist/index.mjs",
|
|
29
|
+
"types": "dist/index.d.ts",
|
|
19
30
|
"exports": {
|
|
20
31
|
".": {
|
|
21
32
|
"types": "./dist/index.d.ts",
|
|
@@ -25,42 +36,26 @@
|
|
|
25
36
|
},
|
|
26
37
|
"./package.json": "./package.json"
|
|
27
38
|
},
|
|
28
|
-
"
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
"dist"
|
|
32
|
-
],
|
|
33
|
-
"repository": {
|
|
34
|
-
"type": "git",
|
|
35
|
-
"url": "https://github.com/unadlib/coaction.git",
|
|
36
|
-
"directory": "packages/coaction-vue"
|
|
37
|
-
},
|
|
38
|
-
"bugs": {
|
|
39
|
-
"url": "https://github.com/unadlib/coaction/issues"
|
|
40
|
-
},
|
|
41
|
-
"peerDependencies": {
|
|
42
|
-
"coaction": "^1.5.0",
|
|
43
|
-
"vue": "^3.0.0"
|
|
44
|
-
},
|
|
45
|
-
"peerDependenciesMeta": {
|
|
46
|
-
"coaction": {
|
|
47
|
-
"optional": true
|
|
48
|
-
}
|
|
39
|
+
"publishConfig": {
|
|
40
|
+
"access": "public",
|
|
41
|
+
"provenance": true
|
|
49
42
|
},
|
|
50
43
|
"devDependencies": {
|
|
51
44
|
"@testing-library/vue": "^8.1.0",
|
|
52
45
|
"vue": "^3.5.30",
|
|
53
|
-
"coaction": "
|
|
46
|
+
"coaction": "2.0.0"
|
|
54
47
|
},
|
|
55
|
-
"
|
|
56
|
-
"
|
|
57
|
-
"
|
|
48
|
+
"peerDependencies": {
|
|
49
|
+
"coaction": "^2.0.0",
|
|
50
|
+
"vue": "^3.0.0"
|
|
58
51
|
},
|
|
59
|
-
"
|
|
52
|
+
"authors": [
|
|
53
|
+
"Michael Lin <unadlib@gmail.com> (https://github.com/unadlib)"
|
|
54
|
+
],
|
|
60
55
|
"scripts": {
|
|
61
56
|
"clean": "rm -rf dist",
|
|
62
57
|
"test": "vitest run test",
|
|
63
|
-
"build": "
|
|
64
|
-
"dev": "
|
|
58
|
+
"build": "node ../../scripts/build-package.mjs",
|
|
59
|
+
"dev": "node ../../scripts/build-package.mjs --watch"
|
|
65
60
|
}
|
|
66
61
|
}
|