Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

debounce app state updates #54

Open
wants to merge 1 commit into
base: kavilla/clean_up_app_state
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* SPDX-License-Identifier: Apache-2.0
*/

import { debounceTime } from 'rxjs/operators';
import { migrateAppState } from '../lib/migrate_app_state';
import {
IOsdUrlStateStorage,
Expand All @@ -21,6 +22,7 @@ import { syncQueryStateWithUrl } from '../../../../data/public';
import { SavedObjectDashboard } from '../../saved_dashboards';

const APP_STATE_STORAGE_KEY = '_a';
const APP_STATE_ON_CHANGE_DEBOUNCE_MS = 5;

interface Arguments {
osdUrlStateStorage: IOsdUrlStateStorage;
Expand Down Expand Up @@ -118,6 +120,9 @@ export const createDashboardGlobalAndAppState = ({
stateStorage: osdUrlStateStorage,
});

// creates observable that debounces to prevent url state async updates
const onStateChange$ = stateContainer.state$.pipe(debounceTime(APP_STATE_ON_CHANGE_DEBOUNCE_MS));

// starts syncing `_g` portion of url with query services
// it is important to start this syncing after we set the time filter if timeRestore = true
// otherwise it will case redundant browser history records and browser navigation like going back will not work correctly
Expand All @@ -129,7 +134,7 @@ export const createDashboardGlobalAndAppState = ({
updateStateUrl({ osdUrlStateStorage, state: initialState, replace: true });
// start syncing the appState with the ('_a') url
startStateSync();
return { stateContainer, stopStateSync, stopSyncingQueryServiceStateWithUrl };
return { stateContainer, onStateChange$, stopStateSync, stopSyncingQueryServiceStateWithUrl };
};

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ export const useDashboardAppAndGlobalState = ({

const {
stateContainer,
onStateChange$,
stopStateSync,
stopSyncingQueryServiceStateWithUrl,
} = createDashboardGlobalAndAppState({
Expand Down Expand Up @@ -162,7 +163,7 @@ export const useDashboardAppAndGlobalState = ({

// If app state is changes, then set unsaved changes to true
// the only thing app state is not tracking is the time filter, need to check the previous dashboard if they count time filter change or not
const stopSyncingFromAppState = stateContainer.subscribe((appStateData) => {
const stopSyncingFromAppState = onStateChange$.subscribe((appStateData) => {
refreshDashboardContainer({
dashboardContainer,
dashboardServices: services,
Expand Down