@@ -5295,9 +5295,9 @@ return /******/ (function(modules) { // webpackBootstrap
5295
5295
/**
5296
5296
* The state for the whole cluster
5297
5297
*/
5298
- this . __state = Immutable . Map ( { } )
5298
+ this . state = Immutable . Map ( { } )
5299
5299
/**
5300
- * Holds a map of id => reactor instance
5300
+ * Holds a map of id => store instance
5301
5301
*/
5302
5302
this . __stores = Immutable . Map ( { } )
5303
5303
@@ -5306,7 +5306,10 @@ return /******/ (function(modules) { // webpackBootstrap
5306
5306
* Change observer interface to observe certain keypaths
5307
5307
* Created after __initialize so it starts with initialState
5308
5308
*/
5309
- this . __changeObserver = new ChangeObserver ( this . __state , this . __evaluator )
5309
+ this . __changeObserver = new ChangeObserver ( this . state , this . __evaluator )
5310
+
5311
+ this . __isBatching = false ;
5312
+ this . __batchDispatchCount = 0 ;
5310
5313
}
5311
5314
5312
5315
/**
@@ -5315,7 +5318,7 @@ return /******/ (function(modules) { // webpackBootstrap
5315
5318
* @return {* }
5316
5319
*/
5317
5320
Object . defineProperty ( Reactor . prototype , "evaluate" , { writable :true , configurable :true , value :function ( keyPathOrGetter ) { "use strict" ;
5318
- return this . __evaluator . evaluate ( this . __state , keyPathOrGetter )
5321
+ return this . __evaluator . evaluate ( this . state , keyPathOrGetter )
5319
5322
} } ) ;
5320
5323
5321
5324
/**
@@ -5360,43 +5363,26 @@ return /******/ (function(modules) { // webpackBootstrap
5360
5363
* @param {object|undefined } payload
5361
5364
*/
5362
5365
Object . defineProperty ( Reactor . prototype , "dispatch" , { writable :true , configurable :true , value :function ( actionType , payload ) { "use strict" ;
5363
- var debug = this . debug
5364
- var prevState = this . __state
5365
-
5366
- this . __state = this . __state . withMutations ( function ( state ) {
5367
- if ( this . debug ) {
5368
- logging . dispatchStart ( actionType , payload )
5369
- }
5370
-
5371
- // let each core handle the message
5372
- this . __stores . forEach ( function ( store , id ) {
5373
- var currState = state . get ( id )
5374
- var newState = store . handle ( currState , actionType , payload )
5366
+ var prevState = this . state
5367
+ this . state = this . __handleAction ( prevState , actionType , payload )
5375
5368
5376
- if ( debug && newState === undefined ) {
5377
- var error = 'Store handler must return a value, did you forget a return statement'
5378
- logging . dispatchError ( error )
5379
- throw new Error ( error )
5380
- }
5381
-
5382
- state . set ( id , newState )
5383
-
5384
- if ( this . debug ) {
5385
- logging . storeHandled ( id , currState , newState )
5386
- }
5387
- } . bind ( this ) )
5388
-
5389
- if ( this . debug ) {
5390
- logging . dispatchEnd ( state )
5391
- }
5392
- } . bind ( this ) )
5393
-
5394
- // write the new state to the output stream if changed
5395
- if ( this . __state !== prevState ) {
5396
- this . __changeObserver . notifyObservers ( this . __state )
5369
+ if ( this . __isBatching ) {
5370
+ this . __batchDispatchCount ++
5371
+ } else if ( this . state !== prevState ) {
5372
+ this . __notify ( )
5397
5373
}
5398
5374
} } ) ;
5399
5375
5376
+ /**
5377
+ * Allows batching of dispatches before notifying change observers
5378
+ * @param {Function } fn
5379
+ */
5380
+ Object . defineProperty ( Reactor . prototype , "batch" , { writable :true , configurable :true , value :function ( fn ) { "use strict" ;
5381
+ this . __batchStart ( )
5382
+ fn ( )
5383
+ this . __batchEnd ( )
5384
+ } } ) ;
5385
+
5400
5386
/**
5401
5387
* @deprecated
5402
5388
* @param {String } id
@@ -5429,10 +5415,10 @@ return /******/ (function(modules) { // webpackBootstrap
5429
5415
}
5430
5416
5431
5417
this . __stores = this . __stores . set ( id , store )
5432
- this . __state = this . __state . set ( id , initialState )
5418
+ this . state = this . state . set ( id , initialState )
5433
5419
} . bind ( this ) )
5434
5420
5435
- this . __changeObserver . notifyObservers ( this . __state )
5421
+ this . __notify ( )
5436
5422
} } ) ;
5437
5423
5438
5424
/**
@@ -5442,7 +5428,7 @@ return /******/ (function(modules) { // webpackBootstrap
5442
5428
Object . defineProperty ( Reactor . prototype , "serialize" , { writable :true , configurable :true , value :function ( ) { "use strict" ;
5443
5429
var serialized = { }
5444
5430
this . __stores . forEach ( function ( store , id ) {
5445
- var storeState = this . __state . get ( id )
5431
+ var storeState = this . state . get ( id )
5446
5432
serialized [ id ] = store . serialize ( storeState )
5447
5433
} . bind ( this ) )
5448
5434
return serialized
@@ -5461,18 +5447,18 @@ return /******/ (function(modules) { // webpackBootstrap
5461
5447
} . bind ( this ) )
5462
5448
} . bind ( this ) )
5463
5449
5464
- this . __state = this . __state . merge ( stateToLoad )
5465
- this . __changeObserver . notifyObservers ( this . __state )
5450
+ this . state = this . state . merge ( stateToLoad )
5451
+ this . __notify ( )
5466
5452
} } ) ;
5467
5453
5468
5454
/**
5469
5455
* Resets the state of a reactor and returns back to initial state
5470
5456
*/
5471
5457
Object . defineProperty ( Reactor . prototype , "reset" , { writable :true , configurable :true , value :function ( ) { "use strict" ;
5472
5458
var debug = this . debug
5473
- var prevState = this . __state
5459
+ var prevState = this . state
5474
5460
5475
- this . __state = Immutable . Map ( ) . withMutations ( function ( state ) {
5461
+ this . state = Immutable . Map ( ) . withMutations ( function ( state ) {
5476
5462
this . __stores . forEach ( function ( store , id ) {
5477
5463
var storeState = prevState . get ( id )
5478
5464
var resetStoreState = store . handleReset ( storeState )
@@ -5487,7 +5473,70 @@ return /******/ (function(modules) { // webpackBootstrap
5487
5473
} . bind ( this ) )
5488
5474
5489
5475
this . __evaluator . reset ( )
5490
- this . __changeObserver . reset ( this . __state )
5476
+ this . __changeObserver . reset ( this . state )
5477
+ } } ) ;
5478
+
5479
+ /**
5480
+ * Notifies all change observers with the current state
5481
+ * @private
5482
+ */
5483
+ Object . defineProperty ( Reactor . prototype , "__notify" , { writable :true , configurable :true , value :function ( ) { "use strict" ;
5484
+ this . __changeObserver . notifyObservers ( this . state )
5485
+ } } ) ;
5486
+
5487
+
5488
+ /**
5489
+ * Reduces the current state to the new state given actionType / message
5490
+ * @param {string } actionType
5491
+ * @param {object|undefined } payload
5492
+ * @return {Immutable.Map }
5493
+ */
5494
+ Object . defineProperty ( Reactor . prototype , "__handleAction" , { writable :true , configurable :true , value :function ( state , actionType , payload ) { "use strict" ;
5495
+ return state . withMutations ( function ( state ) {
5496
+ if ( this . debug ) {
5497
+ logging . dispatchStart ( actionType , payload )
5498
+ }
5499
+
5500
+ // let each core handle the message
5501
+ this . __stores . forEach ( function ( store , id ) {
5502
+ var currState = state . get ( id )
5503
+ var newState = store . handle ( currState , actionType , payload )
5504
+
5505
+ if ( this . debug && newState === undefined ) {
5506
+ var error = 'Store handler must return a value, did you forget a return statement'
5507
+ logging . dispatchError ( error )
5508
+ throw new Error ( error )
5509
+ }
5510
+
5511
+ state . set ( id , newState )
5512
+
5513
+ if ( this . debug ) {
5514
+ logging . storeHandled ( id , currState , newState )
5515
+ }
5516
+ } . bind ( this ) )
5517
+
5518
+ if ( this . debug ) {
5519
+ logging . dispatchEnd ( state )
5520
+ }
5521
+ } . bind ( this ) )
5522
+ } } ) ;
5523
+
5524
+ Object . defineProperty ( Reactor . prototype , "__batchStart" , { writable :true , configurable :true , value :function ( ) { "use strict" ;
5525
+ if ( this . __isBatching ) {
5526
+ throw new Error ( 'Reactor already in batch mode' )
5527
+ }
5528
+ this . __isBatching = true
5529
+ } } ) ;
5530
+
5531
+ Object . defineProperty ( Reactor . prototype , "__batchEnd" , { writable :true , configurable :true , value :function ( ) { "use strict" ;
5532
+ if ( ! this . __isBatching ) {
5533
+ throw new Error ( 'Reactor is not in batch mode' )
5534
+ }
5535
+
5536
+ if ( this . __batchDispatchCount > 0 ) {
5537
+ this . __notify ( )
5538
+ this . __batchDispatchCount = 0
5539
+ }
5491
5540
} } ) ;
5492
5541
5493
5542
@@ -5521,7 +5570,7 @@ return /******/ (function(modules) { // webpackBootstrap
5521
5570
exports . storeHandled = function ( id , before , after ) {
5522
5571
if ( console . group ) {
5523
5572
if ( before !== after ) {
5524
- console . debug ( 'Core changed: ' + id )
5573
+ console . debug ( 'Store ' + id + ' handled action' )
5525
5574
}
5526
5575
}
5527
5576
}
@@ -5852,8 +5901,13 @@ return /******/ (function(modules) { // webpackBootstrap
5852
5901
}
5853
5902
5854
5903
__applyingComputeFn = true
5855
- var evaluatedValue = getComputeFn ( keyPathOrGetter ) . apply ( null , args )
5856
- __applyingComputeFn = false
5904
+ try {
5905
+ var evaluatedValue = getComputeFn ( keyPathOrGetter ) . apply ( null , args )
5906
+ __applyingComputeFn = false
5907
+ } catch ( e ) {
5908
+ __applyingComputeFn = false
5909
+ throw e
5910
+ }
5857
5911
5858
5912
this . __cacheValue ( state , keyPathOrGetter , args , evaluatedValue )
5859
5913
0 commit comments