ccstate-svelte 4.13.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 CHANGED
@@ -1,5 +1,21 @@
1
1
  # ccstate-svelte
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
+
3
19
  ## 4.13.0
4
20
 
5
21
  ### Patch Changes
package/dist/index.cjs CHANGED
@@ -1,6 +1,5 @@
1
1
  'use strict';
2
2
 
3
- var ccstate = require('ccstate');
4
3
  var svelte = require('svelte');
5
4
 
6
5
  var StoreKey = Symbol('ccstate-svelte-store');
@@ -10,7 +9,7 @@ var provideStore = function provideStore(store) {
10
9
  var useStore = function useStore() {
11
10
  var store = svelte.getContext(StoreKey);
12
11
  if (!store) {
13
- return ccstate.getDefaultStore();
12
+ throw new Error('useStore must be used within a StoreProvider');
14
13
  }
15
14
  return store;
16
15
  };
@@ -19,12 +18,15 @@ function useGet(atom) {
19
18
  var store = useStore();
20
19
  return {
21
20
  subscribe: function subscribe(fn) {
22
- fn(store.get(atom));
23
- return store.sub(atom, ccstate.command(function (_ref) {
24
- var get = _ref.get;
25
- var nextValue = get(atom);
26
- fn(nextValue);
27
- }));
21
+ var controller = new AbortController();
22
+ store.watch(function (get) {
23
+ fn(get(atom));
24
+ }, {
25
+ signal: controller.signal
26
+ });
27
+ return function () {
28
+ controller.abort();
29
+ };
28
30
  }
29
31
  };
30
32
  }
package/dist/index.js CHANGED
@@ -1,4 +1,3 @@
1
- import { getDefaultStore, command } from 'ccstate';
2
1
  import { setContext, getContext } from 'svelte';
3
2
 
4
3
  var StoreKey = Symbol('ccstate-svelte-store');
@@ -8,7 +7,7 @@ var provideStore = function provideStore(store) {
8
7
  var useStore = function useStore() {
9
8
  var store = getContext(StoreKey);
10
9
  if (!store) {
11
- return getDefaultStore();
10
+ throw new Error('useStore must be used within a StoreProvider');
12
11
  }
13
12
  return store;
14
13
  };
@@ -17,12 +16,15 @@ function useGet(atom) {
17
16
  var store = useStore();
18
17
  return {
19
18
  subscribe: function subscribe(fn) {
20
- fn(store.get(atom));
21
- return store.sub(atom, command(function (_ref) {
22
- var get = _ref.get;
23
- var nextValue = get(atom);
24
- fn(nextValue);
25
- }));
19
+ var controller = new AbortController();
20
+ store.watch(function (get) {
21
+ fn(get(atom));
22
+ }, {
23
+ signal: controller.signal
24
+ });
25
+ return function () {
26
+ controller.abort();
27
+ };
26
28
  }
27
29
  };
28
30
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ccstate-svelte",
3
- "version": "4.13.0",
3
+ "version": "5.0.0",
4
4
  "description": "CCState Svelte Hooks",
5
5
  "repository": {
6
6
  "type": "git",
@@ -17,7 +17,7 @@
17
17
  }
18
18
  },
19
19
  "dependencies": {
20
- "ccstate": "^4.13.0"
20
+ "ccstate": "^5.0.0"
21
21
  },
22
22
  "devDependencies": {
23
23
  "@babel/preset-env": "^7.26.0",
@@ -38,7 +38,7 @@
38
38
  "svelte": "^5.15.0",
39
39
  "vite": "^6.0.5",
40
40
  "vitest": "^2.1.8",
41
- "ccstate": "^4.13.0"
41
+ "ccstate": "^5.0.0"
42
42
  },
43
43
  "scripts": {
44
44
  "build": "rollup -c",
@@ -1,6 +1,6 @@
1
1
  import { render, cleanup, screen } from '@testing-library/svelte';
2
2
  import { afterEach, expect, it } from 'vitest';
3
- import { createStore, getDefaultStore } from 'ccstate';
3
+ import { createStore } from 'ccstate';
4
4
  import '@testing-library/jest-dom/vitest';
5
5
  import App from './App.svelte';
6
6
  import { count$ } from './store';
@@ -13,10 +13,11 @@ afterEach(() => {
13
13
  const user = userEvent.setup();
14
14
 
15
15
  it('simple counter', async () => {
16
- render(App);
16
+ const store = createStore();
17
+ render(App, { props: { store } });
17
18
 
18
19
  expect(screen.getByText('count: 0')).toBeInTheDocument();
19
- getDefaultStore().set(count$, 1);
20
+ store.set(count$, 1);
20
21
 
21
22
  expect(await screen.findByText('count: 1')).toBeInTheDocument();
22
23
 
@@ -52,3 +53,9 @@ it('useStore custom store', async () => {
52
53
 
53
54
  expect(store.get(count$)).toBe(101);
54
55
  });
56
+
57
+ it('should throw error when store is not provided', () => {
58
+ expect(() => {
59
+ render(App);
60
+ }).toThrowError('useStore must be used within a StoreProvider');
61
+ });
@@ -1,6 +1,6 @@
1
1
  import { render, cleanup, screen } from '@testing-library/svelte';
2
2
  import { afterEach, expect, it } from 'vitest';
3
- import { createStore, getDefaultStore, state } from 'ccstate';
3
+ import { createStore, state } from 'ccstate';
4
4
  import '@testing-library/jest-dom/vitest';
5
5
  import Loadable from './Loadable.svelte';
6
6
  import LastLoadable from './LastLoadable.svelte';
@@ -36,11 +36,13 @@ afterEach(() => {
36
36
  });
37
37
 
38
38
  it('simple loadable', async () => {
39
+ const store = createStore();
39
40
  const promise$ = state(Promise.resolve('bar'));
40
41
  render(Loadable, {
41
42
  props: {
42
43
  promise$: () => promise$,
43
44
  },
45
+ context: new Map([[StoreKey, store]]),
44
46
  });
45
47
 
46
48
  expect(screen.getByText('Loading')).toBeInTheDocument();
@@ -49,11 +51,13 @@ it('simple loadable', async () => {
49
51
  });
50
52
 
51
53
  it('error loadable', async () => {
54
+ const store = createStore();
52
55
  const promise$ = state(Promise.reject(new Error('INTEST')));
53
56
  render(Loadable, {
54
57
  props: {
55
58
  promise$: () => promise$,
56
59
  },
60
+ context: new Map([[StoreKey, store]]),
57
61
  });
58
62
 
59
63
  expect(screen.getByText('Loading')).toBeInTheDocument();
@@ -157,6 +161,7 @@ it('simple resolved', async () => {
157
161
  props: {
158
162
  promise$: () => promise$,
159
163
  },
164
+ context: new Map([[StoreKey, createStore()]]),
160
165
  });
161
166
 
162
167
  expect(screen.getByText('Loading')).toBeInTheDocument();
@@ -165,11 +170,13 @@ it('simple resolved', async () => {
165
170
  });
166
171
 
167
172
  it('simple last resolved', async () => {
173
+ const store = createStore();
168
174
  const promise$ = state(Promise.resolve('bar'));
169
175
  render(LastResolved, {
170
176
  props: {
171
177
  promise$: () => promise$,
172
178
  },
179
+ context: new Map([[StoreKey, store]]),
173
180
  });
174
181
 
175
182
  expect(screen.getByText('Loading')).toBeInTheDocument();
@@ -177,7 +184,7 @@ it('simple last resolved', async () => {
177
184
  expect(screen.getByText('Result: bar')).toBeInTheDocument();
178
185
 
179
186
  const deferred = makeDefered<string>();
180
- getDefaultStore().set(promise$, deferred.promise);
187
+ store.set(promise$, deferred.promise);
181
188
  await expect(screen.findByText('Loading')).rejects.toThrow();
182
189
  deferred.resolve('second');
183
190
  expect(await screen.findByText('Result: second')).toBeInTheDocument();
@@ -3,21 +3,23 @@ import '@testing-library/jest-dom/vitest';
3
3
  import LeakDetector from 'jest-leak-detector';
4
4
  import { render, cleanup, screen } from '@testing-library/svelte';
5
5
  import { expect, it } from 'vitest';
6
- import { computed, getDefaultStore, type Computed } from 'ccstate';
6
+ import { computed, createStore, type Computed } from 'ccstate';
7
7
  import Memory from './Memory.svelte';
8
8
  import AsyncMemory from './AsyncMemory.svelte';
9
+ import { StoreKey } from '../provider';
9
10
 
10
11
  it('should release memory after view cleanup', async () => {
11
12
  let obj$: Computed<{ foo: string }> | undefined = computed(() => {
12
13
  return { foo: 'bar' };
13
14
  });
14
- const store = getDefaultStore();
15
+ const store = createStore();
15
16
  const leakDetector = new LeakDetector(store.get(obj$ as Computed<{ foo: string }>));
16
17
 
17
18
  render(Memory, {
18
19
  props: {
19
20
  obj$,
20
21
  },
22
+ context: new Map([[StoreKey, store]]),
21
23
  });
22
24
 
23
25
  expect(screen.getByText('obj: bar')).toBeInTheDocument();
@@ -32,13 +34,14 @@ it('should release promise memory after view cleanup', async () => {
32
34
  let obj$: Computed<Promise<{ foo: string }>> | undefined = computed(() => {
33
35
  return Promise.resolve({ foo: 'bar' });
34
36
  });
35
- const store = getDefaultStore();
37
+ const store = createStore();
36
38
  const leakDetector = new LeakDetector(await store.get(obj$ as Computed<Promise<{ foo: string }>>));
37
39
 
38
40
  render(AsyncMemory, {
39
41
  props: {
40
42
  obj$: () => obj$ as Computed<Promise<{ foo: string }>>,
41
43
  },
44
+ context: new Map([[StoreKey, store]]),
42
45
  });
43
46
 
44
47
  expect(await screen.findByText('obj: bar')).toBeInTheDocument();
package/src/provider.ts CHANGED
@@ -1,4 +1,3 @@
1
- import { getDefaultStore } from 'ccstate';
2
1
  import type { Store } from 'ccstate';
3
2
  import { getContext, setContext } from 'svelte';
4
3
 
@@ -12,7 +11,7 @@ export const useStore = (): Store => {
12
11
  const store = getContext(StoreKey);
13
12
 
14
13
  if (!store) {
15
- return getDefaultStore();
14
+ throw new Error('useStore must be used within a StoreProvider');
16
15
  }
17
16
 
18
17
  return store as Store;
package/src/useGet.ts CHANGED
@@ -1,19 +1,24 @@
1
1
  import { useStore } from './provider';
2
- import { command } from 'ccstate';
3
2
  import type { Computed, State } from 'ccstate';
4
3
 
5
4
  export function useGet<T>(atom: State<T> | Computed<T>) {
6
5
  const store = useStore();
7
6
  return {
8
7
  subscribe(fn: (payload: T) => void) {
9
- fn(store.get(atom));
10
- return store.sub(
11
- atom,
12
- command(({ get }) => {
13
- const nextValue = get(atom);
14
- fn(nextValue);
15
- }),
8
+ const controller = new AbortController();
9
+
10
+ store.watch(
11
+ (get) => {
12
+ fn(get(atom));
13
+ },
14
+ {
15
+ signal: controller.signal,
16
+ },
16
17
  );
18
+
19
+ return () => {
20
+ controller.abort();
21
+ };
17
22
  },
18
23
  };
19
24
  }