diff --git a/src/plugins/kibana_utils/public/state_sync/state_sync_state_storage/create_kbn_url_state_storage.test.ts b/src/plugins/kibana_utils/public/state_sync/state_sync_state_storage/create_kbn_url_state_storage.test.ts index ca39d49af3cb1..f52ed8ee9c525 100644 --- a/src/plugins/kibana_utils/public/state_sync/state_sync_state_storage/create_kbn_url_state_storage.test.ts +++ b/src/plugins/kibana_utils/public/state_sync/state_sync_state_storage/create_kbn_url_state_storage.test.ts @@ -102,6 +102,16 @@ describe('KbnUrlStateStorage', () => { expect(cb).toBeCalledWith(expect.any(Error)); }); + it('should notify about errors throttled', () => { + const cb = jest.fn(); + urlStateStorage = createKbnUrlStateStorage({ useHash: false, history, onGetError: cb }); + const key = '_s'; + history.replace(`/#?${key}=(ok:2,test:`); // malformed rison + urlStateStorage.get(key); + urlStateStorage.get(key); + expect(cb).toBeCalledTimes(1); + }); + describe('withNotifyOnErrors integration', () => { test('toast is shown', () => { const toasts = coreMock.createStart().notifications.toasts; diff --git a/src/plugins/kibana_utils/public/state_sync/state_sync_state_storage/create_kbn_url_state_storage.ts b/src/plugins/kibana_utils/public/state_sync/state_sync_state_storage/create_kbn_url_state_storage.ts index 97f048ea0d916..6ad69238608c6 100644 --- a/src/plugins/kibana_utils/public/state_sync/state_sync_state_storage/create_kbn_url_state_storage.ts +++ b/src/plugins/kibana_utils/public/state_sync/state_sync_state_storage/create_kbn_url_state_storage.ts @@ -9,6 +9,7 @@ import { Observable, of } from 'rxjs'; import { catchError, map, share } from 'rxjs/operators'; import { History } from 'history'; +import { throttle } from 'lodash'; import { IStateStorage } from './types'; import { createKbnUrlControls, @@ -74,6 +75,7 @@ export const createKbnUrlStateStorage = ( } ): IKbnUrlStateStorage => { const url = createKbnUrlControls(history); + const onGetErrorThrottled = onGetError ? throttle((e) => onGetError(e), 100) : undefined; return { set: ( key: string, @@ -100,7 +102,7 @@ export const createKbnUrlStateStorage = ( try { return getStateFromKbnUrl(key, url.getPendingUrl(), { getFromHashQuery: useHashQuery }); } catch (e) { - if (onGetError) onGetError(e); + if (onGetErrorThrottled) onGetErrorThrottled(e); return null; } }, @@ -116,7 +118,7 @@ export const createKbnUrlStateStorage = ( }).pipe( map(() => getStateFromKbnUrl(key, undefined, { getFromHashQuery: useHashQuery })), catchError((error) => { - if (onGetError) onGetError(error); + if (onGetErrorThrottled) onGetErrorThrottled(error); return of(null); }), share()