ngx-decorate
Version:
Useful decorators for Angular 2 and above
51 lines • 2.07 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 setProp_1 = require("./processors/setProp");
/**
* Mark the property as a subject setter. When written to, it will call .next(value) on the subject.
* The decorator's <b>default</b> config option only works on items that make use of the OnInit hook,
* i.e. it will <b>not</b> work for services and pipes.
*
* Because the <b>default</b> config option utilises the OnInit hook, it should not be used on
* properties that are component inputs as the input would get overridden during the hook.
*
* @param subjectPropName Name of the property at which the subject resides
* @param conf Optional configuration
*/
function SubjectSetter(subjectPropName, conf = {}) {
return (target, prop) => {
const symbolValue = Symbol(`subjectSetter:${prop.toString()}`);
const defaultDescriptor = {
configurable: true,
enumerable: true
};
const defaultValue = conf.default || target[prop];
if (defaultValue) {
ensureSymbol_1.ensureSymbol(target, symbols_1._setProp, [])
.push({ prop, value: defaultValue });
HookManager_1.HookManager.for(target).add(2 /* POST_INIT */, setProp_1.setProp);
}
Object.defineProperties(target, {
[symbolValue]: {
configurable: false,
enumerable: false,
value: defaultValue,
writable: true
},
[prop]: Object.assign(defaultDescriptor, conf.desc || {}, {
get() {
return this[symbolValue];
},
set(v) {
this[symbolValue] = v;
this[subjectPropName].next(v);
}
})
});
};
}
exports.SubjectSetter = SubjectSetter;
//# sourceMappingURL=SubjectSetter.js.map