Refactor event category/setting event binding
This commit is contained in:
parent
b7a769e883
commit
c13884414d
|
@ -13,8 +13,6 @@ import BaseSetting from './types/basesetting';
|
|||
import { ClientLogger as Logger, AsyncEventEmitter } from 'common';
|
||||
import { SettingUpdatedEvent, SettingsUpdatedEvent } from 'structs';
|
||||
|
||||
let instances = 0;
|
||||
|
||||
export default class SettingsCategory {
|
||||
|
||||
constructor(args, ...merge) {
|
||||
|
@ -27,10 +25,12 @@ export default class SettingsCategory {
|
|||
this._merge(newCategory);
|
||||
}
|
||||
|
||||
this._eventsKey = instances++;
|
||||
this.__settingUpdated = this.__settingUpdated.bind(this);
|
||||
this.__settingsUpdated = this.__settingsUpdated.bind(this);
|
||||
|
||||
for (let setting of this.settings) {
|
||||
this._bindSettingEvents(setting);
|
||||
setting.on('setting-updated', this.__settingUpdated);
|
||||
setting.on('settings-updated', this.__settingsUpdated);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -80,20 +80,23 @@ export default class SettingsCategory {
|
|||
}
|
||||
|
||||
/**
|
||||
* Binds events to a category.
|
||||
* This only exists for use by the constructor and settingsset.addCategory.
|
||||
* Setting event listeners.
|
||||
* This only exists for use by the constructor and settingscategory.addSetting.
|
||||
*/
|
||||
_bindSettingEvents(setting) {
|
||||
setting.on('setting-updated', setting[this._eventsKey + '_settingscategory_event_setting-updated'] = ({ value, old_value }) => this.emit('setting-updated', new SettingUpdatedEvent({
|
||||
__settingUpdated({ setting, value, old_value }) {
|
||||
return this.emit('setting-updated', new SettingUpdatedEvent({
|
||||
category: this, category_id: this.id,
|
||||
setting, setting_id: setting.id,
|
||||
value, old_value
|
||||
})));
|
||||
setting.on('settings-updated', setting[this._eventsKey + '_settingscategory_event_settings-updated'] = ({ updatedSettings }) => this.emit('settings-updated', new SettingsUpdatedEvent({
|
||||
}));
|
||||
}
|
||||
|
||||
__settingsUpdated({ updatedSettings }) {
|
||||
return this.emit('settings-updated', new SettingsUpdatedEvent({
|
||||
updatedSettings: updatedSettings.map(updatedSetting => new SettingUpdatedEvent(Object.assign({
|
||||
category: this, category_id: this.id
|
||||
}, updatedSetting)))
|
||||
})));
|
||||
}));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -111,7 +114,9 @@ export default class SettingsCategory {
|
|||
if (this.getSetting(setting.id))
|
||||
throw {message: 'A setting with this ID already exists.'};
|
||||
|
||||
this._bindSettingEvents(setting);
|
||||
setting.on('setting-updated', this.__settingUpdated);
|
||||
setting.on('settings-updated', this.__settingsUpdated);
|
||||
|
||||
if (index === undefined) index = this.settings.length;
|
||||
this.settings.splice(index, 0, setting);
|
||||
|
||||
|
@ -132,8 +137,8 @@ export default class SettingsCategory {
|
|||
* @return {Promise}
|
||||
*/
|
||||
async removeSetting(setting) {
|
||||
setting.off('setting-updated', setting[this._eventsKey + '_settingscategory_event_setting-updated']);
|
||||
setting.off('settings-updated', setting[this._eventsKey + '_settingscategory_event_settings-updated']);
|
||||
setting.off('setting-updated', this.__settingUpdated);
|
||||
setting.off('settings-updated', this.__settingsUpdated);
|
||||
|
||||
let index;
|
||||
while ((index = this.settings.findIndex(s => s === setting)) > -1) {
|
||||
|
|
|
@ -14,8 +14,6 @@ import { ClientLogger as Logger, AsyncEventEmitter } from 'common';
|
|||
import { SettingUpdatedEvent, SettingsUpdatedEvent } from 'structs';
|
||||
import { Modals } from 'ui';
|
||||
|
||||
let instances = 0;
|
||||
|
||||
export default class SettingsSet {
|
||||
|
||||
constructor(args, ...merge) {
|
||||
|
@ -29,10 +27,16 @@ export default class SettingsSet {
|
|||
this._merge(newSet);
|
||||
}
|
||||
|
||||
this._eventsKey = instances++;
|
||||
this.__settingUpdated = this.__settingUpdated.bind(this);
|
||||
this.__settingsUpdated = this.__settingsUpdated.bind(this);
|
||||
this.__addedSetting = this.__addedSetting.bind(this);
|
||||
this.__removedSetting = this.__removedSetting.bind(this);
|
||||
|
||||
for (let category of this.categories) {
|
||||
this._bindCategoryEvents(category);
|
||||
category.on('setting-updated', this.__settingUpdated);
|
||||
category.on('settings-updated', this.__settingsUpdated);
|
||||
category.on('added-setting', this.__addedSetting);
|
||||
category.on('removed-setting', this.__removedSetting);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -96,33 +100,42 @@ export default class SettingsSet {
|
|||
}
|
||||
|
||||
/**
|
||||
* Binds events to a category.
|
||||
* This only exists for use by the constructor and settingsset.addCategory.
|
||||
* Category event listeners.
|
||||
* These only exists for use by the constructor and settingsset.addCategory.
|
||||
*/
|
||||
_bindCategoryEvents(category) {
|
||||
category.on('setting-updated', category[this._eventsKey + '_settingsset_event_setting-updated'] = ({ setting, value, old_value }) => this.emit('setting-updated', new SettingUpdatedEvent({
|
||||
__settingUpdated({ category, setting, value, old_value }) {
|
||||
return this.emit('setting-updated', new SettingUpdatedEvent({
|
||||
set: this, set_id: this.id,
|
||||
category, category_id: category.id,
|
||||
setting, setting_id: setting.id,
|
||||
value, old_value
|
||||
})));
|
||||
category.on('settings-updated', category[this._eventsKey + '_settingsset_event_settings-updated'] = ({ updatedSettings }) => this.emit('settings-updated', new SettingsUpdatedEvent({
|
||||
}));
|
||||
}
|
||||
|
||||
__settingsUpdated({ updatedSettings }) {
|
||||
return this.emit('settings-updated', new SettingsUpdatedEvent({
|
||||
updatedSettings: updatedSettings.map(updatedSetting => new SettingUpdatedEvent(Object.assign({
|
||||
set: this, set_id: this.id
|
||||
}, updatedSetting)))
|
||||
})));
|
||||
category.on('added-setting', category[this._eventsKey + '_settingsset_event_added-setting'] = ({ setting, at_index }) => this.emit('added-setting', {
|
||||
}));
|
||||
}
|
||||
|
||||
__addedSetting({ category, setting, at_index }) {
|
||||
return this.emit('added-setting', {
|
||||
set: this, set_id: this.id,
|
||||
category, category_id: category.id,
|
||||
setting, setting_id: setting.id,
|
||||
at_index
|
||||
}));
|
||||
category.on('removed-setting', category[this._eventsKey + '_settingsset_event_removed-setting'] = ({ setting, from_index }) => this.emit('removed-setting', {
|
||||
});
|
||||
}
|
||||
|
||||
__removedSetting({ category, setting, from_index }) {
|
||||
return this.emit('removed-setting', {
|
||||
set: this, set_id: this.id,
|
||||
category, category_id: category.id,
|
||||
setting, setting_id: setting.id,
|
||||
from_index
|
||||
}));
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -140,7 +153,11 @@ export default class SettingsSet {
|
|||
if (this.getCategory(category.id))
|
||||
throw {message: 'A category with this ID already exists.'};
|
||||
|
||||
this._bindCategoryEvents(category);
|
||||
category.on('setting-updated', this.__settingUpdated);
|
||||
category.on('settings-updated', this.__settingsUpdated);
|
||||
category.on('added-setting', this.__addedSetting);
|
||||
category.on('removed-setting', this.__removedSetting);
|
||||
|
||||
if (index === undefined) index = this.categories.length;
|
||||
this.categories.splice(index, 0, category);
|
||||
|
||||
|
@ -161,10 +178,10 @@ export default class SettingsSet {
|
|||
* @return {Promise}
|
||||
*/
|
||||
async removeCategory(category) {
|
||||
category.off('setting-updated', category[this._eventsKey + '_settingsset_event_setting-updated']);
|
||||
category.off('settings-updated', category[this._eventsKey + '_settingsset_event_settings-updated']);
|
||||
category.off('added-setting', category[this._eventsKey + '_settingsset_event_added-setting']);
|
||||
category.off('removed-setting', category[this._eventsKey + '_settingsset_event_removed-setting']);
|
||||
category.off('setting-updated', this.__settingUpdated);
|
||||
category.off('settings-updated', this.__settingsUpdated);
|
||||
category.off('added-setting', this.__addedSetting);
|
||||
category.off('removed-setting', this.__removedSetting);
|
||||
|
||||
let index;
|
||||
while ((index = this.categories.findIndex(c => c === category)) > -1) {
|
||||
|
|
Loading…
Reference in New Issue