Skip to content

Added support for resetting settings via revision number (#52) #76

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

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
2 changes: 1 addition & 1 deletion __tests__/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ describe('Settings', () => {
expect(settings.get('osjs/jest', 'foo')).toBe('Hello World');
expect(settings.get('osjs/jest', 'bar')).toBe(undefined);
expect(settings.get('osjs/jest', 'baz', 'default')).toBe('default');
expect(settings.get()).toEqual({
expect(settings.get()).toMatchObject({
'osjs/default-application': {},
'osjs/desktop': {},
'osjs/locale': {},
Expand Down
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