Skip to content

Commit d1170ec

Browse files
Hide more of the internals of runtime checking (#302)
1 parent 14d3dc1 commit d1170ec

File tree

1 file changed

+19
-12
lines changed

1 file changed

+19
-12
lines changed

src/features/runtime-checks.js

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -37,22 +37,27 @@ class DDGRuntimeChecks extends HTMLElement {
3737
this.#connected = false
3838
}
3939

40+
/**
41+
* This method is called once and externally so has to remain public.
42+
**/
4043
setTagName (tagName) {
4144
this.#tagName = tagName
45+
// Clear the method so it can't be called again
46+
delete this.setTagName
4247
}
4348

4449
connectedCallback () {
4550
// Solves re-entrancy issues from React
4651
if (this.#connected) return
4752
this.#connected = true
48-
if (!this.transplantElement) {
53+
if (!this.#transplantElement) {
4954
// Restore the 'this' object with the DDGRuntimeChecks prototype as sometimes pages will overwrite it.
5055
Object.setPrototypeOf(this, DDGRuntimeChecks.prototype)
5156
}
52-
this.transplantElement()
57+
this.#transplantElement()
5358
}
5459

55-
monitorProperties (el) {
60+
#monitorProperties (el) {
5661
// Mutation oberver and observedAttributes don't work on property accessors
5762
// So instead we need to monitor all properties on the prototypes and forward them to the real element
5863
let propertyNames = []
@@ -85,7 +90,7 @@ class DDGRuntimeChecks extends HTMLElement {
8590
* The element has been moved to the DOM, so we can now reflect all changes to a real element.
8691
* This is to allow us to interrogate the real element before it is moved to the DOM.
8792
*/
88-
transplantElement () {
93+
#transplantElement () {
8994
// Creeate the real element
9095
const el = initialCreateElement.call(document, this.#tagName)
9196

@@ -134,7 +139,7 @@ class DDGRuntimeChecks extends HTMLElement {
134139
this.insertAdjacentElement('afterend', el)
135140
} catch (e) { console.warn(e) }
136141

137-
this.monitorProperties(el)
142+
this.#monitorProperties(el)
138143
// TODO pollyfill WeakRef
139144
this.#el = new WeakRef(el)
140145

@@ -144,13 +149,15 @@ class DDGRuntimeChecks extends HTMLElement {
144149
}, elementRemovalTimeout)
145150
}
146151

147-
getElement () {
152+
#getElement () {
148153
return this.#el?.deref()
149154
}
150155

156+
/* Native DOM element methods we're capturing to supplant values into the constructed node or store data for. */
157+
151158
setAttribute (name, value) {
152159
if (shouldFilterKey(this.#tagName, 'attribute', name)) return
153-
const el = this.getElement()
160+
const el = this.#getElement()
154161
if (el) {
155162
return el.setAttribute(name, value)
156163
}
@@ -159,7 +166,7 @@ class DDGRuntimeChecks extends HTMLElement {
159166

160167
removeAttribute (name) {
161168
if (shouldFilterKey(this.#tagName, 'attribute', name)) return
162-
const el = this.getElement()
169+
const el = this.#getElement()
163170
if (el) {
164171
return el.removeAttribute(name)
165172
}
@@ -168,7 +175,7 @@ class DDGRuntimeChecks extends HTMLElement {
168175

169176
addEventListener (...args) {
170177
if (shouldFilterKey(this.#tagName, 'listener', args[0])) return
171-
const el = this.getElement()
178+
const el = this.#getElement()
172179
if (el) {
173180
return el.addEventListener(...args)
174181
}
@@ -177,7 +184,7 @@ class DDGRuntimeChecks extends HTMLElement {
177184

178185
removeEventListener (...args) {
179186
if (shouldFilterKey(this.#tagName, 'listener', args[0])) return
180-
const el = this.getElement()
187+
const el = this.#getElement()
181188
if (el) {
182189
return el.removeEventListener(...args)
183190
}
@@ -200,15 +207,15 @@ class DDGRuntimeChecks extends HTMLElement {
200207
}
201208

202209
remove () {
203-
const el = this.getElement()
210+
const el = this.#getElement()
204211
if (el) {
205212
return el.remove()
206213
}
207214
return super.remove()
208215
}
209216

210217
removeChild (child) {
211-
const el = this.getElement()
218+
const el = this.#getElement()
212219
if (el) {
213220
return el.removeChild(child)
214221
}

0 commit comments

Comments
 (0)