From 4f08fd87113339255c74f6d3e33c9a423391e3e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20B=C3=BCchele?= Date: Fri, 19 Oct 2018 05:12:29 -0700 Subject: [PATCH] remove pluginStates on disconnect Summary: When a client disconnects, we want to remove all plugins states for this client, so the next time the client reconnects the plugins start with an empty state. The main use case for this is when recompiling the app and launching it in the simulator, we don't want to show old network-requests or QPL events. Reviewed By: passy Differential Revision: D10447808 fbshipit-source-id: 5fb3f24ee37f564e8dc00315bff86a2bcd5f65f2 --- src/dispatcher/server.js | 4 ++++ src/reducers/pluginStates.js | 29 ++++++++++++++++++++++------- 2 files changed, 26 insertions(+), 7 deletions(-) diff --git a/src/dispatcher/server.js b/src/dispatcher/server.js index 8eab4b1cf..dda29807d 100644 --- a/src/dispatcher/server.js +++ b/src/dispatcher/server.js @@ -26,6 +26,10 @@ export default (store: Store, logger: Logger) => { type: 'CLIENT_REMOVED', payload: id, }); + store.dispatch({ + type: 'CLEAR_PLUGIN_STATE', + payload: id, + }); }); server.addListener('error', err => { diff --git a/src/reducers/pluginStates.js b/src/reducers/pluginStates.js index c264d255c..0c249da37 100644 --- a/src/reducers/pluginStates.js +++ b/src/reducers/pluginStates.js @@ -9,13 +9,18 @@ export type State = { [pluginKey: string]: Object, }; -export type Action = { - type: 'SET_PLUGIN_STATE', - payload: { - pluginKey: string, - state: Object, - }, -}; +export type Action = + | { + type: 'SET_PLUGIN_STATE', + payload: { + pluginKey: string, + state: Object, + }, + } + | { + type: 'CLEAR_PLUGIN_STATE', + payload: string, + }; const INITIAL_STATE: State = {}; @@ -31,6 +36,16 @@ export default function reducer( ...action.payload.state, }, }; + } else if (action.type === 'CLEAR_PLUGIN_STATE') { + const {payload} = action; + return Object.keys(state).reduce((newState, pluginKey) => { + // Only add the pluginState, if its from a plugin other than the one that + // was removed. pluginKeys are in the form of ${clientID}#${pluginID}. + if (!pluginKey.startsWith(payload)) { + newState[pluginKey] = state[pluginKey]; + } + return newState; + }, {}); } else { return state; }