Skip to content

Commit

Permalink
feat(visibility): added notify method and unify event structure
Browse files Browse the repository at this point in the history
  • Loading branch information
mjancarik committed Jun 28, 2018
1 parent 79f953a commit 7b4047a
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/UIComponentHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
44 changes: 40 additions & 4 deletions src/Visibility.js
Original file line number Diff line number Diff line change
@@ -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
*/

/**
Expand Down Expand Up @@ -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);
}
Expand All @@ -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',
Expand All @@ -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);
}
}

0 comments on commit 7b4047a

Please sign in to comment.