Skip to content

Instantly share code, notes, and snippets.

@mhartington
Created May 31, 2017 14:22
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mhartington/5fdb572a561cd2e39d3362f36760aa55 to your computer and use it in GitHub Desktop.
Save mhartington/5fdb572a561cd2e39d3362f36760aa55 to your computer and use it in GitHub Desktop.
const noop = () => { };
const DEVICEREADYEVENT = window['cordova'] ? "deviceready" : "DOMContentLoaded"
export function platformReady() {
return function decorator(proto, method, descriptor) {
const {
ngOnInit = noop,
ngOnDestroy = noop
} = proto;
const symbolHandler = Symbol(method);
function addListener() {
const handler = this[symbolHandler] = (...args) => {
descriptor.value.apply(this, args);
};
document.addEventListener(DEVICEREADYEVENT, handler);
}
function removeListener() {
document.addEventListener(DEVICEREADYEVENT, this[symbolHandler]);
}
proto.ngOnInit = function onInitWrapper() {
ngOnInit.call(this);
addListener.call(this);
};
proto.ngOnDestroy = function onDestroyWrapper() {
ngOnDestroy.call(this)
removeListener.call(this);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment