46 lines
1.2 KiB
TypeScript
46 lines
1.2 KiB
TypeScript
|
import { TypedRequest } from '@apiglobal/typedrequest';
|
||
|
import * as plugins from './dees-comms.plugins';
|
||
|
|
||
|
/**
|
||
|
* a comm class for client side communication between workers and tabs.
|
||
|
*/
|
||
|
export class DeesComms {
|
||
|
// sending messages
|
||
|
private postChannel = new BroadcastChannel('dees-comms');
|
||
|
private typedrouter = new plugins.typedrequest.TypedRouter();
|
||
|
|
||
|
private subscriptionChannel = new BroadcastChannel('dees-comms');
|
||
|
|
||
|
|
||
|
constructor() {
|
||
|
this.subscriptionChannel.onmessage = (eventArg) => {
|
||
|
const message = eventArg.data;
|
||
|
this.typedrouter.routeAndAddResponse(message);
|
||
|
};
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* creates a typedrequest with this classes postMessage as postMethod
|
||
|
*/
|
||
|
public createTypedRequest<T extends plugins.typedrequestInterfaces.ITypedRequest>(
|
||
|
methodName: T['method']
|
||
|
): TypedRequest<T> {
|
||
|
const typedrequest = new plugins.typedrequest.TypedRequest(this.postMessage, methodName, this.typedrouter);
|
||
|
return typedrequest;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* posts a typedrequestmessage
|
||
|
*/
|
||
|
public async postMessage<T = plugins.typedrequestInterfaces.ITypedRequest>(
|
||
|
messageArg: T
|
||
|
): Promise<void> {
|
||
|
this.postChannel.postMessage(messageArg);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* subscribe to messages
|
||
|
*/
|
||
|
public async subscribe() {}
|
||
|
}
|