11import { getActionsArray , evalAction } from 'remotedev-utils' ;
2+ import throttle from 'lodash/throttle' ;
23import createStore from '../../../app/stores/createStore' ;
34import configureStore , { getUrlParam } from '../../../app/stores/enhancerStore' ;
45import { isAllowed } from '../options/syncOptions' ;
@@ -32,18 +33,17 @@ const devToolsExtension = function(reducer, preloadedState, config) {
3233
3334 let store ;
3435 let errorOccurred = false ;
35- let sendingActionTimeout ;
36- let sendingStateTimeout ;
37- let sendingActionId ;
3836 let maxAge ;
3937 let actionCreators ;
4038 let shouldSerialize ;
4139 let serializeState ;
4240 let serializeAction ;
41+ let sendingActionId = 1 ;
4342 const instanceId = generateId ( config . instanceId ) ;
4443 const localFilter = getLocalFilter ( config ) ;
45- const latency = config . latency || 500 ;
46- let { statesFilter, actionsFilter, stateSanitizer, actionSanitizer, predicate } = config ;
44+ let {
45+ statesFilter, actionsFilter, stateSanitizer, actionSanitizer, predicate, latency = 500
46+ } = config ;
4747
4848 // Deprecate statesFilter and actionsFilter
4949 if ( statesFilter ) {
@@ -104,33 +104,40 @@ const devToolsExtension = function(reducer, preloadedState, config) {
104104 toContentScript ( message , serializeState , serializeAction , shouldSerialize ) ;
105105 }
106106
107- function relayState ( actions , shouldInit ) {
108- relay ( 'STATE' , store . liftedStore . getState ( ) , actions , undefined , shouldInit ) ;
109- }
107+ const relayState = throttle ( ( liftedState , actions , shouldInit ) => {
108+ relayAction . cancel ( ) ;
109+ const state = liftedState || store . liftedStore . getState ( ) ;
110+ sendingActionId = state . nextActionId ;
111+ relay ( 'STATE' , state , actions , undefined , shouldInit ) ;
112+ } , latency ) ;
110113
111- function relayPendingAction ( ) {
112- sendingActionTimeout = undefined ;
114+ const relayAction = throttle ( ( ) => {
113115 const liftedState = store . liftedStore . getState ( ) ;
114116 const nextActionId = liftedState . nextActionId ;
115- const state = liftedState . computedStates [ liftedState . computedStates . length - 1 ] . state ;
116- relay ( 'ACTION' , state , liftedState . actionsById [ nextActionId - 1 ] , nextActionId ) ;
117- }
118-
119- function relayPendingActions ( ) {
120- sendingStateTimeout = undefined ;
121- sendingActionTimeout = undefined ;
117+ const currentActionId = nextActionId - 1 ;
118+ const liftedAction = liftedState . actionsById [ currentActionId ] ;
122119
123- if ( sendingActionId === undefined ) {
124- relayState ( ) ; return ;
120+ // Send a single action
121+ if ( sendingActionId === currentActionId ) {
122+ sendingActionId = nextActionId ;
123+ const action = liftedAction . action ;
124+ const computedStates = liftedState . computedStates ;
125+ if (
126+ isFiltered ( action , localFilter ) ||
127+ predicate && ! predicate ( computedStates [ computedStates . length - 1 ] . state , action )
128+ ) return ;
129+ const state = liftedState . computedStates [ liftedState . computedStates . length - 1 ] . state ;
130+ relay ( 'ACTION' , state , liftedState . actionsById [ nextActionId - 1 ] , nextActionId ) ;
131+ return ;
125132 }
126133
127- const liftedState = store . liftedStore . getState ( ) ;
134+ // Send multiple actions
128135 const payload = startingFrom (
129136 sendingActionId ,
130137 liftedState ,
131138 localFilter , stateSanitizer , actionSanitizer , predicate
132139 ) ;
133- sendingActionId = undefined ;
140+ sendingActionId = nextActionId ;
134141 if ( typeof payload === 'undefined' ) return ;
135142 if ( typeof payload . skippedActionIds !== 'undefined' ) {
136143 relay ( 'STATE' , payload ) ;
@@ -143,7 +150,7 @@ const devToolsExtension = function(reducer, preloadedState, config) {
143150 instanceId,
144151 maxAge
145152 } , serializeState , serializeAction , shouldSerialize ) ;
146- }
153+ } , latency ) ;
147154
148155 function dispatchRemotely ( action ) {
149156 try {
@@ -187,7 +194,7 @@ const devToolsExtension = function(reducer, preloadedState, config) {
187194 if ( ! actionCreators && config . actionCreators ) {
188195 actionCreators = getActionsArray ( config . actionCreators ) ;
189196 }
190- relayState ( JSON . stringify ( actionCreators ) , true ) ;
197+ relayState ( undefined , JSON . stringify ( actionCreators ) , true ) ;
191198
192199 if ( reportId ) {
193200 relay ( 'GET_REPORT' , reportId ) ;
@@ -196,10 +203,8 @@ const devToolsExtension = function(reducer, preloadedState, config) {
196203 return ;
197204 case 'STOP' :
198205 monitor . stop ( ) ;
199- clearTimeout ( sendingActionTimeout ) ;
200- clearTimeout ( sendingStateTimeout ) ;
201- sendingStateTimeout = undefined ;
202- sendingActionTimeout = undefined ;
206+ relayAction . cancel ( ) ;
207+ relayState . cancel ( ) ;
203208 if ( ! message . failed ) relay ( 'STOP' ) ;
204209 }
205210 }
@@ -212,7 +217,7 @@ const devToolsExtension = function(reducer, preloadedState, config) {
212217 errorOccurred = true ;
213218 const state = store . liftedStore . getState ( ) ;
214219 if ( state . computedStates [ state . currentStateIndex ] . error ) {
215- relay ( 'STATE' , state ) ;
220+ relayState ( state ) ;
216221 }
217222 return true ;
218223 } ) ;
@@ -227,41 +232,17 @@ const devToolsExtension = function(reducer, preloadedState, config) {
227232 }
228233
229234 function handleChange ( ) {
230- if ( ! monitor . active || sendingStateTimeout ) return ;
235+ if ( ! monitor . active ) return ;
231236 if ( ! errorOccurred && ! monitor . isMonitorAction ( ) ) {
232- const liftedState = store . liftedStore . getState ( ) ;
233- const nextActionId = liftedState . nextActionId ;
234- const currentActionId = nextActionId - 1 ;
235- const liftedAction = liftedState . actionsById [ currentActionId ] ;
236- const action = liftedAction . action ;
237- if ( isFiltered ( action , localFilter ) ) return ;
238- if (
239- predicate &&
240- ! predicate ( liftedState . computedStates [ liftedState . computedStates . length - 1 ] . state , action )
241- ) return ;
242- if ( sendingActionTimeout ) {
243- clearTimeout ( sendingActionTimeout ) ;
244- sendingStateTimeout = setTimeout ( relayPendingActions , latency ) ;
245- return ;
246- }
247- sendingActionTimeout = setTimeout ( relayPendingAction , latency ) ;
248- sendingActionId = currentActionId ;
249- } else {
250- if ( monitor . isPaused ( ) || monitor . isLocked ( ) || monitor . isTimeTraveling ( ) ) return ;
251- if ( sendingStateTimeout ) {
252- clearTimeout ( sendingStateTimeout ) ;
253- sendingStateTimeout = undefined ;
254- sendingActionTimeout = undefined ;
255- } else if ( sendingActionTimeout ) {
256- clearTimeout ( sendingActionTimeout ) ;
257- sendingActionTimeout = undefined ;
258- }
259- const liftedState = store . liftedStore . getState ( ) ;
260- if ( errorOccurred && ! liftedState . computedStates [ liftedState . currentStateIndex ] . error ) {
261- errorOccurred = false ;
262- }
263- relay ( 'STATE' , liftedState ) ;
237+ relayAction ( ) ;
238+ return ;
239+ }
240+ if ( monitor . isPaused ( ) || monitor . isLocked ( ) || monitor . isTimeTraveling ( ) ) return ;
241+ const liftedState = store . liftedStore . getState ( ) ;
242+ if ( errorOccurred && ! liftedState . computedStates [ liftedState . currentStateIndex ] . error ) {
243+ errorOccurred = false ;
264244 }
245+ relayState ( liftedState ) ;
265246 }
266247
267248 const enhance = ( ) => ( next ) => {
0 commit comments