-
-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f954149
commit ef30466
Showing
3 changed files
with
202 additions
and
17 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
const observers = new Map(); | ||
|
||
export function createObserver(options) { | ||
if (typeof IntersectionObserver === `undefined`) return null; | ||
|
||
const optionKey = JSON.stringify(options); | ||
if (observers.has(optionKey)) return observers.get(optionKey); | ||
|
||
const observer = new IntersectionObserver((entries) => { | ||
entries.forEach((entry) => { | ||
// Use `intersectionRatio` because of Edge 15's | ||
// lack of support for `isIntersecting`. | ||
// See: https://github.com/w3c/IntersectionObserver/issues/211 | ||
const isIntersecting = entry.isIntersecting || entry.intersectionRatio > 0; | ||
if (!isIntersecting || !entry.target.hydrate) return; | ||
entry.target.hydrate(); | ||
}); | ||
}, options); | ||
observers.set(optionKey, observer); | ||
|
||
return observer; | ||
} | ||
|
||
export function loadingComponentFactory(resolvableComponent, options) { | ||
return { | ||
render(h) { | ||
const tag = this.$el ? this.$el.tagName : `div`; | ||
|
||
// eslint-disable-next-line no-underscore-dangle | ||
if (!this.$el) resolvableComponent._resolve(); | ||
|
||
return h(tag); | ||
}, | ||
...options, | ||
}; | ||
} | ||
|
||
export function resolvableComponentFactory(component) { | ||
let resolve; | ||
const promise = new Promise((newResolve) => { | ||
resolve = newResolve; | ||
}); | ||
// eslint-disable-next-line no-underscore-dangle | ||
promise._resolve = async () => { | ||
resolve(typeof component === `function` ? await component() : component); | ||
}; | ||
|
||
return promise; | ||
} |