Skip to content

Commit b96cf35

Browse files
iisakkirotkomanzt
andauthored
fix: comm messages were lost before model was loaded (#804)
* fix: comm messages were lost before model was loaded * add changeset * incorporate suggestions from code review * formatting --------- Co-authored-by: Trevor Manz <[email protected]>
1 parent ce7f298 commit b96cf35

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

.changeset/happy-windows-refuse.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"anywidget": patch
3+
---
4+
5+
Comm messages sent quickly after the creation of a widget could be lost because custom widgets' models were loaded asynchronously

packages/anywidget/src/widget.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,9 +309,14 @@ class Runtime {
309309
/** @type {import('solid-js').Resource<Result<AnyWidget & { url: string }>>} */
310310
// @ts-expect-error - Set synchronously in constructor.
311311
#widget_result;
312+
/** @type {Promise<void>} */
313+
ready;
312314

313315
/** @param {base.DOMWidgetModel} model */
314316
constructor(model) {
317+
/** @type {PromiseWithResolvers<void>} */
318+
const { promise, resolve } = Promise.withResolvers();
319+
this.ready = promise;
315320
this.#disposer = solid.createRoot((dispose) => {
316321
let [css, set_css] = solid.createSignal(model.get("_css"));
317322
model.on("change:_css", () => {
@@ -338,6 +343,7 @@ class Runtime {
338343
try {
339344
model.off(null, null, INITIALIZE_MARKER);
340345
let widget = await load_widget(update, model.get("_anywidget_id"));
346+
resolve();
341347
cleanup = await widget.initialize?.({
342348
model: model_proxy(model, INITIALIZE_MARKER),
343349
experimental: {
@@ -453,6 +459,13 @@ export default function ({ DOMWidgetModel, DOMWidgetView }) {
453459
RUNTIMES.set(this, runtime);
454460
}
455461

462+
/** @param {Parameters<InstanceType<DOMWidgetModel>["_handle_comm_msg"]>} msg */
463+
async _handle_comm_msg(...msg) {
464+
const runtime = RUNTIMES.get(this);
465+
await runtime?.ready;
466+
return super._handle_comm_msg(...msg);
467+
}
468+
456469
/**
457470
* @param {Record<string, any>} state
458471
*

0 commit comments

Comments
 (0)