@coaction/solid 1.4.1 → 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 +90 -112
- package/dist/index.mjs +82 -109
- package/package.json +30 -38
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# @coaction/solid
|
|
2
2
|
|
|
3
|
-

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

|
|
6
6
|
|
|
@@ -34,4 +34,4 @@ console.log(count());
|
|
|
34
34
|
|
|
35
35
|
## Documentation
|
|
36
36
|
|
|
37
|
-
You can find the documentation [here](https://github.com/
|
|
37
|
+
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 { Accessor } from "solid-js";
|
|
3
|
+
export * from "coaction";
|
|
4
4
|
|
|
5
|
+
//#region \0rolldown/runtime.js
|
|
6
|
+
//#endregion
|
|
7
|
+
//#region packages/coaction-solid/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[] ? Accessor<T[K]> : T[K] extends object ? AutoSelector<T[K]> : Accessor<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[] ? Accessor<T> : T extends LeafObject ? Accessor<T> : T extends object ? AutoSelector<T> : Accessor<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): Accessor<P>;
|
|
16
|
+
(options: {
|
|
17
|
+
autoSelector: true;
|
|
18
|
+
}): AutoSelector<T>;
|
|
19
|
+
(options?: SelectorOptions): Accessor<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): Accessor<P>;
|
|
23
|
+
(options: {
|
|
24
|
+
autoSelector: true;
|
|
25
|
+
}): AutoSelector<Asyncify<T, D>>;
|
|
26
|
+
(options?: SelectorOptions): Accessor<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 { Accessor } from "solid-js";
|
|
3
|
+
export * from "coaction";
|
|
4
4
|
|
|
5
|
+
//#region \0rolldown/runtime.js
|
|
6
|
+
//#endregion
|
|
7
|
+
//#region packages/coaction-solid/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[] ? Accessor<T[K]> : T[K] extends object ? AutoSelector<T[K]> : Accessor<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[] ? Accessor<T> : T extends LeafObject ? Accessor<T> : T extends object ? AutoSelector<T> : Accessor<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): Accessor<P>;
|
|
16
|
+
(options: {
|
|
17
|
+
autoSelector: true;
|
|
18
|
+
}): AutoSelector<T>;
|
|
19
|
+
(options?: SelectorOptions): Accessor<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): Accessor<P>;
|
|
23
|
+
(options: {
|
|
24
|
+
autoSelector: true;
|
|
25
|
+
}): AutoSelector<Asyncify<T, D>>;
|
|
26
|
+
(options?: SelectorOptions): Accessor<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,116 +1,94 @@
|
|
|
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 solid_js = require("solid-js");
|
|
5
|
+
//#region packages/coaction-solid/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
|
-
|
|
11
|
+
const createAutoSelector = (store, getVersion) => {
|
|
12
|
+
const getPathValue = (path) => {
|
|
13
|
+
let current = store.getState();
|
|
14
|
+
for (const key of path) {
|
|
15
|
+
if (typeof current !== "object" && typeof current !== "function" || current === null) return;
|
|
16
|
+
current = current[key];
|
|
17
|
+
}
|
|
18
|
+
return current;
|
|
19
|
+
};
|
|
20
|
+
const createAction = (path) => ((...args) => {
|
|
21
|
+
const fn = getPathValue(path);
|
|
22
|
+
if (typeof fn !== "function") return;
|
|
23
|
+
const receiverPath = path.slice(0, -1);
|
|
24
|
+
const receiver = receiverPath.length ? getPathValue(receiverPath) : store.getState();
|
|
25
|
+
return fn.apply(receiver, args);
|
|
26
|
+
});
|
|
27
|
+
const createNode = (path, value, ancestors = []) => {
|
|
28
|
+
if (typeof value === "function") return createAction(path);
|
|
29
|
+
if (typeof value !== "object" || value === null || Array.isArray(value) || !isPlainObject(value)) return () => {
|
|
30
|
+
getVersion();
|
|
31
|
+
return getPathValue(path);
|
|
32
|
+
};
|
|
33
|
+
if (ancestors.includes(value)) return () => {
|
|
34
|
+
getVersion();
|
|
35
|
+
return getPathValue(path);
|
|
36
|
+
};
|
|
37
|
+
const node = {};
|
|
38
|
+
const nextAncestors = [...ancestors, value];
|
|
39
|
+
for (const key of getOwnEnumerableKeys(value)) node[key] = createNode([...path, key], value[key], nextAncestors);
|
|
40
|
+
return node;
|
|
41
|
+
};
|
|
42
|
+
const state = store.getState();
|
|
43
|
+
const autoSelector = {};
|
|
44
|
+
if (!store.isSliceStore) {
|
|
45
|
+
for (const key of getOwnEnumerableKeys(state)) autoSelector[key] = createNode([key], state[key]);
|
|
46
|
+
return autoSelector;
|
|
47
|
+
}
|
|
48
|
+
for (const sliceKey of getOwnEnumerableKeys(state)) {
|
|
49
|
+
const slice = state[sliceKey];
|
|
50
|
+
if (typeof slice !== "object" || slice === null) continue;
|
|
51
|
+
autoSelector[sliceKey] = createNode([sliceKey], slice);
|
|
52
|
+
}
|
|
53
|
+
return autoSelector;
|
|
17
54
|
};
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
getVersion();
|
|
48
|
-
return store.getState()[key];
|
|
49
|
-
};
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
return autoSelector2;
|
|
53
|
-
}
|
|
54
|
-
const autoSelector = {};
|
|
55
|
-
for (const sliceKey of Object.keys(state)) {
|
|
56
|
-
const slice = state[sliceKey];
|
|
57
|
-
if (typeof slice !== "object" || slice === null) {
|
|
58
|
-
continue;
|
|
59
|
-
}
|
|
60
|
-
const sliceAutoSelector = {};
|
|
61
|
-
const descriptors = Object.getOwnPropertyDescriptors(slice);
|
|
62
|
-
for (const key of Object.keys(descriptors)) {
|
|
63
|
-
const descriptor = descriptors[key];
|
|
64
|
-
if (typeof descriptor.value === "function") {
|
|
65
|
-
sliceAutoSelector[key] = descriptor.value.bind(slice);
|
|
66
|
-
} else {
|
|
67
|
-
sliceAutoSelector[key] = () => {
|
|
68
|
-
getVersion();
|
|
69
|
-
return store.getState()[sliceKey][key];
|
|
70
|
-
};
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
autoSelector[sliceKey] = sliceAutoSelector;
|
|
74
|
-
}
|
|
75
|
-
return autoSelector;
|
|
76
|
-
};
|
|
77
|
-
var create = (createState, options) => {
|
|
78
|
-
const store = (0, import_coaction.create)(createState, options);
|
|
79
|
-
const [version, setVersion] = (0, import_solid_js.createSignal)(0, {
|
|
80
|
-
equals: false
|
|
81
|
-
});
|
|
82
|
-
const unsubscribe = store.subscribe(() => {
|
|
83
|
-
setVersion((value) => value + 1);
|
|
84
|
-
});
|
|
85
|
-
const baseDestroy = store.destroy;
|
|
86
|
-
store.destroy = () => {
|
|
87
|
-
unsubscribe();
|
|
88
|
-
baseDestroy();
|
|
89
|
-
};
|
|
90
|
-
let autoSelector;
|
|
91
|
-
return (0, import_coaction.wrapStore)(store, (selector) => {
|
|
92
|
-
if (typeof selector === "function") {
|
|
93
|
-
return () => {
|
|
94
|
-
version();
|
|
95
|
-
return selector(store.getState());
|
|
96
|
-
};
|
|
97
|
-
}
|
|
98
|
-
if (selector?.autoSelector) {
|
|
99
|
-
if (!autoSelector) {
|
|
100
|
-
autoSelector = createAutoSelector(store, version);
|
|
101
|
-
}
|
|
102
|
-
return autoSelector;
|
|
103
|
-
}
|
|
104
|
-
return () => {
|
|
105
|
-
version();
|
|
106
|
-
return store.getState();
|
|
107
|
-
};
|
|
108
|
-
});
|
|
55
|
+
const create = (createState, options) => {
|
|
56
|
+
const store = (0, coaction.create)(createState, options);
|
|
57
|
+
const [version, setVersion] = (0, solid_js.createSignal)(0, { equals: false });
|
|
58
|
+
const unsubscribe = store.subscribe(() => {
|
|
59
|
+
setVersion((value) => value + 1);
|
|
60
|
+
});
|
|
61
|
+
const baseDestroy = store.destroy;
|
|
62
|
+
let destroyed = false;
|
|
63
|
+
store.destroy = () => {
|
|
64
|
+
if (destroyed) return;
|
|
65
|
+
destroyed = true;
|
|
66
|
+
unsubscribe();
|
|
67
|
+
baseDestroy();
|
|
68
|
+
};
|
|
69
|
+
let autoSelector;
|
|
70
|
+
return (0, coaction.wrapStore)(store, (selector) => {
|
|
71
|
+
if (typeof selector === "function") return () => {
|
|
72
|
+
version();
|
|
73
|
+
return selector(store.getState());
|
|
74
|
+
};
|
|
75
|
+
if (selector?.autoSelector) {
|
|
76
|
+
if (!autoSelector) autoSelector = createAutoSelector(store, version);
|
|
77
|
+
return autoSelector;
|
|
78
|
+
}
|
|
79
|
+
return () => {
|
|
80
|
+
version();
|
|
81
|
+
return store.getState();
|
|
82
|
+
};
|
|
83
|
+
});
|
|
109
84
|
};
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
85
|
+
//#endregion
|
|
86
|
+
exports.create = create;
|
|
87
|
+
Object.keys(coaction).forEach(function(k) {
|
|
88
|
+
if (k !== "default" && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {
|
|
89
|
+
enumerable: true,
|
|
90
|
+
get: function() {
|
|
91
|
+
return coaction[k];
|
|
92
|
+
}
|
|
93
|
+
});
|
|
116
94
|
});
|
package/dist/index.mjs
CHANGED
|
@@ -1,113 +1,86 @@
|
|
|
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 { createSignal } from "solid-js";
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
const descriptor = descriptors[key];
|
|
41
|
-
if (typeof descriptor.value === "function") {
|
|
42
|
-
autoSelector2[key] = descriptor.value.bind(state);
|
|
43
|
-
} else {
|
|
44
|
-
autoSelector2[key] = () => {
|
|
45
|
-
getVersion();
|
|
46
|
-
return store.getState()[key];
|
|
47
|
-
};
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
return autoSelector2;
|
|
51
|
-
}
|
|
52
|
-
const autoSelector = {};
|
|
53
|
-
for (const sliceKey of Object.keys(state)) {
|
|
54
|
-
const slice = state[sliceKey];
|
|
55
|
-
if (typeof slice !== "object" || slice === null) {
|
|
56
|
-
continue;
|
|
57
|
-
}
|
|
58
|
-
const sliceAutoSelector = {};
|
|
59
|
-
const descriptors = Object.getOwnPropertyDescriptors(slice);
|
|
60
|
-
for (const key of Object.keys(descriptors)) {
|
|
61
|
-
const descriptor = descriptors[key];
|
|
62
|
-
if (typeof descriptor.value === "function") {
|
|
63
|
-
sliceAutoSelector[key] = descriptor.value.bind(slice);
|
|
64
|
-
} else {
|
|
65
|
-
sliceAutoSelector[key] = () => {
|
|
66
|
-
getVersion();
|
|
67
|
-
return store.getState()[sliceKey][key];
|
|
68
|
-
};
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
autoSelector[sliceKey] = sliceAutoSelector;
|
|
72
|
-
}
|
|
73
|
-
return autoSelector;
|
|
3
|
+
export * from "coaction";
|
|
4
|
+
//#endregion
|
|
5
|
+
//#region packages/coaction-solid/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;
|
|
74
10
|
};
|
|
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
|
-
|
|
106
|
-
|
|
11
|
+
const createAutoSelector = (store, getVersion) => {
|
|
12
|
+
const getPathValue = (path) => {
|
|
13
|
+
let current = store.getState();
|
|
14
|
+
for (const key of path) {
|
|
15
|
+
if (typeof current !== "object" && typeof current !== "function" || current === null) return;
|
|
16
|
+
current = current[key];
|
|
17
|
+
}
|
|
18
|
+
return current;
|
|
19
|
+
};
|
|
20
|
+
const createAction = (path) => ((...args) => {
|
|
21
|
+
const fn = getPathValue(path);
|
|
22
|
+
if (typeof fn !== "function") return;
|
|
23
|
+
const receiverPath = path.slice(0, -1);
|
|
24
|
+
const receiver = receiverPath.length ? getPathValue(receiverPath) : store.getState();
|
|
25
|
+
return fn.apply(receiver, args);
|
|
26
|
+
});
|
|
27
|
+
const createNode = (path, value, ancestors = []) => {
|
|
28
|
+
if (typeof value === "function") return createAction(path);
|
|
29
|
+
if (typeof value !== "object" || value === null || Array.isArray(value) || !isPlainObject(value)) return () => {
|
|
30
|
+
getVersion();
|
|
31
|
+
return getPathValue(path);
|
|
32
|
+
};
|
|
33
|
+
if (ancestors.includes(value)) return () => {
|
|
34
|
+
getVersion();
|
|
35
|
+
return getPathValue(path);
|
|
36
|
+
};
|
|
37
|
+
const node = {};
|
|
38
|
+
const nextAncestors = [...ancestors, value];
|
|
39
|
+
for (const key of getOwnEnumerableKeys(value)) node[key] = createNode([...path, key], value[key], nextAncestors);
|
|
40
|
+
return node;
|
|
41
|
+
};
|
|
42
|
+
const state = store.getState();
|
|
43
|
+
const autoSelector = {};
|
|
44
|
+
if (!store.isSliceStore) {
|
|
45
|
+
for (const key of getOwnEnumerableKeys(state)) autoSelector[key] = createNode([key], state[key]);
|
|
46
|
+
return autoSelector;
|
|
47
|
+
}
|
|
48
|
+
for (const sliceKey of getOwnEnumerableKeys(state)) {
|
|
49
|
+
const slice = state[sliceKey];
|
|
50
|
+
if (typeof slice !== "object" || slice === null) continue;
|
|
51
|
+
autoSelector[sliceKey] = createNode([sliceKey], slice);
|
|
52
|
+
}
|
|
53
|
+
return autoSelector;
|
|
107
54
|
};
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
55
|
+
const create = (createState, options) => {
|
|
56
|
+
const store = create$1(createState, options);
|
|
57
|
+
const [version, setVersion] = createSignal(0, { equals: false });
|
|
58
|
+
const unsubscribe = store.subscribe(() => {
|
|
59
|
+
setVersion((value) => value + 1);
|
|
60
|
+
});
|
|
61
|
+
const baseDestroy = store.destroy;
|
|
62
|
+
let destroyed = false;
|
|
63
|
+
store.destroy = () => {
|
|
64
|
+
if (destroyed) return;
|
|
65
|
+
destroyed = true;
|
|
66
|
+
unsubscribe();
|
|
67
|
+
baseDestroy();
|
|
68
|
+
};
|
|
69
|
+
let autoSelector;
|
|
70
|
+
return wrapStore(store, (selector) => {
|
|
71
|
+
if (typeof selector === "function") return () => {
|
|
72
|
+
version();
|
|
73
|
+
return selector(store.getState());
|
|
74
|
+
};
|
|
75
|
+
if (selector?.autoSelector) {
|
|
76
|
+
if (!autoSelector) autoSelector = createAutoSelector(store, version);
|
|
77
|
+
return autoSelector;
|
|
78
|
+
}
|
|
79
|
+
return () => {
|
|
80
|
+
version();
|
|
81
|
+
return store.getState();
|
|
82
|
+
};
|
|
83
|
+
});
|
|
113
84
|
};
|
|
85
|
+
//#endregion
|
|
86
|
+
export { create };
|
package/package.json
CHANGED
|
@@ -1,20 +1,31 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@coaction/solid",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.0",
|
|
4
4
|
"description": "A Coaction integration tool for Solid",
|
|
5
5
|
"keywords": [
|
|
6
|
-
"state",
|
|
7
6
|
"coaction",
|
|
7
|
+
"signals",
|
|
8
8
|
"solid",
|
|
9
|
-
"
|
|
9
|
+
"state"
|
|
10
10
|
],
|
|
11
|
-
"
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
11
|
+
"homepage": "https://github.com/coactionjs/coaction/tree/main/packages/coaction-solid#readme",
|
|
12
|
+
"bugs": {
|
|
13
|
+
"url": "https://github.com/coactionjs/coaction/issues"
|
|
14
|
+
},
|
|
15
15
|
"license": "MIT",
|
|
16
|
+
"author": "unadlib",
|
|
17
|
+
"repository": {
|
|
18
|
+
"type": "git",
|
|
19
|
+
"url": "https://github.com/coactionjs/coaction.git",
|
|
20
|
+
"directory": "packages/coaction-solid"
|
|
21
|
+
},
|
|
22
|
+
"files": [
|
|
23
|
+
"dist"
|
|
24
|
+
],
|
|
25
|
+
"sideEffects": false,
|
|
16
26
|
"main": "dist/index.js",
|
|
17
27
|
"module": "dist/index.mjs",
|
|
28
|
+
"types": "dist/index.d.ts",
|
|
18
29
|
"exports": {
|
|
19
30
|
".": {
|
|
20
31
|
"types": "./dist/index.d.ts",
|
|
@@ -24,44 +35,25 @@
|
|
|
24
35
|
},
|
|
25
36
|
"./package.json": "./package.json"
|
|
26
37
|
},
|
|
27
|
-
"
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
"dist"
|
|
31
|
-
],
|
|
32
|
-
"repository": {
|
|
33
|
-
"type": "git",
|
|
34
|
-
"url": "https://github.com/unadlib/coaction.git",
|
|
35
|
-
"directory": "packages/coaction-solid"
|
|
36
|
-
},
|
|
37
|
-
"bugs": {
|
|
38
|
-
"url": "https://github.com/unadlib/coaction/issues"
|
|
39
|
-
},
|
|
40
|
-
"peerDependencies": {
|
|
41
|
-
"coaction": "^1.4.1",
|
|
42
|
-
"solid-js": "^1.8.0"
|
|
43
|
-
},
|
|
44
|
-
"peerDependenciesMeta": {
|
|
45
|
-
"coaction": {
|
|
46
|
-
"optional": true
|
|
47
|
-
},
|
|
48
|
-
"solid-js": {
|
|
49
|
-
"optional": true
|
|
50
|
-
}
|
|
38
|
+
"publishConfig": {
|
|
39
|
+
"access": "public",
|
|
40
|
+
"provenance": true
|
|
51
41
|
},
|
|
52
42
|
"devDependencies": {
|
|
53
43
|
"solid-js": "^1.9.10",
|
|
54
|
-
"coaction": "
|
|
44
|
+
"coaction": "2.0.0"
|
|
55
45
|
},
|
|
56
|
-
"
|
|
57
|
-
"
|
|
58
|
-
"
|
|
46
|
+
"peerDependencies": {
|
|
47
|
+
"coaction": "^2.0.0",
|
|
48
|
+
"solid-js": "^1.8.0"
|
|
59
49
|
},
|
|
60
|
-
"
|
|
50
|
+
"authors": [
|
|
51
|
+
"Michael Lin <unadlib@gmail.com> (https://github.com/unadlib)"
|
|
52
|
+
],
|
|
61
53
|
"scripts": {
|
|
62
54
|
"clean": "rm -rf dist",
|
|
63
55
|
"test": "vitest run test",
|
|
64
|
-
"build": "
|
|
65
|
-
"dev": "
|
|
56
|
+
"build": "node ../../scripts/build-package.mjs",
|
|
57
|
+
"dev": "node ../../scripts/build-package.mjs --watch"
|
|
66
58
|
}
|
|
67
59
|
}
|