Skip to content

Commit

Permalink
ssr mode fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
stevensJourney committed Sep 9, 2024
1 parent f202944 commit 64c1840
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 15 deletions.
6 changes: 6 additions & 0 deletions .changeset/four-birds-raise.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@powersync/common': minor
'@powersync/web': minor
---

Fixed SSR Mode detection for DB adapters. Removed the potential for SSR Web Streamining implementations from to perform syncing operations.
3 changes: 1 addition & 2 deletions packages/common/src/client/AbstractPowerSyncDatabase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import { CrudBatch } from './sync/bucket/CrudBatch';
import { CrudEntry, CrudEntryJSON } from './sync/bucket/CrudEntry';
import { CrudTransaction } from './sync/bucket/CrudTransaction';
import {
AbstractStreamingSyncImplementation,
DEFAULT_CRUD_UPLOAD_THROTTLE_MS,
PowerSyncConnectionOptions,
StreamingSyncImplementation,
Expand Down Expand Up @@ -239,7 +238,7 @@ export abstract class AbstractPowerSyncDatabase extends BaseObserver<PowerSyncDB

protected abstract generateSyncStreamImplementation(
connector: PowerSyncBackendConnector
): AbstractStreamingSyncImplementation;
): StreamingSyncImplementation;

protected abstract generateBucketStorageAdapter(): BucketStorageAdapter;

Expand Down
8 changes: 3 additions & 5 deletions packages/web/src/db/PowerSyncDatabase.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import {
type AbstractStreamingSyncImplementation,
type BucketStorageAdapter,
type PowerSyncBackendConnector,
type PowerSyncCloseOptions,
Expand All @@ -11,7 +10,8 @@ import {
PowerSyncDatabaseOptionsWithDBAdapter,
PowerSyncDatabaseOptionsWithOpenFactory,
PowerSyncDatabaseOptionsWithSettings,
SqliteBucketStorage
SqliteBucketStorage,
StreamingSyncImplementation
} from '@powersync/common';
import { Mutex } from 'async-mutex';
import { WASQLiteOpenFactory } from './adapters/wa-sqlite/WASQLiteOpenFactory';
Expand Down Expand Up @@ -138,9 +138,7 @@ export class PowerSyncDatabase extends AbstractPowerSyncDatabase {
return navigator.locks.request(`lock-${this.database.name}`, cb);
}

protected generateSyncStreamImplementation(
connector: PowerSyncBackendConnector
): AbstractStreamingSyncImplementation {
protected generateSyncStreamImplementation(connector: PowerSyncBackendConnector): StreamingSyncImplementation {
const remote = new WebRemote(connector);

const syncOptions: WebStreamingSyncImplementationOptions = {
Expand Down
10 changes: 6 additions & 4 deletions packages/web/src/db/adapters/AbstractWebSQLOpenFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ export abstract class AbstractWebSQLOpenFactory implements SQLOpenFactory {
* A SSR implementation is loaded if SSR mode is detected.
*/
openDB() {
const isSSR = isServerSide();
if (isSSR && !this.resolvedFlags.disableSSRWarning) {
const {
resolvedFlags: { disableSSRWarning, enableMultiTabs, ssrMode = isServerSide() }
} = this;
if (ssrMode && !disableSSRWarning) {
console.warn(
`
Running PowerSync in SSR mode.
Expand All @@ -29,13 +31,13 @@ export abstract class AbstractWebSQLOpenFactory implements SQLOpenFactory {
);
}

if (!this.resolvedFlags.enableMultiTabs) {
if (!enableMultiTabs) {
console.warn(
'Multiple tab support is not enabled. Using this site across multiple tabs may not function correctly.'
);
}

if (isSSR) {
if (ssrMode) {
return new SSRDBAdapter();
}

Expand Down
61 changes: 57 additions & 4 deletions packages/web/src/db/sync/SSRWebStreamingSyncImplementation.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,76 @@
import {
AbstractStreamingSyncImplementation,
AbstractStreamingSyncImplementationOptions,
BaseObserver,
LockOptions,
LockType
LockType,
PowerSyncConnectionOptions,
StreamingSyncImplementation,
SyncStatus,
SyncStatusOptions
} from '@powersync/common';
import { Mutex } from 'async-mutex';

export class SSRStreamingSyncImplementation extends AbstractStreamingSyncImplementation {
export class SSRStreamingSyncImplementation extends BaseObserver implements StreamingSyncImplementation {
syncMutex: Mutex;
crudMutex: Mutex;

isConnected: boolean;
lastSyncedAt?: Date | undefined;
syncStatus: SyncStatus;

constructor(options: AbstractStreamingSyncImplementationOptions) {
super(options);
super();
this.syncMutex = new Mutex();
this.crudMutex = new Mutex();
this.syncStatus = new SyncStatus({});
this.isConnected = false;
}

obtainLock<T>(lockOptions: LockOptions<T>): Promise<T> {
const mutex = lockOptions.type == LockType.CRUD ? this.crudMutex : this.syncMutex;
return mutex.runExclusive(lockOptions.callback);
}

/**
* This is a no-op in SSR mode
*/
async connect(options?: PowerSyncConnectionOptions): Promise<void> {}

async dispose() {}

/**
* This is a no-op in SSR mode
*/
async disconnect(): Promise<void> {}

/**
* This SSR Mode implementation is immediately ready.
*/
async waitForReady() {}

/**
* This will never resolve in SSR Mode.
*/
async waitForStatus(status: SyncStatusOptions) {
return new Promise<void>((r) => {});
}

/**
* Returns a placeholder checkpoint. This should not be used.
*/
async getWriteCheckpoint() {
return '1';
}

/**
* The SSR mode adapter will never complete syncing.
*/
async hasCompletedSync() {
return false;
}

/**
* This is a no-op in SSR mode.
*/
triggerCrudUpload() {}
}

0 comments on commit 64c1840

Please sign in to comment.