This repository was archived by the owner on Jun 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathreducer.js
105 lines (96 loc) · 3.05 KB
/
reducer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
'use strict';
import R, {concat, lensPath, view} from 'ramda';
import {combineReducers} from 'redux';
import layout from './layout';
import graphs from './dependencyGraph';
import paths from './paths';
import requestQueue from './requestQueue';
import appLifecycle from './appLifecycle';
import history from './history';
import * as API from './api';
import config from './config';
const reducer = combineReducers({
appLifecycle,
layout,
graphs,
paths,
requestQueue,
config,
dependenciesRequest: API.dependenciesRequest,
layoutRequest: API.layoutRequest,
reloadRequest: API.reloadRequest,
history,
});
function getInputHistoryState(itempath, props, state) {
const {graphs, layout, paths} = state;
const {InputGraph} = graphs;
const keyObj = R.filter(R.equals(itempath), paths);
let historyEntry;
if (!R.isEmpty(keyObj)) {
const id = R.keys(keyObj)[0];
historyEntry = {id, props: {}};
R.keys(props).forEach(propKey => {
const inputKey = `${id}.${propKey}`;
if (
InputGraph.hasNode(inputKey) &&
InputGraph.dependenciesOf(inputKey).length > 0
) {
historyEntry.props[propKey] = view(
lensPath(concat(paths[id], ['props', propKey])),
layout
);
}
});
}
return historyEntry;
}
function recordHistory(reducer) {
return function(state, action) {
// Record initial state
if (action.type === 'ON_PROP_CHANGE') {
const {itempath, props} = action.payload;
const historyEntry = getInputHistoryState(itempath, props, state);
if (historyEntry && !R.isEmpty(historyEntry.props)) {
state.history.present = historyEntry;
}
}
const nextState = reducer(state, action);
if (
action.type === 'ON_PROP_CHANGE' &&
action.payload.source !== 'response'
) {
const {itempath, props} = action.payload;
/*
* if the prop change is an input, then
* record it so that it can be played back
*/
const historyEntry = getInputHistoryState(
itempath,
props,
nextState
);
if (historyEntry && !R.isEmpty(historyEntry.props)) {
nextState.history = {
past: [
...nextState.history.past,
state.history.present
],
present: historyEntry,
future: [],
};
}
}
return nextState;
};
}
function reloaderReducer(reducer) {
return function(state, action) {
if (action.type === 'RELOAD') {
const {history} = state;
// eslint-disable-next-line no-param-reassign
state = {history};
}
return reducer(state, action);
};
}
export default reloaderReducer(recordHistory(reducer));