Skip to content

Commit

Permalink
Data: Add error handle to the 'registry.batch' method (#62322)
Browse files Browse the repository at this point in the history
Co-authored-by: Mamaduka <[email protected]>
Co-authored-by: jsnajdr <[email protected]>
Co-authored-by: tyxla <[email protected]>
Co-authored-by: Mr2P <[email protected]>
  • Loading branch information
5 people authored and ellatrix committed Jun 11, 2024
1 parent bd94497 commit 8b3c7c2
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
11 changes: 8 additions & 3 deletions packages/data/src/registry.js
Original file line number Diff line number Diff line change
Expand Up @@ -322,9 +322,14 @@ export function createRegistry( storeConfigs = {}, parent = null ) {

emitter.pause();
Object.values( stores ).forEach( ( store ) => store.emitter.pause() );
callback();
emitter.resume();
Object.values( stores ).forEach( ( store ) => store.emitter.resume() );
try {
callback();
} finally {
emitter.resume();
Object.values( stores ).forEach( ( store ) =>
store.emitter.resume()
);
}
}

let registry = {
Expand Down
22 changes: 22 additions & 0 deletions packages/data/src/test/registry.js
Original file line number Diff line number Diff line change
Expand Up @@ -755,6 +755,28 @@ describe( 'createRegistry', () => {
} );
expect( listener ).toHaveBeenCalledTimes( 1 );
} );

it( 'should handle errors', () => {
const store = registry.registerStore( 'myAwesomeReducer', {
reducer: ( state = 0 ) => state + 1,
} );
const listener = jest.fn();
const error = new Error( 'Whoops' );
subscribeWithUnsubscribe( listener );

expect( () => {
registry.batch( () => {
throw error;
} );
} ).toThrow( error );
expect( listener ).not.toHaveBeenCalled();

registry.batch( () => {
store.dispatch( { type: 'dummy' } );
store.dispatch( { type: 'dummy' } );
} );
expect( listener ).toHaveBeenCalledTimes( 1 );
} );
} );

describe( 'use', () => {
Expand Down

0 comments on commit 8b3c7c2

Please sign in to comment.