1
- class RemoteInputElement extends HTMLElement {
2
- currentQuery : string | null
3
- debounceInputChange : ( event : Event ) => void
4
- boundFetchResults : ( event : Event ) => void
1
+ const states = new WeakMap ( )
5
2
3
+ class RemoteInputElement extends HTMLElement {
6
4
constructor ( ) {
7
5
super ( )
8
- this . currentQuery = null
9
- this . debounceInputChange = debounce ( ( ) => fetchResults ( this ) )
10
- this . boundFetchResults = ( ) => fetchResults ( this )
6
+ const fetch = fetchResults . bind ( null , this , true )
7
+ const state = { currentQuery : null , oninput : debounce ( fetch ) , fetch }
8
+ states . set ( this , state )
11
9
}
12
10
13
11
static get observedAttributes ( ) {
@@ -27,18 +25,24 @@ class RemoteInputElement extends HTMLElement {
27
25
input . setAttribute ( 'autocomplete' , 'off' )
28
26
input . setAttribute ( 'spellcheck' , 'false' )
29
27
30
- input . addEventListener ( 'focus' , this . boundFetchResults )
31
- input . addEventListener ( 'change' , this . boundFetchResults )
32
- input . addEventListener ( 'input' , this . debounceInputChange )
28
+ const state = states . get ( this )
29
+ if ( ! state ) return
30
+
31
+ input . addEventListener ( 'focus' , state . fetch )
32
+ input . addEventListener ( 'change' , state . fetch )
33
+ input . addEventListener ( 'input' , state . oninput )
33
34
}
34
35
35
36
disconnectedCallback ( ) {
36
37
const input = this . input
37
38
if ( ! input ) return
38
39
39
- input . removeEventListener ( 'focus' , this . boundFetchResults )
40
- input . removeEventListener ( 'change' , this . boundFetchResults )
41
- input . removeEventListener ( 'input' , this . debounceInputChange )
40
+ const state = states . get ( this )
41
+ if ( ! state ) return
42
+
43
+ input . removeEventListener ( 'focus' , state . fetch )
44
+ input . removeEventListener ( 'change' , state . fetch )
45
+ input . removeEventListener ( 'input' , state . oninput )
42
46
}
43
47
44
48
get input ( ) : HTMLInputElement | HTMLTextAreaElement | null {
@@ -55,14 +59,17 @@ class RemoteInputElement extends HTMLElement {
55
59
}
56
60
}
57
61
58
- async function fetchResults ( remoteInput : RemoteInputElement , checkCurrentQuery : boolean = true ) {
62
+ async function fetchResults ( remoteInput : RemoteInputElement , checkCurrentQuery : boolean ) {
59
63
const input = remoteInput . input
60
64
if ( ! input ) return
61
65
66
+ const state = states . get ( remoteInput )
67
+ if ( ! state ) return
68
+
62
69
const query = input . value
63
- if ( checkCurrentQuery && remoteInput . currentQuery === query ) return
70
+ if ( checkCurrentQuery && state . currentQuery === query ) return
64
71
65
- remoteInput . currentQuery = query
72
+ state . currentQuery = query
66
73
67
74
const src = remoteInput . src
68
75
if ( ! src ) return
0 commit comments