Huakun Shen
Huakun Shen

Reputation: 41

How to prevent comlink auto `relaseProxy`?

I am using comlink to communicate between a web page and an embedded iframe. The iframe calls functions exposed from the main page. Everything works fine when iframe web page is running on dev server (vite), but stops working in production build.

In production build, the API call to main page works for the first ~25ms then stops working (this is not a stable behaviour, sometimes longer). After inspecting all messages from iframe I found a message with type RELEASE.

// event.data:
{id: "cab182ad02316-c2df13233da1c-1094e4796c86f9-a927d7c70548e", type: "RELEASE"}

Then I found this in comlink's docs.

If the browser supports the WeakRef proposal, releaseProxy will be called automatically when the proxy created by wrap() gets garbage collected.

Is there a way to prevent auto release? The iframe page is like an extension, it needs to call APIs exposed from main page throughout its lifetime.

I thought about garbage collector. I am constructing API object on iframe side like this. Still got auto released.

const api = wrap<{
    readText(): Promise<string>
}>(windowEndpoint(window.parent))

Upvotes: 0

Views: 30

Answers (0)

Related Questions