Skip to content

Commit

Permalink
New option 'disableEagerSync'
Browse files Browse the repository at this point in the history
  • Loading branch information
dfahlander committed Oct 5, 2023
1 parent a16b3b0 commit a52b1e2
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 9 deletions.
3 changes: 3 additions & 0 deletions addons/dexie-cloud/src/DexieCloudOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ export interface DexieCloudOptions {
// Disable websocket connection
disableWebSocket?: boolean;

// Disable automatic sync on changes
disableEagerSync?: boolean;

// Provides a custom way of fetching the JWT tokens. This option
// can be used when integrating with custom authentication.
// See https://dexie.org/cloud/docs/db.cloud.configure()#fetchtoken
Expand Down
5 changes: 4 additions & 1 deletion addons/dexie-cloud/src/dexie-cloud-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ 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';
export { DexieCloudTable } from './DexieCloudTable';
export * from './getTiedRealmId';
export {
Expand Down Expand Up @@ -423,7 +424,9 @@ export function dexieCloud(dexie: Dexie) {
db.syncStateChangedEvent.next({
phase: 'not-in-sync',
});
triggerSync(db, 'push');
if (!isEagerSyncDisabled(db)) {
triggerSync(db, 'push');
}
}),
fromEvent(self, 'offline').subscribe(() => {
console.debug('offline!');
Expand Down
9 changes: 9 additions & 0 deletions addons/dexie-cloud/src/isEagerSyncDisabled.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { DexieCloudDB } from './db/DexieCloudDB';

export function isEagerSyncDisabled(db: DexieCloudDB) {
return (
db.cloud.options?.disableEagerSync ||
db.cloud.currentUser.value?.userId !== 'ok' ||
!db.cloud.options?.databaseUrl
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { guardedTable } from '../middleware-helpers/guardedTable';
import { registerSyncEvent } from '../sync/registerSyncEvent';
import { TXExpandos } from '../types/TXExpandos';
import { outstandingTransactions } from './outstandingTransaction';
import { isEagerSyncDisabled } from '../isEagerSyncDisabled';

export interface MutationTrackingMiddlewareArgs {
currentUserObservable: BehaviorSubject<UserLogin>;
Expand Down Expand Up @@ -94,15 +95,9 @@ export function createMutationTrackingMiddleware({
const txComplete = () => {
if (
tx.mutationsAdded &&
db.cloud.options?.databaseUrl &&
(db.cloud.currentUser.value.license?.status || 'ok') === 'ok'
!isEagerSyncDisabled(db)
) {
if (db.cloud.usingServiceWorker) {
console.debug('registering sync event');
registerSyncEvent(db, 'push');
} else {
db.localSyncEvent.next({ purpose: 'push' });
}
registerSyncEvent(db, 'push');
}
removeTransaction();
};
Expand Down
1 change: 1 addition & 0 deletions addons/dexie-cloud/src/sync/triggerSync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { registerSyncEvent } from "./registerSyncEvent";

export function triggerSync(db: DexieCloudDB, purpose: "push" | "pull") {
if (db.cloud.usingServiceWorker) {
console.debug('registering sync event');
registerSyncEvent(db, purpose);
} else {
db.localSyncEvent.next({purpose});
Expand Down

0 comments on commit a52b1e2

Please sign in to comment.