1- import { action , computed , observable } from 'mobx' ;
1+ import { action , computed , observable , IObservableValue } from 'mobx' ;
22import { RestoreCallback , Snapshots } from './types' ;
33import { recordedStores , restoreSnapshot } from './record' ;
44import { dehydrate } from './hydrate' ;
5+ import { config } from './configure' ;
56
67export class TimeTraveler {
78 @observable
8- private stacks : Snapshots [ ] = [ ] ;
9+ private stacks : IObservableValue < Snapshots > [ ] = [ ] ;
910
1011 @observable
1112 private cursor = 0 ;
1213
14+ private lastUpdate = 0 ;
15+
1316 public getSnapshots = ( ) : Snapshots => {
1417 return Object . entries ( recordedStores ) . reduce < Snapshots > ( ( accu , [ k , v ] ) => {
1518 accu [ k ] = dehydrate ( v ) ;
@@ -23,19 +26,32 @@ export class TimeTraveler {
2326 if ( payload ) {
2427 item . payload = payload ;
2528 }
26- this . stacks = [ item ] ;
29+ this . stacks = [ observable . box ( item , { deep : false } ) ] ;
2730 this . cursor = 0 ;
2831 } ;
2932
3033 @action
3134 public updateSnapshots = ( payload ?: Record < string , any > , snapshots ?: Snapshots ) => {
32- const { stacks } = this ;
35+ const { stacks, cursor } = this ;
3336 const item = snapshots || this . getSnapshots ( ) ;
3437 if ( payload ) {
3538 item . payload = payload ;
3639 }
37- stacks . push ( item ) ;
40+
41+ if ( cursor < stacks . length - 1 ) {
42+ stacks . splice ( cursor + 1 , stacks . length - cursor ) ;
43+ }
44+
45+ const boxedItem = observable . box ( item , { deep : false } ) ;
46+ const debounce = Date . now ( ) - this . lastUpdate < config . debounceTime ;
47+ if ( debounce ) {
48+ stacks [ stacks . length - 1 ] = boxedItem ;
49+ } else {
50+ stacks . push ( boxedItem ) ;
51+ }
52+
3853 this . cursor = stacks . length - 1 ;
54+ this . lastUpdate = Date . now ( ) ;
3955 } ;
4056
4157 @computed
@@ -56,8 +72,8 @@ export class TimeTraveler {
5672 if ( ! this . canUndo ) {
5773 return ;
5874 }
59- const currentSnapshots = stacks [ cursor ] ;
60- const snapshots = stacks [ cursor - 1 ] ;
75+ const currentSnapshots = stacks [ cursor ] . get ( ) ;
76+ const snapshots = stacks [ cursor - 1 ] . get ( ) ;
6177 this . cursor -= 1 ;
6278 return restoreSnapshot ( snapshots , ( ) => {
6379 this . restoreCallbacks . forEach ( callback => callback ( 'undo' , snapshots , currentSnapshots ) ) ;
@@ -70,8 +86,8 @@ export class TimeTraveler {
7086 if ( ! this . canRedo ) {
7187 return ;
7288 }
73- const currentSnapshots = stacks [ cursor ] ;
74- const snapshots = stacks [ cursor + 1 ] ;
89+ const currentSnapshots = stacks [ cursor ] . get ( ) ;
90+ const snapshots = stacks [ cursor + 1 ] . get ( ) ;
7591 this . cursor += 1 ;
7692 return restoreSnapshot ( snapshots , ( ) => {
7793 this . restoreCallbacks . forEach ( callback => callback ( 'redo' , snapshots , currentSnapshots ) ) ;
@@ -92,3 +108,5 @@ export class TimeTraveler {
92108}
93109
94110export const timeTraveler = new TimeTraveler ( ) ;
111+
112+ ( window as any ) . timeTraveler = timeTraveler ;
0 commit comments