@broxus/js-core
Version:
MobX-based JavaScript Core library
99 lines (98 loc) • 3.6 kB
JavaScript
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
import { action, makeObservable, observable, toJS } from 'mobx';
export class AbstractStore {
/**
* Store data (e.g. user data, account data, form data etc.)
* @protected
*/
_data = {};
/**
* Store state (e.g. interface states, notations, errors etc.)
* @protected
*/
_state = {};
constructor(options) {
makeObservable(this, undefined, options);
}
/**
* Pass `key:value` hash (one or many keys) of the data.
* You may also pass individual keys and values to change data.
* @template {object} T
* @template {keyof T & string} K
* @param {Pick<T, K> | ((prevData: Readonly<T>) => Pick<T, K>) | K} keyOrData
* @param {T[K]} [value]
*/
setData(keyOrData, value) {
if (typeof keyOrData === 'function') {
this._data = keyOrData({ ...this._data });
return this;
}
if (typeof keyOrData === 'string') {
this._data = {
...this._data,
[keyOrData]: value,
};
return this;
}
if (typeof keyOrData === 'object' && !Array.isArray(keyOrData)) {
this._data = { ...this._data, ...keyOrData };
}
return this;
}
/**
* Pass `key:value` hash (one or many keys) of the state.
* You may also pass individual keys and values to change state.
* @template {object} U
* @template {keyof U & string} K
* @param {Pick<U, K> | ((prevState: Readonly<U>) => Pick<U, K>) | K} keyOrState
* @param {U[K]} [value]
*/
setState(keyOrState, value) {
if (typeof keyOrState === 'function') {
this._state = keyOrState({ ...this._state });
return this;
}
if (typeof keyOrState === 'string') {
this._state = {
...this._state,
[keyOrState]: value,
};
return this;
}
if (typeof keyOrState === 'object' && !Array.isArray(keyOrState)) {
this._state = { ...this._state, ...keyOrState };
}
return this;
}
toJSON() {
return toJS(this._data);
}
}
__decorate([
observable,
__metadata("design:type", Object)
], AbstractStore.prototype, "_data", void 0);
__decorate([
observable,
__metadata("design:type", Object)
], AbstractStore.prototype, "_state", void 0);
__decorate([
action.bound,
__metadata("design:type", Function),
__metadata("design:paramtypes", [Object, Object]),
__metadata("design:returntype", Object)
], AbstractStore.prototype, "setData", null);
__decorate([
action.bound,
__metadata("design:type", Function),
__metadata("design:paramtypes", [Object, Object]),
__metadata("design:returntype", Object)
], AbstractStore.prototype, "setState", null);