ccstate-svelte 4.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 +11 -0
- package/LICENSE +21 -0
- package/babel.config.json +3 -0
- package/dist/index.cjs +51 -0
- package/dist/index.d.cts +13 -0
- package/dist/index.d.ts +13 -0
- package/dist/index.js +46 -0
- package/package.json +47 -0
- package/rollup.config.mjs +84 -0
- package/src/__tests__/App.svelte +13 -0
- package/src/__tests__/AsyncMemory.svelte +9 -0
- package/src/__tests__/Counter.svelte +20 -0
- package/src/__tests__/LastLoadable.svelte +15 -0
- package/src/__tests__/LastResolved.svelte +13 -0
- package/src/__tests__/Loadable.svelte +15 -0
- package/src/__tests__/Memory.svelte +9 -0
- package/src/__tests__/Prop.svelte +9 -0
- package/src/__tests__/Resolved.svelte +13 -0
- package/src/__tests__/counter.test.ts +54 -0
- package/src/__tests__/loadable.test.ts +184 -0
- package/src/__tests__/memory.test.ts +50 -0
- package/src/__tests__/prop.test.ts +25 -0
- package/src/__tests__/store.ts +11 -0
- package/src/__tests__/svelte.d.ts +1 -0
- package/src/index.ts +3 -0
- package/src/provider.ts +19 -0
- package/src/useGet.ts +19 -0
- package/src/useLoadable.ts +74 -0
- package/src/useResolved.ts +13 -0
- package/src/useSet.ts +22 -0
- package/tsconfig.json +7 -0
- package/vitest.config.ts +12 -0
package/CHANGELOG.md
ADDED
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
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.
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var ccstate = require('ccstate');
|
|
4
|
+
var svelte = require('svelte');
|
|
5
|
+
|
|
6
|
+
var StoreKey = Symbol('ccstate-svelte-store');
|
|
7
|
+
var provideStore = function provideStore(store) {
|
|
8
|
+
svelte.setContext(StoreKey, store);
|
|
9
|
+
};
|
|
10
|
+
var useStore = function useStore() {
|
|
11
|
+
var store = svelte.getContext(StoreKey);
|
|
12
|
+
if (!store) {
|
|
13
|
+
return ccstate.getDefaultStore();
|
|
14
|
+
}
|
|
15
|
+
return store;
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
function useGet(atom) {
|
|
19
|
+
var store = useStore();
|
|
20
|
+
return {
|
|
21
|
+
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
|
+
}));
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function useSet(atom) {
|
|
33
|
+
var store = useStore();
|
|
34
|
+
if ('write' in atom) {
|
|
35
|
+
return function () {
|
|
36
|
+
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
|
|
37
|
+
args[_key] = arguments[_key];
|
|
38
|
+
}
|
|
39
|
+
var ret = store.set.apply(store, [atom].concat(args));
|
|
40
|
+
return ret;
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
return function (value) {
|
|
44
|
+
store.set(atom, value);
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
exports.provideStore = provideStore;
|
|
49
|
+
exports.useGet = useGet;
|
|
50
|
+
exports.useSet = useSet;
|
|
51
|
+
exports.useStore = useStore;
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { State, Computed, Updater, Command, Store } from 'ccstate';
|
|
2
|
+
|
|
3
|
+
declare function useGet<T>(atom: State<T> | Computed<T>): {
|
|
4
|
+
subscribe(fn: (payload: T) => void): () => void;
|
|
5
|
+
};
|
|
6
|
+
|
|
7
|
+
declare function useSet<T>(atom: State<T>): (value: T | Updater<T>) => void;
|
|
8
|
+
declare function useSet<T, ARGS extends unknown[]>(atom: Command<T, ARGS>): (...args: ARGS) => T;
|
|
9
|
+
|
|
10
|
+
declare const provideStore: (store: Store) => void;
|
|
11
|
+
declare const useStore: () => Store;
|
|
12
|
+
|
|
13
|
+
export { provideStore, useGet, useSet, useStore };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { State, Computed, Updater, Command, Store } from 'ccstate';
|
|
2
|
+
|
|
3
|
+
declare function useGet<T>(atom: State<T> | Computed<T>): {
|
|
4
|
+
subscribe(fn: (payload: T) => void): () => void;
|
|
5
|
+
};
|
|
6
|
+
|
|
7
|
+
declare function useSet<T>(atom: State<T>): (value: T | Updater<T>) => void;
|
|
8
|
+
declare function useSet<T, ARGS extends unknown[]>(atom: Command<T, ARGS>): (...args: ARGS) => T;
|
|
9
|
+
|
|
10
|
+
declare const provideStore: (store: Store) => void;
|
|
11
|
+
declare const useStore: () => Store;
|
|
12
|
+
|
|
13
|
+
export { provideStore, useGet, useSet, useStore };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { getDefaultStore, command } from 'ccstate';
|
|
2
|
+
import { setContext, getContext } from 'svelte';
|
|
3
|
+
|
|
4
|
+
var StoreKey = Symbol('ccstate-svelte-store');
|
|
5
|
+
var provideStore = function provideStore(store) {
|
|
6
|
+
setContext(StoreKey, store);
|
|
7
|
+
};
|
|
8
|
+
var useStore = function useStore() {
|
|
9
|
+
var store = getContext(StoreKey);
|
|
10
|
+
if (!store) {
|
|
11
|
+
return getDefaultStore();
|
|
12
|
+
}
|
|
13
|
+
return store;
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
function useGet(atom) {
|
|
17
|
+
var store = useStore();
|
|
18
|
+
return {
|
|
19
|
+
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
|
+
}));
|
|
26
|
+
}
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function useSet(atom) {
|
|
31
|
+
var store = useStore();
|
|
32
|
+
if ('write' in atom) {
|
|
33
|
+
return function () {
|
|
34
|
+
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
|
|
35
|
+
args[_key] = arguments[_key];
|
|
36
|
+
}
|
|
37
|
+
var ret = store.set.apply(store, [atom].concat(args));
|
|
38
|
+
return ret;
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
return function (value) {
|
|
42
|
+
store.set(atom, value);
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export { provideStore, useGet, useSet, useStore };
|
package/package.json
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "ccstate-svelte",
|
|
3
|
+
"version": "4.0.0",
|
|
4
|
+
"description": "CCState Svelte Hooks",
|
|
5
|
+
"repository": {
|
|
6
|
+
"type": "git",
|
|
7
|
+
"url": "git+https://github.com/e7h4n/ccstate.git"
|
|
8
|
+
},
|
|
9
|
+
"license": "MIT",
|
|
10
|
+
"type": "module",
|
|
11
|
+
"main": "./dist/index.cjs",
|
|
12
|
+
"module": "./dist/index.js",
|
|
13
|
+
"exports": {
|
|
14
|
+
".": {
|
|
15
|
+
"import": "./dist/index.js",
|
|
16
|
+
"require": "./dist/index.cjs"
|
|
17
|
+
}
|
|
18
|
+
},
|
|
19
|
+
"peerDependencies": {
|
|
20
|
+
"ccstate": "^4.0.0"
|
|
21
|
+
},
|
|
22
|
+
"devDependencies": {
|
|
23
|
+
"@babel/preset-env": "^7.26.0",
|
|
24
|
+
"@babel/preset-typescript": "^7.26.0",
|
|
25
|
+
"@rollup/plugin-babel": "^6.0.4",
|
|
26
|
+
"@rollup/plugin-node-resolve": "^15.3.0",
|
|
27
|
+
"@sveltejs/vite-plugin-svelte": "^5.0.3",
|
|
28
|
+
"@testing-library/jest-dom": "^6.6.3",
|
|
29
|
+
"@testing-library/svelte": "^5.2.6",
|
|
30
|
+
"@testing-library/user-event": "^14.5.2",
|
|
31
|
+
"happy-dom": "^15.11.7",
|
|
32
|
+
"jest-leak-detector": "^29.7.0",
|
|
33
|
+
"json": "^11.0.0",
|
|
34
|
+
"rollup": "^4.28.1",
|
|
35
|
+
"rollup-plugin-dts": "^6.1.1",
|
|
36
|
+
"shx": "^0.3.4",
|
|
37
|
+
"signal-timers": "^1.0.4",
|
|
38
|
+
"svelte": "^5.15.0",
|
|
39
|
+
"vite": "^6.0.5",
|
|
40
|
+
"vitest": "^2.1.8",
|
|
41
|
+
"ccstate": "^4.0.0"
|
|
42
|
+
},
|
|
43
|
+
"scripts": {
|
|
44
|
+
"build": "rollup -c",
|
|
45
|
+
"prebuild": "shx rm -rf dist"
|
|
46
|
+
}
|
|
47
|
+
}
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
import * as fs from 'node:fs';
|
|
3
|
+
import * as path from 'node:path';
|
|
4
|
+
import { babel } from '@rollup/plugin-babel';
|
|
5
|
+
import { dts } from 'rollup-plugin-dts';
|
|
6
|
+
import { nodeResolve } from '@rollup/plugin-node-resolve';
|
|
7
|
+
|
|
8
|
+
const __dirname = path.dirname(new URL(import.meta.url).pathname);
|
|
9
|
+
const projectRootDir = path.resolve(__dirname);
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* @param {string} id
|
|
13
|
+
* @returns {boolean}
|
|
14
|
+
*/
|
|
15
|
+
function external(id) {
|
|
16
|
+
return !id.startsWith('.') && !id.startsWith(projectRootDir);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* @param {{input:string, targetCJS:string, targetES:string}} param0
|
|
21
|
+
* @returns {import('rollup').RollupOptions[]}
|
|
22
|
+
*/
|
|
23
|
+
function generateTarget({ input, targetCJS, targetES }) {
|
|
24
|
+
return [
|
|
25
|
+
{
|
|
26
|
+
input,
|
|
27
|
+
onwarn: (warning) => {
|
|
28
|
+
throw new Error(warning?.message);
|
|
29
|
+
},
|
|
30
|
+
external,
|
|
31
|
+
plugins: [
|
|
32
|
+
nodeResolve({
|
|
33
|
+
extensions: ['.ts'],
|
|
34
|
+
}),
|
|
35
|
+
babel({
|
|
36
|
+
exclude: 'node_modules/**',
|
|
37
|
+
extensions: ['.ts'],
|
|
38
|
+
babelHelpers: 'bundled',
|
|
39
|
+
configFile: path.resolve(projectRootDir, './babel.config.json'),
|
|
40
|
+
}),
|
|
41
|
+
],
|
|
42
|
+
output: [
|
|
43
|
+
{
|
|
44
|
+
file: targetCJS,
|
|
45
|
+
format: 'cjs',
|
|
46
|
+
},
|
|
47
|
+
{
|
|
48
|
+
file: targetES,
|
|
49
|
+
format: 'es',
|
|
50
|
+
},
|
|
51
|
+
],
|
|
52
|
+
},
|
|
53
|
+
{
|
|
54
|
+
input,
|
|
55
|
+
onwarn: (warning) => {
|
|
56
|
+
throw new Error(warning?.message);
|
|
57
|
+
},
|
|
58
|
+
external,
|
|
59
|
+
plugins: [
|
|
60
|
+
dts({
|
|
61
|
+
respectExternal: true,
|
|
62
|
+
tsconfig: path.resolve(projectRootDir, './tsconfig.json'),
|
|
63
|
+
// https://github.com/Swatinem/rollup-plugin-dts/issues/143
|
|
64
|
+
compilerOptions: { preserveSymlinks: false },
|
|
65
|
+
}),
|
|
66
|
+
],
|
|
67
|
+
output: [
|
|
68
|
+
{
|
|
69
|
+
file: targetCJS.replace(/\.cjs$/, '.d.cts'),
|
|
70
|
+
},
|
|
71
|
+
{
|
|
72
|
+
file: targetES.replace(/\.js$/, '.d.ts'),
|
|
73
|
+
},
|
|
74
|
+
],
|
|
75
|
+
},
|
|
76
|
+
];
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/** @type { Array<import('rollup').RollupOptions> } */
|
|
80
|
+
export default generateTarget({
|
|
81
|
+
input: './src/index.ts',
|
|
82
|
+
targetCJS: './dist/index.cjs',
|
|
83
|
+
targetES: './dist/index.js',
|
|
84
|
+
});
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import Counter from './Counter.svelte';
|
|
3
|
+
import { provideStore } from '..';
|
|
4
|
+
import type { Store } from 'ccstate';
|
|
5
|
+
|
|
6
|
+
export let store: Store | undefined = undefined;
|
|
7
|
+
|
|
8
|
+
if (store) {
|
|
9
|
+
provideStore(store);
|
|
10
|
+
}
|
|
11
|
+
</script>
|
|
12
|
+
|
|
13
|
+
<Counter />
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import { count$, doubleIncrease$ } from './store';
|
|
3
|
+
import { useGet } from '..';
|
|
4
|
+
import { useSet } from '..';
|
|
5
|
+
|
|
6
|
+
const count = useGet(count$);
|
|
7
|
+
const setCount = useSet(count$);
|
|
8
|
+
const doubleIncrease = useSet(doubleIncrease$);
|
|
9
|
+
</script>
|
|
10
|
+
|
|
11
|
+
<div>count: {$count}</div>
|
|
12
|
+
<button
|
|
13
|
+
on:click={() => {
|
|
14
|
+
setCount((x) => x + 1);
|
|
15
|
+
}}
|
|
16
|
+
>
|
|
17
|
+
Increment
|
|
18
|
+
</button>
|
|
19
|
+
|
|
20
|
+
<button on:click={() => doubleIncrease(10)}>Double Increase</button>
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import type { State } from 'ccstate';
|
|
3
|
+
import { useLastLoadable } from '../useLoadable';
|
|
4
|
+
|
|
5
|
+
export let promise$: () => State<Promise<string>>;
|
|
6
|
+
const result = useLastLoadable(promise$());
|
|
7
|
+
</script>
|
|
8
|
+
|
|
9
|
+
{#if $result.state === 'loading'}
|
|
10
|
+
<div>Loading</div>
|
|
11
|
+
{:else if $result.state === 'hasError'}
|
|
12
|
+
<div>{$result.error}</div>
|
|
13
|
+
{:else}
|
|
14
|
+
<div>Result: {$result.data}</div>
|
|
15
|
+
{/if}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import type { State } from 'ccstate';
|
|
3
|
+
import { useLastResolved } from '../useResolved';
|
|
4
|
+
|
|
5
|
+
export let promise$: () => State<Promise<string>>;
|
|
6
|
+
const result = useLastResolved(promise$());
|
|
7
|
+
</script>
|
|
8
|
+
|
|
9
|
+
{#if $result}
|
|
10
|
+
<div>Result: {$result}</div>
|
|
11
|
+
{:else}
|
|
12
|
+
<div>Loading</div>
|
|
13
|
+
{/if}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import type { State } from 'ccstate';
|
|
3
|
+
import { useLoadable } from '../useLoadable';
|
|
4
|
+
|
|
5
|
+
export let promise$: () => State<Promise<string>>;
|
|
6
|
+
const result = useLoadable(promise$());
|
|
7
|
+
</script>
|
|
8
|
+
|
|
9
|
+
{#if $result.state === 'loading'}
|
|
10
|
+
<div>Loading</div>
|
|
11
|
+
{:else if $result.state === 'hasError'}
|
|
12
|
+
<div>{$result.error}</div>
|
|
13
|
+
{:else}
|
|
14
|
+
<div>Result: {$result.data}</div>
|
|
15
|
+
{/if}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import type { State } from 'ccstate';
|
|
3
|
+
import { useResolved } from '../useResolved';
|
|
4
|
+
|
|
5
|
+
export let promise$: () => State<Promise<string>>;
|
|
6
|
+
const result = useResolved(promise$());
|
|
7
|
+
</script>
|
|
8
|
+
|
|
9
|
+
{#if $result}
|
|
10
|
+
<div>Result: {$result}</div>
|
|
11
|
+
{:else}
|
|
12
|
+
<div>Loading</div>
|
|
13
|
+
{/if}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { render, cleanup, screen } from '@testing-library/svelte';
|
|
2
|
+
import { afterEach, expect, it } from 'vitest';
|
|
3
|
+
import { createStore, getDefaultStore } from 'ccstate';
|
|
4
|
+
import '@testing-library/jest-dom/vitest';
|
|
5
|
+
import App from './App.svelte';
|
|
6
|
+
import { count$ } from './store';
|
|
7
|
+
import { userEvent } from '@testing-library/user-event';
|
|
8
|
+
|
|
9
|
+
afterEach(() => {
|
|
10
|
+
cleanup();
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
const user = userEvent.setup();
|
|
14
|
+
|
|
15
|
+
it('simple counter', async () => {
|
|
16
|
+
render(App);
|
|
17
|
+
|
|
18
|
+
expect(screen.getByText('count: 0')).toBeInTheDocument();
|
|
19
|
+
getDefaultStore().set(count$, 1);
|
|
20
|
+
|
|
21
|
+
expect(await screen.findByText('count: 1')).toBeInTheDocument();
|
|
22
|
+
|
|
23
|
+
const button = screen.getByText('Increment');
|
|
24
|
+
|
|
25
|
+
await user.click(button);
|
|
26
|
+
await user.click(button);
|
|
27
|
+
expect(await screen.findByText('count: 3')).toBeInTheDocument();
|
|
28
|
+
|
|
29
|
+
await user.click(button);
|
|
30
|
+
expect(await screen.findByText('count: 4')).toBeInTheDocument();
|
|
31
|
+
|
|
32
|
+
const doubleButton = screen.getByText('Double Increase');
|
|
33
|
+
await user.click(doubleButton);
|
|
34
|
+
expect(await screen.findByText('count: 24')).toBeInTheDocument();
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
it('useStore custom store', async () => {
|
|
38
|
+
const store = createStore();
|
|
39
|
+
store.set(count$, 100);
|
|
40
|
+
|
|
41
|
+
render(App, {
|
|
42
|
+
props: {
|
|
43
|
+
store,
|
|
44
|
+
},
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
expect(screen.getByText('count: 100')).toBeInTheDocument();
|
|
48
|
+
|
|
49
|
+
const button = screen.getByText('Increment');
|
|
50
|
+
|
|
51
|
+
await user.click(button);
|
|
52
|
+
|
|
53
|
+
expect(store.get(count$)).toBe(101);
|
|
54
|
+
});
|
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
import { render, cleanup, screen } from '@testing-library/svelte';
|
|
2
|
+
import { afterEach, expect, it } from 'vitest';
|
|
3
|
+
import { createStore, getDefaultStore, state } from 'ccstate';
|
|
4
|
+
import '@testing-library/jest-dom/vitest';
|
|
5
|
+
import Loadable from './Loadable.svelte';
|
|
6
|
+
import LastLoadable from './LastLoadable.svelte';
|
|
7
|
+
import { StoreKey } from '../provider';
|
|
8
|
+
import Resolved from './Resolved.svelte';
|
|
9
|
+
import LastResolved from './LastResolved.svelte';
|
|
10
|
+
|
|
11
|
+
function makeDefered<T>(): {
|
|
12
|
+
resolve: (value: T) => void;
|
|
13
|
+
reject: (error: unknown) => void;
|
|
14
|
+
promise: Promise<T>;
|
|
15
|
+
} {
|
|
16
|
+
const deferred: {
|
|
17
|
+
resolve: (value: T) => void;
|
|
18
|
+
reject: (error: unknown) => void;
|
|
19
|
+
promise: Promise<T>;
|
|
20
|
+
} = {} as {
|
|
21
|
+
resolve: (value: T) => void;
|
|
22
|
+
reject: (error: unknown) => void;
|
|
23
|
+
promise: Promise<T>;
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
deferred.promise = new Promise((resolve, reject) => {
|
|
27
|
+
deferred.resolve = resolve;
|
|
28
|
+
deferred.reject = reject;
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
return deferred;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
afterEach(() => {
|
|
35
|
+
cleanup();
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
it('simple loadable', async () => {
|
|
39
|
+
const promise$ = state(Promise.resolve('bar'));
|
|
40
|
+
render(Loadable, {
|
|
41
|
+
props: {
|
|
42
|
+
promise$: () => promise$,
|
|
43
|
+
},
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
expect(screen.getByText('Loading')).toBeInTheDocument();
|
|
47
|
+
await Promise.resolve();
|
|
48
|
+
expect(screen.getByText('Result: bar')).toBeInTheDocument();
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
it('error loadable', async () => {
|
|
52
|
+
const promise$ = state(Promise.reject(new Error('INTEST')));
|
|
53
|
+
render(Loadable, {
|
|
54
|
+
props: {
|
|
55
|
+
promise$: () => promise$,
|
|
56
|
+
},
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
expect(screen.getByText('Loading')).toBeInTheDocument();
|
|
60
|
+
await Promise.resolve();
|
|
61
|
+
expect(screen.getByText('Error: INTEST')).toBeInTheDocument();
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
it('switchMap', async () => {
|
|
65
|
+
const store = createStore();
|
|
66
|
+
const first = makeDefered<string>();
|
|
67
|
+
|
|
68
|
+
const promise$ = state(first.promise, {
|
|
69
|
+
debugLabel: 'promise$',
|
|
70
|
+
});
|
|
71
|
+
render(Loadable, {
|
|
72
|
+
props: {
|
|
73
|
+
promise$: () => promise$,
|
|
74
|
+
},
|
|
75
|
+
context: new Map([[StoreKey, store]]),
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
expect(screen.getByText('Loading')).toBeInTheDocument();
|
|
79
|
+
|
|
80
|
+
store.set(promise$, Promise.resolve('second'));
|
|
81
|
+
expect(await screen.findByText('Result: second')).toBeInTheDocument();
|
|
82
|
+
|
|
83
|
+
first.resolve('first');
|
|
84
|
+
await expect(screen.findByText('Result: first')).rejects.toThrow();
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
it('switchMap and throw', async () => {
|
|
88
|
+
const store = createStore();
|
|
89
|
+
const first = makeDefered<string>();
|
|
90
|
+
|
|
91
|
+
const promise$ = state(first.promise, {
|
|
92
|
+
debugLabel: 'promise$',
|
|
93
|
+
});
|
|
94
|
+
render(Loadable, {
|
|
95
|
+
props: {
|
|
96
|
+
promise$: () => promise$,
|
|
97
|
+
},
|
|
98
|
+
context: new Map([[StoreKey, store]]),
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
expect(screen.getByText('Loading')).toBeInTheDocument();
|
|
102
|
+
|
|
103
|
+
store.set(promise$, Promise.resolve('second'));
|
|
104
|
+
expect(await screen.findByText('Result: second')).toBeInTheDocument();
|
|
105
|
+
|
|
106
|
+
first.reject(new Error('INTEST'));
|
|
107
|
+
await expect(screen.findByText('Result: first')).rejects.toThrow();
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
it('update should change state to loading', async () => {
|
|
111
|
+
const store = createStore();
|
|
112
|
+
|
|
113
|
+
const promise$ = state(Promise.resolve('first'), {
|
|
114
|
+
debugLabel: 'promise$',
|
|
115
|
+
});
|
|
116
|
+
render(Loadable, {
|
|
117
|
+
props: {
|
|
118
|
+
promise$: () => promise$,
|
|
119
|
+
},
|
|
120
|
+
context: new Map([[StoreKey, store]]),
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
expect(await screen.findByText('Result: first')).toBeInTheDocument();
|
|
124
|
+
|
|
125
|
+
const deferred = makeDefered<string>();
|
|
126
|
+
store.set(promise$, deferred.promise);
|
|
127
|
+
expect(await screen.findByText('Loading')).toBeInTheDocument();
|
|
128
|
+
deferred.resolve('second');
|
|
129
|
+
expect(await screen.findByText('Result: second')).toBeInTheDocument();
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
it('use last loadable will keep latest resolved value', async () => {
|
|
133
|
+
const store = createStore();
|
|
134
|
+
|
|
135
|
+
const promise$ = state(Promise.resolve('first'), {
|
|
136
|
+
debugLabel: 'promise$',
|
|
137
|
+
});
|
|
138
|
+
render(LastLoadable, {
|
|
139
|
+
props: {
|
|
140
|
+
promise$: () => promise$,
|
|
141
|
+
},
|
|
142
|
+
context: new Map([[StoreKey, store]]),
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
expect(await screen.findByText('Result: first')).toBeInTheDocument();
|
|
146
|
+
|
|
147
|
+
const deferred = makeDefered<string>();
|
|
148
|
+
store.set(promise$, deferred.promise);
|
|
149
|
+
await expect(screen.findByText('Loading')).rejects.toThrow();
|
|
150
|
+
deferred.resolve('second');
|
|
151
|
+
expect(await screen.findByText('Result: second')).toBeInTheDocument();
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
it('simple resolved', async () => {
|
|
155
|
+
const promise$ = state(Promise.resolve('bar'));
|
|
156
|
+
render(Resolved, {
|
|
157
|
+
props: {
|
|
158
|
+
promise$: () => promise$,
|
|
159
|
+
},
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
expect(screen.getByText('Loading')).toBeInTheDocument();
|
|
163
|
+
await Promise.resolve();
|
|
164
|
+
expect(screen.getByText('Result: bar')).toBeInTheDocument();
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
it('simple last resolved', async () => {
|
|
168
|
+
const promise$ = state(Promise.resolve('bar'));
|
|
169
|
+
render(LastResolved, {
|
|
170
|
+
props: {
|
|
171
|
+
promise$: () => promise$,
|
|
172
|
+
},
|
|
173
|
+
});
|
|
174
|
+
|
|
175
|
+
expect(screen.getByText('Loading')).toBeInTheDocument();
|
|
176
|
+
await Promise.resolve();
|
|
177
|
+
expect(screen.getByText('Result: bar')).toBeInTheDocument();
|
|
178
|
+
|
|
179
|
+
const deferred = makeDefered<string>();
|
|
180
|
+
getDefaultStore().set(promise$, deferred.promise);
|
|
181
|
+
await expect(screen.findByText('Loading')).rejects.toThrow();
|
|
182
|
+
deferred.resolve('second');
|
|
183
|
+
expect(await screen.findByText('Result: second')).toBeInTheDocument();
|
|
184
|
+
});
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
// @vitest-environment happy-dom
|
|
2
|
+
import '@testing-library/jest-dom/vitest';
|
|
3
|
+
import LeakDetector from 'jest-leak-detector';
|
|
4
|
+
import { render, cleanup, screen } from '@testing-library/svelte';
|
|
5
|
+
import { expect, it } from 'vitest';
|
|
6
|
+
import { computed, getDefaultStore, type Computed } from 'ccstate';
|
|
7
|
+
import Memory from './Memory.svelte';
|
|
8
|
+
import AsyncMemory from './AsyncMemory.svelte';
|
|
9
|
+
|
|
10
|
+
it('should release memory after view cleanup', async () => {
|
|
11
|
+
let obj$: Computed<{ foo: string }> | undefined = computed(() => {
|
|
12
|
+
return { foo: 'bar' };
|
|
13
|
+
});
|
|
14
|
+
const store = getDefaultStore();
|
|
15
|
+
const leakDetector = new LeakDetector(store.get(obj$ as Computed<{ foo: string }>));
|
|
16
|
+
|
|
17
|
+
render(Memory, {
|
|
18
|
+
props: {
|
|
19
|
+
obj$,
|
|
20
|
+
},
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
expect(screen.getByText('obj: bar')).toBeInTheDocument();
|
|
24
|
+
|
|
25
|
+
obj$ = undefined;
|
|
26
|
+
cleanup();
|
|
27
|
+
|
|
28
|
+
expect(await leakDetector.isLeaking()).toBe(false);
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
it('should release promise memory after view cleanup', async () => {
|
|
32
|
+
let obj$: Computed<Promise<{ foo: string }>> | undefined = computed(() => {
|
|
33
|
+
return Promise.resolve({ foo: 'bar' });
|
|
34
|
+
});
|
|
35
|
+
const store = getDefaultStore();
|
|
36
|
+
const leakDetector = new LeakDetector(await store.get(obj$ as Computed<Promise<{ foo: string }>>));
|
|
37
|
+
|
|
38
|
+
render(AsyncMemory, {
|
|
39
|
+
props: {
|
|
40
|
+
obj$: () => obj$ as Computed<Promise<{ foo: string }>>,
|
|
41
|
+
},
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
expect(await screen.findByText('obj: bar')).toBeInTheDocument();
|
|
45
|
+
|
|
46
|
+
obj$ = undefined;
|
|
47
|
+
cleanup();
|
|
48
|
+
|
|
49
|
+
expect(await leakDetector.isLeaking()).toBe(false);
|
|
50
|
+
});
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { render, cleanup, screen } from '@testing-library/svelte';
|
|
2
|
+
import { afterEach, expect, it } from 'vitest';
|
|
3
|
+
import '@testing-library/jest-dom/vitest';
|
|
4
|
+
import Prop from './Prop.svelte';
|
|
5
|
+
|
|
6
|
+
afterEach(() => {
|
|
7
|
+
cleanup();
|
|
8
|
+
});
|
|
9
|
+
|
|
10
|
+
it('prop takes same atom', async () => {
|
|
11
|
+
const obj = {};
|
|
12
|
+
let callbackObj: object | undefined;
|
|
13
|
+
|
|
14
|
+
render(Prop, {
|
|
15
|
+
props: {
|
|
16
|
+
obj: () => obj,
|
|
17
|
+
cb: (obj: object) => {
|
|
18
|
+
callbackObj = obj;
|
|
19
|
+
},
|
|
20
|
+
},
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
expect(await screen.findByText('done')).toBeInTheDocument();
|
|
24
|
+
expect(callbackObj).toBe(obj);
|
|
25
|
+
});
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { command, computed, state } from 'ccstate';
|
|
2
|
+
|
|
3
|
+
export const count$ = state(0);
|
|
4
|
+
|
|
5
|
+
export const doubleIncrease$ = command(({ get, set }, x: number) => {
|
|
6
|
+
set(count$, get(count$) + x * 2);
|
|
7
|
+
});
|
|
8
|
+
|
|
9
|
+
export const objToTestLeak$ = computed(() => {
|
|
10
|
+
return { foo: 'bar' };
|
|
11
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
declare module '*.svelte';
|
package/src/index.ts
ADDED
package/src/provider.ts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { getDefaultStore } from 'ccstate';
|
|
2
|
+
import type { Store } from 'ccstate';
|
|
3
|
+
import { getContext, setContext } from 'svelte';
|
|
4
|
+
|
|
5
|
+
export const StoreKey = Symbol('ccstate-svelte-store');
|
|
6
|
+
|
|
7
|
+
export const provideStore = (store: Store) => {
|
|
8
|
+
setContext(StoreKey, store);
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
export const useStore = (): Store => {
|
|
12
|
+
const store = getContext(StoreKey);
|
|
13
|
+
|
|
14
|
+
if (!store) {
|
|
15
|
+
return getDefaultStore();
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
return store as Store;
|
|
19
|
+
};
|
package/src/useGet.ts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { useStore } from './provider';
|
|
2
|
+
import { command } from 'ccstate';
|
|
3
|
+
import type { Computed, State } from 'ccstate';
|
|
4
|
+
|
|
5
|
+
export function useGet<T>(atom: State<T> | Computed<T>) {
|
|
6
|
+
const store = useStore();
|
|
7
|
+
return {
|
|
8
|
+
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
|
+
}),
|
|
16
|
+
);
|
|
17
|
+
},
|
|
18
|
+
};
|
|
19
|
+
}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { useGet } from './useGet';
|
|
2
|
+
import type { Computed, State } from 'ccstate';
|
|
3
|
+
import { onDestroy } from 'svelte';
|
|
4
|
+
import { writable, type Readable } from 'svelte/store';
|
|
5
|
+
|
|
6
|
+
type Loadable<T> =
|
|
7
|
+
| {
|
|
8
|
+
state: 'loading';
|
|
9
|
+
}
|
|
10
|
+
| {
|
|
11
|
+
state: 'hasData';
|
|
12
|
+
data: T;
|
|
13
|
+
}
|
|
14
|
+
| {
|
|
15
|
+
state: 'hasError';
|
|
16
|
+
error: unknown;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
function useLoadableInternal<T>(
|
|
20
|
+
atom: State<Promise<T>> | Computed<Promise<T>>,
|
|
21
|
+
keepLastResolved: boolean,
|
|
22
|
+
): Readable<Loadable<T>> {
|
|
23
|
+
const promise = useGet(atom);
|
|
24
|
+
const loadable = writable<Loadable<T>>({
|
|
25
|
+
state: 'loading',
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
let abortController = new AbortController();
|
|
29
|
+
onDestroy(() => {
|
|
30
|
+
abortController.abort();
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
promise.subscribe((promiseValue) => {
|
|
34
|
+
abortController.abort();
|
|
35
|
+
abortController = new AbortController();
|
|
36
|
+
const signal = abortController.signal;
|
|
37
|
+
|
|
38
|
+
if (!keepLastResolved) {
|
|
39
|
+
loadable.set({
|
|
40
|
+
state: 'loading',
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
void promiseValue
|
|
45
|
+
.then((ret) => {
|
|
46
|
+
if (signal.aborted) return;
|
|
47
|
+
|
|
48
|
+
loadable.set({
|
|
49
|
+
state: 'hasData',
|
|
50
|
+
data: ret,
|
|
51
|
+
});
|
|
52
|
+
})
|
|
53
|
+
.catch(() => void 0);
|
|
54
|
+
|
|
55
|
+
void promiseValue.catch((error: unknown) => {
|
|
56
|
+
if (signal.aborted) return;
|
|
57
|
+
|
|
58
|
+
loadable.set({
|
|
59
|
+
state: 'hasError',
|
|
60
|
+
error,
|
|
61
|
+
});
|
|
62
|
+
});
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
return loadable;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export function useLoadable<T>(atom: State<Promise<T>> | Computed<Promise<T>>): Readable<Loadable<T>> {
|
|
69
|
+
return useLoadableInternal(atom, false);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export function useLastLoadable<T>(atom: State<Promise<T>> | Computed<Promise<T>>): Readable<Loadable<T>> {
|
|
73
|
+
return useLoadableInternal(atom, true);
|
|
74
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { Computed, State } from 'ccstate';
|
|
2
|
+
import { derived, type Readable } from 'svelte/store';
|
|
3
|
+
import { useLastLoadable, useLoadable } from './useLoadable';
|
|
4
|
+
|
|
5
|
+
export function useResolved<T>(atom: State<Promise<T>> | Computed<Promise<T>>): Readable<T | undefined> {
|
|
6
|
+
const loadable = useLoadable(atom);
|
|
7
|
+
return derived(loadable, ($loadable) => ($loadable.state === 'hasData' ? $loadable.data : undefined));
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export function useLastResolved<T>(atom: State<Promise<T>> | Computed<Promise<T>>): Readable<T | undefined> {
|
|
11
|
+
const loadable = useLastLoadable(atom);
|
|
12
|
+
return derived(loadable, ($loadable) => ($loadable.state === 'hasData' ? $loadable.data : undefined));
|
|
13
|
+
}
|
package/src/useSet.ts
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { useStore } from './provider';
|
|
2
|
+
import type { Command, Updater, State } from 'ccstate';
|
|
3
|
+
|
|
4
|
+
export function useSet<T>(atom: State<T>): (value: T | Updater<T>) => void;
|
|
5
|
+
export function useSet<T, ARGS extends unknown[]>(atom: Command<T, ARGS>): (...args: ARGS) => T;
|
|
6
|
+
export function useSet<T, ARGS extends unknown[]>(
|
|
7
|
+
atom: State<T> | Command<T, ARGS>,
|
|
8
|
+
): ((value: T | Updater<T>) => void) | ((...args: ARGS) => T) {
|
|
9
|
+
const store = useStore();
|
|
10
|
+
|
|
11
|
+
if ('write' in atom) {
|
|
12
|
+
return (...args: ARGS): T => {
|
|
13
|
+
const ret = store.set(atom, ...args);
|
|
14
|
+
|
|
15
|
+
return ret;
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
return (value: T | Updater<T>) => {
|
|
20
|
+
store.set(atom, value);
|
|
21
|
+
};
|
|
22
|
+
}
|
package/tsconfig.json
ADDED
package/vitest.config.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { defineConfig, type Plugin } from 'vitest/config';
|
|
2
|
+
import { svelte } from '@sveltejs/vite-plugin-svelte';
|
|
3
|
+
|
|
4
|
+
export default defineConfig({
|
|
5
|
+
plugins: [svelte() as Plugin[]],
|
|
6
|
+
test: {
|
|
7
|
+
environment: 'happy-dom',
|
|
8
|
+
},
|
|
9
|
+
resolve: {
|
|
10
|
+
conditions: ['development', 'browser'],
|
|
11
|
+
},
|
|
12
|
+
});
|