Skip to content

Commit

Permalink
#52 Allow spaniel to use native IntersectionObserver if browser supports
Browse files Browse the repository at this point in the history
  • Loading branch information
SparshithNR authored and asakusuma committed Jul 19, 2017
1 parent ef26153 commit ab6a522
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 13 deletions.
9 changes: 7 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ import {
SpanielTrackedElement,
SpanielObserverEntry,
DOMString,
DOMMargin
DOMMargin,
IntersectionObserverClass
} from './interfaces';

export { Watcher, WatcherConfig } from './watcher';
Expand All @@ -46,12 +47,16 @@ import {
Frame
} from './metal/index';

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

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

export {
on,
off,
scheduleRead,
scheduleWork,
SpanielIntersectionObserver as IntersectionObserver,
IntersectionObserver,
SpanielObserver,
SpanielTrackedElement,
setGlobalEngine,
Expand Down
8 changes: 7 additions & 1 deletion src/metal/window-proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// detect the presence of DOM
const nop = () => 0;

import {
IntersectionObserverClass
} from '../interfaces';

interface WindowProxy {
hasDOM: boolean;
hasRAF: boolean;
Expand All @@ -20,6 +24,7 @@ interface WindowProxy {
getHeight: Function;
getWidth: Function;
rAF: Function;
IntersectionObserver: IntersectionObserverClass;
}

const hasDOM = !!((typeof window !== 'undefined') && window && (typeof document !== 'undefined') && document);
Expand All @@ -32,7 +37,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
5 changes: 4 additions & 1 deletion src/spaniel-observer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ import { generateToken, on, scheduleWork } from './metal/index';

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

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

export function DOMMarginToRootMargin(d: DOMMargin): DOMString {
return `${d.top}px ${d.right}px ${d.bottom}px ${d.left}px`;
}
Expand All @@ -62,7 +64,8 @@ export class SpanielObserver implements SpanielObserverInterface {
rootMargin: convertedRootMargin,
threshold: this.thresholds.map((t: SpanielThreshold) => t.ratio)
};
this.observer = new SpanielIntersectionObserver((records: IntersectionObserverEntry[]) => this.internalCallback(records), o);

this.observer = new IntersectionObserver((records: IntersectionObserverEntry[]) => this.internalCallback(records), o);

if (w.hasDOM) {
on('unload', this.onWindowClosed.bind(this));
Expand Down
4 changes: 4 additions & 0 deletions test/app/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
</div>
<div id="root">
</div>
<script>
// Disable browser default IntersectionObserver so as to use IntersectionObserver of spaniel.
window.IntersectionObserver = null;
</script>
<script type="text/javascript" src="../../exports/spaniel.js"></script>
<script type="text/javascript" src="setup.js"></script>
<script type="text/javascript" src="index.js"></script>
Expand Down
8 changes: 4 additions & 4 deletions test/headless/specs/intersection-observer.js
Original file line number Diff line number Diff line change
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
4 changes: 2 additions & 2 deletions test/setup/environment.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ function cleanUp(value) {
value.observer.disconnect();
}

function wait50ms(result) {
function wait100ms(result) {
return new RSVP.Promise(function(resolve) {
setTimeout(function() {
resolve(result);
}, 50);
}, 100);
});
}
6 changes: 3 additions & 3 deletions test/specs/spaniel-observer.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,15 @@ describe('SpanielObserver', function() {
ratio: 0.5
}).then(function(result) {
result.observer.onTabHidden();
return wait50ms(result);
return wait100ms(result);
}).then(function(result) {
expect(result.entries.length).to.equal(2, 'Two events have been fired');
expect(result.entries[1].entering).to.equal(false, 'Second event is exiting');
result.observer.onTabShown();
return wait50ms(result);
return wait100ms(result);
}).then(function(result) {
result.observer.onTabHidden();
return wait50ms(result);
return wait100ms(result);
}).then(function(result) {
expect(result.entries.length).to.equal(4, 'Three events have been fired');
expect(result.entries[2].entering).to.equal(true, 'second to last event is entering');
Expand Down

0 comments on commit ab6a522

Please sign in to comment.