@powersync/react 0.0.0-dev-20260202162549 → 0.0.0-dev-20260202163643
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/lib/QueryStore.d.ts +10 -0
- package/lib/QueryStore.js +63 -0
- package/lib/QueryStore.js.map +1 -0
- package/lib/hooks/PowerSyncContext.d.ts +21 -0
- package/lib/hooks/PowerSyncContext.js +21 -0
- package/lib/hooks/PowerSyncContext.js.map +1 -0
- package/lib/hooks/deprecated/usePowerSyncQuery.d.ts +7 -0
- package/lib/hooks/deprecated/usePowerSyncQuery.js +26 -0
- package/lib/hooks/deprecated/usePowerSyncQuery.js.map +1 -0
- package/lib/hooks/deprecated/usePowerSyncStatus.d.ts +15 -0
- package/lib/hooks/deprecated/usePowerSyncStatus.js +30 -0
- package/lib/hooks/deprecated/usePowerSyncStatus.js.map +1 -0
- package/lib/hooks/deprecated/usePowerSyncWatchedQuery.d.ts +17 -0
- package/lib/hooks/deprecated/usePowerSyncWatchedQuery.js +45 -0
- package/lib/hooks/deprecated/usePowerSyncWatchedQuery.js.map +1 -0
- package/lib/hooks/streams.d.ts +31 -0
- package/lib/hooks/streams.js +93 -0
- package/lib/hooks/streams.js.map +1 -0
- package/lib/hooks/suspense/SuspenseQueryResult.d.ts +3 -0
- package/lib/hooks/suspense/SuspenseQueryResult.js +2 -0
- package/lib/hooks/suspense/SuspenseQueryResult.js.map +1 -0
- package/lib/hooks/suspense/suspense-utils.d.ts +16 -0
- package/lib/hooks/suspense/suspense-utils.js +76 -0
- package/lib/hooks/suspense/suspense-utils.js.map +1 -0
- package/lib/hooks/suspense/useSingleSuspenseQuery.d.ts +10 -0
- package/lib/hooks/suspense/useSingleSuspenseQuery.js +73 -0
- package/lib/hooks/suspense/useSingleSuspenseQuery.js.map +1 -0
- package/lib/hooks/suspense/useSuspenseQuery.d.ts +53 -0
- package/lib/hooks/suspense/useSuspenseQuery.js +11 -0
- package/lib/hooks/suspense/useSuspenseQuery.js.map +1 -0
- package/lib/hooks/suspense/useWatchedQuerySuspenseSubscription.d.ts +23 -0
- package/lib/hooks/suspense/useWatchedQuerySuspenseSubscription.js +52 -0
- package/lib/hooks/suspense/useWatchedQuerySuspenseSubscription.js.map +1 -0
- package/lib/hooks/suspense/useWatchedSuspenseQuery.d.ts +6 -0
- package/lib/hooks/suspense/useWatchedSuspenseQuery.js +26 -0
- package/lib/hooks/suspense/useWatchedSuspenseQuery.js.map +1 -0
- package/lib/hooks/useStatus.d.ts +15 -0
- package/lib/hooks/useStatus.js +17 -0
- package/lib/hooks/useStatus.js.map +1 -0
- package/lib/hooks/watched/useQuery.d.ts +39 -0
- package/lib/hooks/watched/useQuery.js +43 -0
- package/lib/hooks/watched/useQuery.js.map +1 -0
- package/lib/hooks/watched/useSingleQuery.d.ts +6 -0
- package/lib/hooks/watched/useSingleQuery.js +59 -0
- package/lib/hooks/watched/useSingleQuery.js.map +1 -0
- package/lib/hooks/watched/useWatchedQuery.d.ts +13 -0
- package/lib/hooks/watched/useWatchedQuery.js +78 -0
- package/lib/hooks/watched/useWatchedQuery.js.map +1 -0
- package/lib/hooks/watched/useWatchedQuerySubscription.d.ts +20 -0
- package/lib/hooks/watched/useWatchedQuerySubscription.js +37 -0
- package/lib/hooks/watched/useWatchedQuerySubscription.js.map +1 -0
- package/lib/hooks/watched/watch-types.d.ts +87 -0
- package/lib/hooks/watched/watch-types.js +2 -0
- package/lib/hooks/watched/watch-types.js.map +1 -0
- package/lib/hooks/watched/watch-utils.d.ts +17 -0
- package/lib/hooks/watched/watch-utils.js +62 -0
- package/lib/hooks/watched/watch-utils.js.map +1 -0
- package/lib/index.d.ts +12 -0
- package/lib/index.js +11 -0
- package/lib/index.js.map +1 -0
- package/package.json +5 -5
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { AbstractPowerSyncDatabase, WatchCompatibleQuery, WatchedQuery } from '@powersync/common';
|
|
2
|
+
import { DifferentialHookOptions } from './hooks/watched/watch-types.js';
|
|
3
|
+
export declare function generateQueryKey(sqlStatement: string, parameters: ReadonlyArray<unknown>, options: DifferentialHookOptions<unknown>): string;
|
|
4
|
+
export declare class QueryStore {
|
|
5
|
+
private db;
|
|
6
|
+
cache: Map<string, WatchedQuery<unknown, import("@powersync/common").WatchedQueryOptions, import("@powersync/common").WatchedQueryListener<unknown>>>;
|
|
7
|
+
constructor(db: AbstractPowerSyncDatabase);
|
|
8
|
+
getQuery<RowType>(key: string, query: WatchCompatibleQuery<RowType[]>, options: DifferentialHookOptions<RowType>): import("@powersync/common").DifferentialWatchedQuery<RowType>;
|
|
9
|
+
}
|
|
10
|
+
export declare function getQueryStore(db: AbstractPowerSyncDatabase): QueryStore;
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { WatchedQueryListenerEvent } from '@powersync/common';
|
|
2
|
+
export function generateQueryKey(sqlStatement, parameters, options) {
|
|
3
|
+
return `${sqlStatement} -- ${JSON.stringify(parameters)} -- ${JSON.stringify(options)}`;
|
|
4
|
+
}
|
|
5
|
+
export class QueryStore {
|
|
6
|
+
db;
|
|
7
|
+
cache = new Map();
|
|
8
|
+
constructor(db) {
|
|
9
|
+
this.db = db;
|
|
10
|
+
}
|
|
11
|
+
getQuery(key, query, options) {
|
|
12
|
+
if (this.cache.has(key)) {
|
|
13
|
+
return this.cache.get(key);
|
|
14
|
+
}
|
|
15
|
+
const watch = options.rowComparator
|
|
16
|
+
? this.db.customQuery(query).differentialWatch({
|
|
17
|
+
rowComparator: options.rowComparator,
|
|
18
|
+
reportFetching: options.reportFetching,
|
|
19
|
+
throttleMs: options.throttleMs
|
|
20
|
+
})
|
|
21
|
+
: this.db.customQuery(query).watch({
|
|
22
|
+
reportFetching: options.reportFetching,
|
|
23
|
+
throttleMs: options.throttleMs
|
|
24
|
+
});
|
|
25
|
+
this.cache.set(key, watch);
|
|
26
|
+
const disposer = watch.registerListener({
|
|
27
|
+
closed: () => {
|
|
28
|
+
this.cache.delete(key);
|
|
29
|
+
disposer?.();
|
|
30
|
+
}
|
|
31
|
+
});
|
|
32
|
+
watch.listenerMeta.registerListener({
|
|
33
|
+
listenersChanged: (counts) => {
|
|
34
|
+
// Dispose this query if there are no subscribers present
|
|
35
|
+
// We don't use the total here since we don't want to consider `onclose` listeners
|
|
36
|
+
const relevantCounts = [
|
|
37
|
+
WatchedQueryListenerEvent.ON_DATA,
|
|
38
|
+
WatchedQueryListenerEvent.ON_STATE_CHANGE,
|
|
39
|
+
WatchedQueryListenerEvent.ON_ERROR
|
|
40
|
+
].reduce((sum, event) => {
|
|
41
|
+
return sum + (counts[event] || 0);
|
|
42
|
+
}, 0);
|
|
43
|
+
if (relevantCounts == 0) {
|
|
44
|
+
watch.close();
|
|
45
|
+
this.cache.delete(key);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
});
|
|
49
|
+
return watch;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
let queryStores = undefined;
|
|
53
|
+
export function getQueryStore(db) {
|
|
54
|
+
queryStores ||= new WeakMap();
|
|
55
|
+
const existing = queryStores.get(db);
|
|
56
|
+
if (existing) {
|
|
57
|
+
return existing;
|
|
58
|
+
}
|
|
59
|
+
const store = new QueryStore(db);
|
|
60
|
+
queryStores.set(db, store);
|
|
61
|
+
return store;
|
|
62
|
+
}
|
|
63
|
+
//# sourceMappingURL=QueryStore.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"QueryStore.js","sourceRoot":"","sources":["../src/QueryStore.ts"],"names":[],"mappings":"AAAA,OAAO,EAIL,yBAAyB,EAC1B,MAAM,mBAAmB,CAAC;AAG3B,MAAM,UAAU,gBAAgB,CAC9B,YAAoB,EACpB,UAAkC,EAClC,OAAyC;IAEzC,OAAO,GAAG,YAAY,OAAO,IAAI,CAAC,SAAS,CAAC,UAAU,CAAC,OAAO,IAAI,CAAC,SAAS,CAAC,OAAO,CAAC,EAAE,CAAC;AAC1F,CAAC;AAED,MAAM,OAAO,UAAU;IAGD;IAF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AAyB,CAAC,QAAQ;iBACnC,CAAC,MAAM,CAAC,CAAC,GAAG,EAAE,KAAK,EAAE,EAAE;oBACtB,OAAO,GAAG,GAAG,CAAC,MAAM,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,CAAC;gBACpC,CAAC,EAAE,CAAC,CAAC,CAAC;gBAEN,IAAI,cAAc,IAAI,CAAC,EAAE,CAAC;oBACxB,KAAK,CAAC,KAAK,EAAE,CAAC;oBACd,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;gBACzB,CAAC;YACH,CAAC;SACF,CAAC,CAAC;QAEH,OAAO,KAAK,CAAC;IACf,CAAC;CACF;AAED,IAAI,WAAW,GAA+D,SAAS,CAAC;AAExF,MAAM,UAAU,aAAa,CAAC,EAA6B;IACzD,WAAW,KAAK,IAAI,OAAO,EAAE,CAAC;IAC9B,MAAM,QAAQ,GAAG,WAAW,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC;IACrC,IAAI,QAAQ,EAAE,CAAC;QACb,OAAO,QAAQ,CAAC;IAClB,CAAC;IACD,MAAM,KAAK,GAAG,IAAI,UAAU,CAAC,EAAE,CAAC,CAAC;IACjC,WAAW,CAAC,GAAG,CAAC,EAAE,EAAE,KAAK,CAAC,CAAC;IAC3B,OAAO,KAAK,CAAC;AACf,CAAC"}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { AbstractPowerSyncDatabase } from '@powersync/common';
|
|
2
|
+
import React from 'react';
|
|
3
|
+
export declare const PowerSyncContext: React.Context<AbstractPowerSyncDatabase>;
|
|
4
|
+
/**
|
|
5
|
+
* Custom hook that provides access to the PowerSync context.
|
|
6
|
+
* @returns The PowerSync Database instance.
|
|
7
|
+
* @example
|
|
8
|
+
* const Component = () => {
|
|
9
|
+
* const db = usePowerSync();
|
|
10
|
+
* const [lists, setLists] = React.useState([]);
|
|
11
|
+
*
|
|
12
|
+
* React.useEffect(() => {
|
|
13
|
+
* powersync.getAll('SELECT * from lists').then(setLists)
|
|
14
|
+
* }, []);
|
|
15
|
+
*
|
|
16
|
+
* return <ul>
|
|
17
|
+
* {lists.map(list => <li key={list.id}>{list.name}</li>)}
|
|
18
|
+
* </ul>
|
|
19
|
+
* };
|
|
20
|
+
*/
|
|
21
|
+
export declare const usePowerSync: () => AbstractPowerSyncDatabase;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
export const PowerSyncContext = React.createContext(null);
|
|
3
|
+
/**
|
|
4
|
+
* Custom hook that provides access to the PowerSync context.
|
|
5
|
+
* @returns The PowerSync Database instance.
|
|
6
|
+
* @example
|
|
7
|
+
* const Component = () => {
|
|
8
|
+
* const db = usePowerSync();
|
|
9
|
+
* const [lists, setLists] = React.useState([]);
|
|
10
|
+
*
|
|
11
|
+
* React.useEffect(() => {
|
|
12
|
+
* powersync.getAll('SELECT * from lists').then(setLists)
|
|
13
|
+
* }, []);
|
|
14
|
+
*
|
|
15
|
+
* return <ul>
|
|
16
|
+
* {lists.map(list => <li key={list.id}>{list.name}</li>)}
|
|
17
|
+
* </ul>
|
|
18
|
+
* };
|
|
19
|
+
*/
|
|
20
|
+
export const usePowerSync = () => React.useContext(PowerSyncContext);
|
|
21
|
+
//# sourceMappingURL=PowerSyncContext.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"PowerSyncContext.js","sourceRoot":"","sources":["../../src/hooks/PowerSyncContext.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,MAAM,OAAO,CAAC;AAE1B,MAAM,CAAC,MAAM,gBAAgB,GAAG,KAAK,CAAC,aAAa,CAAmC,IAAI,CAAC,CAAC;AAC5F;;;;;;;;;;;;;;;;GAgBG;AACH,MAAM,CAAC,MAAM,YAAY,GAAG,GAAG,EAAE,CAAC,KAAK,CAAC,UAAU,CAAC,gBAAgB,CAAC,CAAC"}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @deprecated use {@link useQuery} instead.
|
|
3
|
+
*
|
|
4
|
+
* A hook to access a single static query.
|
|
5
|
+
* For an updated result, use {@link usePowerSyncWatchedQuery} instead.
|
|
6
|
+
*/
|
|
7
|
+
export declare const usePowerSyncQuery: <T = any>(sqlStatement: string, parameters?: any[]) => T[];
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { usePowerSync } from '../PowerSyncContext.js';
|
|
3
|
+
/**
|
|
4
|
+
* @deprecated use {@link useQuery} instead.
|
|
5
|
+
*
|
|
6
|
+
* A hook to access a single static query.
|
|
7
|
+
* For an updated result, use {@link usePowerSyncWatchedQuery} instead.
|
|
8
|
+
*/
|
|
9
|
+
export const usePowerSyncQuery = (sqlStatement, parameters = []) => {
|
|
10
|
+
const powerSync = usePowerSync();
|
|
11
|
+
if (!powerSync) {
|
|
12
|
+
return [];
|
|
13
|
+
}
|
|
14
|
+
// This will return the previous parameters until the contents of parameters array change,
|
|
15
|
+
// thereby providing a stable array reference and preventing excessive useEffect runs
|
|
16
|
+
const memoizedParams = React.useMemo(() => parameters, [...parameters]);
|
|
17
|
+
const [data, setData] = React.useState([]);
|
|
18
|
+
React.useEffect(() => {
|
|
19
|
+
powerSync.readLock(async (tx) => {
|
|
20
|
+
const result = await tx.getAll(sqlStatement, parameters);
|
|
21
|
+
setData(result);
|
|
22
|
+
});
|
|
23
|
+
}, [powerSync, sqlStatement, memoizedParams]);
|
|
24
|
+
return data;
|
|
25
|
+
};
|
|
26
|
+
//# sourceMappingURL=usePowerSyncQuery.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"usePowerSyncQuery.js","sourceRoot":"","sources":["../../../src/hooks/deprecated/usePowerSyncQuery.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,OAAO,CAAC;AAC1B,OAAO,EAAE,YAAY,EAAE,MAAM,wBAAwB,CAAC;AAEtD;;;;;GAKG;AACH,MAAM,CAAC,MAAM,iBAAiB,GAAG,CAAU,YAAoB,EAAE,aAAoB,EAAE,EAAO,EAAE;IAC9F,MAAM,SAAS,GAAG,YAAY,EAAE,CAAC;IACjC,IAAI,CAAC,SAAS,EAAE,CAAC;QACf,OAAO,EAAE,CAAC;IACZ,CAAC;IACD,0FAA0F;IAC1F,qFAAqF;IACrF,MAAM,cAAc,GAAG,KAAK,CAAC,OAAO,CAAC,GAAG,EAAE,CAAC,UAAU,EAAE,CAAC,GAAG,UAAU,CAAC,CAAC,CAAC;IACxE,MAAM,CAAC,IAAI,EAAE,OAAO,CAAC,GAAG,KAAK,CAAC,QAAQ,CAAM,EAAE,CAAC,CAAC;IAEhD,KAAK,CAAC,SAAS,CAAC,GAAG,EAAE;QACnB,SAAS,CAAC,QAAQ,CAAC,KAAK,EAAE,EAAE,EAAE,EAAE;YAC9B,MAAM,MAAM,GAAG,MAAM,EAAE,CAAC,MAAM,CAAI,YAAY,EAAE,UAAU,CAAC,CAAC;YAC5D,OAAO,CAAC,MAAM,CAAC,CAAC;QAClB,CAAC,CAAC,CAAC;IACL,CAAC,EAAE,CAAC,SAAS,EAAE,YAAY,EAAE,cAAc,CAAC,CAAC,CAAC;IAE9C,OAAO,IAAI,CAAC;AACd,CAAC,CAAC"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @deprecated Use {@link useStatus} instead.
|
|
3
|
+
*
|
|
4
|
+
* Custom hook that provides access to the current status of PowerSync.
|
|
5
|
+
* @returns The PowerSync Database status.
|
|
6
|
+
* @example
|
|
7
|
+
* const Component = () => {
|
|
8
|
+
* const status = usePowerSyncStatus();
|
|
9
|
+
*
|
|
10
|
+
* return <div>
|
|
11
|
+
* status.connected ? 'wifi' : 'wifi-off'
|
|
12
|
+
* </div>
|
|
13
|
+
* };
|
|
14
|
+
*/
|
|
15
|
+
export declare const usePowerSyncStatus: () => import("@powersync/common").SyncStatus;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { useContext, useEffect, useState } from 'react';
|
|
2
|
+
import { PowerSyncContext } from '../PowerSyncContext.js';
|
|
3
|
+
/**
|
|
4
|
+
* @deprecated Use {@link useStatus} instead.
|
|
5
|
+
*
|
|
6
|
+
* Custom hook that provides access to the current status of PowerSync.
|
|
7
|
+
* @returns The PowerSync Database status.
|
|
8
|
+
* @example
|
|
9
|
+
* const Component = () => {
|
|
10
|
+
* const status = usePowerSyncStatus();
|
|
11
|
+
*
|
|
12
|
+
* return <div>
|
|
13
|
+
* status.connected ? 'wifi' : 'wifi-off'
|
|
14
|
+
* </div>
|
|
15
|
+
* };
|
|
16
|
+
*/
|
|
17
|
+
export const usePowerSyncStatus = () => {
|
|
18
|
+
const powerSync = useContext(PowerSyncContext);
|
|
19
|
+
const [syncStatus, setSyncStatus] = useState(powerSync.currentStatus);
|
|
20
|
+
useEffect(() => {
|
|
21
|
+
const listener = powerSync.registerListener({
|
|
22
|
+
statusChanged: (status) => {
|
|
23
|
+
setSyncStatus(status);
|
|
24
|
+
}
|
|
25
|
+
});
|
|
26
|
+
return () => listener?.();
|
|
27
|
+
}, [powerSync]);
|
|
28
|
+
return syncStatus;
|
|
29
|
+
};
|
|
30
|
+
//# sourceMappingURL=usePowerSyncStatus.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"usePowerSyncStatus.js","sourceRoot":"","sources":["../../../src/hooks/deprecated/usePowerSyncStatus.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,SAAS,EAAE,QAAQ,EAAE,MAAM,OAAO,CAAC;AACxD,OAAO,EAAE,gBAAgB,EAAE,MAAM,wBAAwB,CAAC;AAE1D;;;;;;;;;;;;;GAaG;AACH,MAAM,CAAC,MAAM,kBAAkB,GAAG,GAAG,EAAE;IACrC,MAAM,SAAS,GAAG,UAAU,CAAC,gBAAgB,CAAC,CAAC;IAC/C,MAAM,CAAC,UAAU,EAAE,aAAa,CAAC,GAAG,QAAQ,CAAC,SAAS,CAAC,aAAa,CAAC,CAAC;IAEtE,SAAS,CAAC,GAAG,EAAE;QACb,MAAM,QAAQ,GAAG,SAAS,CAAC,gBAAgB,CAAC;YAC1C,aAAa,EAAE,CAAC,MAAM,EAAE,EAAE;gBACxB,aAAa,CAAC,MAAM,CAAC,CAAC;YACxB,CAAC;SACF,CAAC,CAAC;QAEH,OAAO,GAAG,EAAE,CAAC,QAAQ,EAAE,EAAE,CAAC;IAC5B,CAAC,EAAE,CAAC,SAAS,CAAC,CAAC,CAAC;IAEhB,OAAO,UAAU,CAAC;AACpB,CAAC,CAAC"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { SQLWatchOptions } from '@powersync/common';
|
|
2
|
+
/**
|
|
3
|
+
* @deprecated use {@link useQuery} instead.
|
|
4
|
+
*
|
|
5
|
+
* A hook to access the results of a watched query.
|
|
6
|
+
* @example
|
|
7
|
+
* export const Component = () => {
|
|
8
|
+
* const lists = usePowerSyncWatchedQuery('SELECT * from lists');
|
|
9
|
+
*
|
|
10
|
+
* return <View>
|
|
11
|
+
* {lists.map((l) => (
|
|
12
|
+
* <Text key={l.id}>{JSON.stringify(l)}</Text>
|
|
13
|
+
* ))}
|
|
14
|
+
* </View>
|
|
15
|
+
* }
|
|
16
|
+
*/
|
|
17
|
+
export declare const usePowerSyncWatchedQuery: <T = any>(sqlStatement: string, parameters?: any[], options?: Omit<SQLWatchOptions, "signal">) => T[];
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { usePowerSync } from '../PowerSyncContext.js';
|
|
3
|
+
/**
|
|
4
|
+
* @deprecated use {@link useQuery} instead.
|
|
5
|
+
*
|
|
6
|
+
* A hook to access the results of a watched query.
|
|
7
|
+
* @example
|
|
8
|
+
* export const Component = () => {
|
|
9
|
+
* const lists = usePowerSyncWatchedQuery('SELECT * from lists');
|
|
10
|
+
*
|
|
11
|
+
* return <View>
|
|
12
|
+
* {lists.map((l) => (
|
|
13
|
+
* <Text key={l.id}>{JSON.stringify(l)}</Text>
|
|
14
|
+
* ))}
|
|
15
|
+
* </View>
|
|
16
|
+
* }
|
|
17
|
+
*/
|
|
18
|
+
export const usePowerSyncWatchedQuery = (sqlStatement, parameters = [], options = {}) => {
|
|
19
|
+
const powerSync = usePowerSync();
|
|
20
|
+
if (!powerSync) {
|
|
21
|
+
return [];
|
|
22
|
+
}
|
|
23
|
+
const memoizedParams = React.useMemo(() => parameters, [...parameters]);
|
|
24
|
+
const memoizedOptions = React.useMemo(() => options, [JSON.stringify(options)]);
|
|
25
|
+
const [data, setData] = React.useState([]);
|
|
26
|
+
const abortController = React.useRef(new AbortController());
|
|
27
|
+
React.useEffect(() => {
|
|
28
|
+
// Abort any previous watches
|
|
29
|
+
abortController.current?.abort();
|
|
30
|
+
abortController.current = new AbortController();
|
|
31
|
+
powerSync.watch(sqlStatement, parameters, {
|
|
32
|
+
onResult(results) {
|
|
33
|
+
setData(results.rows?._array ?? []);
|
|
34
|
+
}
|
|
35
|
+
}, {
|
|
36
|
+
...options,
|
|
37
|
+
signal: abortController.current.signal
|
|
38
|
+
});
|
|
39
|
+
return () => {
|
|
40
|
+
abortController.current?.abort();
|
|
41
|
+
};
|
|
42
|
+
}, [powerSync, sqlStatement, memoizedParams, memoizedOptions]);
|
|
43
|
+
return data;
|
|
44
|
+
};
|
|
45
|
+
//# sourceMappingURL=usePowerSyncWatchedQuery.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"usePowerSyncWatchedQuery.js","sourceRoot":"","sources":["../../../src/hooks/deprecated/usePowerSyncWatchedQuery.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,MAAM,OAAO,CAAC;AAC1B,OAAO,EAAE,YAAY,EAAE,MAAM,wBAAwB,CAAC;AAEtD;;;;;;;;;;;;;;GAcG;AACH,MAAM,CAAC,MAAM,wBAAwB,GAAG,CACtC,YAAoB,EACpB,aAAoB,EAAE,EACtB,UAA2C,EAAE,EACxC,EAAE;IACP,MAAM,SAAS,GAAG,YAAY,EA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
|
|
2
|
+
import { QuerySyncStreamOptions } from './watched/watch-types.js';
|
|
3
|
+
/**
|
|
4
|
+
* A sync stream to subscribe to in {@link useSyncStream}.
|
|
5
|
+
*
|
|
6
|
+
* For more details on sync streams, see the [documentation](https://docs.powersync.com/usage/sync-streams).
|
|
7
|
+
*/
|
|
8
|
+
export interface UseSyncStreamOptions extends SyncStreamSubscribeOptions {
|
|
9
|
+
/**
|
|
10
|
+
* The name of the stream to subscribe to.
|
|
11
|
+
*/
|
|
12
|
+
name: string;
|
|
13
|
+
/**
|
|
14
|
+
* Parameters for the stream subscription. A single stream can have multiple subscriptions with different parameter
|
|
15
|
+
* sets.
|
|
16
|
+
*/
|
|
17
|
+
parameters?: Record<string, any> | null;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Creates a PowerSync stream subscription. The subscription is kept alive as long as the React component calling this
|
|
21
|
+
* function. When it unmounts, {@link SyncStreamSubscription.unsubscribe} is called
|
|
22
|
+
*
|
|
23
|
+
* For more details on sync streams, see the [documentation](https://docs.powersync.com/usage/sync-streams).
|
|
24
|
+
*
|
|
25
|
+
* @returns The status for that stream, or `null` if the stream is currently being resolved.
|
|
26
|
+
*/
|
|
27
|
+
export declare function useSyncStream(options: UseSyncStreamOptions): SyncStreamStatus | null;
|
|
28
|
+
/**
|
|
29
|
+
* @internal
|
|
30
|
+
*/
|
|
31
|
+
export declare function useAllSyncStreamsHaveSynced(db: AbstractPowerSyncDatabase, streams: QuerySyncStreamOptions[] | undefined): boolean;
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import { useEffect, useMemo, useState } from 'react';
|
|
2
|
+
import { usePowerSync } from './PowerSyncContext.js';
|
|
3
|
+
import { useStatus } from './useStatus.js';
|
|
4
|
+
/**
|
|
5
|
+
* Creates a PowerSync stream subscription. The subscription is kept alive as long as the React component calling this
|
|
6
|
+
* function. When it unmounts, {@link SyncStreamSubscription.unsubscribe} is called
|
|
7
|
+
*
|
|
8
|
+
* For more details on sync streams, see the [documentation](https://docs.powersync.com/usage/sync-streams).
|
|
9
|
+
*
|
|
10
|
+
* @returns The status for that stream, or `null` if the stream is currently being resolved.
|
|
11
|
+
*/
|
|
12
|
+
export function useSyncStream(options) {
|
|
13
|
+
const { name, parameters } = options;
|
|
14
|
+
const db = usePowerSync();
|
|
15
|
+
const status = useStatus();
|
|
16
|
+
const [subscription, setSubscription] = useState(null);
|
|
17
|
+
useEffect(() => {
|
|
18
|
+
let active = true;
|
|
19
|
+
let subscription = null;
|
|
20
|
+
db.syncStream(name, parameters)
|
|
21
|
+
.subscribe(options)
|
|
22
|
+
.then((sub) => {
|
|
23
|
+
if (active) {
|
|
24
|
+
subscription = sub;
|
|
25
|
+
setSubscription(sub);
|
|
26
|
+
}
|
|
27
|
+
else {
|
|
28
|
+
// The cleanup function already ran, unsubscribe immediately.
|
|
29
|
+
sub.unsubscribe();
|
|
30
|
+
}
|
|
31
|
+
});
|
|
32
|
+
return () => {
|
|
33
|
+
active = false;
|
|
34
|
+
// If we don't have a subscription yet, it'll still get cleaned up once the promise resolves because we've set
|
|
35
|
+
// active to false.
|
|
36
|
+
subscription?.unsubscribe();
|
|
37
|
+
};
|
|
38
|
+
}, [name, parameters]);
|
|
39
|
+
return subscription && status.forStream(subscription);
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* @internal
|
|
43
|
+
*/
|
|
44
|
+
export function useAllSyncStreamsHaveSynced(db, streams) {
|
|
45
|
+
// Since streams are a user-supplied array, they will likely be different each time this function is called. We don't
|
|
46
|
+
// want to update underlying subscriptions each time, though.
|
|
47
|
+
const hash = useMemo(() => streams && JSON.stringify(streams), [streams]);
|
|
48
|
+
const [synced, setHasSynced] = useState(streams == null || streams.every((e) => e.waitForStream != true));
|
|
49
|
+
useEffect(() => {
|
|
50
|
+
if (streams) {
|
|
51
|
+
setHasSynced(false);
|
|
52
|
+
const promises = [];
|
|
53
|
+
const abort = new AbortController();
|
|
54
|
+
for (const stream of streams) {
|
|
55
|
+
promises.push(db.syncStream(stream.name, stream.parameters).subscribe(stream));
|
|
56
|
+
}
|
|
57
|
+
// First, wait for all subscribe() calls to make all subscriptions active.
|
|
58
|
+
Promise.all(promises).then(async (resolvedStreams) => {
|
|
59
|
+
function allHaveSynced(status) {
|
|
60
|
+
return resolvedStreams.every((s, i) => {
|
|
61
|
+
const request = streams[i];
|
|
62
|
+
return !request.waitForStream || status.forStream(s)?.subscription?.hasSynced;
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
// Wait for the effect to be cancelled or all streams having synced.
|
|
66
|
+
await db.waitForStatus(allHaveSynced, abort.signal);
|
|
67
|
+
if (abort.signal.aborted) {
|
|
68
|
+
// Was cancelled
|
|
69
|
+
}
|
|
70
|
+
else {
|
|
71
|
+
// Has synced, update public state.
|
|
72
|
+
setHasSynced(true);
|
|
73
|
+
// Wait for cancellation before clearing subscriptions.
|
|
74
|
+
await new Promise((resolve) => {
|
|
75
|
+
abort.signal.addEventListener('abort', () => resolve());
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
// Effect was definitely cancelled at this point, so drop the subscriptions.
|
|
79
|
+
for (const stream of resolvedStreams) {
|
|
80
|
+
stream.unsubscribe();
|
|
81
|
+
}
|
|
82
|
+
});
|
|
83
|
+
return () => abort.abort();
|
|
84
|
+
}
|
|
85
|
+
else {
|
|
86
|
+
// There are no streams, so all of them have synced.
|
|
87
|
+
setHasSynced(true);
|
|
88
|
+
return undefined;
|
|
89
|
+
}
|
|
90
|
+
}, [hash]);
|
|
91
|
+
return synced;
|
|
92
|
+
}
|
|
93
|
+
//# sourceMappingURL=streams.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"streams.js","sourceRoot":"","sources":["../../src/hooks/streams.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,OAAO,EAAE,QAAQ,EAAE,MAAM,OAAO,CAAC;AACrD,OAAO,EAAE,YAAY,EAAE,MAAM,uBAAuB,CAAC;A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qBAAM,CAAC;oBACN,mCAAmC;oBACnC,YAAY,CAAC,IAAI,CAAC,CAAC;oBAEnB,uDAAuD;oBACvD,MAAM,IAAI,OAAO,CAAO,CAAC,OAAO,EAAE,EAAE;wBAClC,KAAK,CAAC,MAAM,CAAC,gBAAgB,CAAC,OAAO,EAAE,GAAG,EAAE,CAAC,OAAO,EAAE,CAAC,CAAC;oBAC1D,CAAC,CAAC,CAAC;gBACL,CAAC;gBAED,4EAA4E;gBAC5E,KAAK,MAAM,MAAM,IAAI,eAAe,EAAE,CAAC;oBACrC,MAAM,CAAC,WAAW,EAAE,CAAC;gBACvB,CAAC;YACH,CAAC,CAAC,CAAC;YAEH,OAAO,GAAG,EAAE,CAAC,KAAK,CAAC,KAAK,EAAE,CAAC;QAC7B,CAAC;aAAM,CAAC;YACN,oDAAoD;YACpD,YAAY,CAAC,IAAI,CAAC,CAAC;YACnB,OAAO,SAAS,CAAC;QACnB,CAAC;IACH,CAAC,EAAE,CAAC,IAAI,CAAC,CAAC,CAAC;IAEX,OAAO,MAAM,CAAC;AAChB,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"SuspenseQueryResult.js","sourceRoot":"","sources":["../../../src/hooks/suspense/SuspenseQueryResult.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { WatchedQuery } from '@powersync/common';
|
|
2
|
+
/**
|
|
3
|
+
* The store will dispose this query if it has no subscribers attached to it.
|
|
4
|
+
* The suspense promise adds a subscriber to the query, but the promise could resolve
|
|
5
|
+
* before this component is committed. The promise will release it's listener once the query is no longer loading.
|
|
6
|
+
* This temporary hold is used to ensure that the query is not disposed in the interim.
|
|
7
|
+
* Creates a subscription for state change which creates a temporary hold on the query
|
|
8
|
+
*/
|
|
9
|
+
export declare const useTemporaryHold: (watchedQuery?: WatchedQuery<unknown>) => void;
|
|
10
|
+
/**
|
|
11
|
+
* React suspense relies on a promise that resolves once the initial data has loaded.
|
|
12
|
+
* This creates a promise which registers a listener on the watched query.
|
|
13
|
+
* Registering a listener on the watched query will ensure that the query is not disposed
|
|
14
|
+
* while the component is suspended.
|
|
15
|
+
*/
|
|
16
|
+
export declare const createSuspendingPromise: (query: WatchedQuery<unknown>) => Promise<void>;
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
/**
|
|
3
|
+
* The store will dispose this query if it has no subscribers attached to it.
|
|
4
|
+
* The suspense promise adds a subscriber to the query, but the promise could resolve
|
|
5
|
+
* before this component is committed. The promise will release it's listener once the query is no longer loading.
|
|
6
|
+
* This temporary hold is used to ensure that the query is not disposed in the interim.
|
|
7
|
+
* Creates a subscription for state change which creates a temporary hold on the query
|
|
8
|
+
*/
|
|
9
|
+
export const useTemporaryHold = (watchedQuery) => {
|
|
10
|
+
const releaseTemporaryHold = React.useRef(undefined);
|
|
11
|
+
const addedHoldTo = React.useRef(undefined);
|
|
12
|
+
if (addedHoldTo.current !== watchedQuery) {
|
|
13
|
+
// The query changed, we no longer need the previous hold if present
|
|
14
|
+
releaseTemporaryHold.current?.();
|
|
15
|
+
releaseTemporaryHold.current = undefined;
|
|
16
|
+
addedHoldTo.current = watchedQuery;
|
|
17
|
+
if (!watchedQuery || !watchedQuery.state.isLoading) {
|
|
18
|
+
// No query to hold or no reason to hold, return
|
|
19
|
+
return;
|
|
20
|
+
}
|
|
21
|
+
// Create a hold by subscribing
|
|
22
|
+
const disposeSubscription = watchedQuery.registerListener({
|
|
23
|
+
onStateChange: (state) => { }
|
|
24
|
+
});
|
|
25
|
+
let timeout;
|
|
26
|
+
const disposeClosedListener = watchedQuery.registerListener({
|
|
27
|
+
closed: () => {
|
|
28
|
+
if (timeout) {
|
|
29
|
+
clearTimeout(timeout);
|
|
30
|
+
}
|
|
31
|
+
disposeClosedListener();
|
|
32
|
+
}
|
|
33
|
+
});
|
|
34
|
+
const releaseHold = () => {
|
|
35
|
+
disposeSubscription();
|
|
36
|
+
disposeClosedListener();
|
|
37
|
+
};
|
|
38
|
+
releaseTemporaryHold.current = releaseHold;
|
|
39
|
+
const timeoutPollMs = 5_000;
|
|
40
|
+
const checkHold = () => {
|
|
41
|
+
if (watchedQuery.closed || !watchedQuery.state.isLoading || watchedQuery.state.error) {
|
|
42
|
+
// No need to keep a temporary hold on this query
|
|
43
|
+
releaseHold();
|
|
44
|
+
}
|
|
45
|
+
else {
|
|
46
|
+
// Need to keep the hold, check again after timeout
|
|
47
|
+
setTimeout(checkHold, timeoutPollMs);
|
|
48
|
+
}
|
|
49
|
+
};
|
|
50
|
+
// Set a timeout to conditionally remove the temporary hold
|
|
51
|
+
setTimeout(checkHold, timeoutPollMs);
|
|
52
|
+
}
|
|
53
|
+
};
|
|
54
|
+
/**
|
|
55
|
+
* React suspense relies on a promise that resolves once the initial data has loaded.
|
|
56
|
+
* This creates a promise which registers a listener on the watched query.
|
|
57
|
+
* Registering a listener on the watched query will ensure that the query is not disposed
|
|
58
|
+
* while the component is suspended.
|
|
59
|
+
*/
|
|
60
|
+
export const createSuspendingPromise = (query) => {
|
|
61
|
+
return new Promise((resolve) => {
|
|
62
|
+
// The listener here will dispose itself once the loading is done
|
|
63
|
+
// This decreases the number of listeners on the query
|
|
64
|
+
// even if the component is unmounted
|
|
65
|
+
const dispose = query.registerListener({
|
|
66
|
+
onStateChange: (state) => {
|
|
67
|
+
// Returns to the hook if loading is completed or if loading resulted in an error
|
|
68
|
+
if (!state.isLoading || state.error) {
|
|
69
|
+
resolve();
|
|
70
|
+
dispose();
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
});
|
|
74
|
+
});
|
|
75
|
+
};
|
|
76
|
+
//# sourceMappingURL=suspense-utils.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"suspense-utils.js","sourceRoot":"","sources":["../../../src/hooks/suspense/suspense-utils.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,MAAM,OAAO,CAAC;AAE1B;;;;;;GAMG;AACH,MAAM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EAAE,CAAC;gBACrF,iDAAiD;gBACjD,WAAW,EAAE,CAAC;YAChB,CAAC;iBAAM,CAAC;gBACN,mDAAmD;gBACnD,UAAU,CAAC,SAAS,EAAE,aAAa,CAAC,CAAC;YACvC,CAAC;QACH,CAAC,CAAC;QAEF,2DAA2D;QAC3D,UAAU,CAAC,SAAS,EAAE,aAAa,CAAC,CAAC;IACvC,CAAC;AACH,CAAC,CAAC;AAEF;;;;;GAKG;AACH,MAAM,CAAC,MAAM,uBAAuB,GAAG,CAAC,KAA4B,EAAE,EAAE;IACtE,OAAO,IAAI,OAAO,CAAO,CAAC,OAAO,EAAE,EAAE;QACnC,iEAAiE;QACjE,sDAAsD;QACtD,qCAAqC;QACrC,MAAM,OAAO,GAAG,KAAK,CAAC,gBAAgB,CAAC;YACrC,aAAa,EAAE,CAAC,KAAK,EAAE,EAAE;gBACvB,iFAAiF;gBACjF,IAAI,CAAC,KAAK,CAAC,SAAS,IAAI,KAAK,CAAC,KAAK,EAAE,CAAC;oBACpC,OAAO,EAAE,CAAC;oBACV,OAAO,EAAE,CAAC;gBACZ,CAAC;YACH,CAAC;SACF,CAAC,CAAC;IACL,CAAC,CAAC,CAAC;AACL,CAAC,CAAC"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { CompilableQuery } from '@powersync/common';
|
|
2
|
+
import { AdditionalOptions } from '../watched/watch-types.js';
|
|
3
|
+
import { SuspenseQueryResult } from './SuspenseQueryResult.js';
|
|
4
|
+
/**
|
|
5
|
+
* Use a query which is not watched, but suspends until the initial result has loaded.
|
|
6
|
+
* Internally this uses a WatchedQuery during suspense for state management. The watched
|
|
7
|
+
* query is potentially disposed, if there are no subscribers attached to it, after the initial load.
|
|
8
|
+
* The query can be refreshed by calling the `refresh` function after initial load.
|
|
9
|
+
*/
|
|
10
|
+
export declare const useSingleSuspenseQuery: <T = any>(query: string | CompilableQuery<T>, parameters?: any[], options?: AdditionalOptions) => SuspenseQueryResult<T>;
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { generateQueryKey, getQueryStore } from '../../QueryStore.js';
|
|
3
|
+
import { usePowerSync } from '../PowerSyncContext.js';
|
|
4
|
+
import { constructCompatibleQuery } from '../watched/watch-utils.js';
|
|
5
|
+
import { createSuspendingPromise, useTemporaryHold } from './suspense-utils.js';
|
|
6
|
+
/**
|
|
7
|
+
* Use a query which is not watched, but suspends until the initial result has loaded.
|
|
8
|
+
* Internally this uses a WatchedQuery during suspense for state management. The watched
|
|
9
|
+
* query is potentially disposed, if there are no subscribers attached to it, after the initial load.
|
|
10
|
+
* The query can be refreshed by calling the `refresh` function after initial load.
|
|
11
|
+
*/
|
|
12
|
+
export const useSingleSuspenseQuery = (query, parameters = [], options = {}) => {
|
|
13
|
+
const powerSync = usePowerSync();
|
|
14
|
+
if (!powerSync) {
|
|
15
|
+
throw new Error('PowerSync not configured.');
|
|
16
|
+
}
|
|
17
|
+
// Manually track data for single queries
|
|
18
|
+
const [data, setData] = React.useState(null);
|
|
19
|
+
const [error, setError] = React.useState(null);
|
|
20
|
+
// Note, we don't need to check if the query changed since we fetch the WatchedQuery
|
|
21
|
+
// from the store given these query params
|
|
22
|
+
const { parsedQuery } = constructCompatibleQuery(query, parameters, options);
|
|
23
|
+
const { sql: parsedSql, parameters: parsedParameters } = parsedQuery.compile();
|
|
24
|
+
const key = generateQueryKey(parsedSql, parsedParameters, options);
|
|
25
|
+
const store = getQueryStore(powerSync);
|
|
26
|
+
// Only use a temporary watched query if we don't have data yet.
|
|
27
|
+
const watchedQuery = data ? null : store.getQuery(key, parsedQuery, options);
|
|
28
|
+
useTemporaryHold(watchedQuery);
|
|
29
|
+
React.useEffect(() => {
|
|
30
|
+
// Set the initial yielded data
|
|
31
|
+
// it should be available once we commit the component
|
|
32
|
+
if (watchedQuery?.state.error) {
|
|
33
|
+
setError(watchedQuery.state.error);
|
|
34
|
+
}
|
|
35
|
+
else if (watchedQuery?.state.isLoading === false) {
|
|
36
|
+
setData(watchedQuery.state.data);
|
|
37
|
+
setError(null);
|
|
38
|
+
}
|
|
39
|
+
}, []);
|
|
40
|
+
if (error != null) {
|
|
41
|
+
// Report errors - this is caught by an error boundary
|
|
42
|
+
throw error;
|
|
43
|
+
}
|
|
44
|
+
else if (data || watchedQuery?.state.isLoading === false) {
|
|
45
|
+
// Happy path data return
|
|
46
|
+
return {
|
|
47
|
+
data: data ?? watchedQuery?.state.data ?? [],
|
|
48
|
+
refresh: async (signal) => {
|
|
49
|
+
try {
|
|
50
|
+
const compiledQuery = parsedQuery.compile();
|
|
51
|
+
const result = await parsedQuery.execute({
|
|
52
|
+
sql: compiledQuery.sql,
|
|
53
|
+
parameters: [...compiledQuery.parameters],
|
|
54
|
+
db: powerSync
|
|
55
|
+
});
|
|
56
|
+
if (signal.aborted) {
|
|
57
|
+
return; // Abort if the signal is already aborted
|
|
58
|
+
}
|
|
59
|
+
setData(result);
|
|
60
|
+
setError(null);
|
|
61
|
+
}
|
|
62
|
+
catch (e) {
|
|
63
|
+
setError(e);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
else {
|
|
69
|
+
// Notify suspense is required
|
|
70
|
+
throw createSuspendingPromise(watchedQuery);
|
|
71
|
+
}
|
|
72
|
+
};
|
|
73
|
+
//# sourceMappingURL=useSingleSuspenseQuery.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useSingleSuspenseQuery.js","sourceRoot":"","sources":["../../../src/hooks/suspense/useSingleSuspenseQuery.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,MAAM,OAAO,CAAC;AAC1B,OAAO,EAAE,gBAAgB,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AACtE,OAAO,EAAE,YAAY,EAAE,MAAM,wBAAwB,CAAC;AAEtD,OAAO,EAAE,wBAAwB,EAAE,MAAM,2BAA2B,CAAC;AACrE,OAAO,EAAE,uBAAuB,EAAE,gBAAgB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