ccstate-solid 4.13.0 → 5.2.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 CHANGED
@@ -1,5 +1,34 @@
1
1
  # ccstate-solid
2
2
 
3
+ ## 5.2.0
4
+
5
+ ### Patch Changes
6
+
7
+ - Updated dependencies [304895d]
8
+ - ccstate@5.2.0
9
+
10
+ ## 5.1.0
11
+
12
+ ### Patch Changes
13
+
14
+ - ccstate@5.1.0
15
+
16
+ ## 5.0.0
17
+
18
+ ### Major Changes
19
+
20
+ - 2fdba09: feat: provide watch method to replace sub
21
+
22
+ ### Minor Changes
23
+
24
+ - 52c52fd: refactor: remove defaultStore
25
+
26
+ ### Patch Changes
27
+
28
+ - Updated dependencies [2fdba09]
29
+ - Updated dependencies [52c52fd]
30
+ - ccstate@5.0.0
31
+
3
32
  ## 4.13.0
4
33
 
5
34
  ### Patch Changes
Binary file
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
- return ccstate.getDefaultStore();
59
+ throw new Error('useStore must be used within a StoreProvider');
61
60
  }
62
61
  return store;
63
62
  }
64
63
 
65
- function useGet(atom) {
64
+ function useGet(signal$) {
66
65
  var store = useStore();
67
- var _createSignal = solidJs.createSignal(store.get(atom)),
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 unsub = store.sub(atom, ccstate.command(function () {
70
+ var controller = new AbortController();
71
+ store.watch(function (get) {
72
72
  setValue(function () {
73
- return store.get(atom);
73
+ return get(signal$);
74
74
  });
75
- }));
75
+ }, {
76
+ signal: controller.signal
77
+ });
76
78
  solidJs.onCleanup(function () {
77
- unsub();
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>(atom: State<T> | Computed<T>): solid_js.Accessor<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>(atom: State<T> | Computed<T>): solid_js.Accessor<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
- return getDefaultStore();
57
+ throw new Error('useStore must be used within a StoreProvider');
59
58
  }
60
59
  return store;
61
60
  }
62
61
 
63
- function useGet(atom) {
62
+ function useGet(signal$) {
64
63
  var store = useStore();
65
- var _createSignal = createSignal(store.get(atom)),
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 unsub = store.sub(atom, command(function () {
68
+ var controller = new AbortController();
69
+ store.watch(function (get) {
70
70
  setValue(function () {
71
- return store.get(atom);
71
+ return get(signal$);
72
72
  });
73
- }));
73
+ }, {
74
+ signal: controller.signal
75
+ });
74
76
  onCleanup(function () {
75
- unsub();
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": "4.13.0",
3
+ "version": "5.2.0",
4
4
  "description": "CCState Solid.js Hooks",
5
5
  "repository": {
6
6
  "type": "git",
@@ -8,6 +8,10 @@
8
8
  },
9
9
  "license": "MIT",
10
10
  "type": "module",
11
+ "scripts": {
12
+ "build": "rollup -c",
13
+ "prebuild": "shx rm -rf dist"
14
+ },
11
15
  "main": "./dist/index.cjs",
12
16
  "module": "./dist/index.js",
13
17
  "exports": {
@@ -20,7 +24,7 @@
20
24
  "solid-js": ">=1.6.0"
21
25
  },
22
26
  "dependencies": {
23
- "ccstate": "^4.13.0"
27
+ "ccstate": "workspace:^"
24
28
  },
25
29
  "devDependencies": {
26
30
  "@babel/preset-env": "^7.26.0",
@@ -30,6 +34,7 @@
30
34
  "@solidjs/testing-library": "^0.8.10",
31
35
  "@testing-library/jest-dom": "^6.6.3",
32
36
  "@testing-library/user-event": "^14.5.2",
37
+ "ccstate": "workspace:^",
33
38
  "happy-dom": "^15.11.7",
34
39
  "jest-leak-detector": "^29.7.0",
35
40
  "json": "^11.0.0",
@@ -39,11 +44,6 @@
39
44
  "signal-timers": "^1.0.4",
40
45
  "solid-js": "^1.9.3",
41
46
  "vite-plugin-solid": "^2.11.0",
42
- "vitest": "^2.1.8",
43
- "ccstate": "^4.13.0"
44
- },
45
- "scripts": {
46
- "build": "rollup -c",
47
- "prebuild": "shx rm -rf dist"
47
+ "vitest": "^2.1.8"
48
48
  }
49
- }
49
+ }
@@ -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, getDefaultStore } from 'ccstate';
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 screen = render(() => <App />);
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
- getDefaultStore().set(base$, 1);
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 screen = render(() => <App />);
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
- getDefaultStore().set(base$, 1);
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 screen = render(() => <App />);
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 screen = render(() => <App />);
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
- getDefaultStore().set(state1$, 1);
79
- getDefaultStore().set(state2$, 2);
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 screen = render(() => <App />);
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 screen = render(() => <App />);
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 use default store if no provider', () => {
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
- const screen = render(() => <App />);
148
- expect(screen.getByText('10')).toBeInTheDocument();
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.getSubscribeGraph()).toHaveLength(1);
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.getSubscribeGraph()).toHaveLength(0);
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
- return getDefaultStore();
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 { command, type Computed, type State } from 'ccstate';
2
+ import { type Computed, type State } from 'ccstate';
3
3
  import { createSignal, onCleanup } from 'solid-js';
4
4
 
5
- export function useGet<T>(atom: State<T> | Computed<T>) {
5
+ export function useGet<T>(signal$: State<T> | Computed<T>) {
6
6
  const store = useStore();
7
- const [value, setValue] = createSignal<T>(store.get(atom));
7
+ const [value, setValue] = createSignal<T>(store.get(signal$));
8
8
 
9
- const unsub = store.sub(
10
- atom,
11
- command(() => {
12
- setValue(() => store.get(atom));
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
- unsub();
21
+ controller.abort();
18
22
  });
19
23
 
20
24
  return value;
package/LICENSE DELETED
@@ -1,21 +0,0 @@
1
- MIT License
2
-
3
- Copyright (c) 2019 e7h4n
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 EFFECT 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.