MCPcopy
hub / github.com/mswjs/msw / constructor

Method constructor

src/browser/utils/workerChannel.ts:128–156  ·  view source on GitHub ↗
(options: WorkerChannelOptions)

Source from the content-addressed store, hash-verified

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.

Callers

nothing calls this directly

Calls 3

isObjectFunction · 0.90
invariantFunction · 0.85
addEventListenerMethod · 0.45

Tested by

no test coverage detected