Extract Ref class into separate module
This commit is contained in:
parent
6d4e398336
commit
12daec85e6
2 changed files with 18 additions and 19 deletions
18
ref.js
Normal file
18
ref.js
Normal file
|
@ -0,0 +1,18 @@
|
||||||
|
/** A reference to a reactive element that follows it around through changes */
|
||||||
|
export class Ref {
|
||||||
|
#current
|
||||||
|
/** @param {Element|Text} target A reactive element to follow */
|
||||||
|
constructor(target) {
|
||||||
|
this.#current = target
|
||||||
|
this.follow(target)
|
||||||
|
}
|
||||||
|
|
||||||
|
follow(target) {
|
||||||
|
target.addEventListener("replaced", ({next}) => {
|
||||||
|
this.#current = next
|
||||||
|
this.follow(next)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
deref() { return this.#current }
|
||||||
|
}
|
19
skooma.js
19
skooma.js
|
@ -152,25 +152,6 @@ export const reactiveElement = observable => {
|
||||||
return element
|
return element
|
||||||
}
|
}
|
||||||
|
|
||||||
/** A reference to a reactive element that follows it around through changes */
|
|
||||||
export class Ref {
|
|
||||||
#current
|
|
||||||
/** @param {Element|Text} target A reactive element to follow */
|
|
||||||
constructor(target) {
|
|
||||||
this.#current = target
|
|
||||||
this.follow(target)
|
|
||||||
}
|
|
||||||
|
|
||||||
follow(target) {
|
|
||||||
target.addEventListener("replaced", ({next}) => {
|
|
||||||
this.#current = next
|
|
||||||
this.follow(next)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
deref() { return this.#current }
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Set an attribute on an element
|
/** Set an attribute on an element
|
||||||
* @param {Element} element
|
* @param {Element} element
|
||||||
* @param {string} attribute
|
* @param {string} attribute
|
||||||
|
|
Loading…
Reference in a new issue