@restate/core
Version:
_Restate_ is a predictable, easy to use, easy to integrate, typesafe state container for [React](https://reactjs.org/).
116 lines (115 loc) • 3.64 kB
JavaScript
;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var rx_store_exports = {};
__export(rx_store_exports, {
RestateStore: () => RestateStore
});
module.exports = __toCommonJS(rx_store_exports);
var import_immer = require("immer");
var import_rxjs = require("rxjs");
var import_operators = require("rxjs/operators");
class RestateStore {
_state$;
_close$ = new import_rxjs.Subject();
_options;
_middleware = [];
constructor(stateSubject, middleware, options) {
this._state$ = stateSubject;
this._middleware = middleware;
this._options = options;
}
static of(state, middleware, options) {
return new RestateStore(state, middleware, options);
}
_next(updateFunctionOrNextState, trace) {
try {
let useUpdateFunction2 = function(state) {
const ret = (0, import_immer.createDraft)(state);
if (isUpdateFunction) {
const update = updateFunctionOrNextState(ret);
return update == null ? ret : (0, import_immer.createDraft)(update);
} else {
throw new Error("We should not be here");
}
};
var useUpdateFunction = useUpdateFunction2;
const isUpdateFunction = updateFunctionOrNextState instanceof Function;
const nextStateDraft = isUpdateFunction ? useUpdateFunction2(this.state) : (0, import_immer.createDraft)(updateFunctionOrNextState);
recursiveMiddlewareHandler({
middleware: this._middleware,
nextState: nextStateDraft,
currentState: this.state
});
const nextState = (0, import_immer.finishDraft)(nextStateDraft);
const nextStatePackage = {
state: nextState,
trace
};
this._state$.next(nextStatePackage);
return nextStatePackage;
} catch (e) {
console.error(e);
return { state: this._state$.value };
}
}
next(updateFunctionOrNextState, trace) {
this._next(updateFunctionOrNextState, trace);
}
nextAsync(updateFunctionOrNextState, trace) {
setTimeout(() => {
this._next(updateFunctionOrNextState, trace);
}, 0);
}
get state() {
return this._state$.value.state;
}
close() {
this._close$.next("closing the store");
this._close$.complete();
}
get state$() {
return this._state$.pipe((0, import_operators.takeUntil)(this._close$), (0, import_operators.observeOn)(import_rxjs.queueScheduler));
}
get options() {
return this._options;
}
get middleware() {
return this._middleware;
}
}
function recursiveMiddlewareHandler({
middleware,
nextState,
currentState
}) {
if (middleware.length === 0) {
return;
}
const nextMiddleware = middleware[0];
nextMiddleware({
nextState,
currentState
});
const remainingMiddleware = middleware.slice(1, middleware.length);
return recursiveMiddlewareHandler({
middleware: remainingMiddleware,
nextState,
currentState
});
}