Only store messages for plugins that are starred

Summary:
From several reviews / early feedbacks it was suggested several times to use the star mechanism to distinguish which plugins are allowed to send messages. This diff implements that:

- If a plugin is not selected, and not starred, it will drop the messages it received in the background
- This logic is still behind the same GK
- I think this change warrants upping the message queue limits significantly
- A future optimization would be to disable sending messages from the device side of things to reduce bridge usage, but that change is probably a lot more complicated with less impact
- In the next diff I'll make clear from UI perspective that unstarred plugins don't queue messages

In the attach video one can see how graphQL plugin keeps storing messages if it is starred, but if it isn't starred and not selected either, it will skip messages

{F225692819}

Reviewed By: jknoxville

Differential Revision: D19250928

fbshipit-source-id: 7e6af0eb6830dc79951cfd206e05b44061f1b14a
This commit is contained in:
Michel Weststrate
2020-01-02 07:12:06 -08:00
committed by Facebook Github Bot
parent 1caf5d5d3a
commit 04fcaddded
5 changed files with 140 additions and 81 deletions

View File

@@ -7,7 +7,7 @@
* @format
*/
import {PersistedStateReducer} from '../plugin';
import {PersistedStateReducer, FlipperDevicePlugin} from '../plugin';
import {State, MiddlewareAPI} from '../reducers/index';
import {setPluginState} from '../reducers/pluginStates';
import {flipperRecorderAddEvent} from './pluginStateRecorder';
@@ -18,6 +18,7 @@ import {
} from '../reducers/pluginMessageQueue';
import {Idler, BaseIdler} from './Idler';
import {getPluginKey} from './pluginUtils';
import {deconstructClientId} from './clientUtils';
const MAX_BACKGROUND_TASK_TIME = 25;
@@ -137,35 +138,46 @@ export function processMessageLater(
},
message: {method: string; params?: any},
) {
// TODO: can we make this better?
const selection = store.getState().connections;
const selectedPlugin =
selection.selectedPlugin &&
getPluginKey(
selection.selectedApp,
selection.selectedDevice,
selection.selectedPlugin,
);
// if the plugin is active, and has no queued messaged, process the message immediately
if (
selectedPlugin === pluginKey &&
getPendingMessages(store, pluginKey).length === 0
) {
processMessageImmediately(store, pluginKey, plugin, message);
} else {
// TODO: possible optimization: drop all messages for non-favorited plugins
// TODO: possible optimization: drop messages if queue is too large
store.dispatch(
queueMessage(
pluginKey,
message.method,
message.params,
plugin.maxQueueSize,
),
);
const isSelected = pluginKey === getSelectedPluginKey(store.getState());
switch (true) {
case isSelected && getPendingMessages(store, pluginKey).length === 0:
processMessageImmediately(store, pluginKey, plugin, message);
break;
case isSelected:
case plugin instanceof FlipperDevicePlugin:
case pluginIsStarred(store.getState(), plugin.id):
store.dispatch(
queueMessage(
pluginKey,
message.method,
message.params,
plugin.maxQueueSize,
),
);
break;
}
}
function getSelectedPluginKey(state: State): string | undefined {
return state.connections.selectedPlugin
? getPluginKey(
state.connections.selectedApp,
state.connections.selectedDevice,
state.connections.selectedPlugin,
)
: undefined;
}
function pluginIsStarred(state: State, pluginId: string): boolean {
const {selectedApp} = state.connections;
if (!selectedApp) {
return false;
}
const appInfo = deconstructClientId(selectedApp);
const starred = state.connections.userStarredPlugins[appInfo.app];
return starred && starred.indexOf(pluginId) > -1;
}
export async function processMessageQueue(
plugin: {
defaultPersistedState: any;