Skip to content

Commit

Permalink
Added support for resetting settings via revision number (#52)
Browse files Browse the repository at this point in the history
This is the first step in adding migrations. This commit adds the
support for resetting the user settings when you change the
"__revision__" value of a settings namespace.
  • Loading branch information
andersevenrud committed Oct 12, 2019
1 parent c8e8230 commit e37aa2e
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 4 deletions.
4 changes: 3 additions & 1 deletion src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,9 @@ export const defaultConfiguration = {
defaults: {
'osjs/default-application': {},
'osjs/session': [],
'osjs/desktop': {},
'osjs/desktop': {
__revision__: 0
},
'osjs/locale': {}
}
},
Expand Down
2 changes: 1 addition & 1 deletion src/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ export default class Core extends CoreBase {
this.user = user;

if (this.has('osjs/settings')) {
this.make('osjs/settings').load()
this.make('osjs/settings').load(true)
.then(() => done())
.catch(() => done());
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/providers/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export default class SettingsServiceProvider extends ServiceProvider {
init() {
this.core.singleton('osjs/settings', () => ({
save: () => this.settings.save(),
load: () => this.settings.load(),
load: migrate => this.settings.load(migrate),
clear: (...args) => this.settings.clear(...args),
get: (...args) => this.settings.get(...args),
set: (...args) => this.settings.set(...args)
Expand Down
38 changes: 37 additions & 1 deletion src/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,10 @@ export default class Settings {

/**
* Loads settings
* @param {boolean} [migrate] Run migrations
* @return {Promise<boolean, Error>}
*/
load() {
load(migrate) {
const defaults = this.core.config('settings.defaults', {});

return this.adapter.load()
Expand All @@ -117,6 +118,9 @@ export default class Settings {
arrayMerge: (dest, source) => source
});

return migrate ? this.migrate() : Promise.resolve();
})
.then(() => {
this.core.emit('osjs/settings:load');

return true;
Expand All @@ -128,6 +132,38 @@ export default class Settings {
});
}

/**
* Runs migrations for settings
*/
migrate() {
const defaults = this.core.config('settings.defaults', {});

const revisionsChanged = Object.keys(defaults)
.filter(key => {
const currentRevision = defaults[key].__revision__;
const userRevision = this.settings[key]
? this.settings[key].__revision__
: undefined;

if (typeof currentRevision === 'undefined') {
return false;
}

return userRevision < currentRevision;
});

if (revisionsChanged.length > 0) {
revisionsChanged.forEach(key => {
this.settings[key] = merge(defaults[key], {});
});

return this.save()
.catch(err => console.warn('Settings#migrate', 'failed to save after migration', err));
}

return Promise.resolve();
}

/**
* Gets a settings entry by key
*
Expand Down

0 comments on commit e37aa2e

Please sign in to comment.