-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(doc-storage): operation pattern
- Loading branch information
Showing
33 changed files
with
1,293 additions
and
783 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 was deleted.
Oops, something went wrong.
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,79 @@ | ||
import { | ||
ConnectOp, | ||
DisconnectOp, | ||
OpConsumer, | ||
type OpHandler, | ||
type OpSubscribableHandler, | ||
SubscribeConnectionStatusOp, | ||
} from '../op'; | ||
import type { Storage } from '../storage'; | ||
|
||
export class PeerStorageBackend extends OpConsumer { | ||
private storages: Storage[] = []; | ||
private readonly storageOpts: { impl: string; opts: any }[] = []; | ||
|
||
constructor(port: MessagePort) { | ||
super(port); | ||
this.register(ConnectOp, this.connect); | ||
this.register(DisconnectOp, this.disconnect); | ||
this.registerSubscribable( | ||
SubscribeConnectionStatusOp, | ||
this.onStatusChanged | ||
); | ||
} | ||
|
||
addBackendStorage<T extends new (opts: any) => Storage>( | ||
impl: T, | ||
opts: ConstructorParameters<T>[0] | ||
) { | ||
this.storageOpts.push({ impl: impl.name, opts }); | ||
} | ||
|
||
connect: OpHandler<ConnectOp> = async () => { | ||
await Promise.all( | ||
this.storageOpts.map(async _impl => { | ||
// const storage = new StorageImplementations[impl.impl](impl.opts); | ||
// await storage.connect(); | ||
// storage.register(this.port); | ||
// this.storages[impl.type] = storage; | ||
}) | ||
); | ||
}; | ||
|
||
disconnect: OpHandler<DisconnectOp> = async () => { | ||
await Promise.all( | ||
Object.values(this.storages).map(async storage => { | ||
await storage.disconnect(); | ||
}) | ||
); | ||
this.storages = []; | ||
}; | ||
|
||
onStatusChanged: OpSubscribableHandler<SubscribeConnectionStatusOp> = ( | ||
_, | ||
callback | ||
) => { | ||
callback(/* { status: 'connected' } */); | ||
|
||
return () => {}; | ||
}; | ||
} | ||
|
||
export class PeerWorkerStorageBackend extends PeerStorageBackend { | ||
// override async connect() { | ||
// const worker = await getAndInitWorkerInSomewhere(); | ||
// the worker should proxy all 'op' messages to it's true backend | ||
// worker.postMessage( | ||
// { | ||
// type: 'create-storage-worker-backend', | ||
// storages: this.opts.storages, | ||
// port: this.port, | ||
// }, | ||
// [ | ||
// // transfer ownership of consumer port to worker, | ||
// // this port is no longer usable in main thread | ||
// this.port, | ||
// ] | ||
// ); | ||
// } | ||
} |
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,55 @@ | ||
import { | ||
ConnectOp, | ||
DisconnectOp, | ||
OpProducer, | ||
SubscribeConnectionStatusOp, | ||
} from '../op'; | ||
import { PeerStorageBackend, PeerWorkerStorageBackend } from './backend'; | ||
|
||
class PeerStorageClient extends OpProducer { | ||
constructor( | ||
port: MessagePort, | ||
protected readonly backend: PeerStorageBackend | ||
) { | ||
super(port); | ||
} | ||
|
||
addStorage = this.backend.addBackendStorage.bind(this.backend); | ||
|
||
async connect() { | ||
this.listen(); | ||
await this.send(new ConnectOp()); | ||
} | ||
|
||
async disconnect() { | ||
this.close(); | ||
await this.send(new DisconnectOp()); | ||
} | ||
|
||
onConnectionStatusChanged() { | ||
this.subscribe(new SubscribeConnectionStatusOp(), (/* storage */) => {}); | ||
} | ||
} | ||
|
||
export function createPeerStorageClient() { | ||
const channel = new MessageChannel(); | ||
const producerPort = channel.port1; | ||
const consumerPort = channel.port2; | ||
|
||
const backend = new PeerStorageBackend(consumerPort); | ||
|
||
const client = new PeerStorageClient(producerPort, backend); | ||
|
||
return client; | ||
} | ||
|
||
export function createPeerWorkerStorageClient() { | ||
const channel = new MessageChannel(); | ||
const producerPort = channel.port1; | ||
const consumerPort = channel.port2; | ||
|
||
const backend = new PeerWorkerStorageBackend(consumerPort); | ||
|
||
const client = new PeerStorageClient(producerPort, backend); | ||
return client; | ||
} |
Oops, something went wrong.