Skip to content

Commit 05ae110

Browse files
MamadukajsnajdrtyxlaMr2P
authored
Data: Add error handle to the 'registry.batch' method (#62322)
Co-authored-by: Mamaduka <[email protected]> Co-authored-by: jsnajdr <[email protected]> Co-authored-by: tyxla <[email protected]> Co-authored-by: Mr2P <[email protected]>
1 parent 37fa2b5 commit 05ae110

File tree

2 files changed

+30
-3
lines changed

2 files changed

+30
-3
lines changed

packages/data/src/registry.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -322,9 +322,14 @@ export function createRegistry( storeConfigs = {}, parent = null ) {
322322

323323
emitter.pause();
324324
Object.values( stores ).forEach( ( store ) => store.emitter.pause() );
325-
callback();
326-
emitter.resume();
327-
Object.values( stores ).forEach( ( store ) => store.emitter.resume() );
325+
try {
326+
callback();
327+
} finally {
328+
emitter.resume();
329+
Object.values( stores ).forEach( ( store ) =>
330+
store.emitter.resume()
331+
);
332+
}
328333
}
329334

330335
let registry = {

packages/data/src/test/registry.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -755,6 +755,28 @@ describe( 'createRegistry', () => {
755755
} );
756756
expect( listener ).toHaveBeenCalledTimes( 1 );
757757
} );
758+
759+
it( 'should handle errors', () => {
760+
const store = registry.registerStore( 'myAwesomeReducer', {
761+
reducer: ( state = 0 ) => state + 1,
762+
} );
763+
const listener = jest.fn();
764+
const error = new Error( 'Whoops' );
765+
subscribeWithUnsubscribe( listener );
766+
767+
expect( () => {
768+
registry.batch( () => {
769+
throw error;
770+
} );
771+
} ).toThrow( error );
772+
expect( listener ).not.toHaveBeenCalled();
773+
774+
registry.batch( () => {
775+
store.dispatch( { type: 'dummy' } );
776+
store.dispatch( { type: 'dummy' } );
777+
} );
778+
expect( listener ).toHaveBeenCalledTimes( 1 );
779+
} );
758780
} );
759781

760782
describe( 'use', () => {

0 commit comments

Comments
 (0)