MCPcopy Index your code
hub / github.com/Sandpack/nodebox-runtime / waitFor

Method waitFor

packages/nodebox/src/modules/preview.ts:26–75  ·  view source on GitHub ↗
(
    payload: { sourceShellId?: string; port?: number },
    predicate: (data: PreviewInfo) => boolean,
    timeout = TIMEOUT
  )

Source from the content-addressed store, hash-verified

24 constructor(private readonly channel: MessageSender) {}
25
26 private async waitFor(
27 payload: { sourceShellId?: string; port?: number },
28 predicate: (data: PreviewInfo) => boolean,
29 timeout = TIMEOUT
30 ): Promise<PreviewInfo> {
31 const readyPromise = new DeferredPromise<PreviewInfo>();
32
33 const rejectTimeout = setTimeout(() => {
34 readyPromise.reject();
35 }, timeout);
36
37 // Look up for the informaton on PreviewManager
38 const previewInformation = await this.channel.send('preview/get/info', payload).catch((error) => {
39 readyPromise.reject(
40 new Error(
41 format(
42 'Failed to look up preview information for shell ID "%s" (port: %d)',
43 payload.sourceShellId,
44 payload.port
45 )
46 )
47 );
48 });
49
50 const foundPreview = previewInformation && predicate(previewInformation);
51
52 if (foundPreview) {
53 readyPromise.resolve({
54 url: previewInformation.url,
55 port: previewInformation.port,
56 sourceShellId: previewInformation.sourceShellId,
57 });
58 }
59
60 // Response from PreviewManager
61 this.channel.on('preview/port/ready', ({ data }) => {
62 // Avoid resolve the promise once again
63 if (!foundPreview && predicate(data)) {
64 readyPromise.resolve({
65 url: data.url,
66 port: data.port,
67 sourceShellId: data.sourceShellId,
68 });
69 }
70 });
71
72 return readyPromise.finally(() => {
73 clearTimeout(rejectTimeout);
74 });
75 }
76
77 public async getByShellId(sourceShellId: string, timeout?: number): Promise<PreviewInfo> {
78 return this.waitFor({ sourceShellId }, (data) => data.sourceShellId === sourceShellId, timeout).catch((error) => {

Callers 5

getByShellIdMethod · 0.95
waitForPortMethod · 0.95
getBridgeFrameLocatorFunction · 0.80
getPreviewFrameLocatorFunction · 0.80
getPreviewFrameFunction · 0.80

Calls 2

sendMethod · 0.45
onMethod · 0.45

Tested by

no test coverage detected