@coaction/solid 1.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/LICENSE +21 -0
- package/README.md +37 -0
- package/dist/index.d.mts +34 -0
- package/dist/index.d.ts +34 -0
- package/dist/index.js +116 -0
- package/dist/index.mjs +113 -0
- package/package.json +67 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Michael Lin
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
# @coaction/solid
|
|
2
|
+
|
|
3
|
+

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

|
|
6
|
+
|
|
7
|
+
A Coaction integration tool for Solid.
|
|
8
|
+
|
|
9
|
+
## Installation
|
|
10
|
+
|
|
11
|
+
You can install it via npm, yarn or pnpm.
|
|
12
|
+
|
|
13
|
+
```sh
|
|
14
|
+
npm install coaction @coaction/solid
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
## Usage
|
|
18
|
+
|
|
19
|
+
```tsx
|
|
20
|
+
import { create } from '@coaction/solid';
|
|
21
|
+
|
|
22
|
+
const store = create((set) => ({
|
|
23
|
+
count: 0,
|
|
24
|
+
increment() {
|
|
25
|
+
set((draft) => {
|
|
26
|
+
draft.count += 1;
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
}));
|
|
30
|
+
|
|
31
|
+
const count = store((state) => state.count);
|
|
32
|
+
console.log(count());
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
## Documentation
|
|
36
|
+
|
|
37
|
+
You can find the documentation [here](https://github.com/unadlib/coaction).
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { ISlices, Slice, StoreOptions, Store, SliceState, ClientStoreOptions, Asyncify } from 'coaction';
|
|
2
|
+
export * from 'coaction';
|
|
3
|
+
import { Accessor } from 'solid-js';
|
|
4
|
+
|
|
5
|
+
type SelectorOptions = {
|
|
6
|
+
autoSelector?: boolean;
|
|
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]>;
|
|
10
|
+
};
|
|
11
|
+
type StoreReturn<T extends object> = Store<T> & {
|
|
12
|
+
<P>(selector: (state: T) => P): Accessor<P>;
|
|
13
|
+
(options: {
|
|
14
|
+
autoSelector: true;
|
|
15
|
+
}): AutoSelector<T>;
|
|
16
|
+
(options?: SelectorOptions): Accessor<T>;
|
|
17
|
+
};
|
|
18
|
+
type StoreWithAsyncFunction<T extends object, D extends true | false = false> = Store<Asyncify<T, D>> & {
|
|
19
|
+
<P>(selector: (state: Asyncify<T, D>) => P): Accessor<P>;
|
|
20
|
+
(options: {
|
|
21
|
+
autoSelector: true;
|
|
22
|
+
}): AutoSelector<Asyncify<T, D>>;
|
|
23
|
+
(options?: SelectorOptions): Accessor<Asyncify<T, D>>;
|
|
24
|
+
};
|
|
25
|
+
type CreateState = ISlices | Record<string, Slice<any>>;
|
|
26
|
+
type Creator = {
|
|
27
|
+
<T extends Record<string, Slice<any>>>(createState: T, options?: StoreOptions<T>): StoreReturn<SliceState<T>>;
|
|
28
|
+
<T extends ISlices>(createState: Slice<T>, options?: StoreOptions<T>): StoreReturn<T>;
|
|
29
|
+
<T extends Record<string, Slice<any>>>(createState: T, options?: ClientStoreOptions<T>): StoreWithAsyncFunction<SliceState<T>, true>;
|
|
30
|
+
<T extends ISlices>(createState: Slice<T>, options?: ClientStoreOptions<T>): StoreWithAsyncFunction<T>;
|
|
31
|
+
};
|
|
32
|
+
declare const create: Creator;
|
|
33
|
+
|
|
34
|
+
export { type CreateState, type Creator, type StoreReturn, type StoreWithAsyncFunction, create };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { ISlices, Slice, StoreOptions, Store, SliceState, ClientStoreOptions, Asyncify } from 'coaction';
|
|
2
|
+
export * from 'coaction';
|
|
3
|
+
import { Accessor } from 'solid-js';
|
|
4
|
+
|
|
5
|
+
type SelectorOptions = {
|
|
6
|
+
autoSelector?: boolean;
|
|
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]>;
|
|
10
|
+
};
|
|
11
|
+
type StoreReturn<T extends object> = Store<T> & {
|
|
12
|
+
<P>(selector: (state: T) => P): Accessor<P>;
|
|
13
|
+
(options: {
|
|
14
|
+
autoSelector: true;
|
|
15
|
+
}): AutoSelector<T>;
|
|
16
|
+
(options?: SelectorOptions): Accessor<T>;
|
|
17
|
+
};
|
|
18
|
+
type StoreWithAsyncFunction<T extends object, D extends true | false = false> = Store<Asyncify<T, D>> & {
|
|
19
|
+
<P>(selector: (state: Asyncify<T, D>) => P): Accessor<P>;
|
|
20
|
+
(options: {
|
|
21
|
+
autoSelector: true;
|
|
22
|
+
}): AutoSelector<Asyncify<T, D>>;
|
|
23
|
+
(options?: SelectorOptions): Accessor<Asyncify<T, D>>;
|
|
24
|
+
};
|
|
25
|
+
type CreateState = ISlices | Record<string, Slice<any>>;
|
|
26
|
+
type Creator = {
|
|
27
|
+
<T extends Record<string, Slice<any>>>(createState: T, options?: StoreOptions<T>): StoreReturn<SliceState<T>>;
|
|
28
|
+
<T extends ISlices>(createState: Slice<T>, options?: StoreOptions<T>): StoreReturn<T>;
|
|
29
|
+
<T extends Record<string, Slice<any>>>(createState: T, options?: ClientStoreOptions<T>): StoreWithAsyncFunction<SliceState<T>, true>;
|
|
30
|
+
<T extends ISlices>(createState: Slice<T>, options?: ClientStoreOptions<T>): StoreWithAsyncFunction<T>;
|
|
31
|
+
};
|
|
32
|
+
declare const create: Creator;
|
|
33
|
+
|
|
34
|
+
export { type CreateState, type Creator, type StoreReturn, type StoreWithAsyncFunction, create };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
|
|
19
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
20
|
+
|
|
21
|
+
// index.ts
|
|
22
|
+
var index_exports = {};
|
|
23
|
+
__export(index_exports, {
|
|
24
|
+
create: () => create
|
|
25
|
+
});
|
|
26
|
+
module.exports = __toCommonJS(index_exports);
|
|
27
|
+
|
|
28
|
+
// src/index.ts
|
|
29
|
+
var src_exports = {};
|
|
30
|
+
__export(src_exports, {
|
|
31
|
+
create: () => create
|
|
32
|
+
});
|
|
33
|
+
var import_coaction = require("coaction");
|
|
34
|
+
var import_solid_js = require("solid-js");
|
|
35
|
+
__reExport(src_exports, require("coaction"));
|
|
36
|
+
var createAutoSelector = (store, getVersion) => {
|
|
37
|
+
const state = store.getState();
|
|
38
|
+
if (!store.isSliceStore) {
|
|
39
|
+
const autoSelector2 = {};
|
|
40
|
+
const descriptors = Object.getOwnPropertyDescriptors(state);
|
|
41
|
+
for (const key in descriptors) {
|
|
42
|
+
const descriptor = descriptors[key];
|
|
43
|
+
if (typeof descriptor.value === "function") {
|
|
44
|
+
autoSelector2[key] = descriptor.value.bind(state);
|
|
45
|
+
} else {
|
|
46
|
+
autoSelector2[key] = () => {
|
|
47
|
+
getVersion();
|
|
48
|
+
return store.getState()[key];
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
return autoSelector2;
|
|
53
|
+
}
|
|
54
|
+
const autoSelector = {};
|
|
55
|
+
for (const sliceKey in 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 in 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
|
+
});
|
|
109
|
+
};
|
|
110
|
+
|
|
111
|
+
// index.ts
|
|
112
|
+
__reExport(index_exports, src_exports, module.exports);
|
|
113
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
114
|
+
0 && (module.exports = {
|
|
115
|
+
create
|
|
116
|
+
});
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
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";
|
|
32
|
+
import { createSignal } from "solid-js";
|
|
33
|
+
import * as coaction_star from "coaction";
|
|
34
|
+
var createAutoSelector = (store, getVersion) => {
|
|
35
|
+
const state = store.getState();
|
|
36
|
+
if (!store.isSliceStore) {
|
|
37
|
+
const autoSelector2 = {};
|
|
38
|
+
const descriptors = Object.getOwnPropertyDescriptors(state);
|
|
39
|
+
for (const key in descriptors) {
|
|
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 in 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 in 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;
|
|
74
|
+
};
|
|
75
|
+
var create = (createState, options) => {
|
|
76
|
+
const store = createVanilla(createState, options);
|
|
77
|
+
const [version, setVersion] = createSignal(0, {
|
|
78
|
+
equals: false
|
|
79
|
+
});
|
|
80
|
+
const unsubscribe = store.subscribe(() => {
|
|
81
|
+
setVersion((value) => value + 1);
|
|
82
|
+
});
|
|
83
|
+
const baseDestroy = store.destroy;
|
|
84
|
+
store.destroy = () => {
|
|
85
|
+
unsubscribe();
|
|
86
|
+
baseDestroy();
|
|
87
|
+
};
|
|
88
|
+
let autoSelector;
|
|
89
|
+
return wrapStore(store, (selector) => {
|
|
90
|
+
if (typeof selector === "function") {
|
|
91
|
+
return () => {
|
|
92
|
+
version();
|
|
93
|
+
return selector(store.getState());
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
if (selector?.autoSelector) {
|
|
97
|
+
if (!autoSelector) {
|
|
98
|
+
autoSelector = createAutoSelector(store, version);
|
|
99
|
+
}
|
|
100
|
+
return autoSelector;
|
|
101
|
+
}
|
|
102
|
+
return () => {
|
|
103
|
+
version();
|
|
104
|
+
return store.getState();
|
|
105
|
+
};
|
|
106
|
+
});
|
|
107
|
+
};
|
|
108
|
+
|
|
109
|
+
// index.ts
|
|
110
|
+
__reExport(index_exports, src_exports);
|
|
111
|
+
export {
|
|
112
|
+
create
|
|
113
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@coaction/solid",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "A Coaction integration tool for Solid",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"state",
|
|
7
|
+
"coaction",
|
|
8
|
+
"solid",
|
|
9
|
+
"signals"
|
|
10
|
+
],
|
|
11
|
+
"authors": [
|
|
12
|
+
"Michael Lin <unadlib@gmail.com> (https://github.com/unadlib)"
|
|
13
|
+
],
|
|
14
|
+
"homepage": "https://github.com/unadlib/coaction/tree/main/packages/coaction-solid#readme",
|
|
15
|
+
"license": "MIT",
|
|
16
|
+
"main": "dist/index.js",
|
|
17
|
+
"module": "dist/index.mjs",
|
|
18
|
+
"exports": {
|
|
19
|
+
".": {
|
|
20
|
+
"types": "./dist/index.d.ts",
|
|
21
|
+
"import": "./dist/index.mjs",
|
|
22
|
+
"require": "./dist/index.js",
|
|
23
|
+
"default": "./dist/index.mjs"
|
|
24
|
+
},
|
|
25
|
+
"./package.json": "./package.json"
|
|
26
|
+
},
|
|
27
|
+
"types": "dist/index.d.ts",
|
|
28
|
+
"sideEffects": false,
|
|
29
|
+
"files": [
|
|
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": "^0.2.0",
|
|
42
|
+
"solid-js": "^1.8.0"
|
|
43
|
+
},
|
|
44
|
+
"peerDependenciesMeta": {
|
|
45
|
+
"coaction": {
|
|
46
|
+
"optional": true
|
|
47
|
+
},
|
|
48
|
+
"solid-js": {
|
|
49
|
+
"optional": true
|
|
50
|
+
}
|
|
51
|
+
},
|
|
52
|
+
"devDependencies": {
|
|
53
|
+
"coaction": "^0.2.0",
|
|
54
|
+
"solid-js": "^1.9.10"
|
|
55
|
+
},
|
|
56
|
+
"publishConfig": {
|
|
57
|
+
"access": "public",
|
|
58
|
+
"provenance": true
|
|
59
|
+
},
|
|
60
|
+
"author": "unadlib",
|
|
61
|
+
"scripts": {
|
|
62
|
+
"clean": "rm -rf dist",
|
|
63
|
+
"test": "vitest run test",
|
|
64
|
+
"build": "tsup index.ts --format cjs,esm --dts --clean --out-dir dist",
|
|
65
|
+
"dev": "tsup index.ts --format cjs,esm --dts --watch --out-dir dist"
|
|
66
|
+
}
|
|
67
|
+
}
|