Skip to content

Commit

Permalink
Refresh options component when they change in another tab
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaelgomesxyz committed May 26, 2021
1 parent 95799e0 commit f65fc8a
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/common/BrowserStorage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { CorrectionSuggestion, SavedItem } from '../models/Item';
import { SavedTraktItem, TraktItemBase } from '../models/TraktItem';
import { HboGoApiParams } from '../streaming-services/hbo-go/HboGoApi';
import { StreamingServiceId, streamingServices } from '../streaming-services/streaming-services';
import { EventDispatcher } from './Events';
import { I18N } from './I18N';
import { Shared } from './Shared';

Expand Down Expand Up @@ -283,7 +284,7 @@ class _BrowserStorage {
browser.storage.onChanged.removeListener(this.onStorageChanged);
};

onStorageChanged = (
onStorageChanged = async (
changes: browser.storage.ChangeDict,
areaName: browser.storage.StorageName
) => {
Expand All @@ -298,6 +299,7 @@ class _BrowserStorage {
][]) {
this.addOption({ id, value });
}
await EventDispatcher.dispatch('STORAGE_OPTIONS_CHANGE', null, {});
}
const newSyncOptions = changes.syncOptions?.newValue as StorageValuesSyncOptions | undefined;
if (newSyncOptions) {
Expand Down
1 change: 1 addition & 0 deletions src/common/Events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export interface EventData {
HISTORY_SYNC_SUCCESS: HistorySyncSuccessData;
HISTORY_SYNC_ERROR: ErrorData;
REQUESTS_CANCEL: RequestsCancelData;
STORAGE_OPTIONS_CHANGE: SuccessData;
}

export type Event = keyof EventData;
Expand Down
9 changes: 9 additions & 0 deletions src/modules/options/OptionsApp.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export const OptionsApp: React.FC = () => {
null,
onStreamingServiceOptionChange
);
EventDispatcher.subscribe('STORAGE_OPTIONS_CHANGE', null, onStorageOptionsChange);
};

const stopListeners = () => {
Expand All @@ -50,6 +51,7 @@ export const OptionsApp: React.FC = () => {
null,
onStreamingServiceOptionChange
);
EventDispatcher.unsubscribe('STORAGE_OPTIONS_CHANGE', null, onStorageOptionsChange);
};

const resetOptions = () => {
Expand Down Expand Up @@ -200,6 +202,13 @@ export const OptionsApp: React.FC = () => {
}
};

const onStorageOptionsChange = () => {
setContent({
isLoading: false,
optionsChanged: {},
});
};

startListeners();
return stopListeners;
}, []);
Expand Down

0 comments on commit f65fc8a

Please sign in to comment.