Skip to content

Commit 710f873

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

File tree

7 files changed

+31
-13
lines changed

7 files changed

+31
-13
lines changed

src/index.ts

+7-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ import {
2222
SpanielTrackedElement,
2323
SpanielObserverEntry,
2424
DOMString,
25-
DOMMargin
25+
DOMMargin,
26+
IntersectionObserverClass
2627
} from './interfaces';
2728

2829
export { Watcher, WatcherConfig } from './watcher';
@@ -46,12 +47,16 @@ import {
4647
Frame
4748
} from './metal/index';
4849

50+
import w from './metal/window-proxy';
51+
52+
const IntersectionObserver: IntersectionObserverClass = !!w.IntersectionObserver ? w.IntersectionObserver : SpanielIntersectionObserver;
53+
4954
export {
5055
on,
5156
off,
5257
scheduleRead,
5358
scheduleWork,
54-
SpanielIntersectionObserver as IntersectionObserver,
59+
IntersectionObserver,
5560
SpanielObserver,
5661
SpanielTrackedElement,
5762
setGlobalEngine,

src/metal/window-proxy.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
// detect the presence of DOM
1313
const nop = () => 0;
1414

15+
import {
16+
IntersectionObserverClass
17+
} from '../interfaces';
18+
1519
interface WindowProxy {
1620
hasDOM: boolean;
1721
hasRAF: boolean;
@@ -20,6 +24,7 @@ interface WindowProxy {
2024
getHeight: Function;
2125
getWidth: Function;
2226
rAF: Function;
27+
IntersectionObserver: IntersectionObserverClass;
2328
}
2429

2530
const hasDOM = !!((typeof window !== 'undefined') && window && (typeof document !== 'undefined') && document);
@@ -32,7 +37,8 @@ let W: WindowProxy = {
3237
getScrollLeft: nop,
3338
getHeight: nop,
3439
getWidth: nop,
35-
rAF: hasRAF ? window.requestAnimationFrame.bind(window) : (callback: Function) => { callback(); }
40+
rAF: hasRAF ? window.requestAnimationFrame.bind(window) : (callback: Function) => { callback(); },
41+
IntersectionObserver: hasDOM && (window as any).IntersectionObserver
3642
};
3743

3844
function hasDomSetup() {

src/spaniel-observer.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ import { generateToken, on, scheduleWork } from './metal/index';
3636

3737
let emptyRect = { x: 0, y: 0, width: 0, height: 0 };
3838

39+
const IntersectionObserver = !!w.IntersectionObserver ? w.IntersectionObserver : SpanielIntersectionObserver;
40+
3941
export function DOMMarginToRootMargin(d: DOMMargin): DOMString {
4042
return `${d.top}px ${d.right}px ${d.bottom}px ${d.left}px`;
4143
}
@@ -62,7 +64,8 @@ export class SpanielObserver implements SpanielObserverInterface {
6264
rootMargin: convertedRootMargin,
6365
threshold: this.thresholds.map((t: SpanielThreshold) => t.ratio)
6466
};
65-
this.observer = new SpanielIntersectionObserver((records: IntersectionObserverEntry[]) => this.internalCallback(records), o);
67+
68+
this.observer = new IntersectionObserver((records: IntersectionObserverEntry[]) => this.internalCallback(records), o);
6669

6770
if (w.hasDOM) {
6871
on('unload', this.onWindowClosed.bind(this));

test/app/index.html

+4
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99
</div>
1010
<div id="root">
1111
</div>
12+
<script>
13+
// Disable browser default IntersectionObserver so as to use IntersectionObserver of spaniel.
14+
window.IntersectionObserver = null;
15+
</script>
1216
<script type="text/javascript" src="../../exports/spaniel.js"></script>
1317
<script type="text/javascript" src="setup.js"></script>
1418
<script type="text/javascript" src="index.js"></script>

test/headless/specs/intersection-observer.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -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);

test/setup/environment.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,10 @@ function cleanUp(value) {
3939
value.observer.disconnect();
4040
}
4141

42-
function wait50ms(result) {
42+
function wait100ms(result) {
4343
return new RSVP.Promise(function(resolve) {
4444
setTimeout(function() {
4545
resolve(result);
46-
}, 50);
46+
}, 100);
4747
});
4848
}

test/specs/spaniel-observer.spec.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,15 @@ describe('SpanielObserver', function() {
4444
ratio: 0.5
4545
}).then(function(result) {
4646
result.observer.onTabHidden();
47-
return wait50ms(result);
47+
return wait100ms(result);
4848
}).then(function(result) {
4949
expect(result.entries.length).to.equal(2, 'Two events have been fired');
5050
expect(result.entries[1].entering).to.equal(false, 'Second event is exiting');
5151
result.observer.onTabShown();
52-
return wait50ms(result);
52+
return wait100ms(result);
5353
}).then(function(result) {
5454
result.observer.onTabHidden();
55-
return wait50ms(result);
55+
return wait100ms(result);
5656
}).then(function(result) {
5757
expect(result.entries.length).to.equal(4, 'Three events have been fired');
5858
expect(result.entries[2].entering).to.equal(true, 'second to last event is entering');

0 commit comments

Comments
 (0)