11// debug output
22let __DEBUG__
3- function debug ( ...args ) {
4- /* istanbul ignore if */
5- if ( __DEBUG__ ) {
6- if ( ! console . group ) {
7- args . unshift ( '%credux-undo' , 'font-style: italic' )
3+ /* istanbul ignore next: debug messaging is not tested */
4+ let debug = ( function debugGrouper ( ) {
5+ let displayBuffer
6+ const colors = {
7+ prevState : '#9E9E9E' ,
8+ action : '#03A9F4' ,
9+ nextState : '#4CAF50'
10+ }
11+ function initBuffer ( ) {
12+ displayBuffer = {
13+ header : [ ] ,
14+ prev : [ ] ,
15+ action : [ ] ,
16+ next : [ ] ,
17+ msgs : [ ]
818 }
9- console . log ( ...args )
1019 }
11- }
12- function debugStart ( action , state ) {
13- /* istanbul ignore if */
14- if ( __DEBUG__ ) {
15- const args = [ 'action' , action . type ]
20+ function printBuffer ( ) {
21+ let { header, prev, next, action, msgs } = displayBuffer
1622 if ( console . group ) {
17- args . unshift ( '%credux-undo' , 'font-style: italic' )
18- console . groupCollapsed ( ...args )
19- console . log ( 'received' , { state, action} )
23+ console . groupCollapsed ( ...header )
24+ console . log ( ...prev )
25+ console . log ( ...action )
26+ console . log ( ...next )
27+ console . log ( ...msgs )
28+ console . groupEnd ( )
2029 } else {
21- debug ( ...args )
30+ console . log ( ...header )
31+ console . log ( ...prev )
32+ console . log ( ...action )
33+ console . log ( ...next )
34+ console . log ( ...msgs )
2235 }
2336 }
24- }
25- function debugEnd ( ) {
26- /* istanbul ignore if */
27- if ( __DEBUG__ ) {
28- return console . groupEnd && console . groupEnd ( )
37+
38+ function colorFormat ( text , color , obj ) {
39+ return [
40+ `%c${ text } ` ,
41+ `color: ${ color } ; font-weight: bold` ,
42+ obj
43+ ]
2944 }
30- }
45+ function start ( action , state ) {
46+ initBuffer ( )
47+ if ( __DEBUG__ ) {
48+ if ( console . group ) {
49+ displayBuffer . header = [ '%credux-undo' , 'font-style: italic' , 'action' , action . type ]
50+ displayBuffer . action = colorFormat ( 'action' , colors . action , action )
51+ displayBuffer . prev = colorFormat ( 'prev history' , colors . prevState , state )
52+ } else {
53+ displayBuffer . header = [ 'redux-undo action' , action . type ]
54+ displayBuffer . action = [ 'action' , action ]
55+ displayBuffer . prev = [ 'prev history' , state ]
56+ }
57+ }
58+ }
59+
60+ function end ( nextState ) {
61+ if ( __DEBUG__ ) {
62+ if ( console . group ) {
63+ displayBuffer . next = colorFormat ( 'next history' , colors . nextState , nextState )
64+ } else {
65+ displayBuffer . next = [ 'next history' , nextState ]
66+ }
67+ printBuffer ( )
68+ }
69+ }
70+
71+ function log ( ...args ) {
72+ if ( __DEBUG__ ) {
73+ displayBuffer . msgs = displayBuffer . msgs
74+ . concat ( [ ...args , '\n' ] )
75+ }
76+ }
77+
78+ return {
79+ start,
80+ end,
81+ log
82+ }
83+ } ) ( )
3184// /debug output
3285
3386// action types
@@ -75,7 +128,8 @@ function length (history) {
75128// into `past`, setting the new `state` as `present` and erasing
76129// the `future`.
77130function insert ( history , state , limit ) {
78- debug ( 'insert' , { state, history, free : limit - length ( history ) } )
131+ debug . log ( 'inserting' , state )
132+ debug . log ( 'new free: ' , limit - length ( history ) )
79133
80134 const { past, present } = history
81135 const historyOverflow = limit && length ( history ) >= limit
@@ -93,8 +147,6 @@ function insert (history, state, limit) {
93147
94148// undo: go back to the previous point in history
95149function undo ( history ) {
96- debug ( 'undo' , { history} )
97-
98150 const { past, present, future } = history
99151
100152 if ( past . length <= 0 ) return history
@@ -112,8 +164,6 @@ function undo (history) {
112164
113165// redo: go to the next point in history
114166function redo ( history ) {
115- debug ( 'redo' , { history} )
116-
117167 const { past, present, future } = history
118168
119169 if ( future . length <= 0 ) return history
@@ -208,21 +258,21 @@ export default function undoable (reducer, rawConfig = {}) {
208258 }
209259
210260 return ( state = config . history , action = { } ) => {
211- debugStart ( action , state )
261+ debug . start ( action , state )
212262
213263 let history = state
214264 if ( ! config . history ) {
215- debug ( 'history is uninitialized' )
265+ debug . log ( 'history is uninitialized' )
216266
217267 if ( state === undefined ) {
218268 history = createHistory ( reducer ( state , { } ) )
219- debug ( 'do not initialize on probe actions' )
269+ debug . log ( 'do not initialize on probe actions' )
220270 } else if ( isHistory ( state ) ) {
221271 history = config . history = state
222- debug ( 'initialHistory initialized: initialState is a history' , config . history )
272+ debug . log ( 'initialHistory initialized: initialState is a history' , config . history )
223273 } else {
224274 history = config . history = createHistory ( state )
225- debug ( 'initialHistory initialized: initialState is not a history' , config . history )
275+ debug . log ( 'initialHistory initialized: initialState is not a history' , config . history )
226276 }
227277 }
228278
@@ -233,67 +283,68 @@ export default function undoable (reducer, rawConfig = {}) {
233283
234284 case config . undoType :
235285 res = undo ( history )
236- debug ( 'after undo', res )
237- debugEnd ( )
286+ debug . log ( 'perform undo', res )
287+ debug . end ( res )
238288 return res
239289
240290 case config . redoType :
241291 res = redo ( history )
242- debug ( 'after redo', res )
243- debugEnd ( )
292+ debug . log ( 'perform redo', res )
293+ debug . end ( res )
244294 return res
245295
246296 case config . jumpToPastType :
247297 res = jumpToPast ( history , action . index )
248- debug ( 'after jumpToPast' , res )
249- debugEnd ( )
298+ debug . log ( `perform jumpToPast to ${ action . index } ` , res )
299+ debug . end ( res )
250300 return res
251301
252302 case config . jumpToFutureType :
253303 res = jumpToFuture ( history , action . index )
254- debug ( 'after jumpToFuture' , res )
255- debugEnd ( )
304+ debug . log ( `perform jumpToFuture to ${ action . index } ` , res )
305+ debug . end ( res )
256306 return res
257307
258308 case config . jumpType :
259309 res = jump ( history , action . index )
260- debug ( 'after jump' , res )
261- debugEnd ( )
310+ debug . log ( `perform jump to ${ action . index } ` , res )
311+ debug . end ( res )
262312 return res
263313
264314 case config . clearHistoryType :
265315 res = createHistory ( history . present )
266- debug ( 'cleared history ', res )
267- debugEnd ( )
316+ debug . log ( 'perform clearHistory ', res )
317+ debug . end ( res )
268318 return res
269319
270320 default :
271321 res = reducer ( history . present , action )
272322
273323 if ( config . initTypes . some ( ( actionType ) => actionType === action . type ) ) {
274- debug ( 'reset history due to init action' )
275- debugEnd ( )
324+ debug . log ( 'reset history due to init action' )
325+ debug . end ( config . history )
276326 return config . history
277327 }
278328
329+ if ( history . present === res ) {
330+ // Don't handle this action. Do not call debug.end here,
331+ // because this action should not produce side effects to the console
332+ return history
333+ }
334+
279335 if ( typeof config . filter === 'function' && ! config . filter ( action , res , history ) ) {
280- debug ( 'filter prevented action, not storing it' )
281- debugEnd ( )
282- return {
336+ const nextState = {
283337 ...history ,
284338 present : res
285339 }
340+ debug . log ( 'filter prevented action, not storing it' )
341+ debug . end ( nextState )
342+ return nextState
286343 }
287344
288- if ( history . present !== res ) {
289- history = insert ( history , res , config . limit )
290- debug ( 'inserted new state into history' )
291- } else {
292- debug ( 'not inserted, history is unchanged' )
293- }
294-
295- debug ( 'history: ' , history , ' free: ' , config . limit - length ( history ) )
296- debugEnd ( )
345+ history = insert ( history , res , config . limit )
346+ debug . log ( 'inserted new state into history' )
347+ debug . end ( history )
297348 return history
298349 }
299350 }
0 commit comments