@powersync/react 1.9.1 → 1.10.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.
@@ -25,6 +25,12 @@ export interface UseSyncStreamOptions extends SyncStreamSubscribeOptions {
25
25
  * @returns The status for that stream, or `null` if the stream is currently being resolved.
26
26
  */
27
27
  export declare function useSyncStream(options: UseSyncStreamOptions): SyncStreamStatus | null;
28
+ /**
29
+ * Creates multiple PowerSync stream subscriptions. Subscriptions are kept alive as long as the
30
+ * React component calling this function. When it unmounts, or when the streams array contents
31
+ * change, all previous subscriptions are unsubscribed before new ones are created.
32
+ */
33
+ export declare function useSyncStreams(streamOptions: UseSyncStreamOptions[]): SyncStreamStatus[];
28
34
  /**
29
35
  * Returns `true` once all streams in the array have synced at least once.
30
36
  */
@@ -1,4 +1,4 @@
1
- import { useEffect, useMemo, useState } from 'react';
1
+ import { useEffect, useMemo } from 'react';
2
2
  import { usePowerSync } from './PowerSyncContext.js';
3
3
  import { useStatus } from './useStatus.js';
4
4
  /**
@@ -10,81 +10,57 @@ import { useStatus } from './useStatus.js';
10
10
  * @returns The status for that stream, or `null` if the stream is currently being resolved.
11
11
  */
12
12
  export function useSyncStream(options) {
13
- const { name, parameters } = options;
13
+ return useSyncStreams([options])[0];
14
+ }
15
+ /**
16
+ * Creates multiple PowerSync stream subscriptions. Subscriptions are kept alive as long as the
17
+ * React component calling this function. When it unmounts, or when the streams array contents
18
+ * change, all previous subscriptions are unsubscribed before new ones are created.
19
+ */
20
+ export function useSyncStreams(streamOptions) {
14
21
  const db = usePowerSync();
15
22
  const status = useStatus();
16
- const stream = useMemo(() => db.syncStream(name, parameters), [name, parameters]);
23
+ const stringifiedOptions = useMemo(() => JSON.stringify(streamOptions), [streamOptions]);
24
+ const syncStreams = useMemo(() => streamOptions.map((options) => {
25
+ return {
26
+ stream: db.syncStream(options.name, options.parameters ?? undefined),
27
+ options
28
+ };
29
+ }), [stringifiedOptions]);
17
30
  useEffect(() => {
18
31
  let active = true;
19
- let subscription = null;
20
- stream.subscribe(options).then((sub) => {
21
- if (active) {
22
- subscription = sub;
23
- }
24
- else {
25
- // The cleanup function already ran, unsubscribe immediately.
26
- sub.unsubscribe();
27
- }
28
- });
32
+ const resolvedSubs = [];
33
+ for (const entry of syncStreams) {
34
+ entry.stream.subscribe(entry.options).then((sub) => {
35
+ if (active) {
36
+ resolvedSubs.push(sub);
37
+ }
38
+ else {
39
+ // The cleanup function already ran, unsubscribe immediately.
40
+ sub.unsubscribe();
41
+ }
42
+ });
43
+ }
29
44
  return () => {
30
45
  active = false;
31
- // If we don't have a subscription yet, it'll still get cleaned up once the promise resolves because we've set
32
- // active to false.
33
- subscription?.unsubscribe();
46
+ for (const sub of resolvedSubs) {
47
+ sub.unsubscribe();
48
+ }
34
49
  };
35
- }, [stream]);
36
- return status.forStream(stream) ?? null;
50
+ }, [stringifiedOptions]);
51
+ return useMemo(() => syncStreams.map((entry) => status.forStream(entry.stream) ?? null), [status, stringifiedOptions]);
37
52
  }
38
53
  /**
39
54
  * Returns `true` once all streams in the array have synced at least once.
40
55
  */
41
56
  export function useAllSyncStreamsHaveSynced(db, streams) {
42
- // Since streams are a user-supplied array, they will likely be different each time this function is called. We don't
43
- // want to update underlying subscriptions each time, though.
44
- const hash = useMemo(() => streams && JSON.stringify(streams), [streams]);
45
- const [synced, setHasSynced] = useState(streams == null || streams.every((e) => e.waitForStream != true));
46
- useEffect(() => {
47
- if (streams) {
48
- setHasSynced(false);
49
- const promises = [];
50
- const abort = new AbortController();
51
- for (const stream of streams) {
52
- promises.push(db.syncStream(stream.name, stream.parameters).subscribe(stream));
53
- }
54
- // First, wait for all subscribe() calls to make all subscriptions active.
55
- Promise.all(promises).then(async (resolvedStreams) => {
56
- function allHaveSynced(status) {
57
- return resolvedStreams.every((s, i) => {
58
- const request = streams[i];
59
- return !request.waitForStream || status.forStream(s)?.subscription?.hasSynced;
60
- });
61
- }
62
- // Wait for the effect to be cancelled or all streams having synced.
63
- await db.waitForStatus(allHaveSynced, abort.signal);
64
- if (abort.signal.aborted) {
65
- // Was cancelled
66
- }
67
- else {
68
- // Has synced, update public state.
69
- setHasSynced(true);
70
- // Wait for cancellation before clearing subscriptions.
71
- await new Promise((resolve) => {
72
- abort.signal.addEventListener('abort', () => resolve());
73
- });
74
- }
75
- // Effect was definitely cancelled at this point, so drop the subscriptions.
76
- for (const stream of resolvedStreams) {
77
- stream.unsubscribe();
78
- }
79
- });
80
- return () => abort.abort();
81
- }
82
- else {
83
- // There are no streams, so all of them have synced.
84
- setHasSynced(true);
85
- return undefined;
86
- }
87
- }, [hash]);
88
- return synced;
57
+ const statuses = useSyncStreams(streams ?? []);
58
+ if (!streams)
59
+ return true;
60
+ return streams.every((stream, i) => {
61
+ if (!stream.waitForStream)
62
+ return true;
63
+ return statuses[i]?.subscription?.hasSynced === true;
64
+ });
89
65
  }
90
66
  //# sourceMappingURL=streams.js.map
@@ -1 +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;AAQrD,OAAO,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAoB3C;;;;;;;GAOG;AACH,MAAM,UAAU,aAAa,CAAC,OAA6B;IACzD,MAAM,EAAE,IAAI,EAAE,UAAU,EAAE,GAAG,OAAO,CAAC;IACrC,MAAM,EAAE,GAAG,YAAY,EAAE,CAAC;IAC1B,MAAM,MAAM,GAAG,SAAS,EAAE,CAAC;IAC3B,MAAM,MAAM,GAAG,OAAO,CAAC,GAAG,EAAE,CAAC,EAAE,CAAC,UAAU,CAAC,IAAI,EAAE,UAAU,CAAC,EAAE,CAAC,IAAI,EAAE,UAAU,CAAC,CAAC,CAAC;IAElF,SAAS,CAAC,GAAG,EAAE;QACb,IAAI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AACL,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@powersync/react",
3
- "version": "1.9.1",
3
+ "version": "1.10.0",
4
4
  "publishConfig": {
5
5
  "registry": "https://registry.npmjs.org/",
6
6
  "access": "public"
@@ -25,7 +25,7 @@
25
25
  },
26
26
  "homepage": "https://docs.powersync.com",
27
27
  "peerDependencies": {
28
- "@powersync/common": "^1.50.0",
28
+ "@powersync/common": "^1.51.0",
29
29
  "react": "*"
30
30
  },
31
31
  "devDependencies": {
@@ -39,9 +39,9 @@
39
39
  "react": "18.3.1",
40
40
  "react-dom": "18.3.1",
41
41
  "react-error-boundary": "^4.1.0",
42
- "@powersync/common": "1.50.0",
42
+ "@powersync/common": "1.51.0",
43
43
  "@powersync/drizzle-driver": "0.7.3",
44
- "@powersync/web": "1.37.0"
44
+ "@powersync/web": "1.37.1"
45
45
  },
46
46
  "scripts": {
47
47
  "build": "tsc -b",
@@ -36,33 +36,57 @@ export interface UseSyncStreamOptions extends SyncStreamSubscribeOptions {
36
36
  * @returns The status for that stream, or `null` if the stream is currently being resolved.
37
37
  */
38
38
  export function useSyncStream(options: UseSyncStreamOptions): SyncStreamStatus | null {
39
- const { name, parameters } = options;
39
+ return useSyncStreams([options])[0];
40
+ }
41
+
42
+ /**
43
+ * Creates multiple PowerSync stream subscriptions. Subscriptions are kept alive as long as the
44
+ * React component calling this function. When it unmounts, or when the streams array contents
45
+ * change, all previous subscriptions are unsubscribed before new ones are created.
46
+ */
47
+ export function useSyncStreams(streamOptions: UseSyncStreamOptions[]): SyncStreamStatus[] {
40
48
  const db = usePowerSync();
41
49
  const status = useStatus();
42
- const stream = useMemo(() => db.syncStream(name, parameters), [name, parameters]);
50
+
51
+ const stringifiedOptions = useMemo(() => JSON.stringify(streamOptions), [streamOptions]);
52
+ const syncStreams = useMemo(
53
+ () =>
54
+ streamOptions.map((options) => {
55
+ return {
56
+ stream: db.syncStream(options.name, options.parameters ?? undefined),
57
+ options
58
+ };
59
+ }),
60
+ [stringifiedOptions]
61
+ );
43
62
 
44
63
  useEffect(() => {
45
64
  let active = true;
46
- let subscription: SyncStreamSubscription | null = null;
65
+ const resolvedSubs: SyncStreamSubscription[] = [];
47
66
 
48
- stream.subscribe(options).then((sub) => {
49
- if (active) {
50
- subscription = sub;
51
- } else {
52
- // The cleanup function already ran, unsubscribe immediately.
53
- sub.unsubscribe();
54
- }
55
- });
67
+ for (const entry of syncStreams) {
68
+ entry.stream.subscribe(entry.options).then((sub) => {
69
+ if (active) {
70
+ resolvedSubs.push(sub);
71
+ } else {
72
+ // The cleanup function already ran, unsubscribe immediately.
73
+ sub.unsubscribe();
74
+ }
75
+ });
76
+ }
56
77
 
57
78
  return () => {
58
79
  active = false;
59
- // If we don't have a subscription yet, it'll still get cleaned up once the promise resolves because we've set
60
- // active to false.
61
- subscription?.unsubscribe();
80
+ for (const sub of resolvedSubs) {
81
+ sub.unsubscribe();
82
+ }
62
83
  };
63
- }, [stream]);
84
+ }, [stringifiedOptions]);
64
85
 
65
- return status.forStream(stream) ?? null;
86
+ return useMemo(
87
+ () => syncStreams.map((entry) => status.forStream(entry.stream) ?? null),
88
+ [status, stringifiedOptions]
89
+ );
66
90
  }
67
91
 
68
92
  /**
@@ -72,57 +96,12 @@ export function useAllSyncStreamsHaveSynced(
72
96
  db: AbstractPowerSyncDatabase,
73
97
  streams: QuerySyncStreamOptions[] | undefined
74
98
  ): boolean {
75
- // Since streams are a user-supplied array, they will likely be different each time this function is called. We don't
76
- // want to update underlying subscriptions each time, though.
77
- const hash = useMemo(() => streams && JSON.stringify(streams), [streams]);
78
- const [synced, setHasSynced] = useState(streams == null || streams.every((e) => e.waitForStream != true));
99
+ const statuses = useSyncStreams(streams ?? []);
79
100
 
80
- useEffect(() => {
81
- if (streams) {
82
- setHasSynced(false);
83
-
84
- const promises: Promise<SyncStreamSubscription>[] = [];
85
- const abort = new AbortController();
86
- for (const stream of streams) {
87
- promises.push(db.syncStream(stream.name, stream.parameters).subscribe(stream));
88
- }
89
-
90
- // First, wait for all subscribe() calls to make all subscriptions active.
91
- Promise.all(promises).then(async (resolvedStreams) => {
92
- function allHaveSynced(status: SyncStatus) {
93
- return resolvedStreams.every((s, i) => {
94
- const request = streams[i];
95
- return !request.waitForStream || status.forStream(s)?.subscription?.hasSynced;
96
- });
97
- }
98
-
99
- // Wait for the effect to be cancelled or all streams having synced.
100
- await db.waitForStatus(allHaveSynced, abort.signal);
101
- if (abort.signal.aborted) {
102
- // Was cancelled
103
- } else {
104
- // Has synced, update public state.
105
- setHasSynced(true);
106
-
107
- // Wait for cancellation before clearing subscriptions.
108
- await new Promise<void>((resolve) => {
109
- abort.signal.addEventListener('abort', () => resolve());
110
- });
111
- }
112
-
113
- // Effect was definitely cancelled at this point, so drop the subscriptions.
114
- for (const stream of resolvedStreams) {
115
- stream.unsubscribe();
116
- }
117
- });
118
-
119
- return () => abort.abort();
120
- } else {
121
- // There are no streams, so all of them have synced.
122
- setHasSynced(true);
123
- return undefined;
124
- }
125
- }, [hash]);
101
+ if (!streams) return true;
126
102
 
127
- return synced;
103
+ return streams.every((stream, i) => {
104
+ if (!stream.waitForStream) return true;
105
+ return statuses[i]?.subscription?.hasSynced === true;
106
+ });
128
107
  }