@depup/jotai 2.18.0-depup.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.
Files changed (215) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +25 -0
  3. package/babel/plugin-debug-label.d.ts +5 -0
  4. package/babel/plugin-debug-label.js +54 -0
  5. package/babel/plugin-react-refresh.d.ts +5 -0
  6. package/babel/plugin-react-refresh.js +72 -0
  7. package/babel/preset.d.ts +6 -0
  8. package/babel/preset.js +110 -0
  9. package/babel/utils.d.ts +5 -0
  10. package/benchmarks/simple-read.d.ts +2 -0
  11. package/benchmarks/simple-write.d.ts +2 -0
  12. package/benchmarks/subscribe-write.d.ts +2 -0
  13. package/changes.json +5 -0
  14. package/esm/babel/plugin-debug-label.d.mts +5 -0
  15. package/esm/babel/plugin-debug-label.mjs +103 -0
  16. package/esm/babel/plugin-react-refresh.d.mts +5 -0
  17. package/esm/babel/plugin-react-refresh.mjs +121 -0
  18. package/esm/babel/preset.d.mts +6 -0
  19. package/esm/babel/preset.mjs +179 -0
  20. package/esm/babel/utils.d.mts +5 -0
  21. package/esm/index.d.mts +2 -0
  22. package/esm/index.mjs +2 -0
  23. package/esm/react/Provider.d.mts +16 -0
  24. package/esm/react/useAtom.d.mts +13 -0
  25. package/esm/react/useAtomValue.d.mts +9 -0
  26. package/esm/react/useSetAtom.d.mts +7 -0
  27. package/esm/react/utils/useAtomCallback.d.mts +5 -0
  28. package/esm/react/utils/useHydrateAtoms.d.mts +14 -0
  29. package/esm/react/utils/useReducerAtom.d.mts +14 -0
  30. package/esm/react/utils/useResetAtom.d.mts +6 -0
  31. package/esm/react/utils.d.mts +4 -0
  32. package/esm/react/utils.mjs +57 -0
  33. package/esm/react.d.mts +4 -0
  34. package/esm/react.mjs +182 -0
  35. package/esm/utils.d.mts +2 -0
  36. package/esm/utils.mjs +2 -0
  37. package/esm/vanilla/atom.d.mts +47 -0
  38. package/esm/vanilla/internals.d.mts +164 -0
  39. package/esm/vanilla/internals.mjs +715 -0
  40. package/esm/vanilla/store.d.mts +5 -0
  41. package/esm/vanilla/typeUtils.d.mts +7 -0
  42. package/esm/vanilla/utils/atomFamily.d.mts +55 -0
  43. package/esm/vanilla/utils/atomWithDefault.d.mts +6 -0
  44. package/esm/vanilla/utils/atomWithLazy.d.mts +2 -0
  45. package/esm/vanilla/utils/atomWithObservable.d.mts +32 -0
  46. package/esm/vanilla/utils/atomWithReducer.d.mts +3 -0
  47. package/esm/vanilla/utils/atomWithRefresh.d.mts +6 -0
  48. package/esm/vanilla/utils/atomWithReset.d.mts +8 -0
  49. package/esm/vanilla/utils/atomWithStorage.d.mts +50 -0
  50. package/esm/vanilla/utils/constants.d.mts +1 -0
  51. package/esm/vanilla/utils/freezeAtom.d.mts +6 -0
  52. package/esm/vanilla/utils/loadable.d.mts +33 -0
  53. package/esm/vanilla/utils/selectAtom.d.mts +2 -0
  54. package/esm/vanilla/utils/splitAtom.d.mts +16 -0
  55. package/esm/vanilla/utils/unwrap.d.mts +5 -0
  56. package/esm/vanilla/utils.d.mts +14 -0
  57. package/esm/vanilla/utils.mjs +736 -0
  58. package/esm/vanilla.d.mts +4 -0
  59. package/esm/vanilla.mjs +59 -0
  60. package/index.d.ts +2 -0
  61. package/index.js +19 -0
  62. package/package.json +125 -0
  63. package/react/Provider.d.ts +16 -0
  64. package/react/useAtom.d.ts +13 -0
  65. package/react/useAtomValue.d.ts +9 -0
  66. package/react/useSetAtom.d.ts +7 -0
  67. package/react/utils/useAtomCallback.d.ts +5 -0
  68. package/react/utils/useHydrateAtoms.d.ts +14 -0
  69. package/react/utils/useReducerAtom.d.ts +14 -0
  70. package/react/utils/useResetAtom.d.ts +6 -0
  71. package/react/utils.d.ts +4 -0
  72. package/react/utils.js +100 -0
  73. package/react.d.ts +4 -0
  74. package/react.js +182 -0
  75. package/system/babel/plugin-debug-label.development.js +113 -0
  76. package/system/babel/plugin-debug-label.production.js +4 -0
  77. package/system/babel/plugin-react-refresh.development.js +131 -0
  78. package/system/babel/plugin-react-refresh.production.js +11 -0
  79. package/system/babel/preset.development.js +189 -0
  80. package/system/babel/preset.production.js +14 -0
  81. package/system/index.development.js +27 -0
  82. package/system/index.production.js +1 -0
  83. package/system/react/utils.development.js +79 -0
  84. package/system/react/utils.production.js +2 -0
  85. package/system/react.development.js +210 -0
  86. package/system/react.production.js +2 -0
  87. package/system/utils.development.js +27 -0
  88. package/system/utils.production.js +1 -0
  89. package/system/vanilla/internals.development.js +735 -0
  90. package/system/vanilla/internals.production.js +1 -0
  91. package/system/vanilla/utils.development.js +764 -0
  92. package/system/vanilla/utils.production.js +1 -0
  93. package/system/vanilla.development.js +75 -0
  94. package/system/vanilla.production.js +1 -0
  95. package/ts3.8/babel/plugin-debug-label.d.ts +6 -0
  96. package/ts3.8/babel/plugin-react-refresh.d.ts +6 -0
  97. package/ts3.8/babel/preset.d.ts +7 -0
  98. package/ts3.8/babel/utils.d.ts +6 -0
  99. package/ts3.8/benchmarks/simple-read.d.ts +3 -0
  100. package/ts3.8/benchmarks/simple-write.d.ts +3 -0
  101. package/ts3.8/benchmarks/subscribe-write.d.ts +3 -0
  102. package/ts3.8/esm/babel/plugin-debug-label.d.ts +6 -0
  103. package/ts3.8/esm/babel/plugin-react-refresh.d.ts +6 -0
  104. package/ts3.8/esm/babel/preset.d.ts +7 -0
  105. package/ts3.8/esm/babel/utils.d.ts +6 -0
  106. package/ts3.8/esm/index.d.ts +3 -0
  107. package/ts3.8/esm/react/Provider.d.ts +17 -0
  108. package/ts3.8/esm/react/useAtom.d.ts +28 -0
  109. package/ts3.8/esm/react/useAtomValue.d.ts +10 -0
  110. package/ts3.8/esm/react/useSetAtom.d.ts +8 -0
  111. package/ts3.8/esm/react/utils/useAtomCallback.d.ts +6 -0
  112. package/ts3.8/esm/react/utils/useHydrateAtoms.d.ts +27 -0
  113. package/ts3.8/esm/react/utils/useReducerAtom.d.ts +21 -0
  114. package/ts3.8/esm/react/utils/useResetAtom.d.ts +9 -0
  115. package/ts3.8/esm/react/utils.d.ts +5 -0
  116. package/ts3.8/esm/react.d.ts +5 -0
  117. package/ts3.8/esm/utils.d.ts +3 -0
  118. package/ts3.8/esm/vanilla/atom.d.ts +48 -0
  119. package/ts3.8/esm/vanilla/internals.d.ts +165 -0
  120. package/ts3.8/esm/vanilla/store.d.ts +6 -0
  121. package/ts3.8/esm/vanilla/typeUtils.d.ts +8 -0
  122. package/ts3.8/esm/vanilla/utils/atomFamily.d.ts +56 -0
  123. package/ts3.8/esm/vanilla/utils/atomWithDefault.d.ts +11 -0
  124. package/ts3.8/esm/vanilla/utils/atomWithLazy.d.ts +3 -0
  125. package/ts3.8/esm/vanilla/utils/atomWithObservable.d.ts +37 -0
  126. package/ts3.8/esm/vanilla/utils/atomWithReducer.d.ts +8 -0
  127. package/ts3.8/esm/vanilla/utils/atomWithRefresh.d.ts +10 -0
  128. package/ts3.8/esm/vanilla/utils/atomWithReset.d.ts +11 -0
  129. package/ts3.8/esm/vanilla/utils/atomWithStorage.d.ts +53 -0
  130. package/ts3.8/esm/vanilla/utils/constants.d.ts +2 -0
  131. package/ts3.8/esm/vanilla/utils/freezeAtom.d.ts +7 -0
  132. package/ts3.8/esm/vanilla/utils/loadable.d.ts +34 -0
  133. package/ts3.8/esm/vanilla/utils/selectAtom.d.ts +3 -0
  134. package/ts3.8/esm/vanilla/utils/splitAtom.d.ts +21 -0
  135. package/ts3.8/esm/vanilla/utils/unwrap.d.ts +6 -0
  136. package/ts3.8/esm/vanilla/utils.d.ts +15 -0
  137. package/ts3.8/esm/vanilla.d.ts +5 -0
  138. package/ts3.8/index.d.ts +3 -0
  139. package/ts3.8/react/Provider.d.ts +17 -0
  140. package/ts3.8/react/useAtom.d.ts +28 -0
  141. package/ts3.8/react/useAtomValue.d.ts +10 -0
  142. package/ts3.8/react/useSetAtom.d.ts +8 -0
  143. package/ts3.8/react/utils/useAtomCallback.d.ts +6 -0
  144. package/ts3.8/react/utils/useHydrateAtoms.d.ts +27 -0
  145. package/ts3.8/react/utils/useReducerAtom.d.ts +21 -0
  146. package/ts3.8/react/utils/useResetAtom.d.ts +9 -0
  147. package/ts3.8/react/utils.d.ts +5 -0
  148. package/ts3.8/react.d.ts +5 -0
  149. package/ts3.8/utils.d.ts +3 -0
  150. package/ts3.8/vanilla/atom.d.ts +48 -0
  151. package/ts3.8/vanilla/internals.d.ts +165 -0
  152. package/ts3.8/vanilla/store.d.ts +6 -0
  153. package/ts3.8/vanilla/typeUtils.d.ts +8 -0
  154. package/ts3.8/vanilla/utils/atomFamily.d.ts +56 -0
  155. package/ts3.8/vanilla/utils/atomWithDefault.d.ts +11 -0
  156. package/ts3.8/vanilla/utils/atomWithLazy.d.ts +3 -0
  157. package/ts3.8/vanilla/utils/atomWithObservable.d.ts +37 -0
  158. package/ts3.8/vanilla/utils/atomWithReducer.d.ts +8 -0
  159. package/ts3.8/vanilla/utils/atomWithRefresh.d.ts +10 -0
  160. package/ts3.8/vanilla/utils/atomWithReset.d.ts +11 -0
  161. package/ts3.8/vanilla/utils/atomWithStorage.d.ts +53 -0
  162. package/ts3.8/vanilla/utils/constants.d.ts +2 -0
  163. package/ts3.8/vanilla/utils/freezeAtom.d.ts +7 -0
  164. package/ts3.8/vanilla/utils/loadable.d.ts +34 -0
  165. package/ts3.8/vanilla/utils/selectAtom.d.ts +3 -0
  166. package/ts3.8/vanilla/utils/splitAtom.d.ts +21 -0
  167. package/ts3.8/vanilla/utils/unwrap.d.ts +6 -0
  168. package/ts3.8/vanilla/utils.d.ts +15 -0
  169. package/ts3.8/vanilla.d.ts +5 -0
  170. package/ts_version_3.8_and_above_is_required.d.ts +0 -0
  171. package/umd/babel/plugin-debug-label.development.js +58 -0
  172. package/umd/babel/plugin-debug-label.production.js +1 -0
  173. package/umd/babel/plugin-react-refresh.development.js +76 -0
  174. package/umd/babel/plugin-react-refresh.production.js +1 -0
  175. package/umd/babel/preset.development.js +114 -0
  176. package/umd/babel/preset.production.js +1 -0
  177. package/umd/index.development.js +20 -0
  178. package/umd/index.production.js +1 -0
  179. package/umd/react/utils.development.js +101 -0
  180. package/umd/react/utils.production.js +1 -0
  181. package/umd/react.development.js +184 -0
  182. package/umd/react.production.js +1 -0
  183. package/umd/utils.development.js +20 -0
  184. package/umd/utils.production.js +1 -0
  185. package/umd/vanilla/internals.development.js +785 -0
  186. package/umd/vanilla/internals.production.js +1 -0
  187. package/umd/vanilla/utils.development.js +858 -0
  188. package/umd/vanilla/utils.production.js +1 -0
  189. package/umd/vanilla.development.js +64 -0
  190. package/umd/vanilla.production.js +1 -0
  191. package/utils.d.ts +2 -0
  192. package/utils.js +19 -0
  193. package/vanilla/atom.d.ts +47 -0
  194. package/vanilla/internals.d.ts +164 -0
  195. package/vanilla/internals.js +779 -0
  196. package/vanilla/store.d.ts +5 -0
  197. package/vanilla/typeUtils.d.ts +7 -0
  198. package/vanilla/utils/atomFamily.d.ts +55 -0
  199. package/vanilla/utils/atomWithDefault.d.ts +6 -0
  200. package/vanilla/utils/atomWithLazy.d.ts +2 -0
  201. package/vanilla/utils/atomWithObservable.d.ts +32 -0
  202. package/vanilla/utils/atomWithReducer.d.ts +3 -0
  203. package/vanilla/utils/atomWithRefresh.d.ts +6 -0
  204. package/vanilla/utils/atomWithReset.d.ts +8 -0
  205. package/vanilla/utils/atomWithStorage.d.ts +50 -0
  206. package/vanilla/utils/constants.d.ts +1 -0
  207. package/vanilla/utils/freezeAtom.d.ts +6 -0
  208. package/vanilla/utils/loadable.d.ts +33 -0
  209. package/vanilla/utils/selectAtom.d.ts +2 -0
  210. package/vanilla/utils/splitAtom.d.ts +16 -0
  211. package/vanilla/utils/unwrap.d.ts +5 -0
  212. package/vanilla/utils.d.ts +14 -0
  213. package/vanilla/utils.js +854 -0
  214. package/vanilla.d.ts +4 -0
  215. package/vanilla.js +60 -0
@@ -0,0 +1,179 @@
1
+ import '@babel/core';
2
+ import _templateBuilder from '@babel/template';
3
+
4
+ function isAtom(t, callee, customAtomNames = []) {
5
+ const atomNames = [...atomFunctionNames, ...customAtomNames];
6
+ if (t.isIdentifier(callee) && atomNames.includes(callee.name)) {
7
+ return true;
8
+ }
9
+ if (t.isMemberExpression(callee)) {
10
+ const { property } = callee;
11
+ if (t.isIdentifier(property) && atomNames.includes(property.name)) {
12
+ return true;
13
+ }
14
+ }
15
+ return false;
16
+ }
17
+ const atomFunctionNames = [
18
+ // Core
19
+ "atom",
20
+ "atomFamily",
21
+ "atomWithDefault",
22
+ "atomWithObservable",
23
+ "atomWithReducer",
24
+ "atomWithReset",
25
+ "atomWithStorage",
26
+ "freezeAtom",
27
+ "loadable",
28
+ "selectAtom",
29
+ "splitAtom",
30
+ "unwrap",
31
+ // jotai-xstate
32
+ "atomWithMachine",
33
+ // jotai-immer
34
+ "atomWithImmer",
35
+ // jotai-valtio
36
+ "atomWithProxy",
37
+ // jotai-trpc + jotai-relay
38
+ "atomWithQuery",
39
+ "atomWithMutation",
40
+ "atomWithSubscription",
41
+ // jotai-redux + jotai-zustand
42
+ "atomWithStore",
43
+ // jotai-location
44
+ "atomWithHash",
45
+ "atomWithLocation",
46
+ // jotai-optics
47
+ "focusAtom",
48
+ // jotai-form
49
+ "atomWithValidate",
50
+ "validateAtoms",
51
+ // jotai-cache
52
+ "atomWithCache",
53
+ // jotai-recoil
54
+ "atomWithRecoilValue"
55
+ ];
56
+
57
+ const templateBuilder$1 = _templateBuilder.default || _templateBuilder;
58
+ function debugLabelPlugin({ types: t }, options) {
59
+ console.warn(
60
+ "[DEPRECATED] jotai/babel/plugin-debug-label is deprecated and will be removed in v3.\nPlease use the `jotai-babel` package instead: https://github.com/jotaijs/jotai-babel"
61
+ );
62
+ return {
63
+ visitor: {
64
+ ExportDefaultDeclaration(nodePath, state) {
65
+ const { node } = nodePath;
66
+ if (t.isCallExpression(node.declaration) && isAtom(t, node.declaration.callee, options == null ? void 0 : options.customAtomNames)) {
67
+ const filename = (state.filename || "unknown").replace(/\.\w+$/, "");
68
+ let displayName = filename.split("/").pop();
69
+ if (displayName === "index") {
70
+ displayName = filename.slice(0, -"/index".length).split("/").pop() || "unknown";
71
+ }
72
+ const buildExport = templateBuilder$1(`
73
+ const %%atomIdentifier%% = %%atom%%;
74
+ export default %%atomIdentifier%%
75
+ `);
76
+ const ast = buildExport({
77
+ atomIdentifier: t.identifier(displayName),
78
+ atom: node.declaration
79
+ });
80
+ nodePath.replaceWithMultiple(ast);
81
+ }
82
+ },
83
+ VariableDeclarator(path) {
84
+ if (t.isIdentifier(path.node.id) && t.isCallExpression(path.node.init) && isAtom(t, path.node.init.callee, options == null ? void 0 : options.customAtomNames)) {
85
+ path.parentPath.insertAfter(
86
+ t.expressionStatement(
87
+ t.assignmentExpression(
88
+ "=",
89
+ t.memberExpression(
90
+ t.identifier(path.node.id.name),
91
+ t.identifier("debugLabel")
92
+ ),
93
+ t.stringLiteral(path.node.id.name)
94
+ )
95
+ )
96
+ );
97
+ }
98
+ }
99
+ }
100
+ };
101
+ }
102
+
103
+ const templateBuilder = _templateBuilder.default || _templateBuilder;
104
+ function reactRefreshPlugin({ types: t }, options) {
105
+ console.warn(
106
+ "[DEPRECATED] jotai/babel/plugin-react-refresh is deprecated and will be removed in v3.\nPlease use the `jotai-babel` package instead: https://github.com/jotaijs/jotai-babel"
107
+ );
108
+ return {
109
+ pre({ opts }) {
110
+ if (!opts.filename) {
111
+ throw new Error("Filename must be available");
112
+ }
113
+ },
114
+ visitor: {
115
+ Program: {
116
+ exit(path) {
117
+ const jotaiAtomCache = templateBuilder(`
118
+ globalThis.jotaiAtomCache = globalThis.jotaiAtomCache || {
119
+ cache: new Map(),
120
+ get(name, inst) {
121
+ if (this.cache.has(name)) {
122
+ return this.cache.get(name)
123
+ }
124
+ this.cache.set(name, inst)
125
+ return inst
126
+ },
127
+ }`)();
128
+ path.unshiftContainer("body", jotaiAtomCache);
129
+ }
130
+ },
131
+ ExportDefaultDeclaration(nodePath, state) {
132
+ const { node } = nodePath;
133
+ if (t.isCallExpression(node.declaration) && isAtom(t, node.declaration.callee, options == null ? void 0 : options.customAtomNames)) {
134
+ const filename = state.filename || "unknown";
135
+ const atomKey = `${filename}/defaultExport`;
136
+ const buildExport = templateBuilder(
137
+ `export default globalThis.jotaiAtomCache.get(%%atomKey%%, %%atom%%)`
138
+ );
139
+ const ast = buildExport({
140
+ atomKey: t.stringLiteral(atomKey),
141
+ atom: node.declaration
142
+ });
143
+ nodePath.replaceWith(ast);
144
+ }
145
+ },
146
+ VariableDeclarator(nodePath, state) {
147
+ var _a, _b;
148
+ if (t.isIdentifier(nodePath.node.id) && t.isCallExpression(nodePath.node.init) && isAtom(t, nodePath.node.init.callee, options == null ? void 0 : options.customAtomNames) && // Make sure atom declaration is in module scope
149
+ (((_a = nodePath.parentPath.parentPath) == null ? void 0 : _a.isProgram()) || ((_b = nodePath.parentPath.parentPath) == null ? void 0 : _b.isExportNamedDeclaration()))) {
150
+ const filename = state.filename || "unknown";
151
+ const atomKey = `${filename}/${nodePath.node.id.name}`;
152
+ const buildAtomDeclaration = templateBuilder(
153
+ `const %%atomIdentifier%% = globalThis.jotaiAtomCache.get(%%atomKey%%, %%atom%%)`
154
+ );
155
+ const ast = buildAtomDeclaration({
156
+ atomIdentifier: t.identifier(nodePath.node.id.name),
157
+ atomKey: t.stringLiteral(atomKey),
158
+ atom: nodePath.node.init
159
+ });
160
+ nodePath.parentPath.replaceWith(ast);
161
+ }
162
+ }
163
+ }
164
+ };
165
+ }
166
+
167
+ function jotaiPreset(_, options) {
168
+ console.warn(
169
+ "[DEPRECATED] jotai/babel/preset is deprecated and will be removed in v3.\nPlease use the `jotai-babel` package instead: https://github.com/jotaijs/jotai-babel"
170
+ );
171
+ return {
172
+ plugins: [
173
+ [debugLabelPlugin, options],
174
+ [reactRefreshPlugin, options]
175
+ ]
176
+ };
177
+ }
178
+
179
+ export { jotaiPreset as default };
@@ -0,0 +1,5 @@
1
+ import { types } from '@babel/core';
2
+ export interface PluginOptions {
3
+ customAtomNames?: string[];
4
+ }
5
+ export declare function isAtom(t: typeof types, callee: babel.types.Expression | babel.types.V8IntrinsicIdentifier, customAtomNames?: PluginOptions['customAtomNames']): boolean;
@@ -0,0 +1,2 @@
1
+ export * from 'jotai/vanilla';
2
+ export * from 'jotai/react';
package/esm/index.mjs ADDED
@@ -0,0 +1,2 @@
1
+ export * from 'jotai/vanilla';
2
+ export * from 'jotai/react';
@@ -0,0 +1,16 @@
1
+ import type { FunctionComponent, ReactElement, ReactNode } from 'react';
2
+ import { createStore } from 'jotai/vanilla';
3
+ type Store = ReturnType<typeof createStore>;
4
+ type Options = {
5
+ store?: Store;
6
+ };
7
+ export declare function useStore(options?: Options): Store;
8
+ export declare function Provider({ children, store, }: {
9
+ children?: ReactNode;
10
+ store?: Store;
11
+ }): ReactElement<{
12
+ value: Store | undefined;
13
+ }, FunctionComponent<{
14
+ value: Store;
15
+ }>>;
16
+ export {};
@@ -0,0 +1,13 @@
1
+ import type { Atom, ExtractAtomArgs, ExtractAtomResult, ExtractAtomValue, PrimitiveAtom, SetStateAction, WritableAtom } from 'jotai/vanilla';
2
+ import { useAtomValue } from './useAtomValue.mjs';
3
+ type SetAtom<Args extends unknown[], Result> = (...args: Args) => Result;
4
+ type Options = Parameters<typeof useAtomValue>[1];
5
+ export declare function useAtom<Value, Args extends unknown[], Result>(atom: WritableAtom<Value, Args, Result>, options?: Options): [Awaited<Value>, SetAtom<Args, Result>];
6
+ export declare function useAtom<Value>(atom: PrimitiveAtom<Value>, options?: Options): [Awaited<Value>, SetAtom<[SetStateAction<Value>], void>];
7
+ export declare function useAtom<Value>(atom: Atom<Value>, options?: Options): [Awaited<Value>, never];
8
+ export declare function useAtom<AtomType extends WritableAtom<unknown, never[], unknown>>(atom: AtomType, options?: Options): [
9
+ Awaited<ExtractAtomValue<AtomType>>,
10
+ SetAtom<ExtractAtomArgs<AtomType>, ExtractAtomResult<AtomType>>
11
+ ];
12
+ export declare function useAtom<AtomType extends Atom<unknown>>(atom: AtomType, options?: Options): [Awaited<ExtractAtomValue<AtomType>>, never];
13
+ export {};
@@ -0,0 +1,9 @@
1
+ import type { Atom, ExtractAtomValue } from 'jotai/vanilla';
2
+ import { useStore } from './Provider.mjs';
3
+ type Options = Parameters<typeof useStore>[0] & {
4
+ delay?: number;
5
+ unstable_promiseStatus?: boolean;
6
+ };
7
+ export declare function useAtomValue<Value>(atom: Atom<Value>, options?: Options): Awaited<Value>;
8
+ export declare function useAtomValue<AtomType extends Atom<unknown>>(atom: AtomType, options?: Options): Awaited<ExtractAtomValue<AtomType>>;
9
+ export {};
@@ -0,0 +1,7 @@
1
+ import type { ExtractAtomArgs, ExtractAtomResult, WritableAtom } from 'jotai/vanilla';
2
+ import { useStore } from './Provider.mjs';
3
+ type SetAtom<Args extends unknown[], Result> = (...args: Args) => Result;
4
+ type Options = Parameters<typeof useStore>[0];
5
+ export declare function useSetAtom<Value, Args extends unknown[], Result>(atom: WritableAtom<Value, Args, Result>, options?: Options): SetAtom<Args, Result>;
6
+ export declare function useSetAtom<AtomType extends WritableAtom<unknown, never[], unknown>>(atom: AtomType, options?: Options): SetAtom<ExtractAtomArgs<AtomType>, ExtractAtomResult<AtomType>>;
7
+ export {};
@@ -0,0 +1,5 @@
1
+ import { useSetAtom } from 'jotai/react';
2
+ import type { Getter, Setter } from 'jotai/vanilla';
3
+ type Options = Parameters<typeof useSetAtom>[1];
4
+ export declare function useAtomCallback<Result, Args extends unknown[]>(callback: (get: Getter, set: Setter, ...arg: Args) => Result, options?: Options): (...args: Args) => Result;
5
+ export {};
@@ -0,0 +1,14 @@
1
+ import { useStore } from 'jotai/react';
2
+ import { type WritableAtom } from 'jotai/vanilla';
3
+ type Options = Parameters<typeof useStore>[0] & {
4
+ dangerouslyForceHydrate?: boolean;
5
+ };
6
+ type AnyWritableAtom = WritableAtom<any, any[], any>;
7
+ type InferAtomTuples<T> = {
8
+ [K in keyof T]: T[K] extends readonly [infer A, ...infer Rest] ? A extends WritableAtom<unknown, infer Args, unknown> ? Rest extends Args ? readonly [A, ...Rest] : never : T[K] : never;
9
+ };
10
+ export type INTERNAL_InferAtomTuples<T> = InferAtomTuples<T>;
11
+ export declare function useHydrateAtoms<T extends (readonly [AnyWritableAtom, ...unknown[]])[]>(values: InferAtomTuples<T>, options?: Options): void;
12
+ export declare function useHydrateAtoms<T extends Map<AnyWritableAtom, unknown>>(values: T, options?: Options): void;
13
+ export declare function useHydrateAtoms<T extends Iterable<readonly [AnyWritableAtom, ...unknown[]]>>(values: InferAtomTuples<T>, options?: Options): void;
14
+ export {};
@@ -0,0 +1,14 @@
1
+ import { useAtom } from 'jotai/react';
2
+ import type { PrimitiveAtom } from 'jotai/vanilla';
3
+ type Options = Parameters<typeof useAtom>[1];
4
+ /**
5
+ * @deprecated please use a recipe instead
6
+ * https://github.com/pmndrs/jotai/pull/2467
7
+ */
8
+ export declare function useReducerAtom<Value, Action>(anAtom: PrimitiveAtom<Value>, reducer: (v: Value, a?: Action) => Value, options?: Options): [Value, (action?: Action) => void];
9
+ /**
10
+ * @deprecated please use a recipe instead
11
+ * https://github.com/pmndrs/jotai/pull/2467
12
+ */
13
+ export declare function useReducerAtom<Value, Action>(anAtom: PrimitiveAtom<Value>, reducer: (v: Value, a: Action) => Value, options?: Options): [Value, (action: Action) => void];
14
+ export {};
@@ -0,0 +1,6 @@
1
+ import { useSetAtom } from 'jotai/react';
2
+ import { RESET } from 'jotai/vanilla/utils';
3
+ import type { WritableAtom } from 'jotai/vanilla';
4
+ type Options = Parameters<typeof useSetAtom>[1];
5
+ export declare function useResetAtom<T>(anAtom: WritableAtom<unknown, [typeof RESET], T>, options?: Options): () => T;
6
+ export {};
@@ -0,0 +1,4 @@
1
+ export { useResetAtom } from './utils/useResetAtom.mjs';
2
+ export { useReducerAtom } from './utils/useReducerAtom.mjs';
3
+ export { useAtomCallback } from './utils/useAtomCallback.mjs';
4
+ export { useHydrateAtoms } from './utils/useHydrateAtoms.mjs';
@@ -0,0 +1,57 @@
1
+ 'use client';
2
+ import { useCallback, useMemo } from 'react';
3
+ import { useSetAtom, useAtom, useStore } from 'jotai/react';
4
+ import { RESET } from 'jotai/vanilla/utils';
5
+ import { atom } from 'jotai/vanilla';
6
+
7
+ function useResetAtom(anAtom, options) {
8
+ const setAtom = useSetAtom(anAtom, options);
9
+ const resetAtom = useCallback(() => setAtom(RESET), [setAtom]);
10
+ return resetAtom;
11
+ }
12
+
13
+ function useReducerAtom(anAtom, reducer, options) {
14
+ if ((import.meta.env ? import.meta.env.MODE : void 0) !== "production") {
15
+ console.warn(
16
+ "[DEPRECATED] useReducerAtom is deprecated and will be removed in the future. Please create your own version using the recipe. https://github.com/pmndrs/jotai/pull/2467"
17
+ );
18
+ }
19
+ const [state, setState] = useAtom(anAtom, options);
20
+ const dispatch = useCallback(
21
+ (action) => {
22
+ setState((prev) => reducer(prev, action));
23
+ },
24
+ [setState, reducer]
25
+ );
26
+ return [state, dispatch];
27
+ }
28
+
29
+ function useAtomCallback(callback, options) {
30
+ const anAtom = useMemo(
31
+ () => atom(null, (get, set, ...args) => callback(get, set, ...args)),
32
+ [callback]
33
+ );
34
+ return useSetAtom(anAtom, options);
35
+ }
36
+
37
+ const hydratedMap = /* @__PURE__ */ new WeakMap();
38
+ function useHydrateAtoms(values, options) {
39
+ const store = useStore(options);
40
+ const hydratedSet = getHydratedSet(store);
41
+ for (const [atom, ...args] of values) {
42
+ if (!hydratedSet.has(atom) || (options == null ? void 0 : options.dangerouslyForceHydrate)) {
43
+ hydratedSet.add(atom);
44
+ store.set(atom, ...args);
45
+ }
46
+ }
47
+ }
48
+ const getHydratedSet = (store) => {
49
+ let hydratedSet = hydratedMap.get(store);
50
+ if (!hydratedSet) {
51
+ hydratedSet = /* @__PURE__ */ new WeakSet();
52
+ hydratedMap.set(store, hydratedSet);
53
+ }
54
+ return hydratedSet;
55
+ };
56
+
57
+ export { useAtomCallback, useHydrateAtoms, useReducerAtom, useResetAtom };
@@ -0,0 +1,4 @@
1
+ export { Provider, useStore } from './react/Provider.mjs';
2
+ export { useAtomValue } from './react/useAtomValue.mjs';
3
+ export { useSetAtom } from './react/useSetAtom.mjs';
4
+ export { useAtom } from './react/useAtom.mjs';
package/esm/react.mjs ADDED
@@ -0,0 +1,182 @@
1
+ 'use client';
2
+ import React, { createContext, useContext, useRef, createElement, useReducer, useEffect, useDebugValue, useCallback } from 'react';
3
+ import { getDefaultStore, createStore } from 'jotai/vanilla';
4
+ import { INTERNAL_getBuildingBlocksRev2 } from 'jotai/vanilla/internals';
5
+
6
+ const StoreContext = createContext(
7
+ void 0
8
+ );
9
+ function useStore(options) {
10
+ const store = useContext(StoreContext);
11
+ return (options == null ? void 0 : options.store) || store || getDefaultStore();
12
+ }
13
+ function Provider({
14
+ children,
15
+ store
16
+ }) {
17
+ const storeRef = useRef(null);
18
+ if (store) {
19
+ return createElement(StoreContext.Provider, { value: store }, children);
20
+ }
21
+ if (storeRef.current === null) {
22
+ storeRef.current = createStore();
23
+ }
24
+ return createElement(
25
+ StoreContext.Provider,
26
+ {
27
+ // TODO: If this is not a false positive, consider using useState instead of useRef like https://github.com/pmndrs/jotai/pull/2771
28
+ // eslint-disable-next-line react-hooks/refs
29
+ value: storeRef.current
30
+ },
31
+ children
32
+ );
33
+ }
34
+
35
+ const isPromiseLike = (x) => typeof (x == null ? void 0 : x.then) === "function";
36
+ const attachPromiseStatus = (promise) => {
37
+ if (!promise.status) {
38
+ promise.status = "pending";
39
+ promise.then(
40
+ (v) => {
41
+ promise.status = "fulfilled";
42
+ promise.value = v;
43
+ },
44
+ (e) => {
45
+ promise.status = "rejected";
46
+ promise.reason = e;
47
+ }
48
+ );
49
+ }
50
+ };
51
+ const use = React.use || // A shim for older React versions
52
+ ((promise) => {
53
+ if (promise.status === "pending") {
54
+ throw promise;
55
+ } else if (promise.status === "fulfilled") {
56
+ return promise.value;
57
+ } else if (promise.status === "rejected") {
58
+ throw promise.reason;
59
+ } else {
60
+ attachPromiseStatus(promise);
61
+ throw promise;
62
+ }
63
+ });
64
+ const continuablePromiseMap = /* @__PURE__ */ new WeakMap();
65
+ const createContinuablePromise = (store, promise, getValue) => {
66
+ const buildingBlocks = INTERNAL_getBuildingBlocksRev2(store);
67
+ const registerAbortHandler = buildingBlocks[26];
68
+ let continuablePromise = continuablePromiseMap.get(promise);
69
+ if (!continuablePromise) {
70
+ continuablePromise = new Promise((resolve, reject) => {
71
+ let curr = promise;
72
+ const onFulfilled = (me) => (v) => {
73
+ if (curr === me) {
74
+ resolve(v);
75
+ }
76
+ };
77
+ const onRejected = (me) => (e) => {
78
+ if (curr === me) {
79
+ reject(e);
80
+ }
81
+ };
82
+ const onAbort = () => {
83
+ try {
84
+ const nextValue = getValue();
85
+ if (isPromiseLike(nextValue)) {
86
+ continuablePromiseMap.set(nextValue, continuablePromise);
87
+ curr = nextValue;
88
+ nextValue.then(onFulfilled(nextValue), onRejected(nextValue));
89
+ registerAbortHandler(store, nextValue, onAbort);
90
+ } else {
91
+ resolve(nextValue);
92
+ }
93
+ } catch (e) {
94
+ reject(e);
95
+ }
96
+ };
97
+ promise.then(onFulfilled(promise), onRejected(promise));
98
+ registerAbortHandler(store, promise, onAbort);
99
+ });
100
+ continuablePromiseMap.set(promise, continuablePromise);
101
+ }
102
+ return continuablePromise;
103
+ };
104
+ function useAtomValue(atom, options) {
105
+ const { delay, unstable_promiseStatus: promiseStatus = !React.use } = options || {};
106
+ const store = useStore(options);
107
+ const [[valueFromReducer, storeFromReducer, atomFromReducer], rerender] = useReducer(
108
+ (prev) => {
109
+ const nextValue = store.get(atom);
110
+ if (Object.is(prev[0], nextValue) && prev[1] === store && prev[2] === atom) {
111
+ return prev;
112
+ }
113
+ return [nextValue, store, atom];
114
+ },
115
+ void 0,
116
+ () => [store.get(atom), store, atom]
117
+ );
118
+ let value = valueFromReducer;
119
+ if (storeFromReducer !== store || atomFromReducer !== atom) {
120
+ rerender();
121
+ value = store.get(atom);
122
+ }
123
+ useEffect(() => {
124
+ const unsub = store.sub(atom, () => {
125
+ if (promiseStatus) {
126
+ try {
127
+ const value2 = store.get(atom);
128
+ if (isPromiseLike(value2)) {
129
+ attachPromiseStatus(
130
+ createContinuablePromise(store, value2, () => store.get(atom))
131
+ );
132
+ }
133
+ } catch (e) {
134
+ }
135
+ }
136
+ if (typeof delay === "number") {
137
+ setTimeout(rerender, delay);
138
+ return;
139
+ }
140
+ rerender();
141
+ });
142
+ rerender();
143
+ return unsub;
144
+ }, [store, atom, delay, promiseStatus]);
145
+ useDebugValue(value);
146
+ if (isPromiseLike(value)) {
147
+ const promise = createContinuablePromise(
148
+ store,
149
+ value,
150
+ () => store.get(atom)
151
+ );
152
+ if (promiseStatus) {
153
+ attachPromiseStatus(promise);
154
+ }
155
+ return use(promise);
156
+ }
157
+ return value;
158
+ }
159
+
160
+ function useSetAtom(atom, options) {
161
+ const store = useStore(options);
162
+ const setAtom = useCallback(
163
+ (...args) => {
164
+ if ((import.meta.env ? import.meta.env.MODE : void 0) !== "production" && !("write" in atom)) {
165
+ throw new Error("not writable atom");
166
+ }
167
+ return store.set(atom, ...args);
168
+ },
169
+ [store, atom]
170
+ );
171
+ return setAtom;
172
+ }
173
+
174
+ function useAtom(atom, options) {
175
+ return [
176
+ useAtomValue(atom, options),
177
+ // We do wrong type assertion here, which results in throwing an error.
178
+ useSetAtom(atom, options)
179
+ ];
180
+ }
181
+
182
+ export { Provider, useAtom, useAtomValue, useSetAtom, useStore };
@@ -0,0 +1,2 @@
1
+ export * from 'jotai/vanilla/utils';
2
+ export * from 'jotai/react/utils';
package/esm/utils.mjs ADDED
@@ -0,0 +1,2 @@
1
+ export * from 'jotai/vanilla/utils';
2
+ export * from 'jotai/react/utils';
@@ -0,0 +1,47 @@
1
+ import type { Store } from './store.mjs';
2
+ type Getter = <Value>(atom: Atom<Value>) => Value;
3
+ type Setter = <Value, Args extends unknown[], Result>(atom: WritableAtom<Value, Args, Result>, ...args: Args) => Result;
4
+ type SetAtom<Args extends unknown[], Result> = <A extends Args>(...args: A) => Result;
5
+ /**
6
+ * setSelf is for internal use only and subject to change without notice.
7
+ */
8
+ type Read<Value, SetSelf = never> = (get: Getter, options: {
9
+ readonly signal: AbortSignal;
10
+ readonly setSelf: SetSelf;
11
+ }) => Value;
12
+ type Write<Args extends unknown[], Result> = (get: Getter, set: Setter, ...args: Args) => Result;
13
+ type WithInitialValue<Value> = {
14
+ init: Value;
15
+ };
16
+ type OnUnmount = () => void;
17
+ type OnMount<Args extends unknown[], Result> = <S extends SetAtom<Args, Result>>(setAtom: S) => OnUnmount | void;
18
+ export interface Atom<Value> {
19
+ toString: () => string;
20
+ read: Read<Value>;
21
+ debugLabel?: string;
22
+ /**
23
+ * To ONLY be used by Jotai libraries to mark atoms as private. Subject to change.
24
+ * @private
25
+ */
26
+ debugPrivate?: boolean;
27
+ /**
28
+ * Fires after atom is referenced by the store for the first time
29
+ * This is an internal API and subject to change without notice.
30
+ */
31
+ INTERNAL_onInit?: (store: Store) => void;
32
+ }
33
+ export interface WritableAtom<Value, Args extends unknown[], Result> extends Atom<Value> {
34
+ read: Read<Value, SetAtom<Args, unknown>>;
35
+ write: Write<Args, Result>;
36
+ onMount?: OnMount<Args, Result>;
37
+ }
38
+ type SetStateAction<Value> = Value | ((prev: Value) => Value);
39
+ export type PrimitiveAtom<Value> = WritableAtom<Value, [
40
+ SetStateAction<Value>
41
+ ], void>;
42
+ export declare function atom<Value, Args extends unknown[], Result>(read: Read<Value, SetAtom<Args, unknown>>, write: Write<Args, Result>): WritableAtom<Value, Args, Result>;
43
+ export declare function atom<Value>(read: Read<Value>): Atom<Value>;
44
+ export declare function atom<Value, Args extends unknown[], Result>(initialValue: Value, write: Write<Args, Result>): WritableAtom<Value, Args, Result> & WithInitialValue<Value>;
45
+ export declare function atom<Value>(): PrimitiveAtom<Value | undefined> & WithInitialValue<Value | undefined>;
46
+ export declare function atom<Value>(initialValue: Value): PrimitiveAtom<Value> & WithInitialValue<Value>;
47
+ export {};