Skip to content

Commit c4eccb8

Browse files
cleanup
1 parent 2e9df4b commit c4eccb8

File tree

6 files changed

+18
-42
lines changed

6 files changed

+18
-42
lines changed

packages/common/src/client/AbstractPowerSyncDatabase.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -459,7 +459,7 @@ export abstract class AbstractPowerSyncDatabase extends BaseObserver<PowerSyncDB
459459
/**
460460
* Locking mechagnism for exclusively running critical portions of connect/disconnect operations.
461461
*/
462-
protected abstract connectExclusive(callback: () => Promise<void>): Promise<void>;
462+
protected abstract runExclusive<T>(callback: () => Promise<T>): Promise<T>;
463463

464464
protected async connectInternal() {
465465
await this.disconnectInternal();
@@ -472,7 +472,7 @@ export abstract class AbstractPowerSyncDatabase extends BaseObserver<PowerSyncDB
472472
* This is protected in an exclusive lock.
473473
* The promise tracks the creation which is used to synchronize disconnect attempts.
474474
*/
475-
this.syncStreamInitPromise = this.connectExclusive(async () => {
475+
this.syncStreamInitPromise = this.runExclusive(async () => {
476476
if (this.closed) {
477477
throw new Error('Cannot connect using a closed client');
478478
}

packages/node/src/db/PowerSyncDatabase.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,11 @@ export type NodePowerSyncConnectionOptions = PowerSyncConnectionOptions & NodeAd
5656
* ```
5757
*/
5858
export class PowerSyncDatabase extends AbstractPowerSyncDatabase {
59-
protected connectionLock: Lock;
59+
protected lock: Lock;
6060

6161
constructor(options: NodePowerSyncDatabaseOptions) {
6262
super(options);
63-
this.connectionLock = new Lock();
63+
this.lock = new Lock();
6464
}
6565

6666
async _initialize(): Promise<void> {
@@ -85,8 +85,8 @@ export class PowerSyncDatabase extends AbstractPowerSyncDatabase {
8585
return super.connect(connector, options);
8686
}
8787

88-
protected async connectExclusive(callback: () => Promise<void>): Promise<void> {
89-
await this.connectionLock.acquire(`connection-lock-${this.database.name}`, callback);
88+
protected async runExclusive<T>(callback: () => Promise<T>): Promise<T> {
89+
return await this.lock.acquire(`lock-${this.database.name}`, callback);
9090
}
9191

9292
protected generateSyncStreamImplementation(

packages/react-native/src/db/PowerSyncDatabase.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import { ReactNativeQuickSqliteOpenFactory } from './adapters/react-native-quick
2828
* ```
2929
*/
3030
export class PowerSyncDatabase extends AbstractPowerSyncDatabase {
31-
protected connectionLock = new Lock();
31+
protected lock = new Lock();
3232

3333
async _initialize(): Promise<void> {}
3434

@@ -45,8 +45,8 @@ export class PowerSyncDatabase extends AbstractPowerSyncDatabase {
4545
return new SqliteBucketStorage(this.database, AbstractPowerSyncDatabase.transactionMutex);
4646
}
4747

48-
protected async connectExclusive(callback: () => Promise<void>): Promise<void> {
49-
await this.connectionLock.acquire(`connection-lock-${this.database.name}`, callback);
48+
protected async runExclusive<T>(callback: () => Promise<T>): Promise<T> {
49+
return await this.lock.acquire(`lock-${this.database.name}`, callback);
5050
}
5151

5252
protected generateSyncStreamImplementation(

packages/web/src/db/PowerSyncDatabase.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ export class PowerSyncDatabase extends AbstractPowerSyncDatabase {
179179
return new SqliteBucketStorage(this.database, AbstractPowerSyncDatabase.transactionMutex);
180180
}
181181

182-
protected runExclusive<T>(cb: () => Promise<T>) {
182+
protected async runExclusive<T>(cb: () => Promise<T>) {
183183
if (this.resolvedFlags.ssrMode) {
184184
return PowerSyncDatabase.SHARED_MUTEX.runExclusive(cb);
185185
}

packages/web/tests/src/db/AbstractPowerSyncDatabase.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ class TestPowerSyncDatabase extends AbstractPowerSyncDatabase {
3131
return Promise.resolve();
3232
}
3333

34-
protected async connectExclusive(callback: () => Promise<void>): Promise<void> {
35-
await callback();
34+
protected async runExclusive<T>(callback: () => Promise<T>): Promise<T> {
35+
return await callback();
3636
}
3737

3838
get database() {
Lines changed: 6 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { describe, it, expect, beforeEach, vi, afterEach } from 'vitest';
2-
import { AbstractPowerSyncDatabase, PowerSyncDatabase, SyncStreamConnectionMethod } from '@powersync/web';
1+
import { createBaseLogger, PowerSyncDatabase } from '@powersync/web';
2+
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
33
import { testSchema } from '../../utils/testDb';
44

55
describe('PowerSyncDatabase', () => {
@@ -9,9 +9,10 @@ describe('PowerSyncDatabase', () => {
99
let mockSyncImplementation: any;
1010

1111
beforeEach(() => {
12+
const logger = createBaseLogger();
1213
mockLogger = {
13-
debug: vi.fn(),
14-
warn: vi.fn()
14+
debug: vi.spyOn(logger, 'debug'),
15+
warn: vi.spyOn(logger, 'warn')
1516
};
1617

1718
// Initialize with minimal required options
@@ -20,12 +21,8 @@ describe('PowerSyncDatabase', () => {
2021
database: {
2122
dbFilename: 'test.db'
2223
},
23-
logger: mockLogger
24+
logger
2425
});
25-
26-
vi.spyOn(db as any, 'runExclusive').mockImplementation((cb: any) => cb());
27-
28-
vi.spyOn(AbstractPowerSyncDatabase.prototype, 'connect').mockResolvedValue(undefined);
2926
});
3027

3128
afterEach(() => {
@@ -36,27 +33,6 @@ describe('PowerSyncDatabase', () => {
3633
it('should log debug message when attempting to connect', async () => {
3734
await db.connect(mockConnector);
3835
expect(mockLogger.debug).toHaveBeenCalledWith('Attempting to connect to PowerSync instance');
39-
expect(db['runExclusive']).toHaveBeenCalled();
40-
});
41-
42-
it('should use connect with correct options', async () => {
43-
await db.connect(mockConnector, {
44-
retryDelayMs: 1000,
45-
crudUploadThrottleMs: 2000,
46-
params: {
47-
param1: 1
48-
},
49-
connectionMethod: SyncStreamConnectionMethod.HTTP
50-
});
51-
52-
expect(AbstractPowerSyncDatabase.prototype.connect).toHaveBeenCalledWith(mockConnector, {
53-
retryDelayMs: 1000,
54-
crudUploadThrottleMs: 2000,
55-
connectionMethod: 'http',
56-
params: {
57-
param1: 1
58-
}
59-
});
6036
});
6137
});
6238
});

0 commit comments

Comments
 (0)