(options: WorkerChannelOptions)
| 126 | #controller: AbortController |
| 127 | |
| 128 | constructor(options: WorkerChannelOptions) { |
| 129 | super() |
| 130 | |
| 131 | invariant( |
| 132 | SUPPORTS_SERVICE_WORKER, |
| 133 | 'Failed to open a WorkerChannel: Service Worker is not supported in this environment.', |
| 134 | ) |
| 135 | |
| 136 | this.#getWorker = options.getWorker |
| 137 | this.#controller = new AbortController() |
| 138 | |
| 139 | navigator.serviceWorker.addEventListener( |
| 140 | 'message', |
| 141 | async (event) => { |
| 142 | const worker = await this.#getWorker() |
| 143 | |
| 144 | if (event.source != null && event.source !== worker) { |
| 145 | return |
| 146 | } |
| 147 | |
| 148 | if (event.data && isObject(event.data) && 'type' in event.data) { |
| 149 | this.emit(new WorkerEvent<any, any, any>(event)) |
| 150 | } |
| 151 | }, |
| 152 | { |
| 153 | signal: this.#controller.signal, |
| 154 | }, |
| 155 | ) |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * Send data to the Service Worker controlling this client. |
nothing calls this directly
no test coverage detected