(jobUID: number, fn: WorkerFunction, args: any[])
| 116 | } |
| 117 | |
| 118 | async function runFunction(jobUID: number, fn: WorkerFunction, args: any[]) { |
| 119 | let syncResult: any |
| 120 | |
| 121 | try { |
| 122 | syncResult = fn(...args) |
| 123 | } catch (error) { |
| 124 | return postJobErrorMessage(jobUID, error) |
| 125 | } |
| 126 | |
| 127 | const resultType = isObservable(syncResult) ? "observable" : "promise" |
| 128 | postJobStartMessage(jobUID, resultType) |
| 129 | |
| 130 | if (isObservable(syncResult)) { |
| 131 | const subscription = syncResult.subscribe( |
| 132 | value => postJobResultMessage(jobUID, false, serialize(value)), |
| 133 | error => { |
| 134 | postJobErrorMessage(jobUID, serialize(error) as any) |
| 135 | activeSubscriptions.delete(jobUID) |
| 136 | }, |
| 137 | () => { |
| 138 | postJobResultMessage(jobUID, true) |
| 139 | activeSubscriptions.delete(jobUID) |
| 140 | } |
| 141 | ) |
| 142 | activeSubscriptions.set(jobUID, subscription) |
| 143 | } else { |
| 144 | try { |
| 145 | const result = await syncResult |
| 146 | postJobResultMessage(jobUID, true, serialize(result)) |
| 147 | } catch (error) { |
| 148 | postJobErrorMessage(jobUID, serialize(error) as any) |
| 149 | } |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | /** |
| 154 | * Expose a function or a module (an object whose values are functions) |
no test coverage detected