Skip to content

Commit 1d64a8e

Browse files
committed
refactor(anywidget): Clean up HMR internals
1 parent 3bb23e3 commit 1d64a8e

File tree

1 file changed

+16
-20
lines changed

1 file changed

+16
-20
lines changed

packages/anywidget/src/widget.js

+16-20
Original file line numberDiff line numberDiff line change
@@ -324,10 +324,10 @@ class Runtime {
324324
/** @type {import('solid-js').Resource<Result<AnyWidget & { url: string }>>} */
325325
// @ts-expect-error - Set synchronously in constructor.
326326
#widget_result;
327-
/** @type {Promise<void>} */
328-
ready;
329327
/** @type {AbortSignal} */
330328
#signal;
329+
/** @type {Promise<void>} */
330+
ready;
331331

332332
/**
333333
* @param {base.DOMWidgetModel} model
@@ -361,9 +361,9 @@ class Runtime {
361361
});
362362
/** @type {void | (() => Awaitable<void>)} */
363363
let cleanup;
364-
this.#widget_result = solid.createResource(esm, async (update) => {
365-
await safe_cleanup(cleanup, "initialize");
364+
[this.#widget_result] = solid.createResource(esm, async (update) => {
366365
try {
366+
await safe_cleanup(cleanup, "esm update");
367367
model.off(null, null, INITIALIZE_MARKER);
368368
let widget = await load_widget(update, model.get("_anywidget_id"));
369369
resolvers.resolve();
@@ -378,9 +378,9 @@ class Runtime {
378378
} catch (e) {
379379
return error(e);
380380
}
381-
})[0];
382-
return () => {
383-
cleanup?.();
381+
});
382+
return async () => {
383+
await safe_cleanup(cleanup, "esm update");
384384
model.off("change:_css");
385385
model.off("change:_esm");
386386
dispose();
@@ -401,10 +401,10 @@ class Runtime {
401401
let dispose = solid.createRoot((dispose) => {
402402
/** @type {void | (() => Awaitable<void>)} */
403403
let cleanup;
404-
let resource = solid.createResource(
404+
let [resource] = solid.createResource(
405405
this.#widget_result,
406406
async (widget_result) => {
407-
cleanup?.();
407+
await safe_cleanup(cleanup, "dispose view");
408408
// Clear all previous event listeners from this hook.
409409
model.off(null, null, view);
410410
view.$el.empty();
@@ -425,15 +425,15 @@ class Runtime {
425425
throw_anywidget_error(e);
426426
}
427427
},
428-
)[0];
428+
);
429429
solid.createEffect(() => {
430430
if (resource.error) {
431431
// TODO: Show error in the view?
432432
}
433433
});
434-
return () => {
434+
return async () => {
435+
await safe_cleanup(cleanup, "dispose view");
435436
dispose();
436-
cleanup?.();
437437
};
438438
});
439439
}
@@ -463,20 +463,16 @@ export default function ({ DOMWidgetModel, DOMWidgetView }) {
463463
initialize(...args) {
464464
super.initialize(...args);
465465
let controller = new AbortController();
466-
let runtime = new Runtime(this, { signal: controller.signal });
467466
this.once("destroy", () => {
468-
try {
469-
controller.abort("[anywidget] Runtime destroyed.");
470-
} finally {
471-
RUNTIMES.delete(this);
472-
}
467+
controller.abort("[anywidget] Runtime destroyed.");
468+
RUNTIMES.delete(this);
473469
});
474-
RUNTIMES.set(this, runtime);
470+
RUNTIMES.set(this, new Runtime(this, { signal: controller.signal }));
475471
}
476472

477473
/** @param {Parameters<InstanceType<DOMWidgetModel>["_handle_comm_msg"]>} msg */
478474
async _handle_comm_msg(...msg) {
479-
const runtime = RUNTIMES.get(this);
475+
let runtime = RUNTIMES.get(this);
480476
await runtime?.ready;
481477
return super._handle_comm_msg(...msg);
482478
}

0 commit comments

Comments
 (0)