From 45374ea8651048627c7b13b176e38506aea80a24 Mon Sep 17 00:00:00 2001 From: Pedro Chambino Date: Fri, 3 May 2019 16:39:14 +0100 Subject: [PATCH 1/4] Add onSetupError --- src/index.js | 59 +++++++++++++++++++++++++++++++--------------------- 1 file changed, 35 insertions(+), 24 deletions(-) diff --git a/src/index.js b/src/index.js index bcad4ec..8741bf3 100644 --- a/src/index.js +++ b/src/index.js @@ -36,7 +36,13 @@ const elmWebComponents = { register( name, ElmComponent, - { setupPorts = () => {}, staticFlags = {}, onDetached = () => {}, mapFlags = flags => flags } = {} + { + setupPorts = () => {}, + staticFlags = {}, + onDetached = () => {}, + mapFlags = flags => flags, + onSetupError = () => {} + } = {} ) { if (!this.__elmVersion) { if (!hasWarnedAboutMissingElmVersion) { @@ -56,29 +62,34 @@ const elmWebComponents = { class ElmElement extends HTMLElement { connectedCallback() { - let props = Object.assign({}, getProps(this), staticFlags) - if (Object.keys(props).length === 0) props = undefined - - const flags = mapFlags(props) - - if (elmVersion === '0.19') { - /* a change in Elm 0.19 means that ElmComponent.init now replaces the node you give it - * whereas in 0.18 it rendered into it. To avoid Elm therefore destroying our custom element - * we create a div that we let Elm render into, and manually clear any pre-rendered contents. - */ - const elmDiv = document.createElement('div') - - this.innerHTML = '' - this.appendChild(elmDiv) - - const elmElement = ElmComponent.init({ - flags, - node: elmDiv, - }) - setupPorts(elmElement.ports) - } else if (elmVersion === '0.18') { - const elmElement = ElmComponent.embed(this, flags) - setupPorts(elmElement.ports) + try { + let props = Object.assign({}, getProps(this), staticFlags) + if (Object.keys(props).length === 0) props = undefined + + const flags = mapFlags(props) + + if (elmVersion === '0.19') { + /* a change in Elm 0.19 means that ElmComponent.init now replaces the node you give it + * whereas in 0.18 it rendered into it. To avoid Elm therefore destroying our custom element + * we create a div that we let Elm render into, and manually clear any pre-rendered contents. + */ + const elmDiv = document.createElement('div') + + this.innerHTML = '' + this.appendChild(elmDiv) + + const elmElement = ElmComponent.init({ + flags, + node: elmDiv, + }) + setupPorts(elmElement.ports) + } else if (elmVersion === '0.18') { + const elmElement = ElmComponent.embed(this, flags) + setupPorts(elmElement.ports) + } + } catch (error) { + onSetupError(error) + throw error } } From b571aaf4a39a1a5fb46f67a8de3bcfd7d32505f3 Mon Sep 17 00:00:00 2001 From: Pedro Chambino Date: Tue, 7 May 2019 14:44:18 +0100 Subject: [PATCH 2/4] Use console.error instead of re-throwing the error --- src/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/index.js b/src/index.js index 8741bf3..05582e9 100644 --- a/src/index.js +++ b/src/index.js @@ -88,8 +88,8 @@ const elmWebComponents = { setupPorts(elmElement.ports) } } catch (error) { + console.error(error) onSetupError(error) - throw error } } From ddb1b54877c5afd057d0e5213868a5c6a4b21b9e Mon Sep 17 00:00:00 2001 From: Pedro Chambino Date: Tue, 7 May 2019 15:44:44 +0100 Subject: [PATCH 3/4] Pass flags as context to the error --- src/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/index.js b/src/index.js index 05582e9..0ec8b75 100644 --- a/src/index.js +++ b/src/index.js @@ -89,7 +89,7 @@ const elmWebComponents = { } } catch (error) { console.error(error) - onSetupError(error) + onSetupError(error, flags) } } From e883584ed734964933340043555ac08fe9aa0bf9 Mon Sep 17 00:00:00 2001 From: Pedro Chambino Date: Tue, 7 May 2019 16:45:52 +0100 Subject: [PATCH 4/4] Add error context object --- src/index.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/index.js b/src/index.js index 0ec8b75..b39f6f0 100644 --- a/src/index.js +++ b/src/index.js @@ -62,11 +62,13 @@ const elmWebComponents = { class ElmElement extends HTMLElement { connectedCallback() { + const context = {}; try { let props = Object.assign({}, getProps(this), staticFlags) if (Object.keys(props).length === 0) props = undefined const flags = mapFlags(props) + context.flags = flags; if (elmVersion === '0.19') { /* a change in Elm 0.19 means that ElmComponent.init now replaces the node you give it @@ -89,7 +91,7 @@ const elmWebComponents = { } } catch (error) { console.error(error) - onSetupError(error, flags) + onSetupError(error, context) } }