jazz-tools 0.19.7 → 0.19.10

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 (167) hide show
  1. package/.turbo/turbo-build.log +65 -59
  2. package/CHANGELOG.md +34 -3
  3. package/dist/{chunk-CUS6O5NE.js → chunk-FFEEPZEG.js} +454 -122
  4. package/dist/chunk-FFEEPZEG.js.map +1 -0
  5. package/dist/expo/polyfills.js +22 -0
  6. package/dist/expo/polyfills.js.map +1 -0
  7. package/dist/index.js +26 -6
  8. package/dist/index.js.map +1 -1
  9. package/dist/react/hooks.d.ts +1 -1
  10. package/dist/react/hooks.d.ts.map +1 -1
  11. package/dist/react/index.d.ts +1 -1
  12. package/dist/react/index.d.ts.map +1 -1
  13. package/dist/react/index.js +5 -1
  14. package/dist/react/index.js.map +1 -1
  15. package/dist/react-core/hooks.d.ts +59 -0
  16. package/dist/react-core/hooks.d.ts.map +1 -1
  17. package/dist/react-core/index.js +133 -34
  18. package/dist/react-core/index.js.map +1 -1
  19. package/dist/react-core/tests/testUtils.d.ts +1 -0
  20. package/dist/react-core/tests/testUtils.d.ts.map +1 -1
  21. package/dist/react-core/tests/useSuspenseAccount.test.d.ts +2 -0
  22. package/dist/react-core/tests/useSuspenseAccount.test.d.ts.map +1 -0
  23. package/dist/react-core/tests/useSuspenseCoState.test.d.ts +2 -0
  24. package/dist/react-core/tests/useSuspenseCoState.test.d.ts.map +1 -0
  25. package/dist/react-core/use.d.ts +3 -0
  26. package/dist/react-core/use.d.ts.map +1 -0
  27. package/dist/react-native/index.d.ts +1 -1
  28. package/dist/react-native/index.d.ts.map +1 -1
  29. package/dist/react-native/index.js +717 -9
  30. package/dist/react-native/index.js.map +1 -1
  31. package/dist/react-native/polyfills.js +22 -0
  32. package/dist/react-native/polyfills.js.map +1 -0
  33. package/dist/react-native-core/crypto/RNCrypto.d.ts +2 -0
  34. package/dist/react-native-core/crypto/RNCrypto.d.ts.map +1 -0
  35. package/dist/react-native-core/crypto/RNCrypto.js +3 -0
  36. package/dist/react-native-core/crypto/RNCrypto.js.map +1 -0
  37. package/dist/react-native-core/hooks.d.ts +1 -1
  38. package/dist/react-native-core/hooks.d.ts.map +1 -1
  39. package/dist/react-native-core/index.d.ts.map +1 -1
  40. package/dist/react-native-core/index.js +5 -1
  41. package/dist/react-native-core/index.js.map +1 -1
  42. package/dist/react-native-core/platform.d.ts +2 -1
  43. package/dist/react-native-core/platform.d.ts.map +1 -1
  44. package/dist/testing.js +1 -1
  45. package/dist/testing.js.map +1 -1
  46. package/dist/tools/coValues/account.d.ts +3 -3
  47. package/dist/tools/coValues/account.d.ts.map +1 -1
  48. package/dist/tools/coValues/coFeed.d.ts +3 -3
  49. package/dist/tools/coValues/coFeed.d.ts.map +1 -1
  50. package/dist/tools/coValues/coList.d.ts +4 -4
  51. package/dist/tools/coValues/coList.d.ts.map +1 -1
  52. package/dist/tools/coValues/coMap.d.ts +7 -7
  53. package/dist/tools/coValues/coMap.d.ts.map +1 -1
  54. package/dist/tools/coValues/coPlainText.d.ts +2 -2
  55. package/dist/tools/coValues/coPlainText.d.ts.map +1 -1
  56. package/dist/tools/coValues/coVector.d.ts +2 -2
  57. package/dist/tools/coValues/coVector.d.ts.map +1 -1
  58. package/dist/tools/coValues/deepLoading.d.ts +24 -0
  59. package/dist/tools/coValues/deepLoading.d.ts.map +1 -1
  60. package/dist/tools/coValues/group.d.ts +2 -2
  61. package/dist/tools/coValues/group.d.ts.map +1 -1
  62. package/dist/tools/coValues/interfaces.d.ts +7 -7
  63. package/dist/tools/coValues/interfaces.d.ts.map +1 -1
  64. package/dist/tools/coValues/schemaUnion.d.ts +2 -2
  65. package/dist/tools/coValues/schemaUnion.d.ts.map +1 -1
  66. package/dist/tools/config.d.ts +3 -0
  67. package/dist/tools/config.d.ts.map +1 -0
  68. package/dist/tools/exports.d.ts +2 -0
  69. package/dist/tools/exports.d.ts.map +1 -1
  70. package/dist/tools/implementation/ContextManager.d.ts +3 -0
  71. package/dist/tools/implementation/ContextManager.d.ts.map +1 -1
  72. package/dist/tools/implementation/zodSchema/schemaTypes/AccountSchema.d.ts +2 -2
  73. package/dist/tools/implementation/zodSchema/schemaTypes/AccountSchema.d.ts.map +1 -1
  74. package/dist/tools/implementation/zodSchema/schemaTypes/CoDiscriminatedUnionSchema.d.ts +2 -2
  75. package/dist/tools/implementation/zodSchema/schemaTypes/CoDiscriminatedUnionSchema.d.ts.map +1 -1
  76. package/dist/tools/implementation/zodSchema/schemaTypes/CoFeedSchema.d.ts +2 -2
  77. package/dist/tools/implementation/zodSchema/schemaTypes/CoFeedSchema.d.ts.map +1 -1
  78. package/dist/tools/implementation/zodSchema/schemaTypes/CoListSchema.d.ts +4 -4
  79. package/dist/tools/implementation/zodSchema/schemaTypes/CoListSchema.d.ts.map +1 -1
  80. package/dist/tools/implementation/zodSchema/schemaTypes/CoMapSchema.d.ts +4 -4
  81. package/dist/tools/implementation/zodSchema/schemaTypes/CoMapSchema.d.ts.map +1 -1
  82. package/dist/tools/implementation/zodSchema/schemaTypes/CoRecordSchema.d.ts +4 -4
  83. package/dist/tools/implementation/zodSchema/schemaTypes/CoRecordSchema.d.ts.map +1 -1
  84. package/dist/tools/implementation/zodSchema/schemaTypes/FileStreamSchema.d.ts +2 -2
  85. package/dist/tools/implementation/zodSchema/schemaTypes/FileStreamSchema.d.ts.map +1 -1
  86. package/dist/tools/implementation/zodSchema/schemaTypes/GroupSchema.d.ts +2 -2
  87. package/dist/tools/implementation/zodSchema/schemaTypes/GroupSchema.d.ts.map +1 -1
  88. package/dist/tools/implementation/zodSchema/schemaTypes/PlainTextSchema.d.ts +2 -2
  89. package/dist/tools/implementation/zodSchema/schemaTypes/PlainTextSchema.d.ts.map +1 -1
  90. package/dist/tools/implementation/zodSchema/schemaTypes/RichTextSchema.d.ts +2 -2
  91. package/dist/tools/implementation/zodSchema/schemaTypes/RichTextSchema.d.ts.map +1 -1
  92. package/dist/tools/implementation/zodSchema/zodCo.d.ts.map +1 -1
  93. package/dist/tools/subscribe/CoValueCoreSubscription.d.ts +8 -22
  94. package/dist/tools/subscribe/CoValueCoreSubscription.d.ts.map +1 -1
  95. package/dist/tools/subscribe/JazzError.d.ts.map +1 -1
  96. package/dist/tools/subscribe/SubscriptionCache.d.ts +51 -0
  97. package/dist/tools/subscribe/SubscriptionCache.d.ts.map +1 -0
  98. package/dist/tools/subscribe/SubscriptionScope.d.ts +27 -2
  99. package/dist/tools/subscribe/SubscriptionScope.d.ts.map +1 -1
  100. package/dist/tools/subscribe/errorReporting.d.ts +31 -0
  101. package/dist/tools/subscribe/errorReporting.d.ts.map +1 -0
  102. package/dist/tools/subscribe/utils.d.ts +9 -1
  103. package/dist/tools/subscribe/utils.d.ts.map +1 -1
  104. package/dist/tools/testing.d.ts +2 -2
  105. package/dist/tools/testing.d.ts.map +1 -1
  106. package/dist/tools/tests/SubscriptionCache.test.d.ts +2 -0
  107. package/dist/tools/tests/SubscriptionCache.test.d.ts.map +1 -0
  108. package/dist/tools/tests/errorReporting.test.d.ts +2 -0
  109. package/dist/tools/tests/errorReporting.test.d.ts.map +1 -0
  110. package/package.json +22 -7
  111. package/src/react/hooks.tsx +2 -0
  112. package/src/react/index.ts +1 -14
  113. package/src/react-core/hooks.ts +181 -16
  114. package/src/react-core/tests/createCoValueSubscriptionContext.test.tsx +18 -8
  115. package/src/react-core/tests/testUtils.tsx +67 -5
  116. package/src/react-core/tests/useCoState.test.ts +3 -7
  117. package/src/react-core/tests/useSubscriptionSelector.test.ts +3 -7
  118. package/src/react-core/tests/useSuspenseAccount.test.tsx +343 -0
  119. package/src/react-core/tests/useSuspenseCoState.test.tsx +1182 -0
  120. package/src/react-core/use.ts +46 -0
  121. package/src/react-native/index.ts +1 -1
  122. package/src/react-native-core/crypto/RNCrypto.ts +1 -0
  123. package/src/react-native-core/hooks.tsx +2 -0
  124. package/src/react-native-core/index.ts +2 -0
  125. package/src/react-native-core/platform.ts +2 -1
  126. package/src/react-native-core/polyfills/index.js +28 -0
  127. package/src/tools/coValues/account.ts +3 -4
  128. package/src/tools/coValues/coFeed.ts +3 -2
  129. package/src/tools/coValues/coList.ts +4 -4
  130. package/src/tools/coValues/coMap.ts +4 -4
  131. package/src/tools/coValues/coPlainText.ts +2 -2
  132. package/src/tools/coValues/coVector.ts +2 -2
  133. package/src/tools/coValues/deepLoading.ts +31 -0
  134. package/src/tools/coValues/group.ts +2 -2
  135. package/src/tools/coValues/interfaces.ts +21 -26
  136. package/src/tools/coValues/schemaUnion.ts +2 -2
  137. package/src/tools/config.ts +9 -0
  138. package/src/tools/exports.ts +4 -0
  139. package/src/tools/implementation/ContextManager.ts +13 -0
  140. package/src/tools/implementation/zodSchema/schemaTypes/AccountSchema.ts +2 -2
  141. package/src/tools/implementation/zodSchema/schemaTypes/CoDiscriminatedUnionSchema.ts +2 -2
  142. package/src/tools/implementation/zodSchema/schemaTypes/CoFeedSchema.ts +2 -2
  143. package/src/tools/implementation/zodSchema/schemaTypes/CoListSchema.ts +4 -4
  144. package/src/tools/implementation/zodSchema/schemaTypes/CoMapSchema.ts +4 -4
  145. package/src/tools/implementation/zodSchema/schemaTypes/CoRecordSchema.ts +4 -10
  146. package/src/tools/implementation/zodSchema/schemaTypes/FileStreamSchema.ts +2 -2
  147. package/src/tools/implementation/zodSchema/schemaTypes/GroupSchema.ts +2 -2
  148. package/src/tools/implementation/zodSchema/schemaTypes/PlainTextSchema.ts +2 -2
  149. package/src/tools/implementation/zodSchema/schemaTypes/RichTextSchema.ts +2 -2
  150. package/src/tools/subscribe/CoValueCoreSubscription.ts +71 -100
  151. package/src/tools/subscribe/JazzError.ts +9 -6
  152. package/src/tools/subscribe/SubscriptionCache.ts +272 -0
  153. package/src/tools/subscribe/SubscriptionScope.ts +218 -29
  154. package/src/tools/subscribe/errorReporting.ts +67 -0
  155. package/src/tools/subscribe/utils.ts +77 -0
  156. package/src/tools/testing.ts +0 -3
  157. package/src/tools/tests/CoValueCoreSubscription.test.ts +46 -12
  158. package/src/tools/tests/ContextManager.test.ts +85 -0
  159. package/src/tools/tests/SubscriptionCache.test.ts +237 -0
  160. package/src/tools/tests/coMap.test.ts +5 -7
  161. package/src/tools/tests/deepLoading.test.ts +47 -47
  162. package/src/tools/tests/errorReporting.test.ts +103 -0
  163. package/src/tools/tests/load.test.ts +21 -1
  164. package/src/tools/tests/request.test.ts +2 -1
  165. package/src/tools/tests/subscribe.test.ts +44 -0
  166. package/tsup.config.ts +17 -0
  167. package/dist/chunk-CUS6O5NE.js.map +0 -1
@@ -1,11 +1,690 @@
1
- // src/react-native/index.ts
2
- export * from "jazz-tools/react-native-core";
1
+ // src/react-native-core/storage/kv-store-context.ts
2
+ var KvStoreContext = class _KvStoreContext {
3
+ constructor() {
4
+ this.storageInstance = null;
5
+ }
6
+ static getInstance() {
7
+ if (!_KvStoreContext.instance) {
8
+ _KvStoreContext.instance = new _KvStoreContext();
9
+ }
10
+ return _KvStoreContext.instance;
11
+ }
12
+ isInitialized() {
13
+ return this.storageInstance !== null;
14
+ }
15
+ initialize(store) {
16
+ if (!this.storageInstance) {
17
+ this.storageInstance = store;
18
+ }
19
+ }
20
+ getStorage() {
21
+ if (!this.storageInstance) {
22
+ throw new Error("Storage instance is not initialized.");
23
+ }
24
+ return this.storageInstance;
25
+ }
26
+ };
27
+
28
+ // src/react-native-core/auth/DemoAuthUI.tsx
29
+ import { useState } from "react";
30
+ import {
31
+ StyleSheet,
32
+ Text,
33
+ TextInput,
34
+ TouchableOpacity,
35
+ View,
36
+ useColorScheme
37
+ } from "react-native";
38
+ import { jsx, jsxs } from "react/jsx-runtime";
39
+ var DemoAuthBasicUI = ({
40
+ appName,
41
+ auth,
42
+ children
43
+ }) => {
44
+ const colorScheme = useColorScheme();
45
+ const darkMode = colorScheme === "dark";
46
+ const [username, setUsername] = useState("");
47
+ const [errorMessage, setErrorMessage] = useState(null);
48
+ const handleSignUp = () => {
49
+ setErrorMessage(null);
50
+ auth.signUp(username).catch((error) => {
51
+ setErrorMessage(error.message);
52
+ });
53
+ };
54
+ const handleLogIn = (username2) => {
55
+ setErrorMessage(null);
56
+ auth.logIn(username2).catch((error) => {
57
+ setErrorMessage(error.message);
58
+ });
59
+ };
60
+ if (auth.state === "signedIn") {
61
+ return children;
62
+ }
63
+ return /* @__PURE__ */ jsx(
64
+ View,
65
+ {
66
+ style: [
67
+ styles.container,
68
+ darkMode ? styles.darkBackground : styles.lightBackground
69
+ ],
70
+ children: /* @__PURE__ */ jsxs(View, { style: styles.formContainer, children: [
71
+ /* @__PURE__ */ jsx(
72
+ Text,
73
+ {
74
+ style: [
75
+ styles.headerText,
76
+ darkMode ? styles.darkText : styles.lightText
77
+ ],
78
+ children: appName
79
+ }
80
+ ),
81
+ errorMessage && /* @__PURE__ */ jsx(Text, { style: styles.errorText, children: errorMessage }),
82
+ /* @__PURE__ */ jsx(
83
+ TextInput,
84
+ {
85
+ placeholder: "Display name",
86
+ value: username,
87
+ onChangeText: setUsername,
88
+ placeholderTextColor: darkMode ? "#fff" : "#000",
89
+ style: [
90
+ styles.textInput,
91
+ darkMode ? styles.darkInput : styles.lightInput
92
+ ]
93
+ }
94
+ ),
95
+ /* @__PURE__ */ jsx(
96
+ TouchableOpacity,
97
+ {
98
+ onPress: handleSignUp,
99
+ style: [
100
+ styles.button,
101
+ darkMode ? styles.darkButton : styles.lightButton
102
+ ],
103
+ children: /* @__PURE__ */ jsx(
104
+ Text,
105
+ {
106
+ style: darkMode ? styles.darkButtonText : styles.lightButtonText,
107
+ children: "Sign Up as new account"
108
+ }
109
+ )
110
+ }
111
+ ),
112
+ /* @__PURE__ */ jsx(View, { style: styles.existingUsersContainer, children: auth.existingUsers.map((user) => /* @__PURE__ */ jsx(
113
+ TouchableOpacity,
114
+ {
115
+ onPress: () => handleLogIn(user),
116
+ style: [
117
+ styles.existingUserButton,
118
+ darkMode ? styles.darkUserButton : styles.lightUserButton
119
+ ],
120
+ children: /* @__PURE__ */ jsxs(Text, { style: darkMode ? styles.darkText : styles.lightText, children: [
121
+ 'Log In as "',
122
+ user,
123
+ '"'
124
+ ] })
125
+ },
126
+ user
127
+ )) })
128
+ ] })
129
+ }
130
+ );
131
+ };
132
+ var styles = StyleSheet.create({
133
+ container: {
134
+ flex: 1,
135
+ justifyContent: "center",
136
+ alignItems: "center",
137
+ padding: 20
138
+ },
139
+ formContainer: {
140
+ width: "80%",
141
+ alignItems: "center",
142
+ justifyContent: "center"
143
+ },
144
+ headerText: {
145
+ fontSize: 24,
146
+ marginBottom: 20
147
+ },
148
+ errorText: {
149
+ color: "red",
150
+ marginVertical: 5,
151
+ textAlign: "center"
152
+ },
153
+ textInput: {
154
+ borderWidth: 1,
155
+ padding: 10,
156
+ marginVertical: 10,
157
+ width: "100%",
158
+ borderRadius: 6
159
+ },
160
+ darkInput: {
161
+ borderColor: "#444",
162
+ backgroundColor: "#000",
163
+ color: "#fff"
164
+ },
165
+ lightInput: {
166
+ borderColor: "#ddd",
167
+ backgroundColor: "#fff",
168
+ color: "#000"
169
+ },
170
+ button: {
171
+ paddingVertical: 15,
172
+ paddingHorizontal: 10,
173
+ borderRadius: 6,
174
+ width: "100%",
175
+ marginVertical: 10
176
+ },
177
+ darkButton: {
178
+ backgroundColor: "#444"
179
+ },
180
+ lightButton: {
181
+ backgroundColor: "#ddd"
182
+ },
183
+ darkButtonText: {
184
+ color: "#fff",
185
+ textAlign: "center"
186
+ },
187
+ lightButtonText: {
188
+ color: "#000",
189
+ textAlign: "center"
190
+ },
191
+ existingUsersContainer: {
192
+ width: "100%",
193
+ marginTop: 20
194
+ },
195
+ existingUserButton: {
196
+ paddingVertical: 15,
197
+ paddingHorizontal: 10,
198
+ borderRadius: 6,
199
+ marginVertical: 5
200
+ },
201
+ darkUserButton: {
202
+ backgroundColor: "#222"
203
+ },
204
+ lightUserButton: {
205
+ backgroundColor: "#eee"
206
+ },
207
+ loadingText: {
208
+ fontSize: 18,
209
+ color: "#888"
210
+ },
211
+ darkText: {
212
+ color: "#fff"
213
+ },
214
+ lightText: {
215
+ color: "#000"
216
+ },
217
+ darkBackground: {
218
+ backgroundColor: "#000"
219
+ },
220
+ lightBackground: {
221
+ backgroundColor: "#fff"
222
+ }
223
+ });
224
+
225
+ // src/react-native-core/auth/auth.ts
226
+ function clearUserCredentials() {
227
+ const kvStore = KvStoreContext.getInstance().getStorage();
228
+ return Promise.all([
229
+ kvStore.delete("demo-auth-logged-in-secret"),
230
+ kvStore.delete("jazz-clerk-auth"),
231
+ kvStore.delete("jazz-logged-in-secret")
232
+ ]);
233
+ }
234
+
235
+ // src/react-native-core/hooks.tsx
236
+ import { useEffect } from "react";
237
+ import { parseInviteLink } from "jazz-tools";
238
+ import { useJazzContext } from "jazz-tools/react-core";
239
+ import { Linking } from "react-native";
240
+ import {
241
+ useCoState,
242
+ experimental_useInboxSender,
243
+ useDemoAuth,
244
+ usePassphraseAuth,
245
+ useJazzContext as useJazzContext2,
246
+ useAuthSecretStorage,
247
+ useIsAuthenticated,
248
+ useAccount,
249
+ useAgent,
250
+ useLogOut,
251
+ useSyncConnectionStatus,
252
+ useCoValueSubscription,
253
+ useAccountSubscription,
254
+ useSubscriptionSelector,
255
+ useSuspenseCoState,
256
+ useSuspenseAccount
257
+ } from "jazz-tools/react-core";
258
+ function useAcceptInviteNative({
259
+ invitedObjectSchema,
260
+ onAccept,
261
+ forValueHint
262
+ }) {
263
+ const context = useJazzContext();
264
+ if (!("me" in context)) {
265
+ throw new Error(
266
+ "useAcceptInviteNative can't be used in a JazzProvider with auth === 'guest'."
267
+ );
268
+ }
269
+ useEffect(() => {
270
+ const handleDeepLink = ({ url }) => {
271
+ const result = parseInviteLink(url);
272
+ if (result && result.valueHint === forValueHint) {
273
+ context.me.acceptInvite(
274
+ result.valueID,
275
+ result.inviteSecret,
276
+ invitedObjectSchema
277
+ ).then(() => {
278
+ onAccept(result.valueID);
279
+ }).catch((e) => {
280
+ console.error("Failed to accept invite", e);
281
+ });
282
+ }
283
+ };
284
+ const linkingListener = Linking.addEventListener("url", handleDeepLink);
285
+ void Linking.getInitialURL().then((url) => {
286
+ if (url) handleDeepLink({ url });
287
+ });
288
+ return () => {
289
+ linkingListener.remove();
290
+ };
291
+ }, [context, onAccept, invitedObjectSchema, forValueHint]);
292
+ }
293
+
294
+ // src/react-native-core/provider.tsx
295
+ import { JazzContext, JazzContextManagerContext } from "jazz-tools/react-core";
296
+ import React2, { useEffect as useEffect2, useRef } from "react";
297
+
298
+ // src/react-native-core/ReactNativeContextManager.ts
299
+ import {
300
+ JazzContextManager
301
+ } from "jazz-tools";
302
+
303
+ // src/react-native-core/platform.ts
304
+ import NetInfo from "@react-native-community/netinfo";
305
+ import { getSqliteStorageAsync } from "cojson";
306
+ import { PureJSCrypto } from "cojson/dist/crypto/PureJSCrypto";
307
+ import {
308
+ createInviteLink as baseCreateInviteLink,
309
+ createAnonymousJazzContext,
310
+ createJazzContext
311
+ } from "jazz-tools";
312
+ import { WebSocketPeerWithReconnection } from "cojson-transport-ws";
313
+ var ReactNativeWebSocketPeerWithReconnection = class extends WebSocketPeerWithReconnection {
314
+ onNetworkChange(callback) {
315
+ return NetInfo.addEventListener(
316
+ (state) => callback(state.isConnected ?? false)
317
+ );
318
+ }
319
+ };
320
+ async function setupPeers(options) {
321
+ const CryptoProvider2 = options.CryptoProvider || PureJSCrypto;
322
+ const crypto = await CryptoProvider2.create();
323
+ let node = void 0;
324
+ const peers = [];
325
+ const storage2 = options.storage && options.storage !== "disabled" ? await getSqliteStorageAsync(options.storage) : void 0;
326
+ if (options.sync.when === "never") {
327
+ return {
328
+ toggleNetwork: () => {
329
+ },
330
+ addConnectionListener: () => () => {
331
+ },
332
+ connected: () => false,
333
+ peers,
334
+ setNode: () => {
335
+ },
336
+ crypto,
337
+ storage: storage2
338
+ };
339
+ }
340
+ const wsPeer = new ReactNativeWebSocketPeerWithReconnection({
341
+ peer: options.sync.peer,
342
+ reconnectionTimeout: options.reconnectionTimeout,
343
+ addPeer: (peer) => {
344
+ if (node) {
345
+ node.syncManager.addPeer(peer);
346
+ } else {
347
+ peers.push(peer);
348
+ }
349
+ },
350
+ removePeer: (peer) => {
351
+ peers.splice(peers.indexOf(peer), 1);
352
+ }
353
+ });
354
+ function toggleNetwork(enabled) {
355
+ if (enabled) {
356
+ wsPeer.enable();
357
+ } else {
358
+ wsPeer.disable();
359
+ }
360
+ }
361
+ function setNode(value) {
362
+ node = value;
363
+ }
364
+ if (options.sync.when === "always" || !options.sync.when) {
365
+ toggleNetwork(true);
366
+ }
367
+ return {
368
+ toggleNetwork,
369
+ addConnectionListener(listener) {
370
+ wsPeer.subscribe(listener);
371
+ return () => {
372
+ wsPeer.unsubscribe(listener);
373
+ };
374
+ },
375
+ connected: () => wsPeer.connected,
376
+ peers,
377
+ setNode,
378
+ crypto,
379
+ storage: storage2
380
+ };
381
+ }
382
+ async function createJazzReactNativeGuestContext(options) {
383
+ const {
384
+ toggleNetwork,
385
+ peers,
386
+ setNode,
387
+ crypto,
388
+ storage: storage2,
389
+ addConnectionListener,
390
+ connected
391
+ } = await setupPeers(options);
392
+ const context = createAnonymousJazzContext({
393
+ crypto,
394
+ peers,
395
+ storage: storage2
396
+ });
397
+ setNode(context.agent.node);
398
+ options.authSecretStorage.emitUpdate(null);
399
+ return {
400
+ guest: context.agent,
401
+ node: context.agent.node,
402
+ done: () => {
403
+ toggleNetwork(false);
404
+ context.done();
405
+ },
406
+ logOut: () => {
407
+ return context.logOut();
408
+ },
409
+ addConnectionListener,
410
+ connected
411
+ };
412
+ }
413
+ async function createJazzReactNativeContext(options) {
414
+ const {
415
+ toggleNetwork,
416
+ peers,
417
+ setNode,
418
+ crypto,
419
+ storage: storage2,
420
+ addConnectionListener,
421
+ connected
422
+ } = await setupPeers(options);
423
+ let unsubscribeAuthUpdate = () => {
424
+ };
425
+ if (options.sync.when === "signedUp") {
426
+ let handleAuthUpdate2 = function(isAuthenticated) {
427
+ if (isAuthenticated) {
428
+ toggleNetwork(true);
429
+ } else {
430
+ toggleNetwork(false);
431
+ }
432
+ };
433
+ var handleAuthUpdate = handleAuthUpdate2;
434
+ const authSecretStorage = options.authSecretStorage;
435
+ const credentials = options.credentials ?? await authSecretStorage.get();
436
+ authSecretStorage.emitUpdate(credentials);
437
+ unsubscribeAuthUpdate = authSecretStorage.onUpdate(handleAuthUpdate2);
438
+ handleAuthUpdate2(authSecretStorage.isAuthenticated);
439
+ }
440
+ const context = await createJazzContext({
441
+ credentials: options.credentials,
442
+ newAccountProps: options.newAccountProps,
443
+ peers,
444
+ crypto,
445
+ defaultProfileName: options.defaultProfileName,
446
+ AccountSchema: options.AccountSchema,
447
+ sessionProvider: provideLockSession,
448
+ authSecretStorage: options.authSecretStorage,
449
+ storage: storage2
450
+ });
451
+ setNode(context.node);
452
+ return {
453
+ me: context.account,
454
+ node: context.node,
455
+ authSecretStorage: context.authSecretStorage,
456
+ done: () => {
457
+ toggleNetwork(false);
458
+ unsubscribeAuthUpdate();
459
+ context.done();
460
+ },
461
+ logOut: () => {
462
+ unsubscribeAuthUpdate();
463
+ return context.logOut();
464
+ },
465
+ addConnectionListener,
466
+ connected
467
+ };
468
+ }
469
+ async function provideLockSession(accountID, crypto) {
470
+ const sessionDone = () => {
471
+ };
472
+ const kvStore = KvStoreContext.getInstance().getStorage();
473
+ const sessionID = await kvStore.get(accountID) || crypto.newRandomSessionID(accountID);
474
+ await kvStore.set(accountID, sessionID);
475
+ return Promise.resolve({
476
+ sessionID,
477
+ sessionDone
478
+ });
479
+ }
480
+ function createInviteLink(value, role, { baseURL, valueHint } = {}) {
481
+ return baseCreateInviteLink(value, role, baseURL ?? "", valueHint);
482
+ }
483
+ function setupKvStore(kvStore) {
484
+ if (!kvStore) {
485
+ return void 0;
486
+ }
487
+ KvStoreContext.getInstance().initialize(kvStore);
488
+ return kvStore;
489
+ }
490
+
491
+ // src/react-native-core/ReactNativeContextManager.ts
492
+ var ReactNativeContextManager = class extends JazzContextManager {
493
+ async getNewContext(props, authProps) {
494
+ if (props.guestMode) {
495
+ return createJazzReactNativeGuestContext({
496
+ sync: props.sync,
497
+ storage: props.storage,
498
+ authSecretStorage: this.authSecretStorage,
499
+ CryptoProvider: props.CryptoProvider
500
+ });
501
+ } else {
502
+ return createJazzReactNativeContext({
503
+ sync: props.sync,
504
+ storage: props.storage,
505
+ AccountSchema: props.AccountSchema,
506
+ credentials: authProps?.credentials,
507
+ newAccountProps: authProps?.newAccountProps,
508
+ defaultProfileName: props.defaultProfileName,
509
+ authSecretStorage: this.authSecretStorage,
510
+ CryptoProvider: props.CryptoProvider
511
+ });
512
+ }
513
+ }
514
+ getKvStore() {
515
+ return KvStoreContext.getInstance().getStorage();
516
+ }
517
+ propsChanged(props) {
518
+ if (!this.props) {
519
+ return true;
520
+ }
521
+ return this.props.sync.when !== props.sync.when || this.props.sync.peer !== props.sync.peer || this.props.guestMode !== props.guestMode;
522
+ }
523
+ };
524
+
525
+ // src/react-native-core/provider.tsx
526
+ import { jsx as jsx2 } from "react/jsx-runtime";
527
+ function JazzProviderCore({
528
+ children,
529
+ guestMode,
530
+ sync,
531
+ storage: storage2,
532
+ AccountSchema,
533
+ defaultProfileName,
534
+ onLogOut,
535
+ logOutReplacement,
536
+ onAnonymousAccountDiscarded,
537
+ kvStore,
538
+ CryptoProvider: CryptoProvider2,
539
+ authSecretStorageKey
540
+ }) {
541
+ setupKvStore(kvStore);
542
+ const [contextManager] = React2.useState(
543
+ () => new ReactNativeContextManager({ authSecretStorageKey })
544
+ );
545
+ const onLogOutRefCallback = useRefCallback(onLogOut);
546
+ const logOutReplacementRefCallback = useRefCallback(logOutReplacement);
547
+ const onAnonymousAccountDiscardedRefCallback = useRefCallback(
548
+ onAnonymousAccountDiscarded
549
+ );
550
+ const logoutReplacementActiveRef = useRef(false);
551
+ logoutReplacementActiveRef.current = Boolean(logOutReplacement);
552
+ const value = React2.useSyncExternalStore(
553
+ React2.useCallback(
554
+ (callback) => {
555
+ const props = {
556
+ AccountSchema,
557
+ guestMode,
558
+ sync,
559
+ storage: storage2,
560
+ defaultProfileName,
561
+ onLogOut: onLogOutRefCallback,
562
+ logOutReplacement: logoutReplacementActiveRef.current ? logOutReplacementRefCallback : void 0,
563
+ onAnonymousAccountDiscarded: onAnonymousAccountDiscardedRefCallback,
564
+ CryptoProvider: CryptoProvider2
565
+ };
566
+ if (contextManager.propsChanged(props)) {
567
+ contextManager.createContext(props).catch((error) => {
568
+ console.log(error.stack);
569
+ console.error("Error creating Jazz React Native context:", error);
570
+ });
571
+ }
572
+ return contextManager.subscribe(callback);
573
+ },
574
+ [sync, guestMode].concat(storage2)
575
+ ),
576
+ () => contextManager.getCurrentValue(),
577
+ () => contextManager.getCurrentValue()
578
+ );
579
+ useEffect2(() => {
580
+ if (process.env.NODE_ENV === "development") return;
581
+ return () => {
582
+ contextManager.done();
583
+ };
584
+ }, []);
585
+ return /* @__PURE__ */ jsx2(JazzContext.Provider, { value, children: /* @__PURE__ */ jsx2(JazzContextManagerContext.Provider, { value: contextManager, children: value && children }) });
586
+ }
587
+ function useRefCallback(callback) {
588
+ const callbackRef = React2.useRef(callback);
589
+ callbackRef.current = callback;
590
+ return useRef(
591
+ (...args) => callbackRef.current?.(...args)
592
+ ).current;
593
+ }
594
+
595
+ // src/react-native-core/media/image.tsx
596
+ import { ImageDefinition } from "jazz-tools";
597
+ import { highestResAvailable } from "jazz-tools/media";
598
+ import { forwardRef, useEffect as useEffect3, useMemo, useState as useState2 } from "react";
599
+ import { Image as RNImage } from "react-native";
600
+ import { jsx as jsx3 } from "react/jsx-runtime";
601
+ var Image = forwardRef(function Image2({ imageId, width, height, placeholder, ...props }, ref) {
602
+ const image = useCoState(ImageDefinition, imageId, {
603
+ select: (image2) => image2.$isLoaded ? image2 : null
604
+ });
605
+ const [src, setSrc] = useState2(
606
+ image?.placeholderDataURL ?? "data:image/gif;base64,R0lGODlhAQABAIAAAP///wAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw=="
607
+ );
608
+ const dimensions = useMemo(() => {
609
+ const originalWidth = image?.originalSize?.[0];
610
+ const originalHeight = image?.originalSize?.[1];
611
+ if (width === "original" && height === "original") {
612
+ return { width: originalWidth, height: originalHeight };
613
+ }
614
+ if (width === "original" && typeof height === "number") {
615
+ if (originalWidth && originalHeight) {
616
+ return {
617
+ width: Math.round(height * originalWidth / originalHeight),
618
+ height
619
+ };
620
+ }
621
+ return { width: void 0, height };
622
+ }
623
+ if (height === "original" && typeof width === "number") {
624
+ if (originalWidth && originalHeight) {
625
+ return {
626
+ width,
627
+ height: Math.round(width * originalHeight / originalWidth)
628
+ };
629
+ }
630
+ return { width, height: void 0 };
631
+ }
632
+ return {
633
+ width: width === "original" ? originalWidth : width,
634
+ height: height === "original" ? originalHeight : height
635
+ };
636
+ }, [image?.originalSize, width, height]);
637
+ useEffect3(() => {
638
+ if (!image) return;
639
+ let lastBestImage = image?.placeholderDataURL ?? placeholder;
640
+ const unsub = image.$jazz.subscribe({}, (update) => {
641
+ if (lastBestImage === void 0 && update.placeholderDataURL) {
642
+ setSrc(update.placeholderDataURL);
643
+ lastBestImage = update.placeholderDataURL;
644
+ }
645
+ const bestImage = highestResAvailable(
646
+ update,
647
+ dimensions.width || dimensions.height || 9999,
648
+ dimensions.height || dimensions.width || 9999
649
+ );
650
+ if (!bestImage) return;
651
+ if (lastBestImage === bestImage.image) return;
652
+ const url = bestImage.image.asBase64({ dataURL: true });
653
+ if (url) {
654
+ setSrc(url);
655
+ lastBestImage = bestImage.image;
656
+ }
657
+ });
658
+ return unsub;
659
+ }, [image]);
660
+ if (!src) {
661
+ return null;
662
+ }
663
+ return /* @__PURE__ */ jsx3(
664
+ RNImage,
665
+ {
666
+ ref,
667
+ source: { uri: src },
668
+ width: dimensions.width,
669
+ height: dimensions.height,
670
+ ...props
671
+ }
672
+ );
673
+ });
674
+
675
+ // src/react-native-core/index.ts
676
+ import {
677
+ createCoValueSubscriptionContext,
678
+ createAccountSubscriptionContext
679
+ } from "jazz-tools/react-core";
680
+ import { SQLiteDatabaseDriverAsync } from "cojson";
681
+ import { parseInviteLink as parseInviteLink2 } from "jazz-tools";
3
682
 
4
683
  // src/react-native/provider.tsx
5
684
  import {
6
- JazzProviderCore
685
+ JazzProviderCore as JazzProviderCore2
7
686
  } from "jazz-tools/react-native-core";
8
- import { useMemo } from "react";
687
+ import { useMemo as useMemo2 } from "react";
9
688
 
10
689
  // src/react-native/storage/mmkv-store-adapter.ts
11
690
  import { MMKV } from "react-native-mmkv";
@@ -85,19 +764,48 @@ var OPSQLiteAdapter = class {
85
764
  };
86
765
 
87
766
  // src/react-native/provider.tsx
88
- import { jsx } from "react/jsx-runtime";
767
+ import { jsx as jsx4 } from "react/jsx-runtime";
89
768
  function JazzReactNativeProvider(props) {
90
- const storage2 = useMemo(() => {
769
+ const storage2 = useMemo2(() => {
91
770
  return props.storage ?? new OPSQLiteAdapter();
92
771
  }, [props.storage]);
93
- const kvStore = useMemo(() => {
772
+ const kvStore = useMemo2(() => {
94
773
  return props.kvStore ?? new MMKVStore();
95
774
  }, [props.kvStore]);
96
- return /* @__PURE__ */ jsx(JazzProviderCore, { ...props, storage: storage2, kvStore });
775
+ return /* @__PURE__ */ jsx4(JazzProviderCore2, { ...props, storage: storage2, kvStore });
97
776
  }
98
777
  export {
778
+ DemoAuthBasicUI,
779
+ Image,
780
+ JazzProviderCore,
99
781
  JazzReactNativeProvider,
782
+ KvStoreContext,
100
783
  MMKVStore,
101
- OPSQLiteAdapter
784
+ OPSQLiteAdapter,
785
+ ReactNativeContextManager,
786
+ SQLiteDatabaseDriverAsync,
787
+ clearUserCredentials,
788
+ createAccountSubscriptionContext,
789
+ createCoValueSubscriptionContext,
790
+ createInviteLink,
791
+ experimental_useInboxSender,
792
+ parseInviteLink2 as parseInviteLink,
793
+ setupKvStore,
794
+ useAcceptInviteNative,
795
+ useAccount,
796
+ useAccountSubscription,
797
+ useAgent,
798
+ useAuthSecretStorage,
799
+ useCoState,
800
+ useCoValueSubscription,
801
+ useDemoAuth,
802
+ useIsAuthenticated,
803
+ useJazzContext2 as useJazzContext,
804
+ useLogOut,
805
+ usePassphraseAuth,
806
+ useSubscriptionSelector,
807
+ useSuspenseAccount,
808
+ useSuspenseCoState,
809
+ useSyncConnectionStatus
102
810
  };
103
811
  //# sourceMappingURL=index.js.map