Move app/src (mostly) to flipper-ui-core/src
Summary: This diff moves all UI code from app/src to app/flipper-ui-core. That is now slightly too much (e.g. node deps are not removed yet), but from here it should be easier to move things out again, as I don't want this diff to be open for too long to avoid too much merge conflicts. * But at least flipper-ui-core is Electron free :) * Killed all cross module imports as well, as they where now even more in the way * Some unit test needed some changes, most not too big (but emotion hashes got renumbered in the snapshots, feel free to ignore that) * Found some files that were actually meaningless (tsconfig in plugins, WatchTools files, that start generating compile errors, removed those Follow up work: * make flipper-ui-core configurable, and wire up flipper-server-core in Electron instead of here * remove node deps (aigoncharov) * figure out correct place to load GKs, plugins, make intern requests etc., and move to the correct module * clean up deps Reviewed By: aigoncharov Differential Revision: D32427722 fbshipit-source-id: 14fe92e1ceb15b9dcf7bece367c8ab92df927a70
This commit is contained in:
committed by
Facebook GitHub Bot
parent
54b7ce9308
commit
7e50c0466a
129
desktop/flipper-ui-core/src/utils/messageQueue.tsx
Normal file
129
desktop/flipper-ui-core/src/utils/messageQueue.tsx
Normal file
@@ -0,0 +1,129 @@
|
||||
/**
|
||||
* Copyright (c) Facebook, Inc. and its affiliates.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*
|
||||
* @format
|
||||
*/
|
||||
|
||||
import {FlipperDevicePlugin} from '../plugin';
|
||||
import type {MiddlewareAPI} from '../reducers/index';
|
||||
import {
|
||||
clearMessageQueue,
|
||||
queueMessages,
|
||||
Message,
|
||||
DEFAULT_MAX_QUEUE_SIZE,
|
||||
} from '../reducers/pluginMessageQueue';
|
||||
import {IdlerImpl} from './Idler';
|
||||
import {isPluginEnabled, getSelectedPluginKey} from '../reducers/connections';
|
||||
import {deconstructPluginKey} from 'flipper-common';
|
||||
import {defaultEnabledBackgroundPlugins} from './pluginUtils';
|
||||
import {batch, Idler, _SandyPluginInstance} from 'flipper-plugin';
|
||||
import {addBackgroundStat} from './pluginStats';
|
||||
|
||||
function processMessagesImmediately(
|
||||
plugin: _SandyPluginInstance,
|
||||
messages: Message[],
|
||||
) {
|
||||
const reducerStartTime = Date.now();
|
||||
try {
|
||||
plugin.receiveMessages(messages);
|
||||
addBackgroundStat(plugin.definition.id, Date.now() - reducerStartTime);
|
||||
} catch (e) {
|
||||
console.error(
|
||||
`Failed to process event for plugin ${plugin.definition.id}`,
|
||||
e,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
export function processMessagesLater(
|
||||
store: MiddlewareAPI,
|
||||
pluginKey: string,
|
||||
plugin: _SandyPluginInstance,
|
||||
messages: Message[],
|
||||
) {
|
||||
const pluginId = plugin.definition.id;
|
||||
const isSelected =
|
||||
pluginKey === getSelectedPluginKey(store.getState().connections);
|
||||
switch (true) {
|
||||
// Navigation events are always processed immediately, to make sure the navbar stays up to date, see also T69991064
|
||||
case pluginId === 'Navigation':
|
||||
case isSelected && getPendingMessages(store, pluginKey).length === 0:
|
||||
processMessagesImmediately(plugin, messages);
|
||||
break;
|
||||
case isSelected:
|
||||
case plugin instanceof _SandyPluginInstance:
|
||||
case plugin instanceof FlipperDevicePlugin:
|
||||
case (plugin as any).prototype instanceof FlipperDevicePlugin:
|
||||
case isPluginEnabled(
|
||||
store.getState().connections.enabledPlugins,
|
||||
store.getState().connections.enabledDevicePlugins,
|
||||
deconstructPluginKey(pluginKey).client,
|
||||
pluginId,
|
||||
):
|
||||
store.dispatch(
|
||||
queueMessages(pluginKey, messages, DEFAULT_MAX_QUEUE_SIZE),
|
||||
);
|
||||
break;
|
||||
default:
|
||||
// In all other cases, messages will be dropped...
|
||||
if (!defaultEnabledBackgroundPlugins.includes(pluginId))
|
||||
console.warn(
|
||||
`Received message for disabled plugin ${pluginId}, dropping..`,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
export async function processMessageQueue(
|
||||
plugin: _SandyPluginInstance,
|
||||
pluginKey: string,
|
||||
store: MiddlewareAPI,
|
||||
progressCallback?: (progress: {current: number; total: number}) => void,
|
||||
idler: Idler = new IdlerImpl(),
|
||||
): Promise<boolean> {
|
||||
const total = getPendingMessages(store, pluginKey).length;
|
||||
let progress = 0;
|
||||
do {
|
||||
const messages = getPendingMessages(store, pluginKey);
|
||||
if (!messages.length) {
|
||||
break;
|
||||
}
|
||||
// there are messages to process! lets do so until we have to idle
|
||||
let offset = 0;
|
||||
batch(() => {
|
||||
do {
|
||||
// Optimization: we could send a batch of messages here
|
||||
processMessagesImmediately(plugin, [messages[offset]]);
|
||||
offset++;
|
||||
progress++;
|
||||
|
||||
progressCallback?.({
|
||||
total: Math.max(total, progress),
|
||||
current: progress,
|
||||
});
|
||||
} while (offset < messages.length && !idler.shouldIdle());
|
||||
// save progress
|
||||
// by writing progress away first and then idling, we make sure this logic is
|
||||
// resistent to kicking off this process twice; grabbing, processing messages, saving state is done synchronosly
|
||||
// until the idler has to break
|
||||
store.dispatch(clearMessageQueue(pluginKey, offset));
|
||||
});
|
||||
|
||||
if (idler.isCancelled()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
await idler.idle();
|
||||
// new messages might have arrived, so keep looping
|
||||
} while (getPendingMessages(store, pluginKey).length);
|
||||
return true;
|
||||
}
|
||||
|
||||
function getPendingMessages(
|
||||
store: MiddlewareAPI,
|
||||
pluginKey: string,
|
||||
): Message[] {
|
||||
return store.getState().pluginMessageQueue[pluginKey] || [];
|
||||
}
|
||||
Reference in New Issue
Block a user