MCPcopy Create free account
hub / github.com/Sandpack/nodebox-runtime / constructor

Method constructor

packages/nodebox/src/messages.ts:220–246  ·  view source on GitHub ↗
(private readonly target: Window)

Source from the content-addressed store, hash-verified

218 private receiverReadyPromise: DeferredPromise<void>;
219
220 constructor(private readonly target: Window) {
221 this.emitter = new EventTarget();
222 this.channel = new MessageChannel();
223 this.receiverPort = this.channel.port1;
224
225 const receiverReadyPromise = new DeferredPromise<void>();
226 const handshakeListener = (message: MessageEvent) => {
227 if (message.data.type === 'internal/ready') {
228 debug('[message-sender]: runtime is ready');
229 receiverReadyPromise.resolve();
230 }
231 };
232 window.addEventListener('message', handshakeListener);
233 receiverReadyPromise.then(() => {
234 window.removeEventListener('message', handshakeListener);
235 });
236 this.receiverReadyPromise = receiverReadyPromise;
237
238 // Attach a message listener once.
239 this.receiverPort.onmessage = (evt) => {
240 const data = evt.data;
241 if (data.type != null) {
242 debug('[message-sender]: emitting "%s" event...', data.type, data.payload);
243 this.emitter.dispatchEvent(new MessageEvent(data.type, { data: data.payload }));
244 }
245 };
246 }
247
248 public async handshake(): Promise<void> {
249 const handshakePromise = new DeferredPromise<void>();

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected