-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1f87766
commit 2b3b44d
Showing
12 changed files
with
199 additions
and
226 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { DBAdapter } from '@powersync/common'; | ||
|
||
export type SharedConnectionWorker = { | ||
identifier: string; | ||
port: MessagePort; | ||
}; | ||
|
||
export interface WebDBAdapter extends DBAdapter { | ||
/** | ||
* Get a MessagePort which can be used to share the internals of this connection. | ||
*/ | ||
shareConnection(): Promise<SharedConnectionWorker>; | ||
} |
This file was deleted.
Oops, something went wrong.
47 changes: 0 additions & 47 deletions
47
packages/web/src/db/adapters/WorkerLockedAsyncDatabaseAdapter.ts
This file was deleted.
Oops, something went wrong.
69 changes: 69 additions & 0 deletions
69
packages/web/src/db/adapters/WorkerWrappedAsyncDatabaseConnection.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import * as Comlink from 'comlink'; | ||
import { AsyncDatabaseConnection, OnTableChangeCallback, ProxiedQueryResult } from './AsyncDatabaseConnection'; | ||
|
||
export type SharedConnectionWorker = { | ||
identifier: string; | ||
port: MessagePort; | ||
}; | ||
|
||
export type WrappedWorkerConnectionOptions = { | ||
baseConnection: AsyncDatabaseConnection; | ||
identifier: string; | ||
worker: Worker | MessagePort; | ||
}; | ||
|
||
/** | ||
* Wraps a provided instance of {@link AsyncDatabaseConnection}, providing necessary proxy | ||
* functions for worker listeners. | ||
*/ | ||
export class WorkerWrappedAsyncDatabaseConnection implements AsyncDatabaseConnection { | ||
constructor(protected options: WrappedWorkerConnectionOptions) {} | ||
|
||
protected get baseConnection() { | ||
return this.options.baseConnection; | ||
} | ||
|
||
init(): Promise<void> { | ||
return this.baseConnection.init(); | ||
} | ||
|
||
/** | ||
* Get a MessagePort which can be used to share the internals of this connection. | ||
*/ | ||
async shareConnection(): Promise<SharedConnectionWorker> { | ||
const { identifier, worker } = this.options; | ||
if (worker instanceof Worker) { | ||
// We can't transfer a Worker instance, need a MessagePort | ||
// Comlink provides a nice utility for exposing a MessagePort | ||
// from a Worker | ||
const temp = Comlink.wrap(worker); | ||
const newPort = await temp[Comlink.createEndpoint](); | ||
return { port: newPort, identifier }; | ||
} | ||
|
||
return { | ||
identifier: identifier, | ||
port: worker | ||
}; | ||
} | ||
|
||
/** | ||
* Registers a table change notification callback with the base database. | ||
* This can be extended by custom implementations in order to handle proxy events. | ||
*/ | ||
async registerOnTableChange(callback: OnTableChangeCallback) { | ||
return this.baseConnection.registerOnTableChange(Comlink.proxy(callback)); | ||
} | ||
|
||
close(): Promise<void> { | ||
return this.baseConnection.close(); | ||
} | ||
|
||
execute(sql: string, params?: any[]): Promise<ProxiedQueryResult> { | ||
return this.baseConnection.execute(sql, params); | ||
} | ||
|
||
executeBatch(sql: string, params?: any[]): Promise<ProxiedQueryResult> { | ||
return this.baseConnection.executeBatch(sql, params); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.