| 114 | } |
| 115 | |
| 116 | async call(functionName: string, args: unknown[]): Promise<unknown> { |
| 117 | if (!this.ready) { |
| 118 | throw new Error('Worker not ready'); |
| 119 | } |
| 120 | |
| 121 | if (this.busy) { |
| 122 | throw new Error('Worker is busy'); |
| 123 | } |
| 124 | |
| 125 | this.busy = true; |
| 126 | |
| 127 | try { |
| 128 | return await Promise.race([this.executeCall(functionName, args), this.createTimeout()]); |
| 129 | } finally { |
| 130 | this.busy = false; |
| 131 | if (this.requestTimeout) { |
| 132 | clearTimeout(this.requestTimeout); |
| 133 | this.requestTimeout = null; |
| 134 | } |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | private async executeCall(functionName: string, args: unknown[]): Promise<unknown> { |
| 139 | let tempDirectory: string | undefined; |