Skip to content

Commit

Permalink
Refactor lazyload to avoid unecessary object creation
Browse files Browse the repository at this point in the history
  • Loading branch information
MacFJA committed Nov 3, 2020
1 parent 9a7d1b2 commit 060959e
Showing 1 changed file with 24 additions and 13 deletions.
37 changes: 24 additions & 13 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,23 +142,34 @@ export function pannable(node: HTMLElement): ReturnType<Action> {
* Demo: https://svelte.dev/repl/f12988de576b4bf9b541a2a59eb838f6?version=3.23.2
*
*/
export function lazyload(node: HTMLElement, attributes: Object): ReturnType<Action> {
let intersecting = false;

const handleIntersection: IntersectionObserverCallback = (entries) => {
intersecting = entries[0].isIntersecting;
if (entries[0].intersectionRatio > 0) {
const lazyLoadHandleIntersection: IntersectionObserverCallback = (entries) => {
entries.forEach(
entry => {
if (!entry.isIntersecting) {
return
}

if (!(entry.target instanceof HTMLElement)) {
return;
}

let node = entry.target;
let attributes = lazyLoadNodeAttributes.find(item => item.node === node)?.attributes
Object.assign(node, attributes)

lazyLoadObserver.unobserve(node)
}
if (intersecting) {
observer.unobserve(node);
}
}
const observer = new IntersectionObserver(handleIntersection);
observer.observe(node);
)
}
const lazyLoadObserver = new IntersectionObserver(lazyLoadHandleIntersection);
let lazyLoadNodeAttributes: Array<{node: HTMLElement, attributes: Object}> = []
export function lazyload(node: HTMLElement, attributes: Object): ReturnType<Action> {
lazyLoadNodeAttributes.push({node, attributes})

lazyLoadObserver.observe(node);
return {
destroy() {
observer.unobserve(node);
lazyLoadObserver.unobserve(node);
}
};
}

0 comments on commit 060959e

Please sign in to comment.