Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add JUMP_TO_STATE action for slider-like monitors #5

Merged
merged 1 commit into from
Jul 14, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 25 additions & 13 deletions src/devTools.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,14 @@ const ActionTypes = {
ROLLBACK: 'ROLLBACK',
COMMIT: 'COMMIT',
SWEEP: 'SWEEP',
TOGGLE_ACTION: 'TOGGLE_ACTION'
TOGGLE_ACTION: 'TOGGLE_ACTION',
JUMP_TO_STATE: 'JUMP_TO_STATE'
};

const INIT_ACTION = {
type: '@@INIT'
};

function last(arr) {
return arr[arr.length - 1];
}

function toggle(obj, key) {
const clone = { ...obj };
if (clone[key]) {
Expand Down Expand Up @@ -84,7 +81,8 @@ function liftReducer(reducer, initialState) {
const initialLiftedState = {
committedState: initialState,
stagedActions: [INIT_ACTION],
skippedActions: {}
skippedActions: {},
currentStateIndex: 0
};

/**
Expand All @@ -95,34 +93,44 @@ function liftReducer(reducer, initialState) {
committedState,
stagedActions,
skippedActions,
computedStates
computedStates,
currentStateIndex
} = liftedState;

switch (liftedAction.type) {
case ActionTypes.RESET:
committedState = initialState;
stagedActions = [INIT_ACTION];
skippedActions = {};
currentStateIndex = 0;
break;
case ActionTypes.COMMIT:
committedState = last(computedStates).state;
committedState = computedStates[currentStateIndex].state;
stagedActions = [INIT_ACTION];
skippedActions = {};
currentStateIndex = 0;
break;
case ActionTypes.ROLLBACK:
stagedActions = [INIT_ACTION];
skippedActions = {};
currentStateIndex = 0;
break;
case ActionTypes.TOGGLE_ACTION:
const { index } = liftedAction;
skippedActions = toggle(skippedActions, index);
skippedActions = toggle(skippedActions, liftedAction.index);
break;
case ActionTypes.JUMP_TO_STATE:
currentStateIndex = liftedAction.index;
break;
case ActionTypes.SWEEP:
stagedActions = stagedActions.filter((_, i) => !skippedActions[i]);
skippedActions = {};
currentStateIndex = Math.max(currentStateIndex, stagedActions.length - 1);
break;
case ActionTypes.PERFORM_ACTION:
const { action } = liftedAction;
if (currentStateIndex === stagedActions.length - 1) {
currentStateIndex++;
}
stagedActions = [...stagedActions, action];
break;
default:
Expand All @@ -140,7 +148,8 @@ function liftReducer(reducer, initialState) {
committedState,
stagedActions,
skippedActions,
computedStates
computedStates,
currentStateIndex
};
};
}
Expand All @@ -157,8 +166,8 @@ function liftAction(action) {
* Unlifts the DevTools state to the app state.
*/
function unliftState(liftedState) {
const { computedStates } = liftedState;
const { state } = last(computedStates);
const { computedStates, currentStateIndex } = liftedState;
const { state } = computedStates[currentStateIndex];
return state;
}

Expand Down Expand Up @@ -197,6 +206,9 @@ export const ActionCreators = {
},
toggleAction(index) {
return { type: ActionTypes.TOGGLE_ACTION, index };
},
jumpToState(index) {
return { type: ActionTypes.JUMP_TO_STATE, index };
}
};

Expand Down
1 change: 1 addition & 0 deletions src/react/LogMonitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import LogMonitorEntry from './LogMonitorEntry';
export default class LogMonitor {
static propTypes = {
computedStates: PropTypes.array.isRequired,
currentStateIndex: PropTypes.number.isRequired,
stagedActions: PropTypes.array.isRequired,
skippedActions: PropTypes.object.isRequired,
reset: PropTypes.func.isRequired,
Expand Down