ng-focus-apply
Version:
Simplify component interaction in angular with focus-apply pattern.
46 lines (45 loc) • 1.86 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
var rxjs_1 = require("rxjs");
var operators_1 = require("rxjs/operators");
var operators_2 = require("rxjs/operators");
var FocusApplyStore = /** @class */ (function () {
function FocusApplyStore(initialState) {
var _this = this;
this.apply = function () {
var projections = [];
for (var _i = 0; _i < arguments.length; _i++) {
projections[_i] = arguments[_i];
}
var currentState = _this.store.getValue();
projections.forEach(function (projection) { return projection(currentState); });
_this.save(currentState);
};
this.applyAsync = function (obs) {
var projections = [];
for (var _i = 1; _i < arguments.length; _i++) {
projections[_i - 1] = arguments[_i];
}
return obs.pipe(operators_1.map(function (res) {
var currentState = _this.store.getValue();
projections.forEach(function (projection) { return projection(currentState, res); });
_this.save(currentState);
return res;
}));
};
this.focus = function (projection) {
return _this.state$.pipe(operators_1.map(projection), operators_2.distinctUntilChanged());
};
this.focusAndSubscribe = function (projection, subscribeCallback) {
_this.focus(projection).subscribe(subscribeCallback);
return _this;
};
this.store = new rxjs_1.BehaviorSubject(initialState);
this.state$ = this.store.asObservable();
}
FocusApplyStore.prototype.save = function (state) {
this.store.next(state);
};
return FocusApplyStore;
}());
exports.FocusApplyStore = FocusApplyStore;