Extend AsyncEventEmitter

This commit is contained in:
Samuel Elliott 2018-03-30 01:34:37 +01:00
parent 81f3519408
commit da4b592e54
No known key found for this signature in database
GPG Key ID: 8420C7CDE43DC4D6
2 changed files with 7 additions and 44 deletions

View File

@ -12,16 +12,18 @@ import { Utils, FileUtils, ClientLogger as Logger, AsyncEventEmitter } from 'com
import { Modals } from 'ui';
import Database from './database';
export default class Content {
export default class Content extends AsyncEventEmitter {
constructor(internals) {
super();
Utils.deepfreeze(internals.info);
Object.freeze(internals.paths);
this.__internals = internals;
this.settings.on('setting-updated', event => this.events.emit('setting-updated', event));
this.settings.on('settings-updated', event => this.events.emit('settings-updated', event));
this.settings.on('setting-updated', event => this.emit('setting-updated', event));
this.settings.on('settings-updated', event => this.emit('settings-updated', event));
this.settings.on('settings-updated', event => this.__settingsUpdated(event));
// Add hooks
@ -55,7 +57,6 @@ export default class Content {
get settings() { return this.userConfig.config }
get config() { return this.settings.categories }
get data() { return this.userConfig.data || (this.userConfig.data = {}) }
get events() { return this.EventEmitter || (this.EventEmitter = new AsyncEventEmitter()) }
/**
* Opens a settings modal for this content.
@ -128,44 +129,6 @@ export default class Content {
if (save) await this.saveConfiguration();
}
/**
* Adds an event listener.
* @param {String} event The event to add the listener to
* @param {Function} callback The function to call when the event is emitted
*/
on(...args) {
return this.events.on(...args);
}
/**
* Adds an event listener that removes itself when called, therefore only being called once.
* @param {String} event The event to add the listener to
* @param {Function} callback The function to call when the event is emitted
* @return {Promise|undefined}
*/
once(...args) {
return this.events.once(...args);
}
/**
* Removes an event listener.
* @param {String} event The event to remove the listener from
* @param {Function} callback The bound callback (optional)
*/
off(...args) {
return this.events.removeListener(...args);
}
/**
* Emits an event.
* @param {String} event The event to emit
* @param {Any} data Data to be passed to listeners
* @return {Promise|undefined}
*/
emit(...args) {
return this.events.emit(...args);
}
}
Object.freeze(Content.prototype);

View File

@ -15,10 +15,10 @@ module.exports = (Plugin, Api, Vendor, Dependencies) => {
Logger.log('onStart');
Logger.log(`Plugin setting "default-0" value: ${this.settings.get('default-0')}`);
this.events.on('setting-updated', event => {
this.on('setting-updated', event => {
console.log('Received plugin setting update:', event);
});
this.events.on('settings-updated', event => {
this.on('settings-updated', event => {
console.log('Received plugin settings update:', event);
});