Skip to content

Commit 5505870

Browse files
committed
Keep getData as a free floating private module function
1 parent 36daacb commit 5505870

File tree

1 file changed

+20
-20
lines changed

1 file changed

+20
-20
lines changed

include-fragment-element.js

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,27 @@ function handleData(el, data) {
2525
)
2626
}
2727

28+
function getData(el) {
29+
const src = el.src
30+
let data = privateData.get(el)
31+
if (data && data.src === src) {
32+
return data.data
33+
} else {
34+
if (src) {
35+
data = el.load()
36+
} else {
37+
data = Promise.reject(new Error('missing src'))
38+
}
39+
privateData.set(el, {src, data})
40+
return data
41+
}
42+
}
43+
2844
export class IncludeFragmentElement extends HTMLElement {
2945
constructor() {
3046
super()
3147
// Preload data cache
32-
this.getData()['catch'](function() {
48+
getData(this)['catch'](function() {
3349
// Ignore `src missing` error on pre-load.
3450
})
3551
}
@@ -57,30 +73,14 @@ export class IncludeFragmentElement extends HTMLElement {
5773
}
5874
}
5975

60-
getData() {
61-
const src = this.src
62-
let data = privateData.get(this)
63-
if (data && data.src === src) {
64-
return data.data
65-
} else {
66-
if (src) {
67-
data = this.load()
68-
} else {
69-
data = Promise.reject(new Error('missing src'))
70-
}
71-
privateData.set(this, {src, data})
72-
return data
73-
}
74-
}
75-
7676
get data() {
77-
return this.getData()
77+
return getData(this)
7878
}
7979

8080
attributeChangedCallback(attribute) {
8181
if (attribute === 'src') {
8282
// Reload data load cache.
83-
const data = this.getData()
83+
const data = getData(this)
8484

8585
// Source changed after attached so replace element.
8686
if (this._attached) {
@@ -92,7 +92,7 @@ export class IncludeFragmentElement extends HTMLElement {
9292
connectedCallback() {
9393
this._attached = true
9494
if (this.src) {
95-
handleData(this, this.getData())
95+
handleData(this, getData(this))
9696
}
9797
}
9898

0 commit comments

Comments
 (0)