ngx-decorate
Version:
Useful decorators for Angular 2 and above
27 lines • 960 B
JavaScript
import { ensureSymbol } from './lib/ensureSymbol';
import { HookManager } from './lib/HookManager';
import { _subscrTo } from './lib/symbols';
import { subscribeTo } from './processors/subscribe-to';
import { unsubscribe } from './processors/unsubscribe';
/**
* Subscribe to an observable and set its last emitted value to this property
*
* This decorator requires the ngOnInit hook and therefore will only work with directives and components.
*
* @param prop Property at which the observable resides
* @param cfg Optional configuration
*/
export function SubscribeTo(prop, cfg = {}) {
return (target, key) => {
ensureSymbol(target, _subscrTo, [])
.push({
cfg,
source: prop,
target: key
});
const mgr = HookManager.for(target);
mgr.add(2 /* POST_INIT */, subscribeTo);
mgr.add(3 /* POST_DESTROY */, unsubscribe);
};
}
//# sourceMappingURL=SubscribeTo.js.map