Skip to content

Commit 2099f25

Browse files
committed
Adopt in example
1 parent b44e949 commit 2099f25

File tree

6 files changed

+38
-4
lines changed

6 files changed

+38
-4
lines changed

demos/react-supabase-todolist/.env.local.template

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@
33
VITE_SUPABASE_URL=https://foo.supabase.co
44
VITE_SUPABASE_ANON_KEY=foo
55
VITE_POWERSYNC_URL=https://foo.powersync.journeyapps.com
6+
VITE_USE_SYNC_STREAMS=false

demos/react-supabase-todolist/src/app/views/todo-lists/edit/page.tsx

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ import {
1919
} from '@mui/material';
2020
import Fab from '@mui/material/Fab';
2121
import { usePowerSync, useQuery } from '@powersync/react';
22-
import React, { Suspense } from 'react';
22+
import { SyncStreamSubscription } from '@powersync/web';
23+
import React, { Suspense, useEffect } from 'react';
2324
import { useParams } from 'react-router-dom';
2425

2526
/**
@@ -32,6 +33,29 @@ const TodoEditSection = () => {
3233
const supabase = useSupabase();
3334
const { id: listID } = useParams();
3435

36+
if (import.meta.env.VITE_USE_SYNC_STREAMS == 'true') {
37+
useEffect(() => {
38+
let active = true;
39+
let subscription: SyncStreamSubscription | null = null;
40+
41+
powerSync
42+
.syncStream('todos', { list: listID })
43+
.subscribe()
44+
.then((sub) => {
45+
if (!active) {
46+
sub.unsubscribe();
47+
} else {
48+
subscription = sub;
49+
}
50+
});
51+
52+
return () => {
53+
active = false;
54+
subscription?.unsubscribe();
55+
};
56+
}, [listID]);
57+
}
58+
3559
const {
3660
data: [listRecord]
3761
} = useQuery<{ name: string }>(

demos/react-supabase-todolist/src/components/providers/SystemProvider.tsx

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,13 @@ import { AppSchema, ListRecord, LISTS_TABLE, TODOS_TABLE } from '@/library/power
33
import { SupabaseConnector } from '@/library/powersync/SupabaseConnector';
44
import { CircularProgress } from '@mui/material';
55
import { PowerSyncContext } from '@powersync/react';
6-
import { createBaseLogger, DifferentialWatchedQuery, LogLevel, PowerSyncDatabase } from '@powersync/web';
6+
import {
7+
createBaseLogger,
8+
DifferentialWatchedQuery,
9+
LogLevel,
10+
PowerSyncDatabase,
11+
SyncClientImplementation
12+
} from '@powersync/web';
713
import React, { Suspense } from 'react';
814
import { NavigationPanelContextProvider } from '../navigation/NavigationPanelContext';
915

@@ -68,7 +74,7 @@ export const SystemProvider = ({ children }: { children: React.ReactNode }) => {
6874
const l = connector.registerListener({
6975
initialized: () => {},
7076
sessionStarted: () => {
71-
powerSync.connect(connector);
77+
powerSync.connect(connector, { clientImplementation: SyncClientImplementation.RUST });
7278
}
7379
});
7480

demos/react-supabase-todolist/src/library/powersync/vite-env.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ interface ImportMetaEnv {
44
readonly VITE_SUPABASE_URL: string;
55
readonly VITE_SUPABASE_ANON_KEY: string;
66
readonly VITE_POWERSYNC_URL: string;
7+
readonly VITE_USE_SYNC_STREAMS: string;
78
}
89

910
interface ImportMeta {

packages/common/src/client/ConnectionManager.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,7 @@ class ActiveSubscription {
356356

357357
class SyncStreamSubscriptionHandle implements SyncStreamSubscription {
358358
constructor(readonly subscription: ActiveSubscription) {
359+
subscription.refcount++;
359360
_finalizer?.register(this, subscription);
360361
}
361362

packages/web/src/worker/sync/WorkerClient.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ export class WorkerClient {
4242
private async removePort() {
4343
if (this.resolvedPort) {
4444
const release = await this.sync.removePort(this.resolvedPort);
45+
this.resolvedPort = null;
4546
this.port.postMessage({
4647
event: SharedSyncClientEvent.CLOSE_ACK,
4748
data: {}
@@ -89,7 +90,7 @@ export class WorkerClient {
8990

9091
updateSubscriptions(subscriptions: SubscribedStream[]) {
9192
if (this.resolvedPort) {
92-
this.sync.updateSubscriptions;
93+
this.sync.updateSubscriptions(this.resolvedPort, subscriptions);
9394
}
9495
}
9596

0 commit comments

Comments
 (0)