ccstate-solid 3.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/src/useGet.ts ADDED
@@ -0,0 +1,21 @@
1
+ import { useStore } from './provider';
2
+ import { command, type Computed, type State } from 'ccstate';
3
+ import { createSignal, onCleanup } from 'solid-js';
4
+
5
+ export function useGet<T>(atom: State<T> | Computed<T>) {
6
+ const store = useStore();
7
+ const [value, setValue] = createSignal<T>(store.get(atom));
8
+
9
+ const unsub = store.sub(
10
+ atom,
11
+ command(() => {
12
+ setValue(() => store.get(atom));
13
+ }),
14
+ );
15
+
16
+ onCleanup(() => {
17
+ unsub();
18
+ });
19
+
20
+ return value;
21
+ }
@@ -0,0 +1,11 @@
1
+ import { createResource, type Resource } from 'solid-js';
2
+ import { useGet } from './useGet';
3
+ import type { Computed, State } from 'ccstate';
4
+
5
+ export function useResource<T>(atom: State<Promise<T>> | Computed<Promise<T>>): Resource<T> {
6
+ const [data] = createResource(useGet(atom), (promise) => {
7
+ return promise;
8
+ });
9
+
10
+ return data;
11
+ }
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
@@ -0,0 +1,9 @@
1
+ {
2
+ "extends": "../../tsconfig.options.json",
3
+ "compilerOptions": {
4
+ "lib": ["ES2020", "DOM"],
5
+ "jsx": "preserve",
6
+ "jsxImportSource": "solid-js"
7
+ },
8
+ "include": ["src/**/*", "vitest.config.ts"]
9
+ }
@@ -0,0 +1,16 @@
1
+ import { defineConfig } from 'vitest/config';
2
+ import solid from 'vite-plugin-solid';
3
+
4
+ export default defineConfig({
5
+ plugins: [solid()],
6
+ test: {
7
+ environment: 'happy-dom',
8
+ deps: {
9
+ // https://dev.to/mbarzeev/update-testing-a-solidjs-component-using-vitest-1pj9
10
+ inline: [/solid-js/, /solid-testing-library/],
11
+ },
12
+ },
13
+ resolve: {
14
+ conditions: ['development', 'browser'],
15
+ },
16
+ });