| 85 | connect({ onMessage, onDisconnection }) { |
| 86 | return transport.connect!({ |
| 87 | onMessage(payload) { |
| 88 | if (payload.type === 'custom' && payload.event === 'vite:invoke') { |
| 89 | const data = payload.data as InvokeResponseData |
| 90 | if (data.id.startsWith('response:')) { |
| 91 | const invokeId = data.id.slice('response:'.length) |
| 92 | const promise = rpcPromises.get(invokeId) |
| 93 | if (!promise) return |
| 94 | |
| 95 | if (promise.timeoutId) clearTimeout(promise.timeoutId) |
| 96 | |
| 97 | rpcPromises.delete(invokeId) |
| 98 | |
| 99 | const { error, result } = data.data |
| 100 | if (error) { |
| 101 | promise.reject(error) |
| 102 | } else { |
| 103 | promise.resolve(result) |
| 104 | } |
| 105 | return |
| 106 | } |
| 107 | } |
| 108 | onMessage(payload) |
| 109 | }, |
| 110 | onDisconnection, |
| 111 | }) |
| 112 | }, |