Skip to content

Commit 4099e78

Browse files
committed
linkedin#52 Using native IntersectionObserver if browser support
1 parent ef26153 commit 4099e78

File tree

3 files changed

+13
-7
lines changed

3 files changed

+13
-7
lines changed

src/index.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,16 @@ import {
4646
Frame
4747
} from './metal/index';
4848

49+
import w from './metal/window-proxy';
50+
51+
const IntersectionObserver = !!w.IntersectionObserver ? w.IntersectionObserver : SpanielIntersectionObserver;
52+
4953
export {
5054
on,
5155
off,
5256
scheduleRead,
5357
scheduleWork,
54-
SpanielIntersectionObserver as IntersectionObserver,
58+
IntersectionObserver,
5559
SpanielObserver,
5660
SpanielTrackedElement,
5761
setGlobalEngine,

src/metal/window-proxy.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ interface WindowProxy {
2020
getHeight: Function;
2121
getWidth: Function;
2222
rAF: Function;
23+
IntersectionObserver: IntersectionObserver;
2324
}
2425

2526
const hasDOM = !!((typeof window !== 'undefined') && window && (typeof document !== 'undefined') && document);
@@ -32,7 +33,8 @@ let W: WindowProxy = {
3233
getScrollLeft: nop,
3334
getHeight: nop,
3435
getWidth: nop,
35-
rAF: hasRAF ? window.requestAnimationFrame.bind(window) : (callback: Function) => { callback(); }
36+
rAF: hasRAF ? window.requestAnimationFrame.bind(window) : (callback: Function) => { callback(); },
37+
IntersectionObserver: hasDOM && (window as any).IntersectionObserver
3638
};
3739

3840
function hasDomSetup() {

test/headless/specs/intersection-observer.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ testModule('IntersectionObserver', class extends TestClass {
135135
.wait(IMPRESSION_THRESHOLD)
136136
.scrollTo(105)
137137
.wait(IMPRESSION_THRESHOLD)
138-
.scrollTo(95)
138+
.scrollTo(40)
139139
.wait(IMPRESSION_THRESHOLD)
140140
.getExecution()
141141
.evaluate(function() {
@@ -177,8 +177,8 @@ testModule('IntersectionObserver', class extends TestClass {
177177
target1 = document.querySelector('.tracked-item[data-id="1"]');
178178
target2 = document.querySelector('.tracked-item[data-id="2"]');
179179
target3 = document.querySelector('.tracked-item[data-id="3"]');
180-
window.observer = new spaniel.IntersectionObserver(function() {
181-
window.STATE.impressions++;
180+
window.observer = new spaniel.IntersectionObserver(function(event) {
181+
window.STATE.impressions+= event.length;
182182
});
183183
window.observer.observe(target1);
184184
window.observer.observe(target2);
@@ -207,8 +207,8 @@ testModule('IntersectionObserver', class extends TestClass {
207207
target1 = document.querySelector('.tracked-item[data-id="1"]');
208208
target2 = document.querySelector('.tracked-item[data-id="2"]');
209209
target3 = document.querySelector('.tracked-item[data-id="3"]');
210-
window.observer = new spaniel.IntersectionObserver(function() {
211-
window.STATE.impressions++;
210+
window.observer = new spaniel.IntersectionObserver(function(event) {
211+
window.STATE.impressions+= event.length;
212212
});
213213
window.observer.observe(target1);
214214
window.observer.observe(target2);

0 commit comments

Comments
 (0)