@broxus/js-core
Version:
MobX-based JavaScript Core library
108 lines (107 loc) • 4.15 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) {
this._data = (options?.initialData ?? {});
this._state = (options?.initialState ?? {});
makeObservable(this, undefined, options);
}
/**
* Set data using multiple overloads:
* - Pass a partial data object
* - Pass a function that returns partial data
* - Pass a key-value pair
* @template K - Keys of data type T
* @param keyOrData - Key, partial data object, or function
* @param value - Value when using key-value pair approach
*/
setData(keyOrData, value) {
if (typeof keyOrData === 'function') {
const partialData = keyOrData({ ...this._data });
this._data = { ...this._data, ...partialData };
return this;
}
if (typeof keyOrData === 'string' || typeof keyOrData === 'number' || typeof keyOrData === 'symbol') {
this._data = {
...this._data,
[keyOrData]: value,
};
return this;
}
if (keyOrData != null && typeof keyOrData === 'object' && !Array.isArray(keyOrData)) {
this._data = { ...this._data, ...keyOrData };
}
return this;
}
/**
* Set state using multiple overloads:
* - Pass a partial state object
* - Pass a function that returns partial state
* - Pass a key-value pair
* @template K - Keys of state type U
* @param keyOrState - Key, partial state object, or function
* @param value - Value when using key-value pair approach
*/
setState(keyOrState, value) {
if (typeof keyOrState === 'function') {
const partialState = keyOrState({ ...this._state });
this._state = { ...this._state, ...partialState };
return this;
}
if (typeof keyOrState === 'string' || typeof keyOrState === 'number' || typeof keyOrState === 'symbol') {
this._state = {
...this._state,
[keyOrState]: value,
};
return this;
}
if (keyOrState != null && typeof keyOrState === 'object' && !Array.isArray(keyOrState)) {
this._state = { ...this._state, ...keyOrState };
}
return this;
}
toJSON(both) {
if (both === true) {
return [toJS(this._data), toJS(this._state)];
}
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);