Fix initial main menu not loading

Summary: Fixes a regression (D20679687) of the main menu not loading immediately after application start

Reviewed By: passy, priteshrnandgaonkar

Differential Revision: D21227817

fbshipit-source-id: 37e4ddfcb73de3eac04d6162a3e028864d3e9e7f
This commit is contained in:
Michel Weststrate
2020-04-24 10:42:31 -07:00
committed by Facebook GitHub Bot
parent 4d58563168
commit d142369e9d
3 changed files with 31 additions and 5 deletions

View File

@@ -68,7 +68,7 @@ export default (store: Store, _logger: Logger) => {
sideEffect(
store,
{name: 'setupMenuBar', throttleMs: 100},
{name: 'setupMenuBar', throttleMs: 1000, fireImmediately: true},
(state) => state.plugins,
(plugins, store) => {
setupMenuBar(

View File

@@ -221,4 +221,26 @@ describe('sideeffect', () => {
await sleep(100);
expect(events).toEqual(['counter: 1', 'counter: 3', 'counter: 5']);
});
test('can fire immediately', async () => {
store.dispatch({type: 'inc'});
store.dispatch({type: 'inc'});
unsubscribe = sideEffect(
store,
{name: 'test', throttleMs: 1, fireImmediately: true},
(s) => s,
(s) => {
events.push(`counter: ${s.counter.count}`);
},
);
expect(events).toEqual(['counter: 2']);
store.dispatch({type: 'inc'});
store.dispatch({type: 'inc'});
// arrive as a single effect
await sleep(10);
expect(events).toEqual(['counter: 2', 'counter: 4']);
unsubscribe?.();
});
});

View File

@@ -28,7 +28,7 @@ export function sideEffect<
State = Store extends ReduxStore<infer S, any> ? S : never
>(
store: Store,
options: {name: string; throttleMs: number},
options: {name: string; throttleMs: number; fireImmediately?: boolean},
selector: (state: State) => V,
effect: (selectedState: V, store: Store) => void,
): () => void {
@@ -54,9 +54,9 @@ export function sideEffect<
const duration = lastRun - start;
if (duration > 15 && duration > options.throttleMs / 10) {
console.warn(
`Side effect '${
options.name
}' took ${duration}ms, which exceeded its budget of ${Math.floor(
`Side effect '${options.name}' took ${Math.round(
duration,
)}ms, which exceeded its budget of ${Math.floor(
options.throttleMs / 10,
)}ms. Please make the effect faster or increase the throttle time.`,
);
@@ -84,6 +84,10 @@ export function sideEffect<
);
});
if (options.fireImmediately) {
run();
}
return () => {
clearTimeout(timeout);
unsubscribe();