|
77 | 77 | error.message = 'Uncaught (in promise): ' + error.message;
|
78 | 78 | push_logs({ command: 'error', args: [error] });
|
79 | 79 | },
|
| 80 | + on_iframe_reload: () => { |
| 81 | + ready = false; |
| 82 | + }, |
80 | 83 | on_console: (log) => {
|
81 | 84 | switch (log.command) {
|
82 | 85 | case 'clear':
|
|
169 | 172 | console.error(err);
|
170 | 173 | }
|
171 | 174 | }
|
| 175 | + window.__reset_custom_elements?.(); |
172 | 176 |
|
173 | 177 | document.body.innerHTML = '';
|
174 | 178 | window._svelteTransitionManager = null;
|
175 | 179 | }
|
176 | 180 |
|
| 181 | + if (!window.__reset_custom_elements) { |
| 182 | + const registered = new Map(); |
| 183 | + const define = CustomElementRegistry.prototype.define; |
| 184 | + CustomElementRegistry.prototype.define = function(name, el, options) { |
| 185 | + let ce = registered.get(name); |
| 186 | + if (ce) { |
| 187 | + if (ce.registered) { |
| 188 | + // trigger error of re-registering |
| 189 | + define.call(this, name, ce.el, options); |
| 190 | + } |
| 191 | + if (ce.options?.extends != options?.extends) { |
| 192 | + parent.postMessage({ action: 'iframe_reload' }, '*'); |
| 193 | + location.reload(); |
| 194 | + } |
| 195 | + ce.el = el; |
| 196 | + ce.registered = true; |
| 197 | + } else { |
| 198 | + ce = { el, options, registered: true }; |
| 199 | + registered.set(name, ce); |
| 200 | + const Wrapper = class extends el { |
| 201 | + connectedCallback() { |
| 202 | + ce.el.prototype.connectedCallback?.apply(this, arguments); |
| 203 | + } |
| 204 | + disconnectedCallback() { |
| 205 | + ce.el.prototype.disconnectedCallback?.apply(this, arguments); |
| 206 | + } |
| 207 | + adoptedCallback() { |
| 208 | + ce.el.prototype.adoptedCallback?.apply(this, arguments); |
| 209 | + } |
| 210 | + }; |
| 211 | + const DynamicWrapper = new Proxy(Wrapper, { |
| 212 | + construct: function (_, args, newTarget) { |
| 213 | + return Reflect.construct(ce.el, args, newTarget); |
| 214 | + } |
| 215 | + }); |
| 216 | + try { |
| 217 | + define.call(this, name, DynamicWrapper, options); |
| 218 | + } catch (error) { |
| 219 | + console.error(error); |
| 220 | + throw new Error('Failed to define a custom element '+name); |
| 221 | + } |
| 222 | + } |
| 223 | + }; |
| 224 | + window.__reset_custom_elements = () => { |
| 225 | + for (const ce of registered.values()) { |
| 226 | + if (ce.registered) { |
| 227 | + ce.el = HTMLElement; |
| 228 | + ce.registered = false; |
| 229 | + } |
| 230 | + } |
| 231 | + } |
| 232 | + } |
| 233 | +
|
177 | 234 | const __repl_exports = ${bundle.client?.code};
|
178 | 235 | {
|
179 | 236 | const { mount, unmount, App, untrack } = __repl_exports;
|
|
0 commit comments