Skip to content
This repository has been archived by the owner on Aug 28, 2020. It is now read-only.

feat: Added KlasaClient#settingsSync event. #475

Merged
merged 7 commits into from
Nov 6, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions guides/Included Pieces/IncludedEvents.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,20 @@ 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)});
if (entry) {
entry._patch(${JSON.stringify(settings)});
entry.existenceStatus = true;
this.emit('settingsSync', settings);
}
}
`);
}
Expand Down
13 changes: 10 additions & 3 deletions src/lib/Client.js
Original file line number Diff line number Diff line change
Expand Up @@ -633,24 +633,31 @@ 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
*/

/**
* 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
*/
Expand Down
7 changes: 5 additions & 2 deletions src/lib/settings/Settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
});
Expand All @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions src/lib/settings/SettingsFolder.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {};
Expand Down
1 change: 1 addition & 0 deletions src/lib/settings/gateway/Gateway.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ class Gateway extends GatewayStorage {
if (cache) {
cache.existenceStatus = true;
cache._patch(entry);
this.client.emit('settingsSync', cache);
}
}

Expand Down
14 changes: 8 additions & 6 deletions typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,10 @@ declare module 'klasa' {
public on(event: 'taskError', listener: (scheduledTask: ScheduledTask, task: Task, error: Error) => void): this;

// SettingGateway Events
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;

// Klasa Console Custom Events
public on(event: 'log', listener: (data: any) => void): this;
Expand Down Expand Up @@ -193,9 +194,10 @@ declare module 'klasa' {
public once(event: 'taskError', listener: (scheduledTask: ScheduledTask, task: Task, error: Error) => void): this;

// SettingGateway Events
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;

// Klasa Console Custom Events
public once(event: 'log', listener: (data: any) => void): this;
Expand Down