Add disconnected handling parallel to connected
This commit is contained in:
parent
d620c8528a
commit
73a6c6767d
1 changed files with 17 additions and 0 deletions
17
better.js
17
better.js
|
@ -56,6 +56,23 @@ export class Better extends HTMLElement {
|
|||
this.#connected = []
|
||||
}
|
||||
|
||||
// Array of disconnected callbacks
|
||||
#disconnected = [];
|
||||
|
||||
// disconnectedCallback but as a promise.
|
||||
// Resolves instantly when already disconnected and can be used more than once.
|
||||
get disconnected() {
|
||||
if (this.isDisconnected) return Promise.resolve(this)
|
||||
else return new Promise( (yes, no) => this.#disconnected.push({yes, no}) )
|
||||
}
|
||||
|
||||
// Resolves all `disconnected promises
|
||||
disconnectedCallback() {
|
||||
if ("onDisconnect" in this) this.onDisonnect()
|
||||
this.#disconnected.forEach( e => e.yes(this) )
|
||||
this.#disconnected = []
|
||||
}
|
||||
|
||||
setContent(...content) {
|
||||
this.innerHTML = ""
|
||||
content.forEach( element => this.appendChild(element) )
|
||||
|
|
Loading…
Reference in a new issue