Skip to content

Commit 4011d11

Browse files
committed
Move fetchResults out of the prototype
1 parent 26e4705 commit 4011d11

File tree

1 file changed

+29
-29
lines changed

1 file changed

+29
-29
lines changed

index.js

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class RemoteInputElement extends HTMLElement {
1111

1212
attributeChangedCallback(name: string, oldValue: string) {
1313
if (oldValue && name === 'src') {
14-
this.fetchResults(false)
14+
fetchResults(this, false)
1515
}
1616
}
1717

@@ -22,8 +22,8 @@ class RemoteInputElement extends HTMLElement {
2222
input.setAttribute('autocomplete', 'off')
2323
input.setAttribute('spellcheck', 'false')
2424

25-
this.debounceInputChange = debounce(this.fetchResults.bind(this))
26-
this.boundFetchResults = this.fetchResults.bind(this)
25+
this.debounceInputChange = debounce(() => fetchResults(this))
26+
this.boundFetchResults = () => fetchResults(this)
2727
input.addEventListener('focus', this.boundFetchResults)
2828
input.addEventListener('change', this.boundFetchResults)
2929
input.addEventListener('input', this.debounceInputChange)
@@ -62,34 +62,34 @@ class RemoteInputElement extends HTMLElement {
6262
set name(name: string) {
6363
this.setAttribute('name', name)
6464
}
65+
}
6566

66-
async fetchResults(checkCurrentQuery: boolean = true) {
67-
if (!this.input) return
68-
const query = this.input.value
69-
if (checkCurrentQuery && this.currentQuery === query) return
70-
this.currentQuery = query
71-
const src = this.src
72-
if (!src) return
73-
const resultsContainer = this.resultsContainer
74-
if (!resultsContainer) return
75-
76-
const url = new URL(src, window.location.origin)
77-
const params = new URLSearchParams(url.search)
78-
params.append(this.name, query)
79-
url.search = params.toString()
80-
81-
this.dispatchEvent(new CustomEvent('loadstart'))
82-
this.setAttribute('loading', '')
83-
try {
84-
const html = await fetch(url).then(data => data.text())
85-
this.dispatchEvent(new CustomEvent('load'))
86-
resultsContainer.innerHTML = html
87-
} catch {
88-
this.dispatchEvent(new CustomEvent('error'))
89-
}
90-
this.removeAttribute('loading')
91-
this.dispatchEvent(new CustomEvent('loadend'))
67+
async function fetchResults(remoteInput: RemoteInputElement, checkCurrentQuery: boolean = true) {
68+
if (!remoteInput.input) return
69+
const query = remoteInput.input.value
70+
if (checkCurrentQuery && remoteInput.currentQuery === query) return
71+
remoteInput.currentQuery = query
72+
const src = remoteInput.src
73+
if (!src) return
74+
const resultsContainer = remoteInput.resultsContainer
75+
if (!resultsContainer) return
76+
77+
const url = new URL(src, window.location.origin)
78+
const params = new URLSearchParams(url.search)
79+
params.append(remoteInput.name, query)
80+
url.search = params.toString()
81+
82+
remoteInput.dispatchEvent(new CustomEvent('loadstart'))
83+
remoteInput.setAttribute('loading', '')
84+
try {
85+
const html = await fetch(url).then(data => data.text())
86+
remoteInput.dispatchEvent(new CustomEvent('load'))
87+
resultsContainer.innerHTML = html
88+
} catch {
89+
remoteInput.dispatchEvent(new CustomEvent('error'))
9290
}
91+
remoteInput.removeAttribute('loading')
92+
remoteInput.dispatchEvent(new CustomEvent('loadend'))
9393
}
9494

9595
function debounce(callback) {

0 commit comments

Comments
 (0)