Skip to content

Commit a465f6f

Browse files
authored
Merge pull request #895 from powersync-ja/fix-comlink-vue
Use `shallowRef` for database instance to prevent `DataCloneError` in worker communication
2 parents 4b957f0 + 0877ecb commit a465f6f

6 files changed

Lines changed: 23 additions & 13 deletions

File tree

.changeset/mighty-rabbits-sort.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@powersync/vue': patch
3+
---
4+
5+
Use shallowRef instead of ref for database instance to prevent DataCloneError in shared worker communication

.changeset/nice-clocks-smash.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@powersync/nuxt': patch
3+
---
4+
5+
Use shallowRef instead of ref for database instance to prevent DataCloneError in shared worker communication

packages/nuxt/src/runtime/utils/NuxtPowerSyncDatabase.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import { RecordingStorageAdapter } from './RecordingStorageAdapter';
1919
import type { DynamicSchemaManager } from './DynamicSchemaManager';
2020
import { usePowerSyncInspector } from '../composables/usePowerSyncInspector';
2121
import { useDiagnosticsLogger } from '../composables/useDiagnosticsLogger';
22-
import { ref, type Ref } from 'vue';
22+
import { shallowRef, type ShallowRef } from 'vue';
2323
import { useRuntimeConfig } from '#app';
2424
import { RustClientInterceptor } from './RustClientInterceptor';
2525

@@ -111,13 +111,13 @@ export class NuxtPowerSyncDatabase extends PowerSyncDatabase {
111111
const adapter =
112112
clientImplementation === SyncClientImplementation.RUST
113113
? new RustClientInterceptor(
114-
ref(this) as Ref<PowerSyncDatabase>,
114+
shallowRef(this) as ShallowRef<PowerSyncDatabase>,
115115
new WebRemote(connector, logger),
116-
ref(schemaManager) as Ref<DynamicSchemaManager>
116+
shallowRef(schemaManager) as ShallowRef<DynamicSchemaManager>
117117
)
118118
: new RecordingStorageAdapter(
119-
ref(this) as Ref<PowerSyncDatabase>,
120-
ref(schemaManager) as Ref<DynamicSchemaManager>
119+
shallowRef(this) as ShallowRef<PowerSyncDatabase>,
120+
shallowRef(schemaManager) as ShallowRef<DynamicSchemaManager>
121121
);
122122

123123
if (this.options.flags?.enableMultiTabs) {

packages/nuxt/src/runtime/utils/RecordingStorageAdapter.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
import type { Checkpoint, ColumnType, DBAdapter, PowerSyncDatabase, SyncDataBatch } from '@powersync/web';
22
import { AbstractPowerSyncDatabase, SqliteBucketStorage } from '@powersync/web';
33
import type { DynamicSchemaManager } from './DynamicSchemaManager';
4-
import type { Ref } from 'vue';
4+
import type { ShallowRef } from 'vue';
55

66
export class RecordingStorageAdapter extends SqliteBucketStorage {
77
private rdb: DBAdapter;
88
private schemaManager: DynamicSchemaManager;
99

1010
public tables: Record<string, Record<string, ColumnType>> = {};
1111

12-
constructor(db: Ref<PowerSyncDatabase>, schemaManager: Ref<DynamicSchemaManager>) {
12+
constructor(db: ShallowRef<PowerSyncDatabase>, schemaManager: ShallowRef<DynamicSchemaManager>) {
1313
super(db.value.database, (AbstractPowerSyncDatabase as any).transactionMutex);
1414
this.rdb = db.value.database;
1515
this.schemaManager = schemaManager.value;

packages/nuxt/src/runtime/utils/RustClientInterceptor.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import {
1919
SyncDataBucket
2020
} from '@powersync/web';
2121
import type { DynamicSchemaManager } from './DynamicSchemaManager';
22-
import type { Ref } from 'vue';
22+
import type { ShallowRef } from 'vue';
2323
/**
2424
* Tracks per-byte and per-operation progress for the Rust client.
2525
*
@@ -34,9 +34,9 @@ export class RustClientInterceptor extends SqliteBucketStorage {
3434
public tables: Record<string, Record<string, ColumnType>> = {};
3535

3636
constructor(
37-
db: Ref<PowerSyncDatabase>,
37+
db: ShallowRef<PowerSyncDatabase>,
3838
private remote: AbstractRemote,
39-
private schemaManager: Ref<DynamicSchemaManager>
39+
private schemaManager: ShallowRef<DynamicSchemaManager>
4040
) {
4141
super(db.value.database, (AbstractPowerSyncDatabase as any).transactionMutex);
4242
this.rdb = db.value.database;

packages/vue/src/composables/powerSync.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { AbstractPowerSyncDatabase } from '@powersync/common';
2-
import { App, MaybeRef, Ref, hasInjectionContext, inject, provide, ref } from 'vue';
2+
import { App, MaybeRef, Ref, hasInjectionContext, inject, provide, shallowRef, toRaw, toValue } from 'vue';
33
import { setupTopLevelWarningMessage } from './messages.js';
44

55
// Create a unique symbol for the PowerSync context
@@ -13,7 +13,7 @@ const POWERSYNC_KEY = Symbol('POWERSYNC_KEY');
1313
*/
1414
export function createPowerSyncPlugin(powerSyncPluginOptions: { database: MaybeRef<AbstractPowerSyncDatabase> }) {
1515
const install = (app: App) => {
16-
app.provide(POWERSYNC_KEY, ref(powerSyncPluginOptions.database));
16+
app.provide(POWERSYNC_KEY, shallowRef(toRaw(toValue(powerSyncPluginOptions.database))));
1717
};
1818
return { install };
1919
}
@@ -26,7 +26,7 @@ export function createPowerSyncPlugin(powerSyncPluginOptions: { database: MaybeR
2626
* If the key parameter is provided, the client will be provided under that key instead of the default PowerSync key.
2727
*/
2828
export function providePowerSync(database: MaybeRef<AbstractPowerSyncDatabase>, key: string | undefined = undefined) {
29-
provide(key || POWERSYNC_KEY, ref(database));
29+
provide(key || POWERSYNC_KEY, shallowRef(toRaw(toValue(database))));
3030
}
3131

3232
/**

0 commit comments

Comments
 (0)