jazz-tools 0.19.8 → 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.
- package/.turbo/turbo-build.log +44 -42
- package/CHANGELOG.md +19 -3
- package/dist/{chunk-2S3Z2CN6.js → chunk-FFEEPZEG.js} +367 -102
- package/dist/chunk-FFEEPZEG.js.map +1 -0
- package/dist/index.js +1 -1
- package/dist/react/hooks.d.ts +1 -1
- package/dist/react/hooks.d.ts.map +1 -1
- package/dist/react/index.d.ts +1 -1
- package/dist/react/index.d.ts.map +1 -1
- package/dist/react/index.js +5 -1
- package/dist/react/index.js.map +1 -1
- package/dist/react-core/hooks.d.ts +59 -0
- package/dist/react-core/hooks.d.ts.map +1 -1
- package/dist/react-core/index.js +124 -36
- package/dist/react-core/index.js.map +1 -1
- package/dist/react-core/tests/testUtils.d.ts +1 -0
- package/dist/react-core/tests/testUtils.d.ts.map +1 -1
- package/dist/react-core/tests/useSuspenseAccount.test.d.ts +2 -0
- package/dist/react-core/tests/useSuspenseAccount.test.d.ts.map +1 -0
- package/dist/react-core/tests/useSuspenseCoState.test.d.ts +2 -0
- package/dist/react-core/tests/useSuspenseCoState.test.d.ts.map +1 -0
- package/dist/react-core/use.d.ts +3 -0
- package/dist/react-core/use.d.ts.map +1 -0
- package/dist/react-native/index.js +5 -1
- package/dist/react-native/index.js.map +1 -1
- package/dist/react-native-core/crypto/RNCrypto.d.ts +2 -0
- package/dist/react-native-core/crypto/RNCrypto.d.ts.map +1 -0
- package/dist/react-native-core/crypto/RNCrypto.js +3 -0
- package/dist/react-native-core/crypto/RNCrypto.js.map +1 -0
- package/dist/react-native-core/hooks.d.ts +1 -1
- package/dist/react-native-core/hooks.d.ts.map +1 -1
- package/dist/react-native-core/index.js +5 -1
- package/dist/react-native-core/index.js.map +1 -1
- package/dist/react-native-core/platform.d.ts +2 -1
- package/dist/react-native-core/platform.d.ts.map +1 -1
- package/dist/testing.js +1 -1
- package/dist/testing.js.map +1 -1
- package/dist/tools/coValues/interfaces.d.ts +1 -1
- package/dist/tools/coValues/interfaces.d.ts.map +1 -1
- package/dist/tools/implementation/ContextManager.d.ts +3 -0
- package/dist/tools/implementation/ContextManager.d.ts.map +1 -1
- package/dist/tools/subscribe/CoValueCoreSubscription.d.ts +8 -22
- package/dist/tools/subscribe/CoValueCoreSubscription.d.ts.map +1 -1
- package/dist/tools/subscribe/SubscriptionCache.d.ts +51 -0
- package/dist/tools/subscribe/SubscriptionCache.d.ts.map +1 -0
- package/dist/tools/subscribe/SubscriptionScope.d.ts +17 -1
- package/dist/tools/subscribe/SubscriptionScope.d.ts.map +1 -1
- package/dist/tools/subscribe/utils.d.ts +9 -1
- package/dist/tools/subscribe/utils.d.ts.map +1 -1
- package/dist/tools/testing.d.ts +2 -2
- package/dist/tools/testing.d.ts.map +1 -1
- package/dist/tools/tests/SubscriptionCache.test.d.ts +2 -0
- package/dist/tools/tests/SubscriptionCache.test.d.ts.map +1 -0
- package/package.json +13 -6
- package/src/react/hooks.tsx +2 -0
- package/src/react/index.ts +1 -14
- package/src/react-core/hooks.ts +167 -18
- package/src/react-core/tests/createCoValueSubscriptionContext.test.tsx +18 -8
- package/src/react-core/tests/testUtils.tsx +67 -5
- package/src/react-core/tests/useCoState.test.ts +3 -7
- package/src/react-core/tests/useSubscriptionSelector.test.ts +3 -7
- package/src/react-core/tests/useSuspenseAccount.test.tsx +343 -0
- package/src/react-core/tests/useSuspenseCoState.test.tsx +1182 -0
- package/src/react-core/use.ts +46 -0
- package/src/react-native-core/crypto/RNCrypto.ts +1 -0
- package/src/react-native-core/hooks.tsx +2 -0
- package/src/react-native-core/platform.ts +2 -1
- package/src/tools/coValues/interfaces.ts +2 -3
- package/src/tools/implementation/ContextManager.ts +13 -0
- package/src/tools/subscribe/CoValueCoreSubscription.ts +71 -100
- package/src/tools/subscribe/SubscriptionCache.ts +272 -0
- package/src/tools/subscribe/SubscriptionScope.ts +113 -7
- package/src/tools/subscribe/utils.ts +77 -0
- package/src/tools/testing.ts +0 -3
- package/src/tools/tests/CoValueCoreSubscription.test.ts +46 -12
- package/src/tools/tests/ContextManager.test.ts +85 -0
- package/src/tools/tests/SubscriptionCache.test.ts +237 -0
- package/src/tools/tests/coMap.test.ts +5 -7
- package/tsup.config.ts +1 -0
- package/dist/chunk-2S3Z2CN6.js.map +0 -1
package/src/react-core/hooks.ts
CHANGED
|
@@ -23,11 +23,11 @@ import {
|
|
|
23
23
|
Loaded,
|
|
24
24
|
MaybeLoaded,
|
|
25
25
|
NotLoaded,
|
|
26
|
+
RefsToResolve,
|
|
26
27
|
ResolveQuery,
|
|
27
28
|
ResolveQueryStrict,
|
|
28
29
|
SchemaResolveQuery,
|
|
29
30
|
SubscriptionScope,
|
|
30
|
-
coValueClassFromCoValueClassOrSchema,
|
|
31
31
|
importContentPieces,
|
|
32
32
|
captureStack,
|
|
33
33
|
getUnloadedCoValueWithoutId,
|
|
@@ -36,6 +36,7 @@ import {
|
|
|
36
36
|
import { JazzContext, JazzContextManagerContext } from "./provider.js";
|
|
37
37
|
import { getCurrentAccountFromContextManager } from "./utils.js";
|
|
38
38
|
import { CoValueSubscription } from "./types.js";
|
|
39
|
+
import { use } from "./use.js";
|
|
39
40
|
|
|
40
41
|
export function useJazzContext<Acc extends Account>() {
|
|
41
42
|
const value = useContext(JazzContext) as JazzContextType<Acc>;
|
|
@@ -134,20 +135,22 @@ export function useCoValueSubscription<
|
|
|
134
135
|
const resolve = getResolveQuery(Schema, options?.resolve);
|
|
135
136
|
|
|
136
137
|
const node = contextManager.getCurrentValue()!.node;
|
|
137
|
-
const
|
|
138
|
+
const cache = contextManager.getSubscriptionScopeCache();
|
|
139
|
+
const subscription = cache.getOrCreate(
|
|
138
140
|
node,
|
|
139
|
-
|
|
141
|
+
Schema,
|
|
140
142
|
id,
|
|
141
|
-
|
|
142
|
-
ref: coValueClassFromCoValueClassOrSchema(Schema),
|
|
143
|
-
optional: true,
|
|
144
|
-
},
|
|
143
|
+
resolve,
|
|
145
144
|
false,
|
|
146
145
|
false,
|
|
147
146
|
options?.unstable_branch,
|
|
148
|
-
callerStack.current,
|
|
149
147
|
);
|
|
150
148
|
|
|
149
|
+
// Set callerStack on returned subscription after retrieval
|
|
150
|
+
if (callerStack.current) {
|
|
151
|
+
subscription.callerStack = callerStack.current;
|
|
152
|
+
}
|
|
153
|
+
|
|
151
154
|
return {
|
|
152
155
|
value: subscription,
|
|
153
156
|
contextManager,
|
|
@@ -182,7 +185,6 @@ export function useCoValueSubscription<
|
|
|
182
185
|
subscription.branchOwnerId !== branchOwnerId ||
|
|
183
186
|
subscription.agent !== agent
|
|
184
187
|
) {
|
|
185
|
-
subscription.value?.destroy();
|
|
186
188
|
subscriptionRef.current = createSubscription();
|
|
187
189
|
subscription = subscriptionRef.current;
|
|
188
190
|
}
|
|
@@ -442,6 +444,79 @@ export function useCoState<
|
|
|
442
444
|
return value;
|
|
443
445
|
}
|
|
444
446
|
|
|
447
|
+
export function useSuspenseCoState<
|
|
448
|
+
S extends CoValueClassOrSchema,
|
|
449
|
+
// @ts-expect-error we can't statically enforce the schema's resolve query is a valid resolve query, but in practice it is
|
|
450
|
+
const R extends ResolveQuery<S> = SchemaResolveQuery<S>,
|
|
451
|
+
TSelectorReturn = Loaded<S, R>,
|
|
452
|
+
>(
|
|
453
|
+
/** The CoValue schema or class constructor */
|
|
454
|
+
Schema: S,
|
|
455
|
+
/** The ID of the CoValue to subscribe to */
|
|
456
|
+
id: string,
|
|
457
|
+
/** Optional configuration for the subscription */
|
|
458
|
+
options?: {
|
|
459
|
+
/** Resolve query to specify which nested CoValues to load */
|
|
460
|
+
resolve?: ResolveQueryStrict<S, R>;
|
|
461
|
+
/** Select which value to return */
|
|
462
|
+
select?: (value: Loaded<S, R>) => TSelectorReturn;
|
|
463
|
+
/** Equality function to determine if the selected value has changed, defaults to `Object.is` */
|
|
464
|
+
equalityFn?: (a: TSelectorReturn, b: TSelectorReturn) => boolean;
|
|
465
|
+
/**
|
|
466
|
+
* Create or load a branch for isolated editing.
|
|
467
|
+
*
|
|
468
|
+
* Branching lets you take a snapshot of the current state and start modifying it without affecting the canonical/shared version.
|
|
469
|
+
* It's a fork of your data graph: the same schema, but with diverging values.
|
|
470
|
+
*
|
|
471
|
+
* The checkout of the branch is applied on all the resolved values.
|
|
472
|
+
*
|
|
473
|
+
* @param name - A unique name for the branch. This identifies the branch
|
|
474
|
+
* and can be used to switch between different branches of the same CoValue.
|
|
475
|
+
* @param owner - The owner of the branch. Determines who can access and modify
|
|
476
|
+
* the branch. If not provided, the branch is owned by the current user.
|
|
477
|
+
*
|
|
478
|
+
* For more info see the [branching](https://jazz.tools/docs/react/using-covalues/version-control) documentation.
|
|
479
|
+
*/
|
|
480
|
+
unstable_branch?: BranchDefinition;
|
|
481
|
+
preloaded?: ExportedCoValue<Loaded<S, R>>;
|
|
482
|
+
},
|
|
483
|
+
): TSelectorReturn {
|
|
484
|
+
useImportCoValueContent(id, options?.preloaded);
|
|
485
|
+
|
|
486
|
+
const subscription = useCoValueSubscription(Schema, id, options);
|
|
487
|
+
|
|
488
|
+
if (!subscription) {
|
|
489
|
+
throw new Error("Subscription not found");
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
use(subscription.getPromise());
|
|
493
|
+
|
|
494
|
+
const getCurrentValue = () => {
|
|
495
|
+
const value = subscription.getCurrentValue();
|
|
496
|
+
|
|
497
|
+
if (!value.$isLoaded) {
|
|
498
|
+
throw new Error("CoValue must be loaded in a suspense context");
|
|
499
|
+
}
|
|
500
|
+
|
|
501
|
+
return value;
|
|
502
|
+
};
|
|
503
|
+
|
|
504
|
+
const value = useSyncExternalStoreWithSelector<Loaded<S, R>, TSelectorReturn>(
|
|
505
|
+
React.useCallback(
|
|
506
|
+
(callback) => {
|
|
507
|
+
return subscription.subscribe(callback);
|
|
508
|
+
},
|
|
509
|
+
[subscription],
|
|
510
|
+
),
|
|
511
|
+
getCurrentValue,
|
|
512
|
+
getCurrentValue,
|
|
513
|
+
options?.select ?? ((value) => value as TSelectorReturn),
|
|
514
|
+
options?.equalityFn ?? Object.is,
|
|
515
|
+
);
|
|
516
|
+
|
|
517
|
+
return value;
|
|
518
|
+
}
|
|
519
|
+
|
|
445
520
|
export function useSubscriptionSelector<
|
|
446
521
|
S extends CoValueClassOrSchema,
|
|
447
522
|
// @ts-expect-error we can't statically enforce the schema's resolve query is a valid resolve query, but in practice it is
|
|
@@ -510,20 +585,22 @@ export function useAccountSubscription<
|
|
|
510
585
|
const resolve = getResolveQuery(Schema, options?.resolve);
|
|
511
586
|
|
|
512
587
|
const node = contextManager.getCurrentValue()!.node;
|
|
513
|
-
const
|
|
588
|
+
const cache = contextManager.getSubscriptionScopeCache();
|
|
589
|
+
const subscription = cache.getOrCreate(
|
|
514
590
|
node,
|
|
515
|
-
|
|
591
|
+
Schema,
|
|
516
592
|
agent.$jazz.id,
|
|
517
|
-
|
|
518
|
-
ref: coValueClassFromCoValueClassOrSchema(Schema),
|
|
519
|
-
optional: true,
|
|
520
|
-
},
|
|
593
|
+
resolve,
|
|
521
594
|
false,
|
|
522
595
|
false,
|
|
523
596
|
options?.unstable_branch,
|
|
524
|
-
callerStack.current,
|
|
525
597
|
);
|
|
526
598
|
|
|
599
|
+
// Set callerStack on returned subscription after retrieval
|
|
600
|
+
if (callerStack.current) {
|
|
601
|
+
subscription.callerStack = callerStack.current;
|
|
602
|
+
}
|
|
603
|
+
|
|
527
604
|
return {
|
|
528
605
|
subscription,
|
|
529
606
|
contextManager,
|
|
@@ -545,12 +622,12 @@ export function useAccountSubscription<
|
|
|
545
622
|
subscription.branchName !== options?.unstable_branch?.name ||
|
|
546
623
|
subscription.branchOwnerId !== options?.unstable_branch?.owner?.$jazz.id
|
|
547
624
|
) {
|
|
548
|
-
|
|
625
|
+
// No need to manually destroy - cache handles cleanup via SubscriptionScope lifecycle
|
|
549
626
|
setSubscription(createSubscription());
|
|
550
627
|
}
|
|
551
628
|
|
|
552
629
|
return contextManager.subscribe(() => {
|
|
553
|
-
|
|
630
|
+
// No need to manually destroy - cache handles cleanup via SubscriptionScope lifecycle
|
|
554
631
|
setSubscription(createSubscription());
|
|
555
632
|
});
|
|
556
633
|
}, [Schema, contextManager, branchName, branchOwnerId]);
|
|
@@ -705,6 +782,78 @@ export function useAccount<
|
|
|
705
782
|
);
|
|
706
783
|
}
|
|
707
784
|
|
|
785
|
+
export function useSuspenseAccount<
|
|
786
|
+
A extends AccountClass<Account> | AnyAccountSchema,
|
|
787
|
+
// @ts-expect-error we can't statically enforce the schema's resolve query is a valid resolve query, but in practice it is
|
|
788
|
+
const R extends ResolveQuery<A> = SchemaResolveQuery<A>,
|
|
789
|
+
TSelectorReturn = Loaded<A, R>,
|
|
790
|
+
>(
|
|
791
|
+
/** The account schema to use. Defaults to the base Account schema */
|
|
792
|
+
AccountSchema: A = Account as unknown as A,
|
|
793
|
+
/** Optional configuration for the subscription */
|
|
794
|
+
options?: {
|
|
795
|
+
/** Resolve query to specify which nested CoValues to load from the account */
|
|
796
|
+
resolve?: ResolveQueryStrict<A, R>;
|
|
797
|
+
/** Select which value to return from the account data */
|
|
798
|
+
select?: (account: Loaded<A, R>) => TSelectorReturn;
|
|
799
|
+
/** Equality function to determine if the selected value has changed, defaults to `Object.is` */
|
|
800
|
+
equalityFn?: (a: TSelectorReturn, b: TSelectorReturn) => boolean;
|
|
801
|
+
/**
|
|
802
|
+
* Create or load a branch for isolated editing.
|
|
803
|
+
*
|
|
804
|
+
* Branching lets you take a snapshot of the current state and start modifying it without affecting the canonical/shared version.
|
|
805
|
+
* It's a fork of your data graph: the same schema, but with diverging values.
|
|
806
|
+
*
|
|
807
|
+
* The checkout of the branch is applied on all the resolved values.
|
|
808
|
+
*
|
|
809
|
+
* @param name - A unique name for the branch. This identifies the branch
|
|
810
|
+
* and can be used to switch between different branches of the same CoValue.
|
|
811
|
+
* @param owner - The owner of the branch. Determines who can access and modify
|
|
812
|
+
* the branch. If not provided, the branch is owned by the current user.
|
|
813
|
+
*
|
|
814
|
+
* For more info see the [branching](https://jazz.tools/docs/react/using-covalues/version-control) documentation.
|
|
815
|
+
*/
|
|
816
|
+
unstable_branch?: BranchDefinition;
|
|
817
|
+
},
|
|
818
|
+
): TSelectorReturn {
|
|
819
|
+
const subscription = useAccountSubscription(AccountSchema, options);
|
|
820
|
+
|
|
821
|
+
if (!subscription) {
|
|
822
|
+
throw new Error(
|
|
823
|
+
"Subscription not found, are you using useSuspenseAccount in guest mode?",
|
|
824
|
+
);
|
|
825
|
+
}
|
|
826
|
+
|
|
827
|
+
use(subscription.getPromise());
|
|
828
|
+
|
|
829
|
+
const getCurrentValue = () => {
|
|
830
|
+
const value = subscription.getCurrentValue();
|
|
831
|
+
|
|
832
|
+
if (!value.$isLoaded) {
|
|
833
|
+
throw new Error("Account must be loaded in a suspense context");
|
|
834
|
+
}
|
|
835
|
+
|
|
836
|
+
return value;
|
|
837
|
+
};
|
|
838
|
+
|
|
839
|
+
return useSyncExternalStoreWithSelector<Loaded<A, R>, TSelectorReturn>(
|
|
840
|
+
React.useCallback(
|
|
841
|
+
(callback) => {
|
|
842
|
+
if (!subscription) {
|
|
843
|
+
return () => {};
|
|
844
|
+
}
|
|
845
|
+
|
|
846
|
+
return subscription.subscribe(callback);
|
|
847
|
+
},
|
|
848
|
+
[subscription],
|
|
849
|
+
),
|
|
850
|
+
getCurrentValue,
|
|
851
|
+
getCurrentValue,
|
|
852
|
+
options?.select ?? ((value) => value as TSelectorReturn),
|
|
853
|
+
options?.equalityFn ?? Object.is,
|
|
854
|
+
);
|
|
855
|
+
}
|
|
856
|
+
|
|
708
857
|
/**
|
|
709
858
|
* Returns a function for logging out of the current account.
|
|
710
859
|
*/
|
|
@@ -164,15 +164,31 @@ describe("createCoValueSubscriptionContext", () => {
|
|
|
164
164
|
});
|
|
165
165
|
|
|
166
166
|
it("the provider shows a loading fallback when loading the coValue", async () => {
|
|
167
|
+
await setupJazzTestSync({
|
|
168
|
+
asyncPeers: true,
|
|
169
|
+
});
|
|
170
|
+
|
|
167
171
|
const TestMap = co.map({
|
|
168
172
|
value: z.string(),
|
|
169
173
|
});
|
|
170
174
|
|
|
175
|
+
const map = TestMap.create({
|
|
176
|
+
value: "123",
|
|
177
|
+
});
|
|
178
|
+
|
|
179
|
+
await createJazzTestAccount({
|
|
180
|
+
isCurrentActiveAccount: true,
|
|
181
|
+
});
|
|
182
|
+
|
|
171
183
|
const { Provider } = createCoValueSubscriptionContext(TestMap);
|
|
172
184
|
|
|
173
185
|
const { container } = render(
|
|
174
186
|
<JazzTestProvider>
|
|
175
|
-
<Provider
|
|
187
|
+
<Provider
|
|
188
|
+
id={map.$jazz.id}
|
|
189
|
+
loadingFallback={<div>Loading...</div>}
|
|
190
|
+
unavailableFallback={<div>Unavailable</div>}
|
|
191
|
+
>
|
|
176
192
|
<div>Children should not render</div>
|
|
177
193
|
</Provider>
|
|
178
194
|
</JazzTestProvider>,
|
|
@@ -203,13 +219,7 @@ describe("createCoValueSubscriptionContext", () => {
|
|
|
203
219
|
</JazzTestProvider>,
|
|
204
220
|
);
|
|
205
221
|
|
|
206
|
-
|
|
207
|
-
expect(container.textContent).toContain("Loading...");
|
|
208
|
-
|
|
209
|
-
// Should show unavailable fallback after CoValue load timeout
|
|
210
|
-
await waitFor(() => {
|
|
211
|
-
expect(container.textContent).toContain("Unavailable");
|
|
212
|
-
});
|
|
222
|
+
expect(container.textContent).toContain("Unavailable");
|
|
213
223
|
|
|
214
224
|
// Children should never be rendered
|
|
215
225
|
expect(container.textContent).not.toContain("Children should not render");
|
|
@@ -4,9 +4,12 @@ import {
|
|
|
4
4
|
render,
|
|
5
5
|
renderHook,
|
|
6
6
|
} from "@testing-library/react";
|
|
7
|
-
import { Account, AnonymousJazzAgent
|
|
7
|
+
import { Account, AnonymousJazzAgent } from "jazz-tools";
|
|
8
8
|
import React from "react";
|
|
9
9
|
import { JazzTestProvider } from "../testing.js";
|
|
10
|
+
import { getSqliteStorageAsync, SQLiteDatabaseDriverAsync } from "cojson";
|
|
11
|
+
import Database, { type Database as DatabaseT } from "libsql";
|
|
12
|
+
import { onTestFinished } from "vitest";
|
|
10
13
|
|
|
11
14
|
type JazzExtendedOptions = {
|
|
12
15
|
account?: Account | { guest: AnonymousJazzAgent };
|
|
@@ -23,12 +26,16 @@ const customRender = (
|
|
|
23
26
|
account={options.account}
|
|
24
27
|
isAuthenticated={options.isAuthenticated}
|
|
25
28
|
>
|
|
26
|
-
{
|
|
29
|
+
{options.wrapper ? (
|
|
30
|
+
<options.wrapper>{children}</options.wrapper>
|
|
31
|
+
) : (
|
|
32
|
+
children
|
|
33
|
+
)}
|
|
27
34
|
</JazzTestProvider>
|
|
28
35
|
);
|
|
29
36
|
};
|
|
30
37
|
|
|
31
|
-
return render(ui, { wrapper: AllTheProviders
|
|
38
|
+
return render(ui, { ...options, wrapper: AllTheProviders });
|
|
32
39
|
};
|
|
33
40
|
|
|
34
41
|
const customRenderHook = <TProps, TResult>(
|
|
@@ -41,12 +48,16 @@ const customRenderHook = <TProps, TResult>(
|
|
|
41
48
|
account={options.account}
|
|
42
49
|
isAuthenticated={options.isAuthenticated}
|
|
43
50
|
>
|
|
44
|
-
{
|
|
51
|
+
{options.wrapper ? (
|
|
52
|
+
<options.wrapper>{children}</options.wrapper>
|
|
53
|
+
) : (
|
|
54
|
+
children
|
|
55
|
+
)}
|
|
45
56
|
</JazzTestProvider>
|
|
46
57
|
);
|
|
47
58
|
};
|
|
48
59
|
|
|
49
|
-
return renderHook(callback, { wrapper: AllTheProviders
|
|
60
|
+
return renderHook(callback, { ...options, wrapper: AllTheProviders });
|
|
50
61
|
};
|
|
51
62
|
|
|
52
63
|
// re-export everything
|
|
@@ -55,3 +66,54 @@ export * from "@testing-library/react";
|
|
|
55
66
|
// override render method
|
|
56
67
|
export { customRender as render };
|
|
57
68
|
export { customRenderHook as renderHook };
|
|
69
|
+
|
|
70
|
+
class LibSQLSqliteAsyncDriver implements SQLiteDatabaseDriverAsync {
|
|
71
|
+
private readonly db: DatabaseT;
|
|
72
|
+
|
|
73
|
+
constructor(filename: string) {
|
|
74
|
+
this.db = new Database(filename, {});
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
async initialize() {
|
|
78
|
+
await this.db.pragma("journal_mode = WAL");
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
async run(sql: string, params: unknown[]) {
|
|
82
|
+
this.db.prepare(sql).run(params);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
async query<T>(sql: string, params: unknown[]): Promise<T[]> {
|
|
86
|
+
return this.db.prepare(sql).all(params) as T[];
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
async get<T>(sql: string, params: unknown[]): Promise<T | undefined> {
|
|
90
|
+
return this.db.prepare(sql).get(params) as T | undefined;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
async transaction(callback: () => unknown) {
|
|
94
|
+
await this.run("BEGIN TRANSACTION", []);
|
|
95
|
+
|
|
96
|
+
try {
|
|
97
|
+
await callback();
|
|
98
|
+
await this.run("COMMIT", []);
|
|
99
|
+
} catch (error) {
|
|
100
|
+
await this.run("ROLLBACK", []);
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
async closeDb() {
|
|
105
|
+
this.db.close();
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
export async function createAsyncStorage() {
|
|
110
|
+
const storage = await getSqliteStorageAsync(
|
|
111
|
+
new LibSQLSqliteAsyncDriver(":memory:"),
|
|
112
|
+
);
|
|
113
|
+
|
|
114
|
+
onTestFinished(() => {
|
|
115
|
+
storage.close();
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
return storage;
|
|
119
|
+
}
|
|
@@ -64,13 +64,9 @@ describe("useCoState", () => {
|
|
|
64
64
|
account,
|
|
65
65
|
});
|
|
66
66
|
|
|
67
|
-
expect(result.current.$jazz.loadingState).toBe(
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
expect(result.current.$jazz.loadingState).toBe(
|
|
71
|
-
CoValueLoadingState.UNAVAILABLE,
|
|
72
|
-
);
|
|
73
|
-
});
|
|
67
|
+
expect(result.current.$jazz.loadingState).toBe(
|
|
68
|
+
CoValueLoadingState.UNAVAILABLE,
|
|
69
|
+
);
|
|
74
70
|
});
|
|
75
71
|
|
|
76
72
|
it("should update the value when the coValue changes", async () => {
|
|
@@ -89,13 +89,9 @@ describe("useSubscriptionSelector", () => {
|
|
|
89
89
|
return useSubscriptionSelector(subscription);
|
|
90
90
|
});
|
|
91
91
|
|
|
92
|
-
expect(result.current.$jazz.loadingState).toBe(
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
expect(result.current.$jazz.loadingState).toBe(
|
|
96
|
-
CoValueLoadingState.UNAVAILABLE,
|
|
97
|
-
);
|
|
98
|
-
});
|
|
92
|
+
expect(result.current.$jazz.loadingState).toBe(
|
|
93
|
+
CoValueLoadingState.UNAVAILABLE,
|
|
94
|
+
);
|
|
99
95
|
});
|
|
100
96
|
|
|
101
97
|
it("should return coAccount", async () => {
|