Extend AsyncEventEmitter and allow changing of some properties

This commit is contained in:
Samuel Elliott 2018-03-30 02:49:36 +01:00
parent 009d6be057
commit 069b1ff689
No known key found for this signature in database
GPG Key ID: 8420C7CDE43DC4D6
4 changed files with 68 additions and 28 deletions

View File

@ -13,11 +13,18 @@ import BaseSetting from './types/basesetting';
import { ClientLogger as Logger, AsyncEventEmitter } from 'common';
import { SettingUpdatedEvent, SettingsUpdatedEvent } from 'structs';
export default class SettingsCategory {
export default class SettingsCategory extends AsyncEventEmitter {
constructor(args, ...merge) {
this.emitter = new AsyncEventEmitter();
this.args = args.args || args;
super();
if (typeof args === 'string')
args = {id: args};
this.args = args.args || args || {};
this.args.id = this.args.id || this.args.category || 'default';
this.args.name = this.args.name || this.args.category_name || this.id;
this.type = this.args.type;
this.args.settings = this.settings.map(setting => new Setting(setting));
@ -56,6 +63,10 @@ export default class SettingsCategory {
return this.name;
}
set name(value) {
this.args.name = value;
}
/**
* Category type
* Currently either "drawer", "static", or undefined.
@ -64,6 +75,13 @@ export default class SettingsCategory {
return this.args.type;
}
set type(value) {
if (!value) this.args.type = undefined;
else if (value === 'drawer' || value === 'static')
this.args.type = value;
else throw {message: `Invalid category type ${value}`};
}
/**
* An array of settings in this category.
*/
@ -276,8 +294,4 @@ export default class SettingsCategory {
}, ...merge);
}
on(...args) { return this.emitter.on(...args); }
off(...args) { return this.emitter.removeListener(...args); }
emit(...args) { return this.emitter.emit(...args); }
}

View File

@ -14,11 +14,18 @@ import { ClientLogger as Logger, AsyncEventEmitter } from 'common';
import { SettingUpdatedEvent, SettingsUpdatedEvent } from 'structs';
import { Modals } from 'ui';
export default class SettingsSet {
export default class SettingsSet extends AsyncEventEmitter {
constructor(args, ...merge) {
this.emitter = new AsyncEventEmitter();
this.args = args.args || args;
super();
if (typeof args === 'string')
args = {id: args};
this.args = args.args || args || {};
this.args.id = this.args.id || undefined;
this.args.text = this.args.text || undefined;
this.args.headertext = this.args.headertext || undefined;
this.args.categories = this.categories.map(category => new SettingsCategory(category));
this.args.schemes = this.schemes.map(scheme => new SettingsScheme(scheme));
@ -54,6 +61,10 @@ export default class SettingsSet {
return this.args.text;
}
set text(value) {
this.args.text = value;
}
/**
* Text to be displayed with the set.
*/
@ -65,14 +76,6 @@ export default class SettingsSet {
this.args.headertext = headertext;
}
/**
* Whether this set should be displayed.
* Currently only used in the settings menu.
*/
get hidden() {
return this.args.hidden || false;
}
/**
* An array of SettingsCategory objects in this set.
*/
@ -450,8 +453,4 @@ export default class SettingsSet {
}, ...merge);
}
on(...args) { return this.emitter.on(...args); }
off(...args) { return this.emitter.removeListener(...args); }
emit(...args) { return this.emitter.emit(...args); }
}

View File

@ -12,11 +12,19 @@ import { ThemeManager } from 'modules';
import { Utils, AsyncEventEmitter } from 'common';
import { SettingUpdatedEvent, SettingsUpdatedEvent } from 'structs';
export default class Setting {
export default class Setting extends AsyncEventEmitter {
constructor(args, ...merge) {
super();
this.args = args.args || args;
this.args.id = this.args.id || 'default';
this.args.text = this.args.text || undefined;
this.args.hint = this.args.hint || undefined;
this.args.path = this.args.path || undefined;
this.args.disabled = !!this.args.disabled;
this.args.fullwidth = !!this.args.fullwidth;
if (!this.args.hasOwnProperty('value'))
this.args.value = this.defaultValue;
if (!this.args.hasOwnProperty('saved_value'))
@ -26,7 +34,6 @@ export default class Setting {
this._merge(newSetting);
}
this.emitter = new AsyncEventEmitter();
this.changed = !Utils.compare(this.args.value, this.args.saved_value);
}
@ -70,6 +77,10 @@ export default class Setting {
return this.args.text;
}
set text(value) {
this.args.text = value;
}
/**
* Text to be displayed with the setting.
*/
@ -77,9 +88,14 @@ export default class Setting {
return this.args.hint;
}
set hint(value) {
this.args.hint = value;
}
/**
* The path of the plugin/theme this setting is part of.
* Used by settings of type "array", "custom" and "file".
* Use set/category/setting.setContentPath to change.
*/
get path() {
return this.args.path;
@ -93,6 +109,10 @@ export default class Setting {
return this.args.disabled || false;
}
set disabled(value) {
this.args.disabled = !!value;
}
/**
* Whether the setting should take the full width of the settings panel.
* This is only customisable in some setting types.
@ -101,6 +121,10 @@ export default class Setting {
return this.args.fullwidth || false;
}
set fullwidth(value) {
this.args.fullwidth = !!value;
}
/**
* Merges a setting into this setting without emitting events (and therefore synchronously).
* This only exists for use by the constructor and SettingsCategory.
@ -234,8 +258,4 @@ export default class Setting {
}
}
on(...args) { return this.emitter.on(...args); }
off(...args) { return this.emitter.removeListener(...args); }
emit(...args) { return this.emitter.emit(...args); }
}

View File

@ -58,4 +58,11 @@ export default class AsyncEventEmitter extends EventEmitter {
});
}
/**
* Unbinds an event listener.
*/
off(event, callback) {
this.removeListener(event, callback);
}
}