-
-
Notifications
You must be signed in to change notification settings - Fork 711
Expand file tree
/
Copy pathdexie-cloud-client.ts
More file actions
705 lines (667 loc) · 25.7 KB
/
Copy pathdexie-cloud-client.ts
File metadata and controls
705 lines (667 loc) · 25.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
import Dexie, { liveQuery, Subscription, Table } from 'dexie';
import {
DBPermissionSet,
DBRealmMember,
getDbNameFromDbUrl,
} from 'dexie-cloud-common';
import {
BehaviorSubject,
combineLatest,
firstValueFrom,
from,
fromEvent,
Subject,
} from 'rxjs';
import {
createDownloadingState,
observeBlobProgress,
} from './sync/blobProgress';
import { downloadUnresolvedBlobs } from './sync/eagerBlobDownloader';
import { filter, map, skip, startWith, switchMap, take } from 'rxjs/operators';
import { login } from './authentication/login';
import { UNAUTHORIZED_USER } from './authentication/UNAUTHORIZED_USER';
import { DexieCloudDB } from './db/DexieCloudDB';
import { PersistedSyncState } from './db/entities/PersistedSyncState';
import { DexieCloudOptions } from './DexieCloudOptions';
import { DISABLE_SERVICEWORKER_STRATEGY } from './DISABLE_SERVICEWORKER_STRATEGY';
import './extend-dexie-interface';
import { DexieCloudSyncOptions } from './DexieCloudSyncOptions';
import { IS_SERVICE_WORKER } from './helpers/IS_SERVICE_WORKER';
import { throwVersionIncrementNeeded } from './helpers/throwVersionIncrementNeeded';
import { createIdGenerationMiddleware } from './middlewares/createIdGenerationMiddleware';
import { createImplicitPropSetterMiddleware } from './middlewares/createImplicitPropSetterMiddleware';
import { createMutationTrackingMiddleware } from './middlewares/createMutationTrackingMiddleware';
import { createBlobResolveMiddleware } from './middlewares/blobResolveMiddleware';
//import { dexieCloudSyncProtocol } from "./dexieCloudSyncProtocol";
import { overrideParseStoresSpec } from './overrideParseStoresSpec';
import { performInitialSync } from './performInitialSync';
import { connectWebSocket } from './sync/connectWebSocket';
import { isSyncNeeded } from './sync/isSyncNeeded';
import { LocalSyncWorker } from './sync/LocalSyncWorker';
import {
registerPeriodicSyncEvent,
registerSyncEvent,
} from './sync/registerSyncEvent';
import { triggerSync } from './sync/triggerSync';
import { DXCUserInteraction } from './types/DXCUserInteraction';
import { SyncState } from './types/SyncState';
import { updateSchemaFromOptions } from './updateSchemaFromOptions';
import { verifySchema } from './verifySchema';
import { setupDefaultGUI } from './default-ui';
import { DXCWebSocketStatus } from './DXCWebSocketStatus';
import { computeSyncState } from './computeSyncState';
import { generateKey } from './middleware-helpers/idGenerationHelpers';
import { permissions } from './permissions';
import { getCurrentUserEmitter } from './currentUserEmitter';
import { NewIdOptions } from './types/NewIdOptions';
import { getInvitesObservable } from './getInvitesObservable';
import { getGlobalRolesObservable } from './getGlobalRolesObservable';
import { UserLogin } from './db/entities/UserLogin';
import { InvalidLicenseError } from './InvalidLicenseError';
import { logout, _logout } from './authentication/logout';
import { loadAccessToken } from './authentication/authenticate';
import { isEagerSyncDisabled } from './isEagerSyncDisabled';
import { createYHandler } from './yjs/createYHandler';
import { DexieYProvider } from 'y-dexie';
import {
parseOAuthCallback,
cleanupOAuthUrl,
} from './authentication/handleOAuthCallback';
import { OAuthError } from './errors/OAuthError';
import { alertUser } from './authentication/interactWithUser';
import { fetchAuthProviders } from './authentication/fetchAuthProviders';
export { DexieCloudTable } from './DexieCloudTable';
export * from './getTiedRealmId';
export {
DBRealm,
DBRealmMember,
DBRealmRole,
DBSyncedObject,
DBPermissionSet,
AuthProvidersResponse,
OAuthProviderInfo,
} from 'dexie-cloud-common';
export { resolveText } from './helpers/resolveText';
export { Invite } from './Invite';
export type { UserLogin, DXCWebSocketStatus, SyncState };
export type { DexieCloudSyncOptions };
export type {
DexieCloudOptions,
PeriodicSyncOptions,
} from './DexieCloudOptions';
export * from './types/DXCAlert';
export * from './types/DXCInputField';
export * from './types/DXCUserInteraction';
export { defineYDocTrigger } from './define-ydoc-trigger';
const DEFAULT_OPTIONS: Partial<DexieCloudOptions> = {
nameSuffix: true,
};
export function dexieCloud(dexie: Dexie) {
const origIdbName = dexie.name;
//
//
//
const currentUserEmitter = getCurrentUserEmitter(dexie);
const subscriptions: Subscription[] = [];
let configuredProgramatically = false;
// Pending OAuth auth code from dxc-auth redirect (detected in configure())
let pendingOAuthCode: { code: string; provider: string } | null = null;
// Pending OAuth error from dxc-auth redirect (detected in configure())
let pendingOAuthError: OAuthError | null = null;
// local sync worker - used when there's no service worker.
let localSyncWorker: { start: () => void; stop: () => void } | null = null;
dexie.on(
'ready',
async (dexie: Dexie) => {
try {
await onDbReady(dexie);
} catch (error) {
console.error(error);
// Make sure to succeed with database open even if network is down.
}
},
true // true = sticky
);
/** Void starting subscribers after a close has happened. */
let closed = false;
function throwIfClosed() {
if (closed) throw new Dexie.DatabaseClosedError();
}
dexie.once('close', () => {
subscriptions.forEach((subscription) => subscription.unsubscribe());
subscriptions.splice(0, subscriptions.length);
closed = true;
localSyncWorker && localSyncWorker.stop();
localSyncWorker = null;
currentUserEmitter.next(UNAUTHORIZED_USER);
});
const syncComplete = new Subject<void>();
const downloading$ = createDownloadingState();
dexie.cloud = {
// @ts-ignore
version: __VERSION__,
options: { ...DEFAULT_OPTIONS } as DexieCloudOptions,
schema: null,
get currentUserId() {
return currentUserEmitter.value.userId || UNAUTHORIZED_USER.userId!;
},
currentUser: currentUserEmitter,
syncState: new BehaviorSubject<SyncState>({
phase: 'initial',
status: 'not-started',
}),
events: {
syncComplete,
},
persistedSyncState: new BehaviorSubject<PersistedSyncState | undefined>(
undefined
),
blobProgress: observeBlobProgress(DexieCloudDB(dexie), downloading$),
userInteraction: new BehaviorSubject<DXCUserInteraction | undefined>(
undefined
),
webSocketStatus: new BehaviorSubject<DXCWebSocketStatus>('not-started'),
async login(hint) {
const db = DexieCloudDB(dexie);
await db.cloud.sync();
await login(db, hint);
},
invites: getInvitesObservable(dexie),
roles: getGlobalRolesObservable(dexie),
configure(options: DexieCloudOptions) {
// Validate largeStringThreshold (preferred) or maxStringLength (deprecated) —
// Infinity disables offloading, otherwise must be a finite number between 100
// and the server limit (32768).
// Minimum 100 prevents accidental offloading of primary keys and short strings
// that would break sync.
const MIN_STRING_LENGTH = 100;
const MAX_SERVER_STRING_LENGTH = 32768;
if (options.maxStringLength !== undefined) {
console.warn(
'maxStringLength is deprecated, use largeStringThreshold instead'
);
// If largeStringThreshold is not explicitly set, migrate to new name
if (options.largeStringThreshold === undefined) {
options = {
...options,
largeStringThreshold: options.maxStringLength,
};
}
}
const thresholdValue = options.largeStringThreshold;
if (
thresholdValue !== undefined &&
thresholdValue !== Infinity &&
(!Number.isFinite(thresholdValue) ||
thresholdValue < MIN_STRING_LENGTH ||
thresholdValue > MAX_SERVER_STRING_LENGTH)
) {
throw new Error(
`largeStringThreshold must be Infinity or a finite number in [${MIN_STRING_LENGTH}, ${MAX_SERVER_STRING_LENGTH}]. Got: ${thresholdValue}`
);
}
options = dexie.cloud.options = { ...dexie.cloud.options, ...options };
configuredProgramatically = true;
if (options.databaseUrl && options.nameSuffix) {
// @ts-ignore
dexie.name = `${origIdbName}-${getDbNameFromDbUrl(
options.databaseUrl
)}`;
DexieCloudDB(dexie).reconfigure(); // Update observable from new dexie.name
}
updateSchemaFromOptions(dexie.cloud.schema, dexie.cloud.options);
// Check for OAuth callback (dxc-auth query parameter)
// Only check in DOM environment, not workers
if (typeof window !== 'undefined' && window.location) {
try {
const callback = parseOAuthCallback();
if (callback) {
// Store the pending auth code for processing when db is ready
pendingOAuthCode = {
code: callback.code,
provider: callback.provider,
};
console.debug(
'[dexie-cloud] OAuth callback detected, auth code stored for processing'
);
}
} catch (error) {
// parseOAuthCallback throws OAuthError on error callbacks
if (error instanceof OAuthError) {
pendingOAuthError = error;
console.error('[dexie-cloud] OAuth callback error:', error.message);
} else {
console.error('[dexie-cloud] OAuth callback error:', error);
}
}
}
},
async logout({ force } = {}) {
force
? await _logout(DexieCloudDB(dexie), { deleteUnsyncedData: true })
: await logout(DexieCloudDB(dexie));
},
async getAuthProviders() {
const options = dexie.cloud.options;
if (!options?.databaseUrl) {
throw new Error(
'Dexie Cloud not configured. Call db.cloud.configure() first.'
);
}
const socialAuthEnabled = options.socialAuth !== false;
return fetchAuthProviders(options.databaseUrl, socialAuthEnabled);
},
async sync(
{ wait, purpose }: DexieCloudSyncOptions = { wait: true, purpose: 'push' }
) {
if (wait === undefined) wait = true;
const db = DexieCloudDB(dexie);
const licenseStatus = db.cloud.currentUser.value.license?.status || 'ok';
if (licenseStatus !== 'ok') {
// Refresh access token to check for updated license
await loadAccessToken(db);
}
if (purpose === 'pull') {
const syncState = db.cloud.persistedSyncState.value;
triggerSync(db, purpose);
if (wait) {
const newSyncState = await firstValueFrom(
db.cloud.persistedSyncState.pipe(
filter(
(newSyncState) =>
newSyncState?.timestamp != null &&
(!syncState || newSyncState.timestamp > syncState.timestamp!)
)
)
);
if (newSyncState?.error) {
throw new Error(`Sync error: ` + newSyncState.error);
}
}
} else if (await isSyncNeeded(db)) {
const syncState = db.cloud.persistedSyncState.value;
triggerSync(db, purpose);
if (wait) {
console.debug('db.cloud.login() is waiting for sync completion...');
await firstValueFrom(
from(
liveQuery(async () => {
const syncNeeded = await isSyncNeeded(db);
const newSyncState = await db.getPersistedSyncState();
if (
newSyncState?.timestamp !== syncState?.timestamp &&
newSyncState?.error
)
throw new Error(`Sync error: ` + newSyncState.error);
return syncNeeded;
})
).pipe(filter((isNeeded) => !isNeeded))
);
console.debug(
'Done waiting for sync completion because we have nothing to push anymore'
);
}
}
},
permissions(
obj: { owner: string; realmId: string; table?: () => string },
tableName?: string
) {
return permissions(dexie._novip, obj, tableName);
},
};
dexie.Version.prototype['_parseStoresSpec'] = Dexie.override(
dexie.Version.prototype['_parseStoresSpec'],
(origFunc) => overrideParseStoresSpec(origFunc, dexie)
);
dexie.Table.prototype.newId = function (
this: Table<any>,
{ colocateWith }: NewIdOptions = {}
) {
const shardKey =
colocateWith && colocateWith.substr(colocateWith.length - 3);
return generateKey(dexie.cloud.schema![this.name].idPrefix || '', shardKey);
};
dexie.Table.prototype.idPrefix = function (this: Table<any>) {
return this.db.cloud.schema?.[this.name]?.idPrefix || '';
};
dexie.use(createBlobResolveMiddleware(DexieCloudDB(dexie)));
dexie.use(
createMutationTrackingMiddleware({
currentUserObservable: dexie.cloud.currentUser,
db: DexieCloudDB(dexie),
})
);
dexie.use(createImplicitPropSetterMiddleware(DexieCloudDB(dexie)));
dexie.use(createIdGenerationMiddleware(DexieCloudDB(dexie)));
async function onDbReady(dexie: Dexie) {
closed = false; // As Dexie calls us, we are not closed anymore. Maybe reopened? Remember db.ready event is registered with sticky flag!
const db = DexieCloudDB(dexie);
// Setup default GUI:
if (typeof window !== 'undefined' && typeof document !== 'undefined') {
if (!db.cloud.options?.customLoginGui) {
subscriptions.push(setupDefaultGUI(dexie));
}
}
if (!db.cloud.isServiceWorkerDB) {
subscriptions.push(computeSyncState(db).subscribe(dexie.cloud.syncState));
}
// Forward db.syncCompleteEvent to be publicly consumable via db.cloud.events.syncComplete:
subscriptions.push(db.syncCompleteEvent.subscribe(syncComplete));
// Eager blob download: When blobMode='eager' (default), download unresolved blobs after sync
const blobMode = db.cloud.options?.blobMode ?? 'eager';
if (blobMode === 'eager') {
let eagerBlobDownloadInFlight: Promise<void> | null = null;
const downloadBlobs = () => {
if (eagerBlobDownloadInFlight) return;
eagerBlobDownloadInFlight = Dexie.ignoreTransaction(() =>
downloadUnresolvedBlobs(db, downloading$)
)
.catch((err) => {
console.error('[dexie-cloud] Eager blob download failed:', err);
})
.finally(() => {
eagerBlobDownloadInFlight = null;
});
};
setTimeout(downloadBlobs, 0); // Don't block ready event. Start downloading blobs in the background right after.
// And also after every sync completes:
subscriptions.push(db.syncCompleteEvent.subscribe(downloadBlobs));
}
//verifyConfig(db.cloud.options); Not needed (yet at least!)
// Verify the user has allowed version increment.
if (!db.tables.every((table) => table.core)) {
throwVersionIncrementNeeded();
}
const swRegistrations =
'serviceWorker' in navigator
? await navigator.serviceWorker.getRegistrations()
: [];
const [initiallySynced, lastSyncedRealms] = await db.transaction(
'rw',
db.$syncState,
async () => {
const { options, schema } = db.cloud;
const [persistedOptions, persistedSchema, persistedSyncState] =
await Promise.all([
db.getOptions(),
db.getSchema(),
db.getPersistedSyncState(),
]);
if (!configuredProgramatically) {
// Options not specified programatically (use case for SW!)
// Take persisted options:
db.cloud.options = persistedOptions || null;
} else if (
!persistedOptions ||
JSON.stringify(persistedOptions) !== JSON.stringify(options)
) {
// Update persisted options:
if (!options) throw new Error(`Internal error`); // options cannot be null if configuredProgramatically is set.
const newPersistedOptions: DexieCloudOptions = {
...options,
};
delete newPersistedOptions.fetchTokens;
delete newPersistedOptions.awarenessProtocol;
await db.$syncState.put(newPersistedOptions, 'options');
}
if (
db.cloud.options?.tryUseServiceWorker &&
'serviceWorker' in navigator &&
swRegistrations.length > 0 &&
!DISABLE_SERVICEWORKER_STRATEGY
) {
// * Configured for using service worker if available.
// * Browser supports service workers
// * There are at least one service worker registration
console.debug('Dexie Cloud Addon: Using service worker');
db.cloud.usingServiceWorker = true;
} else {
// Not configured for using service worker or no service worker
// registration exists. Don't rely on service worker to do any job.
// Use LocalSyncWorker instead.
if (
db.cloud.options?.tryUseServiceWorker &&
!db.cloud.isServiceWorkerDB
) {
console.debug(
'dexie-cloud-addon: Not using service worker.',
swRegistrations.length === 0
? 'No SW registrations found.'
: 'serviceWorker' in navigator && DISABLE_SERVICEWORKER_STRATEGY
? 'Avoiding SW background sync and SW periodic bg sync for this browser due to browser bugs.'
: 'navigator.serviceWorker not present'
);
}
db.cloud.usingServiceWorker = false;
}
updateSchemaFromOptions(schema, db.cloud.options);
updateSchemaFromOptions(persistedSchema, db.cloud.options);
if (!schema) {
// Database opened dynamically (use case for SW!)
// Take persisted schema:
db.cloud.schema = persistedSchema || null;
} else if (
!persistedSchema ||
JSON.stringify(persistedSchema) !== JSON.stringify(schema)
) {
// Update persisted schema (but don't overwrite table prefixes)
const newPersistedSchema = persistedSchema || {};
for (const [table, tblSchema] of Object.entries(schema)) {
const newTblSchema = newPersistedSchema[table];
if (!newTblSchema) {
newPersistedSchema[table] = { ...tblSchema };
} else {
newTblSchema.markedForSync = tblSchema.markedForSync;
tblSchema.deleted = newTblSchema.deleted;
newTblSchema.generatedGlobalId = tblSchema.generatedGlobalId;
}
}
await db.$syncState.put(newPersistedSchema, 'schema');
// Make sure persisted table prefixes are being used instead of computed ones:
// Let's assign all props as the newPersistedSchems should be what we should be working with.
Object.assign(schema, newPersistedSchema);
}
return [
persistedSyncState?.initiallySynced,
persistedSyncState?.realms,
];
}
);
if (initiallySynced) {
db.setInitiallySynced(true);
}
verifySchema(db);
// Manage CurrentUser observable:
throwIfClosed();
if (!db.cloud.isServiceWorkerDB) {
subscriptions.push(
liveQuery(() =>
db.getCurrentUser().then((user) => {
if (
!user.isLoggedIn &&
typeof location !== 'undefined' &&
/dxc-auth\=/.test(location.search)
) {
// Still loading user because OAuth redirect just happened.
// Keep isLoading true.
return { ...user, isLoading: true };
}
return user;
})
).subscribe(currentUserEmitter)
);
// Manage PersistendSyncState observable:
subscriptions.push(
liveQuery(() => db.getPersistedSyncState()).subscribe(
db.cloud.persistedSyncState
)
);
// Wait till currentUser and persistedSyncState gets populated
// with things from the database and not just the default values.
// This is so that when db.open() completes, user should be safe
// to subscribe to these observables and get actual data.
await firstValueFrom(
combineLatest([
currentUserEmitter.pipe(skip(1), take(1)),
db.cloud.persistedSyncState.pipe(skip(1), take(1)),
])
);
const yHandler = createYHandler(db);
DexieYProvider.on.new.subscribe(yHandler);
db.dx.once('close', () => {
DexieYProvider.on.new.unsubscribe(yHandler);
});
}
// HERE: If requireAuth, do athentication now.
let changedUser = false;
let user = await db.getCurrentUser();
// Show pending OAuth error if present (from dxc-auth redirect)
if (pendingOAuthError && !db.cloud.isServiceWorkerDB) {
const error = pendingOAuthError;
pendingOAuthError = null; // Clear pending error
console.debug('[dexie-cloud] Showing OAuth error:', error.message);
// Show alert to user about the OAuth error
// Guard so UI errors don't abort initialization
try {
await alertUser(db.cloud.userInteraction, 'Authentication Error', {
type: 'error',
messageCode: 'GENERIC_ERROR',
message: error.message,
messageParams: { provider: error.provider || 'unknown' },
});
// Clean up URL (remove dxc-auth param)
cleanupOAuthUrl();
} catch (uiError) {
console.error(
'[dexie-cloud] Failed to show OAuth error alert:',
uiError
);
}
}
// Process pending OAuth callback if present (from dxc-auth redirect)
if (pendingOAuthCode && !db.cloud.isServiceWorkerDB) {
const { code, provider } = pendingOAuthCode;
pendingOAuthCode = null; // Clear pending code
console.debug(
'[dexie-cloud] Processing OAuth callback, provider:',
provider
);
try {
changedUser = await login(db, { oauthCode: code, provider });
user = await db.getCurrentUser();
// Clean up URL (remove dxc-auth param)
cleanupOAuthUrl();
} catch (error) {
console.error('[dexie-cloud] OAuth login failed:', error);
// Continue with normal flow - user can try again
}
}
const requireAuth = db.cloud.options?.requireAuth;
if (requireAuth) {
if (db.cloud.isServiceWorkerDB) {
// If this is a service worker DB, we can't do authentication here,
// we just wait until the application has done it.
console.debug(
'Dexie Cloud Service worker. Waiting for application to authenticate.'
);
await firstValueFrom(
currentUserEmitter.pipe(
filter((user) => !!user.isLoggedIn),
take(1)
)
);
console.debug(
'Dexie Cloud Service worker. Application has authenticated.'
);
} else {
if (typeof requireAuth === 'object') {
// requireAuth contains login hints. Check if we already fulfil it:
if (
!user.isLoggedIn ||
(requireAuth.userId && user.userId !== requireAuth.userId) ||
(requireAuth.email && user.email !== requireAuth.email)
) {
// If not, login the configured user:
changedUser = await login(db, requireAuth);
}
} else if (!user.isLoggedIn) {
// requireAuth is true and user is not logged in
changedUser = await login(db);
}
}
}
if (
user.isLoggedIn &&
(!lastSyncedRealms || !lastSyncedRealms.includes(user.userId!))
) {
// User has been logged in but this is not reflected in the sync state.
// This can happen if page is reloaded after login but before the sync call following
// the login was complete.
// The user is to be viewed as changed becuase current syncState does not reflect the presence
// of the logged-in user.
changedUser = true; // Set changedUser to true to trigger a pull-sync later down.
}
if (localSyncWorker) localSyncWorker.stop();
localSyncWorker = null;
throwIfClosed();
const doInitialSync =
db.cloud.options?.databaseUrl && (!initiallySynced || changedUser);
if (doInitialSync) {
// Do the initial sync directly in the browser thread no matter if we are using service worker or not.
await performInitialSync(db, db.cloud.options!, db.cloud.schema!);
db.setInitiallySynced(true);
}
throwIfClosed();
if (db.cloud.usingServiceWorker && db.cloud.options?.databaseUrl) {
if (!doInitialSync) {
registerSyncEvent(db, 'push').catch(() => {});
}
registerPeriodicSyncEvent(db).catch(() => {});
} else if (
db.cloud.options?.databaseUrl &&
db.cloud.schema &&
!db.cloud.isServiceWorkerDB
) {
// There's no SW. Start SyncWorker instead.
localSyncWorker = LocalSyncWorker(db, db.cloud.options, db.cloud.schema!);
localSyncWorker.start();
if (!doInitialSync) {
triggerSync(db, 'push');
}
}
// Listen to online event and do sync.
throwIfClosed();
if (!db.cloud.isServiceWorkerDB) {
subscriptions.push(
fromEvent(self, 'online').subscribe(() => {
console.debug('online!');
db.syncStateChangedEvent.next({
phase: 'not-in-sync',
});
if (!isEagerSyncDisabled(db)) {
triggerSync(db, 'push');
}
}),
fromEvent(self, 'offline').subscribe(() => {
console.debug('offline!');
db.syncStateChangedEvent.next({
phase: 'offline',
});
})
);
}
// Connect WebSocket unless we are in a service worker or websocket is disabled.
if (
db.cloud.options?.databaseUrl &&
!db.cloud.options?.disableWebSocket &&
!IS_SERVICE_WORKER
) {
subscriptions.push(connectWebSocket(db));
}
}
}
// @ts-ignore
dexieCloud.version = __VERSION__;
Dexie.Cloud = dexieCloud;
export default dexieCloud;