From 7b4047a9a0b1ff4e281590aef7c55ed6e2fc8b4a Mon Sep 17 00:00:00 2001 From: Miroslav Jancarik Date: Thu, 28 Jun 2018 11:12:16 +0200 Subject: [PATCH] feat(visibility): added notify method and unify event structure --- src/UIComponentHelper.js | 1 + src/Visibility.js | 44 ++++++++++++++++++++++++++++++++++++---- 2 files changed, 41 insertions(+), 4 deletions(-) diff --git a/src/UIComponentHelper.js b/src/UIComponentHelper.js index aec5303..9267ad5 100644 --- a/src/UIComponentHelper.js +++ b/src/UIComponentHelper.js @@ -246,6 +246,7 @@ export default class UIComponentHelper { let observer = new IntersectionObserver(entries => { intersectionObserverEntry = entries[0]; + this._visibility.circle.notify({ type: 'intersectionobserver', entries }); }, observerConfig); observer.observe(element); diff --git a/src/Visibility.js b/src/Visibility.js index 05b5903..b0fc7b6 100644 --- a/src/Visibility.js +++ b/src/Visibility.js @@ -1,10 +1,15 @@ import { Circle } from 'infinite-circle'; import RouterEvents from 'ima/router/Events'; +/** + @typedef notifyPayload + @type {object} + @property {string} type - event type + / + /** * @callback notifyCallback - * @param {Object} payload - * @param {string} payload.type + * @param {notifyPayload} payload */ /** @@ -116,13 +121,26 @@ export default class Visibility { }; } + /** + * The method add circle instance to be running in the next infinite loop. + * + * @param {notifyPayload} + */ + notify(...rest) { + this.circle.notify(...rest); + } + /** * The visibility helper start checking visibility of registered entries. * * @param {notifyCallback} */ _listenOnEvents(notify) { - this._dispatcher.listen(RouterEvents.AFTER_HANDLE_ROUTE, notify); + this._dispatcher.listen( + RouterEvents.AFTER_HANDLE_ROUTE, + this._afterHandleRoute, + this + ); this._window.bindEventListener(this._window.getWindow(), 'resize', notify); this._window.bindEventListener(this._window.getWindow(), 'scroll', notify); } @@ -133,7 +151,11 @@ export default class Visibility { * @param {notifyCallback} */ _unlistenOnEvents(notify) { - this._dispatcher.unlisten(RouterEvents.AFTER_HANDLE_ROUTE, notify); + this._dispatcher.unlisten( + RouterEvents.AFTER_HANDLE_ROUTE, + this._afterHandleRoute, + this + ); this._window.unbindEventListener( this._window.getWindow(), 'resize', @@ -145,4 +167,18 @@ export default class Visibility { notify ); } + + /** + * The method normalize routeInfo to {@notifyPayload}. + * + * @param {Object} routeInfo + */ + _afterHandleRoute(routeInfo) { + const payload = Object.assign( + { type: RouterEvents.AFTER_HANDLE_ROUTE }, + routeInfo + ); + + this.notify(payload); + } }