diff --git a/package.json b/package.json index 8a2a725..006bf31 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "vuexok", - "version": "0.0.9", + "version": "0.0.10", "description": "", "main": "dist/vuexok.js", "types": "dist/vuexok.d.ts", diff --git a/src/vuexokWorkerGetActions.ts b/src/vuexokWorkerGetActions.ts index c215873..cd99630 100644 --- a/src/vuexokWorkerGetActions.ts +++ b/src/vuexokWorkerGetActions.ts @@ -1,33 +1,54 @@ import type { Module } from 'vuex' import type { ModuleInstance } from './vuexok' +import type { + VuexokActionRejectEvent, + VuexokActionResolveEvent, +} from './vuexokWorkerWrapper' -const ctx: Worker = self as any +export type VuexokActionCallEvent = { + type: 'vuexok:action', + path: string, + action: string, + payload: any, + vuexokCallbackId: string, +} export type WorkerModuleActions< M extends ModuleInstance> > = M['actions'] +const ctx: Worker = self as any + export const vuexokWorkerGetActions = < M extends ModuleInstance> >(path:string):WorkerModuleActions => { return new Proxy( {}, { - get: (target, action) => { + get: (target, action:string) => { return (payload:any) => new Promise((resolve, reject) => { - const callbackId = Math.random().toString(32) + const vuexokCallbackId = Math.random().toString(32) const removeListener = () => { ctx.removeEventListener('message', callbackListener) } - const callbackListener = (event:MessageEvent) => { - switch (event.data.type) { + const callbackListener = ( + event:MessageEvent< + VuexokActionResolveEvent|VuexokActionRejectEvent + > + ) => { + const { data } = event + if (vuexokCallbackId !== data.vuexokCallbackId) { + return + } + + switch (data.type) { case 'vuexok:action:resolve': removeListener() - resolve(event.data.result) + resolve(data.result) break; case 'vuexok:action:reject': removeListener() - reject(new Error(event.data.errorMessage)) + reject(new Error(data.errorMessage)) break; } } @@ -37,15 +58,17 @@ export const vuexokWorkerGetActions = < callbackListener, ) - ctx.postMessage({ + const eventData:VuexokActionCallEvent = { type: 'vuexok:action', path, action, payload, - callbackId, - }) + vuexokCallbackId, + } + + ctx.postMessage(eventData) }) - } + }, }, ) } diff --git a/src/vuexokWorkerWrapper.ts b/src/vuexokWorkerWrapper.ts index 1916418..e05218b 100644 --- a/src/vuexokWorkerWrapper.ts +++ b/src/vuexokWorkerWrapper.ts @@ -1,10 +1,25 @@ import { modules } from './vuexok' -import { ActionHandler } from './vuexok' +import type { ActionHandler } from './vuexok' +import type { VuexokActionCallEvent } from './vuexokWorkerGetActions' + +export type VuexokActionResolveEvent = { + type: 'vuexok:action:resolve', + result: any, + vuexokCallbackId: string, +} + +export type VuexokActionRejectEvent = { + type: 'vuexok:action:reject', + errorMessage: string, + vuexokCallbackId: string, +} export const vuexokWorkerWrapper = (worker:Worker) => { - const vuexokWorkerEventHandler = async (event:MessageEvent) => { + const vuexokWorkerEventHandler = async ( + event:MessageEvent + ) => { if (event.data.type === 'vuexok:action') { - const { path, action, payload, callbackId } = event.data + const { path, action, payload, vuexokCallbackId } = event.data try { const module = modules.get(path) @@ -22,22 +37,25 @@ export const vuexokWorkerWrapper = (worker:Worker) => { } const result = await moduleAction(payload) - - worker.postMessage({ + const eventData:VuexokActionResolveEvent = { type: 'vuexok:action:resolve', result, - callbackId, - }) + vuexokCallbackId, + } + + worker.postMessage(eventData) } catch (error) { - worker.postMessage({ + const eventData:VuexokActionRejectEvent = { type: 'vuexok:action:reject', errorMessage: `[Vuexok/vuexokWorkerWrapper/${path}/${action}]: ${ error.messgae ?? ( typeof error === 'string' ? error : 'Unknow error;' ) }`, - callbackId, - }) + vuexokCallbackId, + } + + worker.postMessage(eventData) } } }