Skip to content

Commit

Permalink
work on #291
Browse files Browse the repository at this point in the history
correct save options and groups
  • Loading branch information
drive4ik committed Jul 6, 2024
1 parent d06add5 commit bd17853
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 18 deletions.
8 changes: 6 additions & 2 deletions addon/src/js/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -358,9 +358,13 @@ export const DEFAULT_OPTIONS = Object.freeze({
],
});

export const ONLY_BOOL_OPTION_KEYS = Object.freeze(Object.keys(DEFAULT_OPTIONS).filter(key => 'boolean' === typeof DEFAULT_OPTIONS[key]));
const DEFAULT_OPTION_KEYS = Object.keys(DEFAULT_OPTIONS);

export const ALL_OPTIONS_KEYS = Object.freeze(Object.keys(DEFAULT_OPTIONS).filter(key => !['version', 'groups', 'lastCreatedGroupPosition'].includes(key)));
export const ONLY_BOOL_OPTION_KEYS = Object.freeze(DEFAULT_OPTION_KEYS.filter(key => 'boolean' === typeof DEFAULT_OPTIONS[key]));

export const NON_OPTION_KEYS = Object.freeze(['version', 'groups', 'lastCreatedGroupPosition']);

export const ALL_OPTION_KEYS = Object.freeze(DEFAULT_OPTION_KEYS.filter(key => !NON_OPTION_KEYS.includes(key)));

export const ON_UPDATED_TAB_PROPERTIES = browser.tabs ? Object.freeze([ // browser.tabs not defined into web page scripts
browser.tabs.UpdatePropertyName.TITLE, // for cache
Expand Down
8 changes: 4 additions & 4 deletions addon/src/js/sync/cloud/cloud.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ export async function sync(progressFunc = null) {
});

progressFunc?.(45);
// throw Error('aaa');

const syncResult = await syncData(localData, cloudData);

progressFunc?.(50);
Expand Down Expand Up @@ -189,8 +189,8 @@ export async function sync(progressFunc = null) {
progressFunc?.(95);

if (syncResult.changes.local) {
// TODO normal save options
await Storage.set(syncResult.localData);
await backgroundSelf.saveOptions(syncResult.localData);
await Storage.set(Utils.extractKeys(syncResult.localData, Constants.NON_OPTION_KEYS));
}

window.localStorage.autoSyncLastTimeStamp = Utils.unixNow();
Expand Down Expand Up @@ -594,7 +594,7 @@ async function syncOptions(localData, cloudData, sourceOfTruth, changes) {
'sync',
];

for (const key of Constants.ALL_OPTIONS_KEYS) {
for (const key of Constants.ALL_OPTION_KEYS) {
if (EXCLUDE_OPTION_KEY_STARTS_WITH.some(exKey => key.startsWith(exKey))) {
continue;
}
Expand Down
2 changes: 1 addition & 1 deletion addon/src/js/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -546,7 +546,7 @@ export function assignKeys(toObj, fromObj, keys) {
}

export function extractKeys(obj, keys, useClone = false) {
let newObj = {};
const newObj = {};

keys.forEach(key => newObj[key] = (useClone ? JSON.clone(obj[key]) : obj[key]));

Expand Down
2 changes: 1 addition & 1 deletion addon/src/options/Options.vue
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@
async created() {
const data = await Storage.get();
const options = Utils.assignKeys({}, data, Constants.ALL_OPTIONS_KEYS);
const options = Utils.extractKeys(data, Constants.ALL_OPTION_KEYS);
options.autoBackupFolderName = await File.getAutoBackupFolderName();
Expand Down
2 changes: 1 addition & 1 deletion addon/src/options/manage-addon-backup.vue
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@
return false;
}
return Constants.ALL_OPTIONS_KEYS.includes(key);
return Constants.ALL_OPTION_KEYS.includes(key);
},
getData() {
Expand Down
31 changes: 22 additions & 9 deletions addon/src/stg-background.js
Original file line number Diff line number Diff line change
Expand Up @@ -2717,24 +2717,37 @@ async function onBackgroundMessage(message, sender) {
}

async function saveOptions(_options) {
const log = logger.start('saveOptions', _options);
const log = logger.start('saveOptions');

if (!self.inited) {
log.stopError('background not yet inited');
return null;
return;
}

_options = JSON.clone(_options);
const optionsToSave = {};

const optionsKeys = Object.keys(_options);
for (const [key, value] of Object.entries(_options)) {
if (Constants.ALL_OPTION_KEYS.includes(key)) {
if (Utils.isPrimitive(value)) {
optionsToSave[key] = value;
} else {
optionsToSave[key] = JSON.clone(value);
}
} else if (Constants.DEFAULT_OPTIONS[key] === undefined) {
log.throwError(`option key "${key}" is unknown`);
}
}

const optionsKeys = Object.keys(optionsToSave);

if (!optionsKeys.every(key => Constants.ALL_OPTIONS_KEYS.includes(key))) {
log.throwError(['some key in save options are not supported:', optionsKeys]);
if (!optionsKeys.length) {
log.stop('options not found');
return;
}

Object.assign(options, _options);
await Storage.set(optionsToSave);

await Storage.set(_options);
Object.assign(options, optionsToSave);

if (optionsKeys.includes('hotkeys')) {
const tabs = await Tabs.get(null, null, null, {
Expand Down Expand Up @@ -4176,7 +4189,7 @@ async function init() {
throw '';
}

Utils.assignKeys(options, data, Constants.ALL_OPTIONS_KEYS);
Utils.assignKeys(options, data, Constants.ALL_OPTION_KEYS);

dataChanged.add(Groups.normalizeContainersInGroups(data.groups));

Expand Down

0 comments on commit bd17853

Please sign in to comment.