This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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