Instantly share code, notes, and snippets.

Embed
What would you like to do?
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