Skip to content

Commit 0487378

Browse files
authored
chore: stop redux action console flood in dev (#7412)
1 parent b80c568 commit 0487378

3 files changed

Lines changed: 71 additions & 2 deletions

File tree

app/lib/store/actionBuffer.test.ts

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import { applyMiddleware, createStore } from 'redux';
2+
3+
import { actionBuffer } from './actionBuffer';
4+
5+
const trivialReducer = (state = {}, _action: any) => state;
6+
7+
const makeStore = () => createStore(trivialReducer, applyMiddleware(actionBuffer()));
8+
9+
beforeEach(() => {
10+
(global as any).__reduxActions = undefined;
11+
});
12+
13+
describe('actionBuffer middleware', () => {
14+
it('records the dispatched action type in global.__reduxActions', () => {
15+
const store = makeStore();
16+
store.dispatch({ type: 'TEST_ACTION' });
17+
const buf: any[] = (global as any).__reduxActions;
18+
expect(buf).toBeDefined();
19+
expect(buf[buf.length - 1].type).toBe('TEST_ACTION');
20+
});
21+
22+
it('caps the buffer at 100 and drops the oldest entry (FIFO)', () => {
23+
const store = makeStore();
24+
for (let i = 0; i < 110; i++) {
25+
store.dispatch({ type: `ACTION_${i}` });
26+
}
27+
const buf: any[] = (global as any).__reduxActions;
28+
expect(buf.length).toBe(100);
29+
expect(buf[0].type).toBe('ACTION_10');
30+
expect(buf[99].type).toBe('ACTION_109');
31+
});
32+
33+
it('writes nothing to the console', () => {
34+
const logSpy = jest.spyOn(console, 'log').mockImplementation(() => {});
35+
const infoSpy = jest.spyOn(console, 'info').mockImplementation(() => {});
36+
const groupSpy = jest.spyOn(console, 'group').mockImplementation(() => {});
37+
const groupEndSpy = jest.spyOn(console, 'groupEnd').mockImplementation(() => {});
38+
39+
const store = makeStore();
40+
store.dispatch({ type: 'SILENT_ACTION' });
41+
42+
expect(logSpy).not.toHaveBeenCalled();
43+
expect(infoSpy).not.toHaveBeenCalled();
44+
expect(groupSpy).not.toHaveBeenCalled();
45+
expect(groupEndSpy).not.toHaveBeenCalled();
46+
47+
logSpy.mockRestore();
48+
infoSpy.mockRestore();
49+
groupSpy.mockRestore();
50+
groupEndSpy.mockRestore();
51+
});
52+
});

app/lib/store/actionBuffer.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
const CAP = 100;
2+
3+
export const actionBuffer = () => (_store: any) => (next: any) => (action: any) => {
4+
(global as any).__reduxActions ??= [];
5+
const buf = (global as any).__reduxActions;
6+
buf.push({ type: action.type, t: Date.now() });
7+
if (buf.length > CAP) buf.shift();
8+
return next(action);
9+
};

app/lib/store/index.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,25 @@ import reducers from '../../reducers';
55
import sagas from '../../sagas';
66
import applyAppStateMiddleware from './appStateMiddleware';
77
import applyInternetStateMiddleware from './internetStateMiddleware';
8-
import { logger } from './reduxLogger';
98

109
let sagaMiddleware;
1110
let enhancers;
1211

1312
if (__DEV__) {
1413
const reduxImmutableStateInvariant = require('redux-immutable-state-invariant').default();
14+
const { actionBuffer } = require('./actionBuffer');
15+
// Uncomment the next line (and the applyMiddleware(logger) line below) to print every action + next state to Metro.
16+
// const { logger } = require('./reduxLogger');
17+
1518
sagaMiddleware = createSagaMiddleware();
1619

1720
enhancers = compose(
1821
applyAppStateMiddleware(),
1922
applyInternetStateMiddleware(),
2023
applyMiddleware(reduxImmutableStateInvariant),
2124
applyMiddleware(sagaMiddleware),
22-
applyMiddleware(logger)
25+
applyMiddleware(actionBuffer())
26+
// applyMiddleware(logger)
2327
);
2428
} else {
2529
sagaMiddleware = createSagaMiddleware();
@@ -29,4 +33,8 @@ if (__DEV__) {
2933
const store = createStore(reducers, enhancers);
3034
sagaMiddleware.run(sagas);
3135

36+
if (__DEV__) {
37+
(global as any).reduxStore = store;
38+
}
39+
3240
export default store;

0 commit comments

Comments
 (0)