UNPKG

4.06 kBJavaScriptView Raw
1"use strict";
2var __assign = (this && this.__assign) || function () {
3 __assign = Object.assign || function(t) {
4 for (var s, i = 1, n = arguments.length; i < n; i++) {
5 s = arguments[i];
6 for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7 t[p] = s[p];
8 }
9 return t;
10 };
11 return __assign.apply(this, arguments);
12};
13Object.defineProperty(exports, "__esModule", { value: true });
14var redux_1 = require("redux");
15var rxjs_1 = require("rxjs");
16var operators_1 = require("rxjs/operators");
17function enableDevTool(store) {
18 console.warn("enableDevTool requires the browser extension. Note: the 'skip action' feature is not supported (but 'jump' works as expected')");
19 if (typeof window === "undefined") {
20 // nodejs deployments?
21 return;
22 }
23 var extension = window["__REDUX_DEVTOOLS_EXTENSION__"] || window["devToolsExtension"];
24 if (!extension) {
25 console.warn("devToolsExtension not found in window (extension not installed?). Could not enable devTool");
26 return;
27 }
28 var devtoolExtension = extension();
29 var reduxToReactiveSync = new rxjs_1.Subject();
30 var reactiveStateUpdate = new rxjs_1.Subject();
31 // TODO: initialState: S should be auto-infered, maybe a bug in TS or RxJS?
32 store.select().pipe(operators_1.take(1)).subscribe(function (initialState) {
33 var enhancer = function (next) {
34 return function (reducer, preloadedState) {
35 // run any other store enhancers
36 var reduxStore = next(reducer, initialState);
37 // write back the state from DevTools/Redux to our ReactiveState
38 reduxStore.subscribe(function () {
39 // const reduxState = reduxStore.getState();
40 // console.info("RDX UPD STATE: ", reduxState)
41 // console.info("JUMP/SKIP not supported, do not use or you get undefined behaviour!")
42 // reduxToReactiveSync.next(reduxState);
43 });
44 reactiveStateUpdate.subscribe(function (p) {
45 // console.info("RDX DISP", p)
46 reduxStore.dispatch({ type: p.actionName, payload: p.payload, state: p.state });
47 });
48 return reduxStore;
49 };
50 };
51 // TODO: State should be type S, but TS does not yet support it
52 // maybe after TS 2.7: https://github.com/Microsoft/TypeScript/issues/10727
53 var reduxReducer = function (state, action) {
54 // TODO: "skip" in devtools does not work here. In plain redux, we could call our reducers with the state
55 // and the action payload of the (replayed-) action. But we can"t to it with our store, as even if we
56 // could reset it to a state and replay the action, the operation is async. But we must return a state
57 // here in a sync manner... :(
58 if (action.type === "@@INIT") {
59 // redux internal action
60 return __assign({}, state);
61 }
62 // What we actually do is instead of returning reduce(state, action) we return the result-state we have
63 // attached to action, that is kept in the log
64 return __assign({}, action.state);
65 };
66 redux_1.createStore(reduxReducer, initialState, redux_1.compose(enhancer, devtoolExtension));
67 });
68 store.stateChangedNotification.subscribe(function (notification) {
69 var actionName = notification.actionName, actionPayload = notification.actionPayload, newState = notification.newState;
70 if (actionName !== "__INTERNAL_SYNC")
71 reactiveStateUpdate.next({ actionName: actionName || "UNNAMED", payload: actionPayload, state: newState });
72 });
73 var syncReducer = function (state, payload) {
74 return __assign({}, payload);
75 };
76 store.addReducer(reduxToReactiveSync, syncReducer, "__INTERNAL_SYNC");
77}
78exports.enableDevTool = enableDevTool;
79;
80//# sourceMappingURL=devtool.js.map
\No newline at end of file