From 286f0d7acf509c43e9428e35c50d009c98a5f5c6 Mon Sep 17 00:00:00 2001 From: John Knox Date: Thu, 18 Jul 2019 05:23:02 -0700 Subject: [PATCH] Fix persistedState bug Summary: Persisted state was not being cleared across client disconnects. This fixes that. Reviewed By: passy Differential Revision: D16338524 fbshipit-source-id: ec51ec3bd999a388a0e8687f08841970872087ec --- src/reducers/__tests__/pluginStates.node.js | 20 ++++++++++++++++++-- src/reducers/pluginStates.js | 3 ++- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/src/reducers/__tests__/pluginStates.node.js b/src/reducers/__tests__/pluginStates.node.js index e7ab88fd5..57c5a7bd2 100644 --- a/src/reducers/__tests__/pluginStates.node.js +++ b/src/reducers/__tests__/pluginStates.node.js @@ -6,11 +6,27 @@ */ import {default as reducer, setPluginState} from '../pluginStates'; +import type {Action} from '../pluginStates'; test('reduce setPluginState', () => { - const res = reducer( + const result = reducer( {}, setPluginState({pluginKey: 'myPlugin', state: {a: 1}}), ); - expect(res).toEqual({myPlugin: {a: 1}}); + expect(result).toEqual({myPlugin: {a: 1}}); +}); + +test('CLEAR_PLUGIN_STATE removes plugin state', () => { + const clientId = 'app1#device1'; + const pluginKey = 'app1#device1#plugin1'; + + const action: Action = { + type: 'CLEAR_PLUGIN_STATE', + payload: {id: clientId, devicePlugins: new Set()}, + }; + const result = reducer( + {[pluginKey]: {a: 1}, 'anotherPlugin#key': {b: 2}}, + action, + ); + expect(result).toEqual({'anotherPlugin#key': {b: 2}}); }); diff --git a/src/reducers/pluginStates.js b/src/reducers/pluginStates.js index c08ca0cfd..6f899971a 100644 --- a/src/reducers/pluginStates.js +++ b/src/reducers/pluginStates.js @@ -49,8 +49,9 @@ export default function reducer( 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}. + const clientId = pluginKey.slice(0, pluginKey.lastIndexOf('#')); const pluginId = pluginKey.split('#').pop(); - if (pluginId !== payload.id || payload.devicePlugins.has(pluginId)) { + if (clientId !== payload.id || payload.devicePlugins.has(pluginId)) { newState[pluginKey] = state[pluginKey]; } return newState;