@angular-extensions/model
Version:
Angular Model - Simple state management with minimalistic API, one way data flow, multiple model support and immutable data exposed as RxJS Observable.
103 lines (95 loc) • 4.35 kB
JavaScript
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('@angular/core'), require('rxjs'), require('rxjs/operators')) :
typeof define === 'function' && define.amd ? define('@angular-extensions/model', ['exports', '@angular/core', 'rxjs', 'rxjs/operators'], factory) :
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory((global['angular-extensions'] = global['angular-extensions'] || {}, global['angular-extensions'].model = {}), global.ng.core, global.rxjs, global.rxjs.operators));
}(this, (function (exports, i0, rxjs, operators) { 'use strict';
function _interopNamespace(e) {
if (e && e.__esModule) return e;
var n = Object.create(null);
if (e) {
Object.keys(e).forEach(function (k) {
if (k !== 'default') {
var d = Object.getOwnPropertyDescriptor(e, k);
Object.defineProperty(n, k, d.get ? d : {
enumerable: true,
get: function () {
return e[k];
}
});
}
});
}
n['default'] = e;
return Object.freeze(n);
}
var i0__namespace = /*#__PURE__*/_interopNamespace(i0);
var Model = /** @class */ (function () {
function Model(initialData, immutable, sharedSubscription, clone) {
var _this = this;
this.immutable = immutable;
this.clone = clone;
this._data = new rxjs.BehaviorSubject(initialData);
this.data$ = this._data.asObservable().pipe(operators.map(function (data) { return _this.immutable
? clone
? clone(data)
: JSON.parse(JSON.stringify(data))
: data; }), sharedSubscription
? operators.shareReplay({ bufferSize: 1, refCount: true })
: operators.map(function (data) { return data; }));
}
Model.prototype.get = function () {
var data = this._data.getValue();
return this.immutable
? this.clone
? this.clone(data)
: JSON.parse(JSON.stringify(data))
: data;
};
Model.prototype.set = function (data) {
if (this.immutable) {
var clone = this.clone
? this.clone(data)
: JSON.parse(JSON.stringify(data));
this._data.next(clone);
}
else {
this._data.next(data);
}
};
return Model;
}());
var ModelFactory = /** @class */ (function () {
function ModelFactory() {
}
ModelFactory.prototype.create = function (initialData) {
return new Model(initialData, true, false);
};
ModelFactory.prototype.createMutable = function (initialData) {
return new Model(initialData, false, false);
};
ModelFactory.prototype.createMutableWithSharedSubscription = function (initialData) {
return new Model(initialData, false, true);
};
ModelFactory.prototype.createWithCustomClone = function (initialData, clone) {
return new Model(initialData, true, false, clone);
};
ModelFactory.prototype.createWithConfig = function (config) {
var initialData = config.initialData, immutable = config.immutable, sharedSubscription = config.sharedSubscription, clone = config.clone;
return new Model(initialData, immutable, sharedSubscription, clone);
};
return ModelFactory;
}());
ModelFactory.ɵprov = i0__namespace.ɵɵdefineInjectable({ factory: function ModelFactory_Factory() { return new ModelFactory(); }, token: ModelFactory, providedIn: "root" });
ModelFactory.decorators = [
{ type: i0.Injectable, args: [{
providedIn: 'root'
},] }
];
/**
* Generated bundle index. Do not edit.
*/
exports.Model = Model;
exports.ModelFactory = ModelFactory;
Object.defineProperty(exports, '__esModule', { value: true });
})));
//# sourceMappingURL=angular-extensions-model.umd.js.map