import { IPCWorker, IPCMaster, IWorkerMessage, LocalCache } from "../internal" import { ICache, IQuery } from "." // middleware for IPCMaster with Local cache implementation on master export const IPCMasterCache = (collection: string) => { const cache: ICache = new LocalCache() return (ipcm: IPCMaster, message: IWorkerMessage, next: () => void) => { const { event, channel, data: messageData } = message if (event === "publish" && channel === "ipc-request:master" && messageData.data.collection === collection) { const { requestId, method , data } = message.data const publishResult = (result: any) => { ipcm.onPublish("master", `ipc-response:${requestId}`, { data: result }) } console.log(`Cache ${collection} middlware handle method "${method}" from worker`) switch (method) { case "set": return cache.set(data.key, data.data).then(publishResult) case "get": return cache.get(data.key).then(publishResult) case "remove": return cache.remove(data.key).then(publishResult) case "findOne": return cache.findOne(data.query).then(publishResult) case "findMany": return cache.findMany(data.query).then(publishResult) } } else { next() } } } export class IPCWorkerCache implements ICache { public collections: Map> = new Map() constructor(public workerIPCM: IPCWorker, public collection: string) {} public async set(key: string, data: T): Promise { return this.requestMaster("set", { collection: this.collection, key, data }) } public async get(key: string): Promise { return this.requestMaster("get", { collection: this.collection, key }) } public async remove(key: string): Promise { return this.requestMaster("remove", { collection: this.collection, key }) } public async findOne(query?: IQuery): Promise { return this.requestMaster("findOne", { collection: this.collection, query }) } public async findMany(query?: IQuery): Promise { return this.requestMaster("findMany", { collection: this.collection, query }) } private async requestMaster(method: string, data: any): Promise { return this.workerIPCM.requestProcess("master", method, data) } }