1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.Store = void 0;
|
4 | var tslib_1 = require("tslib");
|
5 | var immer_1 = require("immer");
|
6 | var rxjs_1 = require("rxjs");
|
7 | var operators_1 = require("rxjs/operators");
|
8 | var Store = (function () {
|
9 | function Store(args) {
|
10 | var _this = this;
|
11 | this._dispose$ = new rxjs_1.Subject();
|
12 | this._changing$ = new rxjs_1.Subject();
|
13 | this._changed$ = new rxjs_1.Subject();
|
14 | this._ = {
|
15 | state: undefined,
|
16 | };
|
17 | this.dispose$ = this._dispose$.pipe(operators_1.share());
|
18 | this.changing$ = this._changing$.pipe(operators_1.share());
|
19 | this.changed$ = this._changed$.pipe(operators_1.share());
|
20 | this._.state = tslib_1.__assign({}, args.initial);
|
21 | this._event$ = args.event$ || new rxjs_1.Subject();
|
22 | this.event$ = this._event$.pipe(operators_1.takeUntil(this.dispose$), operators_1.map(function (e) { return _this.toDispatchEvent(e); }), operators_1.share());
|
23 | }
|
24 | Store.create = function (args) {
|
25 | return new Store(args);
|
26 | };
|
27 | Store.prototype.dispose = function () {
|
28 | this._dispose$.next();
|
29 | this._dispose$.complete();
|
30 | };
|
31 | Object.defineProperty(Store.prototype, "isDisposed", {
|
32 | get: function () {
|
33 | return this._dispose$.isStopped;
|
34 | },
|
35 | enumerable: false,
|
36 | configurable: true
|
37 | });
|
38 | Object.defineProperty(Store.prototype, "state", {
|
39 | get: function () {
|
40 | return tslib_1.__assign({}, this._.state);
|
41 | },
|
42 | enumerable: false,
|
43 | configurable: true
|
44 | });
|
45 | Store.prototype.dispatch = function (event) {
|
46 | this._event$.next(event);
|
47 | return this;
|
48 | };
|
49 | Store.prototype.on = function (type) {
|
50 | return this.event$.pipe(operators_1.filter(function (e) { return e.type === type; }), operators_1.map(function (e) { return e; }));
|
51 | };
|
52 | Store.prototype.toDispatchEvent = function (event) {
|
53 | var _this = this;
|
54 | var type = event.type, payload = event.payload;
|
55 | var from = this.state;
|
56 | var result = {
|
57 | type: type,
|
58 | payload: payload,
|
59 | get state() {
|
60 | return tslib_1.__assign({}, from);
|
61 | },
|
62 | change: function (next) {
|
63 | var to = typeof next !== 'function'
|
64 | ? tslib_1.__assign({}, next) : immer_1.default(from, function (draft) {
|
65 | next(draft);
|
66 | return undefined;
|
67 | });
|
68 | var isCancelled = false;
|
69 | var change = { type: type, event: event, from: from, to: to };
|
70 | _this._changing$.next({
|
71 | change: change,
|
72 | isCancelled: isCancelled,
|
73 | cancel: function () { return (isCancelled = true); },
|
74 | });
|
75 | if (isCancelled) {
|
76 | return result;
|
77 | }
|
78 | _this._.state = to;
|
79 | _this._changed$.next(change);
|
80 | return result;
|
81 | },
|
82 | dispatch: function (event) {
|
83 | _this.dispatch(event);
|
84 | return result;
|
85 | },
|
86 | };
|
87 | return result;
|
88 | };
|
89 | return Store;
|
90 | }());
|
91 | exports.Store = Store;
|