Handle changes better
This commit is contained in:
parent
47637eca75
commit
9617890ab6
|
@ -24,12 +24,11 @@ export default class ArraySetting extends Setting {
|
|||
this.args.schemes = this.schemes.map(scheme => new SettingsScheme(scheme));
|
||||
this.args.items = this.value ? this.value.map(item => this.createItem(item.args || item)) : [];
|
||||
|
||||
this.args.value = this.items.map(item => {
|
||||
this._setValue(this.items.map(item => {
|
||||
if (!item) return;
|
||||
item.setSaved();
|
||||
return item.strip();
|
||||
});
|
||||
this.changed = !Utils.compare(this.args.value, this.args.saved_value);
|
||||
}));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -147,24 +146,6 @@ export default class ArraySetting extends Setting {
|
|||
return set;
|
||||
}
|
||||
|
||||
/**
|
||||
* Merges a setting into this setting without emitting events (and therefore synchronously).
|
||||
* This only exists for use by the constructor and SettingsCategory.
|
||||
*/
|
||||
_merge(newSetting) {
|
||||
const value = newSetting.args ? newSetting.args.value : newSetting.value;
|
||||
const old_value = this.args.value;
|
||||
if (Utils.compare(value, old_value)) return [];
|
||||
this.args.value = value;
|
||||
this.args.items = this.value ? this.value.map(item => this.createItem(item.args || item)) : [];
|
||||
this.changed = !Utils.compare(this.args.value, this.args.saved_value);
|
||||
|
||||
return [{
|
||||
setting: this, setting_id: this.id,
|
||||
value, old_value
|
||||
}];
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the value of this setting.
|
||||
* This is only intended for use by settings.
|
||||
|
@ -173,20 +154,17 @@ export default class ArraySetting extends Setting {
|
|||
* @param {Boolean} emit Whether to emit a SettingUpdatedEvent
|
||||
* @return {Promise}
|
||||
*/
|
||||
setValue(value, emit_multi = true, emit = true) {
|
||||
this.args.items = value ? value.map(item => this.createItem(item)) : [];
|
||||
this.updateValue(emit_multi, emit);
|
||||
setValueHook(updatedSetting) {
|
||||
this.args.items = updatedSetting.value ? updatedSetting.value.map(item => this.createItem(item)) : [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates the value of this array setting.
|
||||
* This only exists for use by array settings.
|
||||
* @param {Boolean} emit_multi Whether to emit a SettingsUpdatedEvent
|
||||
* @param {Boolean} emit Whether to emit a SettingUpdatedEvent
|
||||
* @return {Promise}
|
||||
*/
|
||||
updateValue(emit_multi = true, emit = true) {
|
||||
return this.__proto__.__proto__.setValue.call(this, this.items.map(item => {
|
||||
return this.setValue.call(this, this.items.map(item => {
|
||||
if (!item) return;
|
||||
item.setSaved();
|
||||
return item.strip();
|
||||
|
|
|
@ -107,75 +107,75 @@ export default class Setting {
|
|||
*/
|
||||
_merge(newSetting) {
|
||||
const value = newSetting.args ? newSetting.args.value : newSetting.value;
|
||||
const old_value = this.args.value;
|
||||
if (Utils.compare(value, old_value)) return [];
|
||||
this.args.value = value;
|
||||
this.changed = !Utils.compare(this.args.value, this.args.saved_value);
|
||||
|
||||
return [{
|
||||
setting: this, setting_id: this.id,
|
||||
value, old_value
|
||||
}];
|
||||
return this._setValue(value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Merges another setting into this setting.
|
||||
* @param {SettingsSetting} newSetting The setting to merge into this setting
|
||||
* @param {Boolean} emit_multi Whether to emit a SettingsUpdatedEvent
|
||||
* @param {Boolean} emit Whether to emit a SettingUpdatedEvent
|
||||
* @return {Promise}
|
||||
*/
|
||||
async merge(newSetting, emit_multi = true, emit = true) {
|
||||
const value = newSetting.args ? newSetting.args.value : newSetting.value;
|
||||
const updatedSettings = this._merge(newSetting);
|
||||
if (!updatedSettings.length) return [];
|
||||
const updatedSetting = updatedSettings[0];
|
||||
|
||||
if (emit)
|
||||
await this.emit('setting-updated', updatedSetting);
|
||||
|
||||
if (emit_multi)
|
||||
await this.emit('settings-updated', new SettingsUpdatedEvent({
|
||||
updatedSettings
|
||||
}));
|
||||
|
||||
return updatedSettings;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the value of this setting.
|
||||
* This only exists for use by the constructor and SettingsCategory.
|
||||
*/
|
||||
_setValue(value) {
|
||||
const old_value = this.args.value;
|
||||
if (Utils.compare(value, old_value)) return [];
|
||||
this.args.value = value;
|
||||
this.changed = !Utils.compare(this.args.value, this.args.saved_value);
|
||||
|
||||
const updatedSetting = {
|
||||
const updatedSetting = new SettingUpdatedEvent({
|
||||
setting: this, setting_id: this.id,
|
||||
value, old_value
|
||||
};
|
||||
});
|
||||
|
||||
if (emit)
|
||||
await this.emit('setting-updated', new SettingUpdatedEvent(updatedSetting));
|
||||
|
||||
if (emit_multi)
|
||||
await this.emit('settings-updated', new SettingsUpdatedEvent({
|
||||
updatedSettings: [updatedSetting]
|
||||
}));
|
||||
this.setValueHook(updatedSetting);
|
||||
|
||||
return [updatedSetting];
|
||||
}
|
||||
|
||||
/**
|
||||
* Function to be called after the value changes.
|
||||
* This can be overridden by other settings types.
|
||||
* @param {SettingUpdatedEvent} updatedSetting
|
||||
*/
|
||||
setValueHook(updatedSetting) {}
|
||||
|
||||
/**
|
||||
* Sets the value of this setting.
|
||||
* This is only intended for use by settings.
|
||||
* @param {Any} value The new value of this setting
|
||||
* @param {Boolean} emit_multi Whether to emit a SettingsUpdatedEvent
|
||||
* @param {Boolean} emit Whether to emit a SettingUpdatedEvent
|
||||
* @return {Promise}
|
||||
*/
|
||||
async setValue(value, emit_multi = true, emit = true) {
|
||||
const old_value = this.args.value;
|
||||
if (Utils.compare(value, old_value)) return [];
|
||||
this.args.value = value;
|
||||
this.changed = !Utils.compare(this.args.value, this.args.saved_value);
|
||||
|
||||
const updatedSetting = {
|
||||
setting: this, setting_id: this.id,
|
||||
value, old_value
|
||||
};
|
||||
const updatedSettings = this._setValue(value);
|
||||
if (!updatedSettings.length) return [];
|
||||
|
||||
if (emit)
|
||||
await this.emit('setting-updated', new SettingUpdatedEvent(updatedSetting));
|
||||
await this.emit('setting-updated', updatedSettings[0]);
|
||||
|
||||
if (emit_multi)
|
||||
await this.emit('settings-updated', new SettingsUpdatedEvent({
|
||||
updatedSettings: [updatedSetting]
|
||||
updatedSettings
|
||||
}));
|
||||
|
||||
return [updatedSetting];
|
||||
return updatedSettings;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Reference in New Issue