Files
flipper/src/reducers/pluginStates.js
Daniel Büchele f5dcaf02a4 persisted plugins state
Summary:
Two pros are passed into every plugin to persist state:
- `this.props.persistedState` which is the object of the persisted state
- `this.props.setPersistedState` which can be used to modify the persisted state

The state itself is stored in redux and therefore persisted when switching plugins.

The lifecycle hooks used a HOC are now implemented by the `ref`-function, which makes the code a little cleaner.

Reviewed By: jknoxville

Differential Revision: D8752097

fbshipit-source-id: d4f081f149cd840a29f1132bde91d72d3fba67ed
2018-07-10 02:33:51 -07:00

46 lines
860 B
JavaScript

/**
* Copyright 2018-present Facebook.
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
* @format
*/
export type State = {
[pluginKey: string]: Object,
};
export type Action = {
type: 'SET_PLUGIN_STATE',
payload: {
pluginKey: string,
state: Object,
},
};
const INITIAL_STATE: State = {};
export default function reducer(
state: State = INITIAL_STATE,
action: Action,
): State {
if (action.type === 'SET_PLUGIN_STATE') {
return {
...state,
[action.payload.pluginKey]: {
...state[action.payload.pluginKey],
...action.payload.state,
},
};
} else {
return state;
}
}
export const setPluginState = (payload: {
pluginKey: string,
state: Object,
}): Action => ({
type: 'SET_PLUGIN_STATE',
payload,
});