ccstate-solid 4.12.0 → 5.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/CHANGELOG.md +22 -0
- package/dist/index.cjs +10 -8
- package/dist/index.d.cts +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +10 -8
- package/package.json +3 -3
- package/src/__tests__/get-and-set.test.tsx +47 -17
- package/src/provider.ts +1 -2
- package/src/useGet.ts +13 -9
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,27 @@
|
|
|
1
1
|
# ccstate-solid
|
|
2
2
|
|
|
3
|
+
## 5.0.0
|
|
4
|
+
|
|
5
|
+
### Major Changes
|
|
6
|
+
|
|
7
|
+
- 2fdba09: feat: provide watch method to replace sub
|
|
8
|
+
|
|
9
|
+
### Minor Changes
|
|
10
|
+
|
|
11
|
+
- 52c52fd: refactor: remove defaultStore
|
|
12
|
+
|
|
13
|
+
### Patch Changes
|
|
14
|
+
|
|
15
|
+
- Updated dependencies [2fdba09]
|
|
16
|
+
- Updated dependencies [52c52fd]
|
|
17
|
+
- ccstate@5.0.0
|
|
18
|
+
|
|
19
|
+
## 4.13.0
|
|
20
|
+
|
|
21
|
+
### Patch Changes
|
|
22
|
+
|
|
23
|
+
- ccstate@4.13.0
|
|
24
|
+
|
|
3
25
|
## 4.12.0
|
|
4
26
|
|
|
5
27
|
### Patch Changes
|
package/dist/index.cjs
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
var solidJs = require('solid-js');
|
|
4
|
-
var ccstate = require('ccstate');
|
|
5
4
|
|
|
6
5
|
function _arrayLikeToArray(r, a) {
|
|
7
6
|
(null == a || a > r.length) && (a = r.length);
|
|
@@ -57,24 +56,27 @@ var StoreProvider = StoreContext.Provider;
|
|
|
57
56
|
function useStore() {
|
|
58
57
|
var store = solidJs.useContext(StoreContext);
|
|
59
58
|
if (!store) {
|
|
60
|
-
|
|
59
|
+
throw new Error('useStore must be used within a StoreProvider');
|
|
61
60
|
}
|
|
62
61
|
return store;
|
|
63
62
|
}
|
|
64
63
|
|
|
65
|
-
function useGet(
|
|
64
|
+
function useGet(signal$) {
|
|
66
65
|
var store = useStore();
|
|
67
|
-
var _createSignal = solidJs.createSignal(store.get(
|
|
66
|
+
var _createSignal = solidJs.createSignal(store.get(signal$)),
|
|
68
67
|
_createSignal2 = _slicedToArray(_createSignal, 2),
|
|
69
68
|
value = _createSignal2[0],
|
|
70
69
|
setValue = _createSignal2[1];
|
|
71
|
-
var
|
|
70
|
+
var controller = new AbortController();
|
|
71
|
+
store.watch(function (get) {
|
|
72
72
|
setValue(function () {
|
|
73
|
-
return
|
|
73
|
+
return get(signal$);
|
|
74
74
|
});
|
|
75
|
-
}
|
|
75
|
+
}, {
|
|
76
|
+
signal: controller.signal
|
|
77
|
+
});
|
|
76
78
|
solidJs.onCleanup(function () {
|
|
77
|
-
|
|
79
|
+
controller.abort();
|
|
78
80
|
});
|
|
79
81
|
return value;
|
|
80
82
|
}
|
package/dist/index.d.cts
CHANGED
|
@@ -2,7 +2,7 @@ import * as solid_js from 'solid-js';
|
|
|
2
2
|
import { Resource } from 'solid-js';
|
|
3
3
|
import { State, Computed, Command, StateArg, Store } from 'ccstate';
|
|
4
4
|
|
|
5
|
-
declare function useGet<T>(
|
|
5
|
+
declare function useGet<T>(signal$: State<T> | Computed<T>): solid_js.Accessor<T>;
|
|
6
6
|
|
|
7
7
|
type ValueSetter<T> = (val: StateArg<T>) => void;
|
|
8
8
|
type CommandInvoker<T, CommandArgs extends unknown[]> = (...args: CommandArgs) => T;
|
package/dist/index.d.ts
CHANGED
|
@@ -2,7 +2,7 @@ import * as solid_js from 'solid-js';
|
|
|
2
2
|
import { Resource } from 'solid-js';
|
|
3
3
|
import { State, Computed, Command, StateArg, Store } from 'ccstate';
|
|
4
4
|
|
|
5
|
-
declare function useGet<T>(
|
|
5
|
+
declare function useGet<T>(signal$: State<T> | Computed<T>): solid_js.Accessor<T>;
|
|
6
6
|
|
|
7
7
|
type ValueSetter<T> = (val: StateArg<T>) => void;
|
|
8
8
|
type CommandInvoker<T, CommandArgs extends unknown[]> = (...args: CommandArgs) => T;
|
package/dist/index.js
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { createContext, useContext, createSignal, onCleanup, createResource } from 'solid-js';
|
|
2
|
-
import { getDefaultStore, command } from 'ccstate';
|
|
3
2
|
|
|
4
3
|
function _arrayLikeToArray(r, a) {
|
|
5
4
|
(null == a || a > r.length) && (a = r.length);
|
|
@@ -55,24 +54,27 @@ var StoreProvider = StoreContext.Provider;
|
|
|
55
54
|
function useStore() {
|
|
56
55
|
var store = useContext(StoreContext);
|
|
57
56
|
if (!store) {
|
|
58
|
-
|
|
57
|
+
throw new Error('useStore must be used within a StoreProvider');
|
|
59
58
|
}
|
|
60
59
|
return store;
|
|
61
60
|
}
|
|
62
61
|
|
|
63
|
-
function useGet(
|
|
62
|
+
function useGet(signal$) {
|
|
64
63
|
var store = useStore();
|
|
65
|
-
var _createSignal = createSignal(store.get(
|
|
64
|
+
var _createSignal = createSignal(store.get(signal$)),
|
|
66
65
|
_createSignal2 = _slicedToArray(_createSignal, 2),
|
|
67
66
|
value = _createSignal2[0],
|
|
68
67
|
setValue = _createSignal2[1];
|
|
69
|
-
var
|
|
68
|
+
var controller = new AbortController();
|
|
69
|
+
store.watch(function (get) {
|
|
70
70
|
setValue(function () {
|
|
71
|
-
return
|
|
71
|
+
return get(signal$);
|
|
72
72
|
});
|
|
73
|
-
}
|
|
73
|
+
}, {
|
|
74
|
+
signal: controller.signal
|
|
75
|
+
});
|
|
74
76
|
onCleanup(function () {
|
|
75
|
-
|
|
77
|
+
controller.abort();
|
|
76
78
|
});
|
|
77
79
|
return value;
|
|
78
80
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ccstate-solid",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "5.0.0",
|
|
4
4
|
"description": "CCState Solid.js Hooks",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -20,7 +20,7 @@
|
|
|
20
20
|
"solid-js": ">=1.6.0"
|
|
21
21
|
},
|
|
22
22
|
"dependencies": {
|
|
23
|
-
"ccstate": "^
|
|
23
|
+
"ccstate": "^5.0.0"
|
|
24
24
|
},
|
|
25
25
|
"devDependencies": {
|
|
26
26
|
"@babel/preset-env": "^7.26.0",
|
|
@@ -40,7 +40,7 @@
|
|
|
40
40
|
"solid-js": "^1.9.3",
|
|
41
41
|
"vite-plugin-solid": "^2.11.0",
|
|
42
42
|
"vitest": "^2.1.8",
|
|
43
|
-
"ccstate": "^
|
|
43
|
+
"ccstate": "^5.0.0"
|
|
44
44
|
},
|
|
45
45
|
"scripts": {
|
|
46
46
|
"build": "rollup -c",
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { render } from '@solidjs/testing-library';
|
|
2
2
|
import userEvent from '@testing-library/user-event';
|
|
3
3
|
import { expect, it } from 'vitest';
|
|
4
|
-
import { computed, command, state, createDebugStore,
|
|
4
|
+
import { computed, command, state, createDebugStore, createStore } from 'ccstate';
|
|
5
5
|
import { StoreProvider, useGet, useSet } from '..';
|
|
6
6
|
import { createSignal } from 'solid-js';
|
|
7
7
|
import '@testing-library/jest-dom/vitest';
|
|
@@ -14,10 +14,15 @@ it('using ccstate in solid', async () => {
|
|
|
14
14
|
return <div>{ret()}</div>;
|
|
15
15
|
}
|
|
16
16
|
|
|
17
|
-
const
|
|
17
|
+
const store = createStore();
|
|
18
|
+
const screen = render(() => (
|
|
19
|
+
<StoreProvider value={store}>
|
|
20
|
+
<App />
|
|
21
|
+
</StoreProvider>
|
|
22
|
+
));
|
|
18
23
|
|
|
19
24
|
expect(screen.getByText('0')).toBeInTheDocument();
|
|
20
|
-
|
|
25
|
+
store.set(base$, 1);
|
|
21
26
|
expect(await screen.findByText('1')).toBeInTheDocument();
|
|
22
27
|
});
|
|
23
28
|
|
|
@@ -30,10 +35,15 @@ it('computed should re-render', async () => {
|
|
|
30
35
|
return <div>{ret()}</div>;
|
|
31
36
|
}
|
|
32
37
|
|
|
33
|
-
const
|
|
38
|
+
const store = createStore();
|
|
39
|
+
const screen = render(() => (
|
|
40
|
+
<StoreProvider value={store}>
|
|
41
|
+
<App />
|
|
42
|
+
</StoreProvider>
|
|
43
|
+
));
|
|
34
44
|
|
|
35
45
|
expect(screen.getByText('0')).toBeInTheDocument();
|
|
36
|
-
|
|
46
|
+
store.set(base$, 1);
|
|
37
47
|
expect(await screen.findByText('2')).toBeInTheDocument();
|
|
38
48
|
});
|
|
39
49
|
|
|
@@ -51,7 +61,12 @@ it('user click counter should increment', async () => {
|
|
|
51
61
|
return <button onClick={onClick}>{ret()}</button>;
|
|
52
62
|
}
|
|
53
63
|
|
|
54
|
-
const
|
|
64
|
+
const store = createStore();
|
|
65
|
+
const screen = render(() => (
|
|
66
|
+
<StoreProvider value={store}>
|
|
67
|
+
<App />
|
|
68
|
+
</StoreProvider>
|
|
69
|
+
));
|
|
55
70
|
const button = screen.getByText('0');
|
|
56
71
|
expect(button).toBeInTheDocument();
|
|
57
72
|
|
|
@@ -72,11 +87,16 @@ it('two atom changes should re-render once', async () => {
|
|
|
72
87
|
return <div>{ret1() + ret2()}</div>;
|
|
73
88
|
}
|
|
74
89
|
|
|
75
|
-
const
|
|
90
|
+
const store = createDebugStore();
|
|
91
|
+
const screen = render(() => (
|
|
92
|
+
<StoreProvider value={store}>
|
|
93
|
+
<App />
|
|
94
|
+
</StoreProvider>
|
|
95
|
+
));
|
|
76
96
|
expect(screen.getByText('0')).toBeInTheDocument();
|
|
77
97
|
|
|
78
|
-
|
|
79
|
-
|
|
98
|
+
store.set(state1$, 1);
|
|
99
|
+
store.set(state2$, 2);
|
|
80
100
|
await Promise.resolve();
|
|
81
101
|
expect(screen.getByText('3')).toBeInTheDocument();
|
|
82
102
|
});
|
|
@@ -103,7 +123,12 @@ it('async callback will trigger rerender', async () => {
|
|
|
103
123
|
);
|
|
104
124
|
}
|
|
105
125
|
|
|
106
|
-
const
|
|
126
|
+
const store = createDebugStore();
|
|
127
|
+
const screen = render(() => (
|
|
128
|
+
<StoreProvider value={store}>
|
|
129
|
+
<App />
|
|
130
|
+
</StoreProvider>
|
|
131
|
+
));
|
|
107
132
|
const button = screen.getByText('0');
|
|
108
133
|
expect(button).toBeInTheDocument();
|
|
109
134
|
|
|
@@ -126,7 +151,12 @@ it('floating promise trigger rerender', async () => {
|
|
|
126
151
|
return <button onClick={onClick}>{val()}</button>;
|
|
127
152
|
}
|
|
128
153
|
|
|
129
|
-
const
|
|
154
|
+
const store = createDebugStore();
|
|
155
|
+
const screen = render(() => (
|
|
156
|
+
<StoreProvider value={store}>
|
|
157
|
+
<App />
|
|
158
|
+
</StoreProvider>
|
|
159
|
+
));
|
|
130
160
|
const button = screen.getByText('0');
|
|
131
161
|
expect(button).toBeInTheDocument();
|
|
132
162
|
|
|
@@ -135,17 +165,17 @@ it('floating promise trigger rerender', async () => {
|
|
|
135
165
|
expect(await screen.findByText('1')).toBeInTheDocument();
|
|
136
166
|
});
|
|
137
167
|
|
|
138
|
-
it('should
|
|
168
|
+
it('should throw Error store if no provider', () => {
|
|
139
169
|
const count$ = state(0);
|
|
140
|
-
getDefaultStore().set(count$, 10);
|
|
141
170
|
|
|
142
171
|
function App() {
|
|
143
172
|
const count = useGet(count$);
|
|
144
173
|
return <div>{count()}</div>;
|
|
145
174
|
}
|
|
146
175
|
|
|
147
|
-
|
|
148
|
-
|
|
176
|
+
expect(() => {
|
|
177
|
+
render(() => <App />);
|
|
178
|
+
}).toThrowError('useStore must be used within a StoreProvider');
|
|
149
179
|
});
|
|
150
180
|
|
|
151
181
|
it('will unmount when component cleanup', async () => {
|
|
@@ -181,9 +211,9 @@ it('will unmount when component cleanup', async () => {
|
|
|
181
211
|
));
|
|
182
212
|
|
|
183
213
|
const user = userEvent.setup();
|
|
184
|
-
expect(store.
|
|
214
|
+
expect(store.isMounted(base$)).toBeTruthy();
|
|
185
215
|
const button = screen.getByText('hide');
|
|
186
216
|
await user.click(button);
|
|
187
217
|
expect(await screen.findByText('unmounted')).toBeInTheDocument();
|
|
188
|
-
expect(store.
|
|
218
|
+
expect(store.isMounted(base$)).toBeFalsy();
|
|
189
219
|
});
|
package/src/provider.ts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { createContext, useContext } from 'solid-js';
|
|
2
|
-
import { getDefaultStore } from 'ccstate';
|
|
3
2
|
import type { Store } from 'ccstate';
|
|
4
3
|
|
|
5
4
|
const StoreContext = createContext<Store | null>(null);
|
|
@@ -10,7 +9,7 @@ export function useStore(): Store {
|
|
|
10
9
|
const store = useContext(StoreContext);
|
|
11
10
|
|
|
12
11
|
if (!store) {
|
|
13
|
-
|
|
12
|
+
throw new Error('useStore must be used within a StoreProvider');
|
|
14
13
|
}
|
|
15
14
|
|
|
16
15
|
return store;
|
package/src/useGet.ts
CHANGED
|
@@ -1,20 +1,24 @@
|
|
|
1
1
|
import { useStore } from './provider';
|
|
2
|
-
import {
|
|
2
|
+
import { type Computed, type State } from 'ccstate';
|
|
3
3
|
import { createSignal, onCleanup } from 'solid-js';
|
|
4
4
|
|
|
5
|
-
export function useGet<T>(
|
|
5
|
+
export function useGet<T>(signal$: State<T> | Computed<T>) {
|
|
6
6
|
const store = useStore();
|
|
7
|
-
const [value, setValue] = createSignal<T>(store.get(
|
|
7
|
+
const [value, setValue] = createSignal<T>(store.get(signal$));
|
|
8
8
|
|
|
9
|
-
const
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
9
|
+
const controller = new AbortController();
|
|
10
|
+
|
|
11
|
+
store.watch(
|
|
12
|
+
(get) => {
|
|
13
|
+
setValue(() => get(signal$));
|
|
14
|
+
},
|
|
15
|
+
{
|
|
16
|
+
signal: controller.signal,
|
|
17
|
+
},
|
|
14
18
|
);
|
|
15
19
|
|
|
16
20
|
onCleanup(() => {
|
|
17
|
-
|
|
21
|
+
controller.abort();
|
|
18
22
|
});
|
|
19
23
|
|
|
20
24
|
return value;
|