File size: 2,638 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/**
 * Action Log Redux store enhancer
 *
 * Inject at the bottom of the `createStore` enhancer
 * chain in order to provide access to dispatched actions
 * as well as the state and store directly from the console.
 *
 * Will only attach if the `window` variable is available
 * globally. If not it will simply be an empty link in the
 * chain, passing straight through.
 */

const state = {
	actionHistory: [],
	shouldRecordActions: true,
	historySize: 100,
	watchPredicate: null,
};

export const queryToPredicate = ( query ) => {
	if ( query instanceof RegExp ) {
		return ( { type } ) => query.test( type );
	}
	if ( 'string' === typeof query ) {
		return ( { type } ) => type === query;
	}
	if ( 'function' === typeof query ) {
		return query;
	}

	throw new TypeError( 'provide string or RegExp matching `action.type` or a predicate function' );
};

const actionLog = {
	clear: () => void ( state.actionHistory = [] ),
	filter: ( query ) => state.actionHistory.filter( queryToPredicate( query ) ),
	setSize: ( size ) => void ( state.historySize = size ),
	start: () => void ( state.shouldRecordActions = true ),
	stop: () => void ( state.shouldRecordActions = false ),
	unwatch: () => void ( state.watchPredicate = null ),
	watch: ( query ) => void ( state.watchPredicate = query ? queryToPredicate( query ) : null ),
};

Object.defineProperty( actionLog, 'history', {
	enumerable: true,
	get: () => state.actionHistory,
} );

const recordAction = ( action ) => {
	const { actionHistory, historySize } = state;

	const thunkDescription = 'function' === typeof action ? { type: 'thunk (hidden)' } : {};

	actionHistory.push( {
		...action,
		...thunkDescription,
		meta: {
			...action.meta,
			timestamp: Date.now(),
		},
	} );

	// cheap optimization to keep from
	// thrashing once we hit our size limit
	if ( actionHistory.length > 2 * historySize ) {
		state.actionHistory = actionHistory.slice( -1 * historySize );
	}
};

export const actionLogger =
	( next ) =>
	( ...args ) => {
		const store = next( ...args );

		if ( 'undefined' === typeof window ) {
			return store;
		}

		const dispatch = ( action ) => {
			if ( state.shouldRecordActions ) {
				recordAction( action );
			}

			/* eslint-disable no-console */
			if (
				'function' === typeof state.watchPredicate &&
				'function' === typeof console.log &&
				state.watchPredicate( action )
			) {
				console.log( 'Watched action observed:\n%o', action );
			}
			/* eslint-enable no-console */

			return store.dispatch( action );
		};

		Object.assign( window, {
			actionLog,
		} );

		return {
			...store,
			dispatch,
		};
	};

export default actionLogger;