Skip to content
This repository was archived by the owner on May 3, 2023. It is now read-only.

Commit 17c5577

Browse files
authored
Merge pull request #9 from PChambino/connected-errors
Add onSetupError
2 parents 022170b + e883584 commit 17c5577

File tree

1 file changed

+37
-24
lines changed

1 file changed

+37
-24
lines changed

src/index.js

Lines changed: 37 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,13 @@ const elmWebComponents = {
3636
register(
3737
name,
3838
ElmComponent,
39-
{ setupPorts = () => {}, staticFlags = {}, onDetached = () => {}, mapFlags = flags => flags } = {}
39+
{
40+
setupPorts = () => {},
41+
staticFlags = {},
42+
onDetached = () => {},
43+
mapFlags = flags => flags,
44+
onSetupError = () => {}
45+
} = {}
4046
) {
4147
if (!this.__elmVersion) {
4248
if (!hasWarnedAboutMissingElmVersion) {
@@ -56,29 +62,36 @@ const elmWebComponents = {
5662

5763
class ElmElement extends HTMLElement {
5864
connectedCallback() {
59-
let props = Object.assign({}, getProps(this), staticFlags)
60-
if (Object.keys(props).length === 0) props = undefined
61-
62-
const flags = mapFlags(props)
63-
64-
if (elmVersion === '0.19') {
65-
/* a change in Elm 0.19 means that ElmComponent.init now replaces the node you give it
66-
* whereas in 0.18 it rendered into it. To avoid Elm therefore destroying our custom element
67-
* we create a div that we let Elm render into, and manually clear any pre-rendered contents.
68-
*/
69-
const elmDiv = document.createElement('div')
70-
71-
this.innerHTML = ''
72-
this.appendChild(elmDiv)
73-
74-
const elmElement = ElmComponent.init({
75-
flags,
76-
node: elmDiv,
77-
})
78-
setupPorts(elmElement.ports)
79-
} else if (elmVersion === '0.18') {
80-
const elmElement = ElmComponent.embed(this, flags)
81-
setupPorts(elmElement.ports)
65+
const context = {};
66+
try {
67+
let props = Object.assign({}, getProps(this), staticFlags)
68+
if (Object.keys(props).length === 0) props = undefined
69+
70+
const flags = mapFlags(props)
71+
context.flags = flags;
72+
73+
if (elmVersion === '0.19') {
74+
/* a change in Elm 0.19 means that ElmComponent.init now replaces the node you give it
75+
* whereas in 0.18 it rendered into it. To avoid Elm therefore destroying our custom element
76+
* we create a div that we let Elm render into, and manually clear any pre-rendered contents.
77+
*/
78+
const elmDiv = document.createElement('div')
79+
80+
this.innerHTML = ''
81+
this.appendChild(elmDiv)
82+
83+
const elmElement = ElmComponent.init({
84+
flags,
85+
node: elmDiv,
86+
})
87+
setupPorts(elmElement.ports)
88+
} else if (elmVersion === '0.18') {
89+
const elmElement = ElmComponent.embed(this, flags)
90+
setupPorts(elmElement.ports)
91+
}
92+
} catch (error) {
93+
console.error(error)
94+
onSetupError(error, context)
8295
}
8396
}
8497

0 commit comments

Comments
 (0)