From 41c307bb08e2ccbdd18f37d3523c51d7fd16ba1b Mon Sep 17 00:00:00 2001 From: Kyra Date: Tue, 6 Nov 2018 17:51:34 +0100 Subject: [PATCH] feat: Added `KlasaClient#settingsSync` event. (#475) * feat: Added `KlasaClient#settingsSyncEntry` event. * docs: Correct PR number in CHANGELOG * src: Emit settingsSyncEntry on broadcastEval * fix: Entry settings not setting their existence status to true in shards * misc: Renamed `KlasaClient#{settingsUpdateEntry,settingsDeleteEntry,settingsCreateEntry}` to `KlasaClient#{settingsUpdate,settingsDelete,settingsCreate}`. * misc: Rename prefix settingsUpdate core event to... coreSettingsUpdate * fix: coreSettingsUpdate is not an once event --- CHANGELOG.md | 2 ++ guides/Included Pieces/IncludedEvents.md | 4 ++-- src/events/coreSettingsUpdate.js | 29 ++++++++++++++++++++++++ src/events/settingsUpdateEntry.js | 24 -------------------- src/lib/Client.js | 13 ++++++++--- src/lib/settings/Settings.js | 7 ++++-- src/lib/settings/SettingsFolder.js | 4 ++-- src/lib/settings/gateway/Gateway.js | 1 + typings/index.d.ts | 14 +++++++----- 9 files changed, 59 insertions(+), 39 deletions(-) create mode 100644 src/events/coreSettingsUpdate.js delete mode 100644 src/events/settingsUpdateEntry.js diff --git a/CHANGELOG.md b/CHANGELOG.md index 7c905204e3..1e9a807019 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ NOTE: For the contributors, you add new entries to this document following this ### Added +- [[#475](https://github.com/dirigeants/klasa/pull/475)] Added `KlasaClient#settingsSync` event. (kyranet) - [[#471](https://github.com/dirigeants/klasa/pull/471)] Added `Gateway#create` and `Gateway#acquire`. (kyranet) - [[#471](https://github.com/dirigeants/klasa/pull/471)] Added `SETTING_GATEWAY_CHOOSE_KEY` and `SETTING_GATEWAY_UNCONFIGURABLE_FOLDER` i18n keys into en-US. (kyranet) - [[#471](https://github.com/dirigeants/klasa/pull/471)] Added `SettingsFolder`, with the common operations (update, reset...) for better OOP compliancy. (kyranet) @@ -130,6 +131,7 @@ NOTE: For the contributors, you add new entries to this document following this ### Changed +- [[#475](https://github.com/dirigeants/klasa/pull/475)] Renamed `KlasaClient#{settingsUpdateEntry,settingsDeleteEntry,settingsCreateEntry}` to `KlasaClient#{settingsUpdate,settingsDelete,settingsCreate}`. (kyranet) - [[#471](https://github.com/dirigeants/klasa/pull/471)] Modified `Schema#defaults` type from Object literal to a `SettingsFolder` instance. (kyranet) - [[#471](https://github.com/dirigeants/klasa/pull/471)] Modified `Schema#defaults` to be a property instead of a getter. (kyranet) - [[#471](https://github.com/dirigeants/klasa/pull/471)] Modified `Gateway#get` to take only id. For get or create, use `Gateway#acquire` instead. (kyranet) diff --git a/guides/Included Pieces/IncludedEvents.md b/guides/Included Pieces/IncludedEvents.md index db568e037d..e3b23671d7 100644 --- a/guides/Included Pieces/IncludedEvents.md +++ b/guides/Included Pieces/IncludedEvents.md @@ -14,13 +14,13 @@ Replies with the reason why the command was inhibited. [events/commandInhibited.js](https://github.com/dirigeants/klasa/blob/master/src/events/commandInhibited.js) -## settingsUpdateEntry +## settingsUpdate Synchronises the user settings between all shards, if the bot is sharded. **Source:** -[events/settingsUpdateEntry.js](https://github.com/dirigeants/klasa/blob/master/src/events/settingsUpdateEntry.js) +[events/coreSettingsUpdate.js](https://github.com/dirigeants/klasa/blob/master/src/events/coreSettingsUpdate.js) ## debug diff --git a/src/events/coreSettingsUpdate.js b/src/events/coreSettingsUpdate.js new file mode 100644 index 0000000000..fb1cc353ce --- /dev/null +++ b/src/events/coreSettingsUpdate.js @@ -0,0 +1,29 @@ +const { Event } = require('klasa'); +const gateways = ['users', 'clientStorage']; + +module.exports = class extends Event { + + constructor(...args) { + super(...args, { event: 'settingsUpdate' }); + } + + run(settings) { + if (this.client.shard && gateways.includes(settings.gateway.name)) { + this.client.shard.broadcastEval(` + if (this.shard.id !== ${this.client.shard.id}) { + const entry = this.gateways.get('${settings.gateway.name}').get('${settings.id}'); + if (entry) { + entry._patch(${JSON.stringify(settings)}); + entry.existenceStatus = true; + this.emit('settingsSync', settings); + } + } + `); + } + } + + init() { + if (!this.client.shard) this.disable(); + } + +}; diff --git a/src/events/settingsUpdateEntry.js b/src/events/settingsUpdateEntry.js deleted file mode 100644 index 76c547de62..0000000000 --- a/src/events/settingsUpdateEntry.js +++ /dev/null @@ -1,24 +0,0 @@ -const { Event } = require('klasa'); -const gateways = ['users', 'clientStorage']; - -module.exports = class extends Event { - - run(settings) { - if (gateways.includes(settings.gateway.type)) { - this.client.shard.broadcastEval(` - if (String(this.shard.id) !== '${this.client.shard.id}') { - const entry = this.gateways.${settings.gateway.type}.get('${settings.id}'); - if (entry) { - entry._patch(${JSON.stringify(settings)}); - entry._existsInDB = true; - } - } - `); - } - } - - init() { - if (!this.client.shard) this.disable(); - } - -}; diff --git a/src/lib/Client.js b/src/lib/Client.js index faed46facf..e28a6dec10 100644 --- a/src/lib/Client.js +++ b/src/lib/Client.js @@ -634,9 +634,16 @@ KlasaClient.defaultClientSchema = new Schema() * @param {(Error|string)} error The task error */ +/** + * Emitted when a {@link Settings} instance synchronizes with the database. + * @event KlasaClient#settingsSync + * @since 0.5.0 + * @param {Settings} entry The patched Settings instance + */ + /** * Emitted when {@link Settings#update} or {@link Settings#reset} is run. - * @event KlasaClient#settingsUpdateEntry + * @event KlasaClient#settingsUpdate * @since 0.5.0 * @param {Settings} entry The patched Settings instance * @param {SettingsUpdateResultEntry[]} updated The keys that were updated @@ -644,14 +651,14 @@ KlasaClient.defaultClientSchema = new Schema() /** * Emitted when {@link Settings#destroy} is run. - * @event KlasaClient#settingsDeleteEntry + * @event KlasaClient#settingsDelete * @since 0.5.0 * @param {Settings} entry The entry which got deleted */ /** * Emitted when a new entry in the database has been created upon update. - * @event KlasaClient#settingsCreateEntry + * @event KlasaClient#settingsCreate * @since 0.5.0 * @param {Settings} entry The entry which got created */ diff --git a/src/lib/settings/Settings.js b/src/lib/settings/Settings.js index 8e6680e7bb..46afc42d2a 100644 --- a/src/lib/settings/Settings.js +++ b/src/lib/settings/Settings.js @@ -83,7 +83,10 @@ class Settings extends SettingsFolder { // If it's not currently synchronizing, create a new sync status for the sync queue const sync = this.gateway.provider.get(this.gateway.name, this.id).then(data => { this.existenceStatus = Boolean(data); - if (data) this._patch(data); + if (data) { + this._patch(data); + this.client.emit('settingsSync', this); + } this.gateway.syncMap.delete(this); return this; }); @@ -101,7 +104,7 @@ class Settings extends SettingsFolder { await this.sync(); if (this.existenceStatus) { await this.gateway.provider.delete(this.gateway.name, this.id); - this.gateway.client.emit('settingsDeleteEntry', this); + this.gateway.client.emit('settingsDelete', this); this.init(); } return this; diff --git a/src/lib/settings/SettingsFolder.js b/src/lib/settings/SettingsFolder.js index 7c57ec29a4..5240efe561 100644 --- a/src/lib/settings/SettingsFolder.js +++ b/src/lib/settings/SettingsFolder.js @@ -321,10 +321,10 @@ class SettingsFolder extends Map { if (status === false) { await this.gateway.provider.create(this.gateway.name, this.base.id, results); this.base.existenceStatus = true; - this.base.gateway.client.emit('settingsCreateEntry', this.base); + this.base.gateway.client.emit('settingsCreate', this.base); } else { await this.gateway.provider.update(this.gateway.name, this.id, results); - this.base.gateway.client.emit('settingsUpdateEntry', this.base, results.slice()); + this.base.gateway.client.emit('settingsUpdate', this.base, results.slice()); } const updateObject = {}; diff --git a/src/lib/settings/gateway/Gateway.js b/src/lib/settings/gateway/Gateway.js index 2b655e44b2..1fc3f0af6c 100644 --- a/src/lib/settings/gateway/Gateway.js +++ b/src/lib/settings/gateway/Gateway.js @@ -114,6 +114,7 @@ class Gateway extends GatewayStorage { if (cache) { cache.existenceStatus = true; cache._patch(entry); + this.client.emit('settingsSync', cache); } } diff --git a/typings/index.d.ts b/typings/index.d.ts index dbf513e760..9cdc723fda 100644 --- a/typings/index.d.ts +++ b/typings/index.d.ts @@ -128,9 +128,10 @@ declare module 'klasa' { public on(event: 'monitorError', listener: (message: KlasaMessage, monitor: Monitor, error: Error | string) => void): this; public on(event: 'finalizerError', listener: (message: KlasaMessage, response: KlasaMessage, runTime: Timestamp, finalizer: Finalizer, error: Error | string) => void): this; public on(event: 'taskError', listener: (scheduledTask: ScheduledTask, task: Task, error: Error) => void): this; - public on(event: 'settingsCreateEntry', listener: (entry: Settings) => void): this; - public on(event: 'settingsDeleteEntry', listener: (entry: Settings) => void): this; - public on(event: 'settingsUpdateEntry', listener: (oldEntry: Settings, newEntry: Settings, path?: string) => void): this; + public on(event: 'settingsSync', listener: (entry: Settings) => void): this; + public on(event: 'settingsCreate', listener: (entry: Settings) => void): this; + public on(event: 'settingsDelete', listener: (entry: Settings) => void): this; + public on(event: 'settingsUpdate', listener: (entry: Settings, newEntry: SettingsFolderUpdateResultEntry[], path: string[]) => void): this; public on(event: 'log', listener: (data: any) => void): this; public on(event: 'verbose', listener: (data: any) => void): this; public on(event: 'wtf', listener: (failure: Error) => void): this; @@ -193,9 +194,10 @@ declare module 'klasa' { public once(event: 'monitorError', listener: (message: KlasaMessage, monitor: Monitor, error: Error | string) => void): this; public once(event: 'finalizerError', listener: (message: KlasaMessage, response: KlasaMessage, runTime: Timestamp, finalizer: Finalizer, error: Error | string) => void): this; public once(event: 'taskError', listener: (scheduledTask: ScheduledTask, task: Task, error: Error) => void): this; - public once(event: 'settingsCreateEntry', listener: (entry: Settings) => void): this; - public once(event: 'settingsDeleteEntry', listener: (entry: Settings) => void): this; - public once(event: 'settingsUpdateEntry', listener: (oldEntry: Settings, newEntry: Settings, path?: string) => void): this; + public once(event: 'settingsSync', listener: (entry: Settings) => void): this; + public once(event: 'settingsCreate', listener: (entry: Settings) => void): this; + public once(event: 'settingsDelete', listener: (entry: Settings) => void): this; + public once(event: 'settingsUpdate', listener: (entry: Settings, newEntry: SettingsFolderUpdateResultEntry[], path: string[]) => void): this; public once(event: 'log', listener: (data: any) => void): this; public once(event: 'verbose', listener: (data: any) => void): this; public once(event: 'wtf', listener: (failure: Error) => void): this;