Skip to content

Instantly share code, notes, and snippets.

@okunokentaro
Created June 16, 2018 05:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save okunokentaro/70cdb7f1873ac75285bc70122c23c770 to your computer and use it in GitHub Desktop.
Save okunokentaro/70cdb7f1873ac75285bc70122c23c770 to your computer and use it in GitHub Desktop.
import { kk } from './emitter';
/**
* AoT のために export function での宣言が必須。
*/
/* tslint:disable:only-arrow-functions */
export function KukerComponent() {
return cls => {
const componentRole = cls.__proto__.name.includes('DecisionMaker')
? 'DecisionMaker'
: 'Presenter';
const proxyNgOnInit = cls.prototype.ngOnInit;
cls.prototype.ngOnInit = function(...args) {
// プロキシのため function使用
if (componentRole === 'DecisionMaker') {
kk.decisionMakerNgOnInit(cls.name);
} else if (componentRole === 'Presenter') {
kk.presenterNgOnInit(cls.name);
}
if (typeof proxyNgOnInit === 'function') {
proxyNgOnInit.apply(this, args);
}
};
const proxyNgOnDestroy = cls.prototype.ngOnDestroy;
cls.prototype.ngOnDestroy = function(...args) {
// プロキシのため function使用
if (componentRole === 'DecisionMaker') {
kk.decisionMakerNgOnDestroy(cls.name);
} else if (componentRole === 'Presenter') {
kk.presenterNgOnDestroy(cls.name);
}
if (typeof proxyNgOnDestroy === 'function') {
proxyNgOnDestroy.apply(this, args);
}
};
Object.keys(cls.prototype).forEach(methodName => {
if (!methodName.match(/^on/)) {
return;
}
const proxy = cls.prototype[methodName];
cls.prototype[methodName] = function(...args) {
// プロキシのため function使用
if (componentRole === 'DecisionMaker') {
kk.decisionMakerHandling(cls.name, methodName);
} else if (componentRole === 'Presenter') {
kk.presenterHandling(cls.name, methodName);
}
proxy.apply(this, args);
};
});
return cls;
};
}
/* tslint:enable:only-arrow-functions */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment