ngx-decorate
Version:
Useful decorators for Angular 2 and above
30 lines • 1.15 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const ensureSymbol_1 = require("./lib/ensureSymbol");
const HookManager_1 = require("./lib/HookManager");
const symbols_1 = require("./lib/symbols");
const subscribe_to_1 = require("./processors/subscribe-to");
const unsubscribe_1 = require("./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
*/
function SubscribeTo(prop, cfg = {}) {
return (target, key) => {
ensureSymbol_1.ensureSymbol(target, symbols_1._subscrTo, [])
.push({
cfg,
source: prop,
target: key
});
const mgr = HookManager_1.HookManager.for(target);
mgr.add(2 /* POST_INIT */, subscribe_to_1.subscribeTo);
mgr.add(3 /* POST_DESTROY */, unsubscribe_1.unsubscribe);
};
}
exports.SubscribeTo = SubscribeTo;
//# sourceMappingURL=SubscribeTo.js.map