UNPKG

4.46 kBJavaScriptView Raw
1"use strict";
2var Configuration_1 = require("./Configuration");
3var DagHistory = require("./DagHistory");
4var DagGraph_1 = require("./DagGraph");
5var log = require('debug')('redux-dag-history:reducer');
6var EMPTY_ACTION = {
7 type: undefined,
8 payload: undefined,
9};
10function trackHistory(reducer, rawConfig) {
11 if (rawConfig === void 0) { rawConfig = {}; }
12 var config = new Configuration_1.default(rawConfig);
13 function logGraphActions(reducer) {
14 return function (state, action) {
15 var result = reducer(state, action);
16 var newStateGraph = new DagGraph_1.default(result.graph);
17 log('Action: \'%s\', State=', action.type, newStateGraph.print());
18 return result;
19 };
20 }
21 function tryToHandleHistoryAction(history, action) {
22 switch (action.type) {
23 case config.loadActionType:
24 history = DagHistory.load(action.payload);
25 break;
26 case config.clearActionType:
27 history = DagHistory.clear(history);
28 break;
29 case config.undoActionType:
30 history = DagHistory.undo(history);
31 break;
32 case config.redoActionType:
33 history = DagHistory.redo(history);
34 break;
35 case config.jumpToStateActionType:
36 history = DagHistory.jumpToState(action.payload, history);
37 break;
38 case config.jumpToBranchActionType:
39 history = DagHistory.jumpToBranch(action.payload, history);
40 break;
41 case config.jumpToLatestOnBranchActionType:
42 history = DagHistory.jumpToLatestOnBranch(action.payload, history);
43 break;
44 case config.createBranchActionType:
45 history = DagHistory.createBranch(action.payload, history);
46 break;
47 case config.renameBranchActionType:
48 history = DagHistory.renameBranch(action.payload.branch, action.payload.name, history);
49 break;
50 case config.squashActionType:
51 history = DagHistory.squash(history);
52 break;
53 case config.renameStateActionType:
54 history = DagHistory.renameState(action.payload.stateId, action.payload.name, history);
55 break;
56 case config.skipToStartActionType:
57 history = DagHistory.skipToStart(history);
58 break;
59 case config.skipToEndActionType:
60 history = DagHistory.skipToEnd(history);
61 break;
62 default:
63 if (config.canHandleAction(action)) {
64 history = config.handleAction(action, history);
65 }
66 }
67 return history;
68 }
69 function handleBlankState(action) {
70 var state = reducer(undefined, action);
71 var result = DagHistory.createHistory(state, config.initialBranchName, config.initialStateName);
72 log('creating new history with initial state', state, result);
73 return result;
74 }
75 function trackHistoryReducer(state, action) {
76 if (action === void 0) { action = EMPTY_ACTION; }
77 if (!state || !state.graph) {
78 return handleBlankState(action);
79 }
80 var history = tryToHandleHistoryAction(state, action);
81 var newState = reducer(history.current, action);
82 var result;
83 var isActionAllowed = config.actionFilter(action.type);
84 var isHistoryHandled = history !== state;
85 var isReplacement = isHistoryHandled || !isActionAllowed;
86 log('is action [%s] replacement? %s; allowed=%s, historyHandled=%s', action.type, isReplacement, isActionAllowed, isHistoryHandled);
87 if (isReplacement) {
88 result = DagHistory.replaceCurrentState(newState, history, config);
89 }
90 else {
91 var existingState = DagHistory.getExistingState(newState, history, config);
92 if (existingState) {
93 result = DagHistory.jumpToStateLogged(existingState, history);
94 }
95 else {
96 result = DagHistory.insert(newState, history, config);
97 }
98 }
99 return result;
100 }
101 return config.debug ? logGraphActions(trackHistoryReducer) : trackHistoryReducer;
102}
103Object.defineProperty(exports, "__esModule", { value: true });
104exports.default = trackHistory;