call notifications API

Summary:
Adding a static method plugins can implement to trigger notifications:

```
static getActiveNotifications: ?(persistedState: P) => Array<Notification>;
```

When the plugin's persisted state changes, this API is called from the `notification`-dispatcher which updates the notifications store.

Reviewed By: passy

Differential Revision: D10378434

fbshipit-source-id: 778fe3ad4229b03bd5ba14ebfdafa5020c25f34f
This commit is contained in:
Daniel Büchele
2018-10-18 02:45:28 -07:00
committed by Facebook Github Bot
parent ce996ba8af
commit 51f70fd78c
4 changed files with 47 additions and 40 deletions

View File

@@ -10,6 +10,7 @@ import type {App} from './App.js';
import type Logger from './fb-stubs/Logger.js'; import type Logger from './fb-stubs/Logger.js';
import type {Store} from './reducers/index.js'; import type {Store} from './reducers/index.js';
import {setPluginState} from './reducers/pluginStates.js';
import {clientPlugins} from './plugins/index.js'; import {clientPlugins} from './plugins/index.js';
import {ReactiveSocket, PartialResponder} from 'rsocket-core'; import {ReactiveSocket, PartialResponder} from 'rsocket-core';
@@ -169,17 +170,17 @@ export default class Client extends EventEmitter {
if (persistingPlugin) { if (persistingPlugin) {
const pluginKey = `${this.id}#${params.api}`; const pluginKey = `${this.id}#${params.api}`;
const persistedState = this.store.getState().pluginStates[pluginKey]; const persistedState = this.store.getState().pluginStates[pluginKey];
this.store.dispatch({
type: 'SET_PLUGIN_STATE',
payload: {
pluginKey,
// $FlowFixMe: We checked persistedStateReducer exists // $FlowFixMe: We checked persistedStateReducer exists
state: persistingPlugin.persistedStateReducer( const newPluginState = persistingPlugin.persistedStateReducer(
persistedState, persistedState,
params.params, params.params,
), );
}, this.store.dispatch(
}); setPluginState({
pluginKey,
state: newPluginState,
}),
);
} else { } else {
const apiCallbacks = this.broadcastCallbacks.get(params.api); const apiCallbacks = this.broadcastCallbacks.get(params.api);
if (!apiCallbacks) { if (!apiCallbacks) {

View File

@@ -6,12 +6,11 @@
*/ */
import type {FlipperPlugin, FlipperBasePlugin} from './plugin.js'; import type {FlipperPlugin, FlipperBasePlugin} from './plugin.js';
import type LogManager from './fb-stubs/Logger'; import type LogManager from './fb-stubs/Logger';
import type Client from './Client.js';
import type BaseDevice from './devices/BaseDevice.js'; import type BaseDevice from './devices/BaseDevice.js';
import type {Props as PluginProps} from './plugin'; import type {Props as PluginProps} from './plugin';
import {FlipperDevicePlugin} from './plugin.js'; import {FlipperDevicePlugin} from './plugin.js';
import type {Notification} from './plugin.js'; import Client from './Client.js';
import { import {
ErrorBoundary, ErrorBoundary,
Component, Component,
@@ -23,7 +22,6 @@ import {
import React from 'react'; import React from 'react';
import {connect} from 'react-redux'; import {connect} from 'react-redux';
import {setPluginState} from './reducers/pluginStates.js'; import {setPluginState} from './reducers/pluginStates.js';
import {setActiveNotifications} from './reducers/notifications.js';
import {devicePlugins, clientPlugins} from './plugins/index.js'; import {devicePlugins, clientPlugins} from './plugins/index.js';
import NotificationsHub from './NotificationsHub'; import NotificationsHub from './NotificationsHub';
import {activateMenuItems} from './MenuBar.js'; import {activateMenuItems} from './MenuBar.js';
@@ -54,10 +52,6 @@ type Props = {
pluginKey: string, pluginKey: string,
state: Object, state: Object,
}) => void, }) => void,
setActiveNotifications: ({
pluginId: string,
notifications: Array<Notification>,
}) => void,
deepLinkPayload: ?string, deepLinkPayload: ?string,
}; };
@@ -131,7 +125,7 @@ class PluginContainer extends Component<Props, State> {
}; };
render() { render() {
const {pluginStates, setPluginState, setActiveNotifications} = this.props; const {pluginStates, setPluginState} = this.props;
const {activePlugin, pluginKey, target} = this.state; const {activePlugin, pluginKey, target} = this.state;
if (!activePlugin || !target) { if (!activePlugin || !target) {
@@ -142,18 +136,7 @@ class PluginContainer extends Component<Props, State> {
key: pluginKey, key: pluginKey,
logger: this.props.logger, logger: this.props.logger,
persistedState: pluginStates[pluginKey] || {}, persistedState: pluginStates[pluginKey] || {},
setPersistedState: state => { setPersistedState: state => setPluginState({pluginKey, state}),
// We are using setTimout here to wait for previous state updated to
// finish before triggering a new state update. Otherwise this can
// cause race conditions, with multiple state updates happening at the
// same time.
setTimeout(() => setPluginState({pluginKey, state}), 0);
},
setActiveNotifications: (notifications: Array<Notification>) =>
setActiveNotifications({
pluginId: pluginKey,
notifications: notifications,
}),
target, target,
deepLinkPayload: this.props.deepLinkPayload, deepLinkPayload: this.props.deepLinkPayload,
ref: this.refChanged, ref: this.refChanged,
@@ -197,6 +180,5 @@ export default connect(
}), }),
{ {
setPluginState, setPluginState,
setActiveNotifications,
}, },
)(PluginContainer); )(PluginContainer);

View File

@@ -8,17 +8,45 @@
import type {Store} from '../reducers/index.js'; import type {Store} from '../reducers/index.js';
import type Logger from '../fb-stubs/Logger.js'; import type Logger from '../fb-stubs/Logger.js';
import type {PluginNotification} from '../reducers/notifications'; import type {PluginNotification} from '../reducers/notifications';
import type {FlipperPlugin} from '../plugin.js';
import {selectPlugin} from '../reducers/connections'; import {selectPlugin} from '../reducers/connections';
import {setActiveNotifications} from '../reducers/notifications';
import {textContent} from '../utils/index'; import {textContent} from '../utils/index';
import {clientPlugins} from '../plugins/index.js';
export default (store: Store, logger: Logger) => { export default (store: Store, logger: Logger) => {
const knownNotifications: Set<string> = new Set(); const knownNotifications: Set<string> = new Set();
const knownPluginStates: Map<string, Object> = new Map();
store.subscribe(() => { store.subscribe(() => {
const { const {notifications, pluginStates} = store.getState();
activeNotifications,
blacklistedPlugins, Object.keys(pluginStates).forEach(key => {
} = store.getState().notifications; if (knownPluginStates.get(key) !== pluginStates[key]) {
knownPluginStates.set(key, pluginStates[key]);
const [client, pluginId] = key.split('#');
const persistingPlugin: ?Class<FlipperPlugin<>> = clientPlugins.find(
(p: Class<FlipperPlugin<>>) =>
p.id === pluginId && p.getActiveNotifications,
);
if (persistingPlugin) {
store.dispatch(
setActiveNotifications({
// $FlowFixMe: Ensured getActiveNotifications is implemented in filter
notifications: persistingPlugin.getActiveNotifications(
pluginStates[key],
),
client,
pluginId,
}),
);
}
}
});
const {activeNotifications, blacklistedPlugins} = notifications;
activeNotifications.forEach((n: PluginNotification) => { activeNotifications.forEach((n: PluginNotification) => {
if ( if (

View File

@@ -38,7 +38,6 @@ export type Props<T> = {
logger: Logger, logger: Logger,
persistedState: T, persistedState: T,
setPersistedState: (state: $Shape<T>) => void, setPersistedState: (state: $Shape<T>) => void,
setActiveNotifications: (Array<Notification>) => void,
target: PluginTarget, target: PluginTarget,
deepLinkPayload: ?string, deepLinkPayload: ?string,
}; };
@@ -97,10 +96,6 @@ export class FlipperBasePlugin<
throw new TypeError(`Reducer ${actionData.type} isn't a function`); throw new TypeError(`Reducer ${actionData.type} isn't a function`);
} }
} }
componentDidUpdate(props: Props<*>, state: State): void {
props.setActiveNotifications(this.computeNotifications(props, state));
}
} }
export class FlipperDevicePlugin<S = *, A = *, P = *> extends FlipperBasePlugin< export class FlipperDevicePlugin<S = *, A = *, P = *> extends FlipperBasePlugin<
@@ -132,6 +127,7 @@ export class FlipperPlugin<S = *, A = *, P = *> extends FlipperBasePlugin<
P, P,
> { > {
static persistedStateReducer: ?(persistedState: P, data: Object) => $Shape<P>; static persistedStateReducer: ?(persistedState: P, data: Object) => $Shape<P>;
static getActiveNotifications: ?(persistedState: P) => Array<Notification>;
constructor(props: Props<*>) { constructor(props: Props<*>) {
super(props); super(props);