@@ -44,18 +44,9 @@ export const db = new PowerSyncDatabase({
4444
4545export const connector = new TokenConnector ( ) ;
4646
47- const remote = new WebRemote ( connector ) ;
4847const adapter = new RecordingStorageAdapter ( db . database , schemaManager ) ;
4948
50- const syncOptions : WebStreamingSyncImplementationOptions = {
51- adapter,
52- remote,
53- uploadCrud : async ( ) => {
54- // No-op
55- } ,
56- identifier : 'diagnostics'
57- } ;
58- export const sync = new WebStreamingSyncImplementation ( syncOptions ) ;
49+ export let sync : WebStreamingSyncImplementation | undefined ;
5950
6051export interface SyncErrorListener extends BaseListener {
6152 lastErrorUpdated ?: ( ( error : Error ) => void ) | undefined ;
@@ -67,28 +58,39 @@ if (connector.hasCredentials()) {
6758
6859export async function connect ( ) {
6960 const params = getParams ( ) ;
61+ await sync ?. disconnect ( ) ;
62+ const remote = new WebRemote ( connector ) ;
63+ const syncOptions : WebStreamingSyncImplementationOptions = {
64+ adapter,
65+ remote,
66+ uploadCrud : async ( ) => {
67+ // No-op
68+ } ,
69+ identifier : 'diagnostics'
70+ } ;
71+ sync = new WebStreamingSyncImplementation ( syncOptions ) ;
7072 await sync . connect ( { params } ) ;
7173 if ( ! sync . syncStatus . connected ) {
7274 const error = sync . syncStatus . dataFlowStatus . downloadError ?? new Error ( 'Failed to connect' ) ;
7375 // Disconnect but don't wait for it
74- sync . disconnect ( ) ;
76+ await sync . disconnect ( ) ;
7577 throw error ;
7678 }
7779}
7880
7981export async function clearData ( ) {
80- await sync . disconnect ( ) ;
82+ await sync ? .disconnect ( ) ;
8183 await db . disconnectAndClear ( ) ;
8284 await schemaManager . clear ( ) ;
8385 await schemaManager . refreshSchema ( db . database ) ;
8486 if ( connector . hasCredentials ( ) ) {
8587 const params = getParams ( ) ;
86- await sync . connect ( { params } ) ;
88+ await sync ? .connect ( { params } ) ;
8789 }
8890}
8991
9092export async function disconnect ( ) {
91- await sync . disconnect ( ) ;
93+ await sync ? .disconnect ( ) ;
9294}
9395
9496export async function signOut ( ) {
0 commit comments