Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
linkedin#52 Using native IntersectionObserver if browser support
  • Loading branch information
SparshithNR committed Jul 18, 2017
1 parent ef26153 commit 468ebbc
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 7 deletions.
6 changes: 5 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,16 @@ import {
Frame
} from './metal/index';

import w from './metal/window-proxy';

const IntersectionObserver = !!w.IntersectionObserver ? w.IntersectionObserver : SpanielIntersectionObserver;

export {
on,
off,
scheduleRead,
scheduleWork,
SpanielIntersectionObserver as IntersectionObserver,
IntersectionObserver,
SpanielObserver,
SpanielTrackedElement,
setGlobalEngine,
Expand Down
4 changes: 3 additions & 1 deletion src/metal/window-proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ interface WindowProxy {
getHeight: Function;
getWidth: Function;
rAF: Function;
IntersectionObserver: IntersectionObserver;
}

const hasDOM = !!((typeof window !== 'undefined') && window && (typeof document !== 'undefined') && document);
Expand All @@ -32,7 +33,8 @@ let W: WindowProxy = {
getScrollLeft: nop,
getHeight: nop,
getWidth: nop,
rAF: hasRAF ? window.requestAnimationFrame.bind(window) : (callback: Function) => { callback(); }
rAF: hasRAF ? window.requestAnimationFrame.bind(window) : (callback: Function) => { callback(); },
IntersectionObserver: hasDOM && (window as any).IntersectionObserver
};

function hasDomSetup() {
Expand Down
10 changes: 5 additions & 5 deletions test/headless/specs/intersection-observer.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ testModule('IntersectionObserver', class extends TestClass {
.wait(IMPRESSION_THRESHOLD)
.scrollTo(105)
.wait(IMPRESSION_THRESHOLD)
.scrollTo(95)
.scrollTo(40)
.wait(IMPRESSION_THRESHOLD)
.getExecution()
.evaluate(function() {
Expand Down Expand Up @@ -177,8 +177,8 @@ testModule('IntersectionObserver', class extends TestClass {
target1 = document.querySelector('.tracked-item[data-id="1"]');
target2 = document.querySelector('.tracked-item[data-id="2"]');
target3 = document.querySelector('.tracked-item[data-id="3"]');
window.observer = new spaniel.IntersectionObserver(function() {
window.STATE.impressions++;
window.observer = new spaniel.IntersectionObserver(function(event) {
window.STATE.impressions+= event.length;
});
window.observer.observe(target1);
window.observer.observe(target2);
Expand Down Expand Up @@ -207,8 +207,8 @@ testModule('IntersectionObserver', class extends TestClass {
target1 = document.querySelector('.tracked-item[data-id="1"]');
target2 = document.querySelector('.tracked-item[data-id="2"]');
target3 = document.querySelector('.tracked-item[data-id="3"]');
window.observer = new spaniel.IntersectionObserver(function() {
window.STATE.impressions++;
window.observer = new spaniel.IntersectionObserver(function(event) {
window.STATE.impressions+= event.length;
});
window.observer.observe(target1);
window.observer.observe(target2);
Expand Down

0 comments on commit 468ebbc

Please sign in to comment.