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( sideEffect(
store, store,
{name: 'setupMenuBar', throttleMs: 100}, {name: 'setupMenuBar', throttleMs: 1000, fireImmediately: true},
(state) => state.plugins, (state) => state.plugins,
(plugins, store) => { (plugins, store) => {
setupMenuBar( setupMenuBar(

View File

@@ -221,4 +221,26 @@ describe('sideeffect', () => {
await sleep(100); await sleep(100);
expect(events).toEqual(['counter: 1', 'counter: 3', 'counter: 5']); 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 State = Store extends ReduxStore<infer S, any> ? S : never
>( >(
store: Store, store: Store,
options: {name: string; throttleMs: number}, options: {name: string; throttleMs: number; fireImmediately?: boolean},
selector: (state: State) => V, selector: (state: State) => V,
effect: (selectedState: V, store: Store) => void, effect: (selectedState: V, store: Store) => void,
): () => void { ): () => void {
@@ -54,9 +54,9 @@ export function sideEffect<
const duration = lastRun - start; const duration = lastRun - start;
if (duration > 15 && duration > options.throttleMs / 10) { if (duration > 15 && duration > options.throttleMs / 10) {
console.warn( console.warn(
`Side effect '${ `Side effect '${options.name}' took ${Math.round(
options.name duration,
}' took ${duration}ms, which exceeded its budget of ${Math.floor( )}ms, which exceeded its budget of ${Math.floor(
options.throttleMs / 10, options.throttleMs / 10,
)}ms. Please make the effect faster or increase the throttle time.`, )}ms. Please make the effect faster or increase the throttle time.`,
); );
@@ -84,6 +84,10 @@ export function sideEffect<
); );
}); });
if (options.fireImmediately) {
run();
}
return () => { return () => {
clearTimeout(timeout); clearTimeout(timeout);
unsubscribe(); unsubscribe();