import { IPCWorker , IPCMaster , IWorkerMessage , LocalCache } from "../internal" ; import { ICache , IQuery } from "." ; export const IPCMasterCache = < T > ( collection ) => { const cache = new LocalCache < T > ( ) ; return ( ipcm , message , next ) => { 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 ) => { 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 < T > implements ICache < T > { public collections : Map < string , ICache < any > > = new Map ( ) ; constructor ( public workerIPCM , public collection ) { } public async set ( key , data ) { return this . requestMaster < void > ( "set" , { collection : this . collection , key , data } ) ; } public async get ( key ) { return this . requestMaster < T > ( "get" , { collection : this . collection , key } ) ; } public async remove ( key ) { return this . requestMaster < void > ( "remove" , { collection : this . collection , key } ) ; } public async findOne ( query ? ) { return this . requestMaster < T > ( "findOne" , { collection : this . collection , query } ) ; } public async findMany ( query ? ) { return this . requestMaster < T [ ] > ( "findMany" , { collection : this . collection , query } ) ; } private async requestMaster < S > ( method , data ) { return this . workerIPCM . requestProcess ( "master" , method , data ) ; } }