From 25158416ce07f56b4cd84beeb67efd67122fe5d3 Mon Sep 17 00:00:00 2001 From: Michel Weststrate Date: Wed, 18 Nov 2020 08:49:30 -0800 Subject: [PATCH] Freeze incoming data Summary: This diff will enabling freezing (making immutable) of all data we receive from the device. This prevents dev mistakes causing components not to update or logic being hard to reason about. Also this makes Immer's `produce` faster in the typical case Since this is potentially a risky change, that might break existing plugin logic, it has been put behind a GK https://www.internalfb.com/intern/gatekeeper/projects/flipper_frozen_data/ I did some quick exploratory testing on all plugins available for Facebook iOS / Android, and the only plugin that caused trouble was Fresco, which is fixed in this diff as well. Reviewed By: nikoant Differential Revision: D25055056 fbshipit-source-id: 8525511f4a8a0221740a6e1371ce7f2b757a203e --- desktop/app/src/Client.tsx | 6 ++++++ desktop/plugins/fresco/index.tsx | 10 +++++++--- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/desktop/app/src/Client.tsx b/desktop/app/src/Client.tsx index dbe21d902..13483049e 100644 --- a/desktop/app/src/Client.tsx +++ b/desktop/app/src/Client.tsx @@ -39,6 +39,8 @@ import {batch} from 'react-redux'; import {_SandyPluginInstance} from 'flipper-plugin'; import {flipperMessagesClientPlugin} from './utils/self-inspection/plugins/FlipperMessagesClientPlugin'; import {getFlipperLibImplementation} from './utils/flipperLibImplementation'; +import {freeze} from 'immer'; +import GK from './fb-stubs/GK'; type Plugins = Array; @@ -133,6 +135,7 @@ export default class Client extends EventEmitter { connection: FlipperClientConnection | null | undefined; store: Store; activePlugins: Set; + freezeData = GK.get('flipper_frozen_data'); /** * @deprecated @@ -395,6 +398,9 @@ export default class Client extends EventEmitter { let rawData; try { rawData = JSON.parse(msg); + if (this.freezeData) { + rawData = freeze(rawData, true); + } } catch (err) { console.error(`Invalid JSON: ${msg}`, 'clientMessage'); return; diff --git a/desktop/plugins/fresco/index.tsx b/desktop/plugins/fresco/index.tsx index ced02004f..a36e309c7 100644 --- a/desktop/plugins/fresco/index.tsx +++ b/desktop/plugins/fresco/index.tsx @@ -132,7 +132,10 @@ export default class FlipperImagesPlugin extends FlipperPlugin< ) { const surface = attribution[0] ? attribution[0].trim() : undefined; if (surface && surface.length > 0) { - pluginData.surfaceList.add(surface); + pluginData.surfaceList = new Set([ + ...pluginData.surfaceList, + surface, + ]); } } pluginData = { @@ -182,16 +185,17 @@ export default class FlipperImagesPlugin extends FlipperPlugin< } else if (method == 'events') { const event: ImageEvent = data as ImageEvent; debugLog('Received events', event); - const {surfaceList} = persistedState; + let {surfaceList} = persistedState; const {attribution} = event; if (attribution instanceof Array && attribution.length > 0) { const surface = attribution[0] ? attribution[0].trim() : undefined; if (surface && surface.length > 0) { - surfaceList.add(surface); + surfaceList = new Set([...surfaceList, surface]); } } return { ...persistedState, + surfaceList, events: [ {eventId: persistedState.nextEventId, ...event}, ...persistedState.events,