Skip to content

fix: update defined custom elements #1400

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/repl/src/lib/Output/ReplProxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ export default class ReplProxy {
return this.handlers.on_error(event.data);
case 'unhandledrejection':
return this.handlers.on_unhandled_rejection(event.data);
case 'iframe_reload':
return this.handlers.on_iframe_reload(event.data);
case 'console':
if (event.data.command === 'info' && event.data.args[0]?.type === '__error') {
const data = event.data.args[0];
Expand Down
57 changes: 57 additions & 0 deletions packages/repl/src/lib/Output/Viewer.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@
error.message = 'Uncaught (in promise): ' + error.message;
push_logs({ command: 'error', args: [error] });
},
on_iframe_reload: () => {
ready = false;
},
on_console: (log) => {
switch (log.command) {
case 'clear':
Expand Down Expand Up @@ -169,11 +172,65 @@
console.error(err);
}
}
window.__reset_custom_elements?.();

document.body.innerHTML = '';
window._svelteTransitionManager = null;
}

if (!window.__reset_custom_elements) {
const registered = new Map();
const define = CustomElementRegistry.prototype.define;
CustomElementRegistry.prototype.define = function(name, el, options) {
let ce = registered.get(name);
if (ce) {
if (ce.registered) {
// trigger error of re-registering
define.call(this, name, ce.el, options);
}
if (ce.options?.extends != options?.extends) {
parent.postMessage({ action: 'iframe_reload' }, '*');
location.reload();
}
ce.el = el;
ce.registered = true;
} else {
ce = { el, options, registered: true };
registered.set(name, ce);
const Wrapper = class extends el {
connectedCallback() {
ce.el.prototype.connectedCallback?.apply(this, arguments);
}
disconnectedCallback() {
ce.el.prototype.disconnectedCallback?.apply(this, arguments);
}
adoptedCallback() {
ce.el.prototype.adoptedCallback?.apply(this, arguments);
}
};
const DynamicWrapper = new Proxy(Wrapper, {
construct: function (_, args, newTarget) {
return Reflect.construct(ce.el, args, newTarget);
}
});
try {
define.call(this, name, DynamicWrapper, options);
} catch (error) {
console.error(error);
throw new Error('Failed to define a custom element '+name);
}
}
};
window.__reset_custom_elements = () => {
for (const ce of registered.values()) {
if (ce.registered) {
ce.el = HTMLElement;
ce.registered = false;
}
}
}
}

const __repl_exports = ${bundle.client?.code};
{
const { mount, unmount, App, untrack } = __repl_exports;
Expand Down
2 changes: 1 addition & 1 deletion packages/repl/src/lib/Output/proxy.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export type Handlers = Record<
'on_fetch_progress' | 'on_error' | 'on_unhandled_rejection' | 'on_console',
'on_fetch_progress' | 'on_error' | 'on_unhandled_rejection' | 'on_iframe_reload' | 'on_console',
(data: any) => void
>;