jotai-state-tree 0.1.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.
@@ -0,0 +1,144 @@
1
+ import React, { ComponentType, FC, ReactNode } from 'react';
2
+
3
+ /**
4
+ * React integration for jotai-state-tree
5
+ * Provides observer HOC and hooks
6
+ */
7
+
8
+ interface ObserverOptions {
9
+ forwardRef?: boolean;
10
+ }
11
+ /**
12
+ * Higher-order component that makes a component reactive to state tree changes.
13
+ * Similar to mobx-react-lite's observer.
14
+ */
15
+ declare function observer<P extends object>(Component: ComponentType<P>, options?: ObserverOptions): ComponentType<P>;
16
+ interface ObserverComponentProps {
17
+ children: () => ReactNode;
18
+ }
19
+ /**
20
+ * Observer component using render props pattern.
21
+ * Useful for inline observation.
22
+ *
23
+ * @example
24
+ * <Observer>
25
+ * {() => <div>{store.count}</div>}
26
+ * </Observer>
27
+ */
28
+ declare const Observer: FC<ObserverComponentProps>;
29
+ /**
30
+ * Hook that re-renders the component when any accessed state tree nodes change.
31
+ */
32
+ declare function useObserver<T>(fn: () => T): T;
33
+ /**
34
+ * Creates a local observable state tree instance.
35
+ * Similar to mobx-react-lite's useLocalObservable.
36
+ */
37
+ declare function useLocalObservable<T>(initializer: () => T, dependencies?: unknown[]): T;
38
+ /**
39
+ * Use a state tree instance with React's useSyncExternalStore.
40
+ * This provides better concurrent mode support.
41
+ */
42
+ declare function useSyncedStore<T>(store: T): T;
43
+ interface ProviderProps<T> {
44
+ store: T;
45
+ children: ReactNode;
46
+ }
47
+ /**
48
+ * Provider component for state tree stores.
49
+ * @deprecated Use createStoreContext() for better type inference
50
+ */
51
+ declare function Provider<T>({ store, children, }: ProviderProps<T>): JSX.Element;
52
+ /**
53
+ * Hook to access the store from context.
54
+ * @deprecated Use createStoreContext() for better type inference
55
+ */
56
+ declare function useStore<T>(): T;
57
+ /**
58
+ * Hook to access the store with snapshot subscription.
59
+ * @deprecated Use createStoreContext() for better type inference
60
+ */
61
+ declare function useStoreSnapshot<T>(): T;
62
+ declare function useStoreSnapshot<T, S>(selector: (store: T) => S): S;
63
+ /**
64
+ * Creates a typed store context with Provider and hooks.
65
+ * This provides full type inference without needing to specify generic types.
66
+ *
67
+ * @example
68
+ * const RootStore = types.model("RootStore", {
69
+ * count: types.number,
70
+ * }).actions(self => ({
71
+ * increment() { self.count += 1; }
72
+ * }));
73
+ *
74
+ * type RootStoreInstance = Instance<typeof RootStore>;
75
+ *
76
+ * const { Provider, useStore, useStoreSnapshot } = createStoreContext<RootStoreInstance>();
77
+ *
78
+ * // In your app:
79
+ * const store = RootStore.create({ count: 0 });
80
+ * <Provider store={store}>
81
+ * <App />
82
+ * </Provider>
83
+ *
84
+ * // In components:
85
+ * const store = useStore(); // Fully typed!
86
+ * store.increment(); // Type-safe
87
+ */
88
+ declare function createStoreContext<T>(): {
89
+ Provider: ({ store, children, }: {
90
+ store: T;
91
+ children: ReactNode;
92
+ }) => JSX.Element;
93
+ useStore: () => T;
94
+ useStoreSnapshot: {
95
+ (): T;
96
+ <S>(selector: (store: T) => S): S;
97
+ };
98
+ useIsAlive: () => boolean;
99
+ Context: React.Context<T | null>;
100
+ };
101
+ /**
102
+ * Hook that returns the current snapshot and re-renders on changes.
103
+ */
104
+ declare function useSnapshot<T>(target: unknown): T;
105
+ /**
106
+ * Hook to watch specific paths in a state tree
107
+ */
108
+ declare function useWatchPath<T>(target: unknown, path: string, defaultValue?: T): T;
109
+ /**
110
+ * Hook that subscribes to patches on a node.
111
+ */
112
+ declare function usePatches(target: unknown, callback: (patch: {
113
+ op: string;
114
+ path: string;
115
+ value?: unknown;
116
+ }) => void): void;
117
+ /**
118
+ * Hook that returns an action bound to a store.
119
+ * Useful for passing actions to child components.
120
+ */
121
+ declare function useAction<T extends (...args: unknown[]) => unknown>(action: T): T;
122
+ /**
123
+ * Hook that returns multiple actions bound to a store.
124
+ */
125
+ declare function useActions<T extends Record<string, (...args: unknown[]) => unknown>>(actions: T): T;
126
+ /**
127
+ * Batch multiple state updates to trigger a single re-render.
128
+ */
129
+ declare function batch(fn: () => void): void;
130
+ /**
131
+ * Schedule an update, batching if we're inside a batch() call.
132
+ */
133
+ declare function scheduleUpdate(update: () => void): void;
134
+ /**
135
+ * Hook that returns whether a node is alive.
136
+ * Uses proper subscription instead of polling for better performance.
137
+ */
138
+ declare function useIsAlive(target: unknown): boolean;
139
+ /**
140
+ * Hook that ensures cleanup when a component unmounts
141
+ */
142
+ declare function useCleanup(cleanupFn: () => void): void;
143
+
144
+ export { Observer, type ObserverOptions, Provider, batch, createStoreContext, observer, scheduleUpdate, useAction, useActions, useCleanup, useIsAlive, useLocalObservable, useObserver, usePatches, useSnapshot, useStore, useStoreSnapshot, useSyncedStore, useWatchPath };
@@ -0,0 +1,144 @@
1
+ import React, { ComponentType, FC, ReactNode } from 'react';
2
+
3
+ /**
4
+ * React integration for jotai-state-tree
5
+ * Provides observer HOC and hooks
6
+ */
7
+
8
+ interface ObserverOptions {
9
+ forwardRef?: boolean;
10
+ }
11
+ /**
12
+ * Higher-order component that makes a component reactive to state tree changes.
13
+ * Similar to mobx-react-lite's observer.
14
+ */
15
+ declare function observer<P extends object>(Component: ComponentType<P>, options?: ObserverOptions): ComponentType<P>;
16
+ interface ObserverComponentProps {
17
+ children: () => ReactNode;
18
+ }
19
+ /**
20
+ * Observer component using render props pattern.
21
+ * Useful for inline observation.
22
+ *
23
+ * @example
24
+ * <Observer>
25
+ * {() => <div>{store.count}</div>}
26
+ * </Observer>
27
+ */
28
+ declare const Observer: FC<ObserverComponentProps>;
29
+ /**
30
+ * Hook that re-renders the component when any accessed state tree nodes change.
31
+ */
32
+ declare function useObserver<T>(fn: () => T): T;
33
+ /**
34
+ * Creates a local observable state tree instance.
35
+ * Similar to mobx-react-lite's useLocalObservable.
36
+ */
37
+ declare function useLocalObservable<T>(initializer: () => T, dependencies?: unknown[]): T;
38
+ /**
39
+ * Use a state tree instance with React's useSyncExternalStore.
40
+ * This provides better concurrent mode support.
41
+ */
42
+ declare function useSyncedStore<T>(store: T): T;
43
+ interface ProviderProps<T> {
44
+ store: T;
45
+ children: ReactNode;
46
+ }
47
+ /**
48
+ * Provider component for state tree stores.
49
+ * @deprecated Use createStoreContext() for better type inference
50
+ */
51
+ declare function Provider<T>({ store, children, }: ProviderProps<T>): JSX.Element;
52
+ /**
53
+ * Hook to access the store from context.
54
+ * @deprecated Use createStoreContext() for better type inference
55
+ */
56
+ declare function useStore<T>(): T;
57
+ /**
58
+ * Hook to access the store with snapshot subscription.
59
+ * @deprecated Use createStoreContext() for better type inference
60
+ */
61
+ declare function useStoreSnapshot<T>(): T;
62
+ declare function useStoreSnapshot<T, S>(selector: (store: T) => S): S;
63
+ /**
64
+ * Creates a typed store context with Provider and hooks.
65
+ * This provides full type inference without needing to specify generic types.
66
+ *
67
+ * @example
68
+ * const RootStore = types.model("RootStore", {
69
+ * count: types.number,
70
+ * }).actions(self => ({
71
+ * increment() { self.count += 1; }
72
+ * }));
73
+ *
74
+ * type RootStoreInstance = Instance<typeof RootStore>;
75
+ *
76
+ * const { Provider, useStore, useStoreSnapshot } = createStoreContext<RootStoreInstance>();
77
+ *
78
+ * // In your app:
79
+ * const store = RootStore.create({ count: 0 });
80
+ * <Provider store={store}>
81
+ * <App />
82
+ * </Provider>
83
+ *
84
+ * // In components:
85
+ * const store = useStore(); // Fully typed!
86
+ * store.increment(); // Type-safe
87
+ */
88
+ declare function createStoreContext<T>(): {
89
+ Provider: ({ store, children, }: {
90
+ store: T;
91
+ children: ReactNode;
92
+ }) => JSX.Element;
93
+ useStore: () => T;
94
+ useStoreSnapshot: {
95
+ (): T;
96
+ <S>(selector: (store: T) => S): S;
97
+ };
98
+ useIsAlive: () => boolean;
99
+ Context: React.Context<T | null>;
100
+ };
101
+ /**
102
+ * Hook that returns the current snapshot and re-renders on changes.
103
+ */
104
+ declare function useSnapshot<T>(target: unknown): T;
105
+ /**
106
+ * Hook to watch specific paths in a state tree
107
+ */
108
+ declare function useWatchPath<T>(target: unknown, path: string, defaultValue?: T): T;
109
+ /**
110
+ * Hook that subscribes to patches on a node.
111
+ */
112
+ declare function usePatches(target: unknown, callback: (patch: {
113
+ op: string;
114
+ path: string;
115
+ value?: unknown;
116
+ }) => void): void;
117
+ /**
118
+ * Hook that returns an action bound to a store.
119
+ * Useful for passing actions to child components.
120
+ */
121
+ declare function useAction<T extends (...args: unknown[]) => unknown>(action: T): T;
122
+ /**
123
+ * Hook that returns multiple actions bound to a store.
124
+ */
125
+ declare function useActions<T extends Record<string, (...args: unknown[]) => unknown>>(actions: T): T;
126
+ /**
127
+ * Batch multiple state updates to trigger a single re-render.
128
+ */
129
+ declare function batch(fn: () => void): void;
130
+ /**
131
+ * Schedule an update, batching if we're inside a batch() call.
132
+ */
133
+ declare function scheduleUpdate(update: () => void): void;
134
+ /**
135
+ * Hook that returns whether a node is alive.
136
+ * Uses proper subscription instead of polling for better performance.
137
+ */
138
+ declare function useIsAlive(target: unknown): boolean;
139
+ /**
140
+ * Hook that ensures cleanup when a component unmounts
141
+ */
142
+ declare function useCleanup(cleanupFn: () => void): void;
143
+
144
+ export { Observer, type ObserverOptions, Provider, batch, createStoreContext, observer, scheduleUpdate, useAction, useActions, useCleanup, useIsAlive, useLocalObservable, useObserver, usePatches, useSnapshot, useStore, useStoreSnapshot, useSyncedStore, useWatchPath };