Skip to content

Commit

Permalink
Fix bug for safari
Browse files Browse the repository at this point in the history
  • Loading branch information
kou_hin committed May 24, 2017
1 parent dfd9e3a commit 2234b96
Showing 1 changed file with 13 additions and 14 deletions.
27 changes: 13 additions & 14 deletions src/watchOnce.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,8 @@ if (canUseDOM) {
require('intersection-observer');
}

const animationFrame = typeof window !== 'undefined'
? (window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.setTimeout) : setTimeout;
const ric = typeof window !== 'undefined'
? (window.requestIdleCallback || window.setTimeout) : setTimeout;

const observers = {};
const callbacks = new WeakMap();
Expand All @@ -24,18 +21,20 @@ function findObserver(offsetString) {
let observer = observers[offsetString];
if (!observer) {
observer = new IntersectionObserver((entries) => {
if (entries.length < 1) {
return;
}
for (let i = 0, len = entries.length; i < len; i += 1) {
const entry = entries[i];
if (entry.intersectionRatio < 1) {
return;
}
const element = entry.target;
const callback = callbacks.get(element);
if (callback) {
animationFrame(callback);
callbacks.delete(element);
if (entry.isIntersecting || entry.intersectionRatio > 0) {
const element = entry.target;
const callback = callbacks.get(element);
if (callback) {
ric(callback);
callbacks.delete(element);
}
observer.unobserve(element);
}
observer.unobserve(element);
}
}, {
rootMargin: offsetString,
Expand Down

0 comments on commit 2234b96

Please sign in to comment.