seitu 0.1.0 → 0.2.1

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/README.md CHANGED
@@ -15,10 +15,10 @@ To quick start you only need to write the following code:
15
15
 
16
16
  ```tsx
17
17
  import { useSubscription } from 'seitu/react'
18
- import { sessionStorageValue } from 'seitu/web'
18
+ import { createSessionStorageValue } from 'seitu/web'
19
19
  import * as z from 'zod'
20
20
 
21
- const value = sessionStorageValue({
21
+ const value = createSessionStorageValue({
22
22
  key: 'test',
23
23
  defaultValue: 0,
24
24
  schema: z.number(),
@@ -30,7 +30,7 @@ value.remove()
30
30
  value.subscribe(v => console.log(v))
31
31
 
32
32
  function Counter() {
33
- const count = useSubscription(() => value)
33
+ const count = useSubscription(value)
34
34
 
35
35
  return (
36
36
  <div>
@@ -1,2 +1,2 @@
1
- export * from './store';
1
+ export * from './schema-store';
2
2
  export * from './subscription';
@@ -0,0 +1,19 @@
1
+ import type { StandardSchemaV1 } from '@standard-schema/spec';
2
+ import type { Simplify } from '../utils';
3
+ import type { Readable, Subscribable, Writable } from './index';
4
+ export type SchemaStoreSchema = Record<string, StandardSchemaV1<unknown, unknown>>;
5
+ export type SchemaStoreOutput<S extends SchemaStoreSchema> = Simplify<{
6
+ [K in keyof S]: StandardSchemaV1.InferOutput<S[K]>;
7
+ }>;
8
+ export interface SchemaStore<O extends Record<string, unknown>> extends Subscribable<O>, Readable<O>, Writable<Partial<O>> {
9
+ getDefaultValue: <K extends keyof O>(key: K) => O[K];
10
+ }
11
+ export interface SchemaStoreOptions<S extends Record<string, StandardSchemaV1>> {
12
+ schemas: S;
13
+ defaultValues: SchemaStoreOutput<S>;
14
+ provider: {
15
+ get: () => SchemaStoreOutput<S>;
16
+ set: (value: Partial<SchemaStoreOutput<S>>) => void;
17
+ };
18
+ }
19
+ export declare function createSchemaStore<S extends Record<string, StandardSchemaV1>>(options: SchemaStoreOptions<S>): SchemaStore<SchemaStoreOutput<S>>;
@@ -1,7 +1,13 @@
1
1
  export interface Subscribable<V> {
2
2
  'subscribe': (callback: (value: V) => any) => () => void;
3
3
  '~': {
4
+ /**
5
+ * Type type with returned value of the subscription.
6
+ */
4
7
  output: V;
8
+ /**
9
+ * A function that notifies all subscribers that the value has changed.
10
+ */
5
11
  notify: () => void;
6
12
  };
7
13
  }
package/dist/core.js CHANGED
@@ -1,2 +1,2 @@
1
1
  import { n as e, t } from "./core-CuCUF5Aj.js";
2
- export { e as createStore, t as createSubscription };
2
+ export { e as createSchemaStore, t as createSubscription };
@@ -1,20 +1,24 @@
1
1
  import type { Readable, Subscribable } from '../core/index';
2
+ import * as React from 'react';
3
+ export interface UseSubscriptionOptions<S extends Subscribable<any> & Readable<any>, R = S['~']['output']> {
4
+ selector?: (value: S['~']['output']) => R;
5
+ deps?: React.DependencyList;
6
+ }
2
7
  /**
3
- * Use this hook to subscribe to a reactive value. The factory function is called only
4
- * once on first render subsequent renders reuse the cached subscription.
8
+ * Use this hook to subscribe to a reactive value. Accepts a subscription object
9
+ * directly, or a factory function that is called only once on first render —
10
+ * subsequent renders reuse the cached subscription unless dependency array changes.
5
11
  *
6
- * @kind hook
7
- *
8
- * @example
12
+ * @example Inline subscription
9
13
  * ```tsx twoslash title="/app/page.tsx"
10
14
  * 'use client'
11
15
  *
12
- * import { sessionStorageValue } from 'seitu/web'
16
+ * import { createSessionStorageValue } from 'seitu/web'
13
17
  * import { useSubscription } from 'seitu/react'
14
18
  * import * as z from 'zod'
15
19
  *
16
20
  * export default function Page() {
17
- * const value = useSubscription(() => sessionStorageValue({
21
+ * const value = useSubscription(() => createSessionStorageValue({
18
22
  * key: 'test',
19
23
  * defaultValue: 0,
20
24
  * schema: z.number(),
@@ -24,7 +28,26 @@ import type { Readable, Subscribable } from '../core/index';
24
28
  * }
25
29
  * ```
26
30
  *
27
- * @example
31
+ * @example Instance outside of component
32
+ * ```tsx twoslash title="/app/page.tsx"
33
+ * 'use client'
34
+ *
35
+ * import { createSessionStorage } from 'seitu/web'
36
+ * import { useSubscription } from 'seitu/react'
37
+ * import * as z from 'zod'
38
+ *
39
+ * const sessionStorage = createSessionStorage({
40
+ * schemas: { count: z.number(), name: z.string() },
41
+ * defaultValues: { count: 0, name: '' },
42
+ * })
43
+ *
44
+ * export default function Page() {
45
+ * const value = useSubscription(sessionStorage)
46
+ * return <div>{value.count}</div>
47
+ * }
48
+ * ```
49
+ *
50
+ * @example Subscription with selector
28
51
  * ```tsx twoslash title="/app/page.tsx"
29
52
  * 'use client'
30
53
  *
@@ -42,10 +65,30 @@ import type { Readable, Subscribable } from '../core/index';
42
65
  *
43
66
  * export default function Page() {
44
67
  * // Usage with selector, re-renders only when count changes
45
- * const count = useSubscription(() => sessionStorage, value => value.count)
68
+ * const count = useSubscription(sessionStorage, { selector: value => value.count })
46
69
  *
47
70
  * return <div>{count}</div>
48
71
  * }
49
72
  * ```
73
+ *
74
+ * @example Ref example
75
+ * ```tsx twoslash title="/app/page.tsx"
76
+ * 'use client'
77
+ *
78
+ * import * as React from 'react'
79
+ * import { scrollState } from 'seitu/web'
80
+ * import { useSubscription } from 'seitu/react'
81
+ *
82
+ * export default function Page() {
83
+ * const ref = React.useRef<HTMLDivElement>(null)
84
+ * const state = useSubscription(() => scrollState({ element: () => ref.current, direction: 'vertical' }))
85
+ *
86
+ * return (
87
+ * <div ref={ref}>
88
+ * {String(state.top.value)}
89
+ * </div>
90
+ * )
91
+ * }
92
+ * ```
50
93
  */
51
- export declare function useSubscription<S extends Subscribable<any> & Readable<any>, R = S['~']['output']>(factory: () => S, selector?: (value: S['~']['output']) => R): R;
94
+ export declare function useSubscription<S extends Subscribable<any> & Readable<any>, R = S['~']['output']>(source: S | (() => S), options?: UseSubscriptionOptions<S, R>): R;