Skip to content

Commit

Permalink
fix: fixed worker callback
Browse files Browse the repository at this point in the history
  • Loading branch information
spb-web committed Jan 21, 2021
1 parent 3de4f29 commit a06208a
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 22 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "vuexok",
"version": "0.0.9",
"version": "0.0.10",
"description": "",
"main": "dist/vuexok.js",
"types": "dist/vuexok.d.ts",
Expand Down
45 changes: 34 additions & 11 deletions src/vuexokWorkerGetActions.ts
Original file line number Diff line number Diff line change
@@ -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<Module<any, any>>
> = M['actions']

const ctx: Worker = self as any

export const vuexokWorkerGetActions = <
M extends ModuleInstance<Module<any, any>>
>(path:string):WorkerModuleActions<M> => {
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;
}
}
Expand All @@ -37,15 +58,17 @@ export const vuexokWorkerGetActions = <
callbackListener,
)

ctx.postMessage({
const eventData:VuexokActionCallEvent = {
type: 'vuexok:action',
path,
action,
payload,
callbackId,
})
vuexokCallbackId,
}

ctx.postMessage(eventData)
})
}
},
},
)
}
38 changes: 28 additions & 10 deletions src/vuexokWorkerWrapper.ts
Original file line number Diff line number Diff line change
@@ -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<VuexokActionCallEvent>
) => {
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)
Expand All @@ -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)
}
}
}
Expand Down

0 comments on commit a06208a

Please sign in to comment.