-
Notifications
You must be signed in to change notification settings - Fork 430
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement
Preloader
with Mutation Observer
Remove all external calls to `preloadOnLoadLinksForView`. In its place, introduce a new `AttributeObserver` to monitor all elements within a scope that: * added the `[data-turbo-preload]` attribute * connected with the `[data-turbo-preload]` attribute * connected with children that have the `[data-turbo-preload]` attribute The details of this observation are abstracted into the `AttributeObserver`, which will notify its delegate whenever those conditions are met. The delegate is responsible for responding to those conditions. For example, the `Preloader` ensures that the element is an `<a>` element, and that the element matches the expectations of **its** own delegate (the `Session` instance).
- Loading branch information
1 parent
00527e5
commit bf2584f
Showing
5 changed files
with
70 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
export class AttributeObserver { | ||
started = false | ||
|
||
constructor(delegate, element, attributeName) { | ||
this.delegate = delegate | ||
this.element = element | ||
this.attributeName = attributeName | ||
this.observer = new MutationObserver(this.#handleMutations) | ||
} | ||
|
||
start() { | ||
if (!this.started) { | ||
this.observer.observe(this.element, { | ||
attributeFilter: [this.attributeName], | ||
subtree: true, | ||
childList: true | ||
}) | ||
|
||
this.started = true | ||
} | ||
|
||
for (const element of this.#queryAllMatches(this.element)) { | ||
this.#handleNode(element) | ||
} | ||
} | ||
|
||
stop() { | ||
if (this.started) { | ||
this.observer.disconnect() | ||
this.started = false | ||
} | ||
} | ||
|
||
#handleMutations = (mutationRecords) => { | ||
for (const { addedNodes, target, type } of mutationRecords) { | ||
if (type === "attributes") { | ||
this.#handleNode(target) | ||
} else { | ||
for (const node of addedNodes) { | ||
if (node instanceof Element) { | ||
this.#handleNode(node) | ||
|
||
for (const element of this.#queryAllMatches(node)) { | ||
this.#handleNode(element) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
#handleNode(node) { | ||
if (node instanceof Element && node.hasAttribute(this.attributeName)) { | ||
this.delegate.observedElementWithAttribute(node, this.attributeName, node.getAttribute(this.attributeName)) | ||
} | ||
} | ||
|
||
#queryAllMatches(parent) { | ||
return parent.querySelectorAll(`[${this.attributeName}]`) | ||
} | ||
} |