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

Commit

Permalink
src: GatewayDriver#register changes (#480)
Browse files Browse the repository at this point in the history
* refactor: Convert GatewayDriver into a Collection<string, Gateway> (#467)

* refactor: Convert GatewayDriver into a Collection<string, Gateway>

* fix: Better error message for gateway existence in register

Co-Authored-By: kyranet <kyradiscord@gmail.com>

* refactor: Upgrade API for Settings#update (#426)

* refactor: No more _update

* cleanup: More re-organization

* src: More refactors

* Settings#{list,resolveString} -> Settings#display

* style: Rename constant name

* refactor: Re-add Conf#check and reduce duplicated code in Settings#reset

* fix: _resolveUpdateOverloads not parsing reset args correctly

* fix: Synchronize the Settings entry on update or reset instead of returning wrong results

* typings: Fixed QB types and updated everything

* docs: Added missing jsdocs and refactored Settings#get

* fix: conf and userconf always throwing on empty values

* fix: Better Schema#get

* fix: Settings#show using Map[key] instead of Map#get(key)

* fix: Settings#_resolveUpdateOverloads not checking for the lack of a key

* fix: conf command not passing all args for check

* fix: Fixed multiple things

* docs: Update guides to reflect latest changes

* pr: Update changelog

* fix: Collection not being destructured from discord.js

Now it's the time when I run and hide for such a silly mistake.

* Settings rewrite (#471)

* wip: Settings rewrite

* design: Chain SettingsFolder#get instead of recursing, remove '*' wildcard

* refactor: Made SettingsFolder#get use Array#reduce

* src: Add Settings#init

* src: Added SettingsFolder#display

* src: Remove Settings and all traces of it

* src: Update Gateway, move SettingsFolder#existenceStatus to Settings

* docs: Documented all methods and properties of Settings and SettingsFolder

* naming: Gateway#type -> Gateway#name

* src: Change all occurrences of Settings[key] to Settings.get(key)

* src: SettingsUpdateOptions#{action|avoidUnconfigurable} -> {arrayAction|onlyConfigurable}

* src: guildSettings[key] -> guildSettings.get(key)

* typings: GatewayStorage#type -> GatewayStorage#name

* refactor: Faster Schema#get

* typings: Fix Settings class typings

* typings: Iterable -> IterableIterator

* docs: Fixed util docs

* fix: Use Map#call instead of inherited SettingsFolder#get and Schema

* fix: Settings#init deepCloning the wrong value

* fix: KlasaGuild#language using Settings[key] instead of Settings.get(key)

* inspection: Make Settings#{id|base} non-enumerable

* fix: Sandbox Schema#get to mimic previous functionality

* fix: Better errors for Settings#{reset,update}

* fix: Fixed SettingsFolder#display

* docs: Added comments to justify the usage of Map.prototype.get.call

* fix: SettingsFolder#display not getting the right path

* fix: Revert change to commandHandler's mention only check

* docs: Justify the prefix.length in commandHandler's mentionOnly

* feat: SettingsFolder#pluck

* fix: Updated providers helpers for the new Gateway results

* fix: SettingsFolder#update not overloading well

* fix: Settings and SettingsFolder trying to access client

* fix: Fixed updating not patching correctly

* fix: settingsUpdateEntry event receiving a SettingsFolder instead of the Settings instance

* src: Better relative path parsing, fixed update inserting full values into db

* fix: JSON provider not parsing create queries

* fix: Configuration commands not checking correctly

* src: Added Settings#target, Gateway#{create,acquire}, removed create in get

* fix: Init schedule before pieces (but after SG)

* i18n,fix: Default guild option in Settings#{update,reset}, i18n all errors

* refactor: Better guild resolving for Settings#{reset,update}

* fix: resolveGuild calls without client

* fix: SQLProvider accessing to the gateway wrongly

* changelog: Updated for the PR

* fix: Settings#clone not working as expected

* misc: Make Settings#{id,gateway,target} enumerable

* revert: Settings#{gateway,target} enumerability

* cleanup: Remove GatewayStorage#defaults

* misc: Make Schema#defaults a property

* fix: Call this.init() in destroy instead of patching

* docs: Updated changelog

* lint: Fixed lint

* docs: Fixed jsdocs

* src: Reorganize files

* style: Remove optional property in Settings#id

* memory: Deepclone patch, not init

* fix: Access to undefined

* fix: Settings#update not serializing arrays correctly

* src: Remove all usage of deepClone in SettingsGateway

* misc: Make Schema#defaults a SettingsFolder instance

* docs: Finished changelog

* misc: Remove guild option from the conf command

* docs: Forgot to mention arrayPosition->arrayIndex in CHANGELOG

* perf: Optimize Gateway#sync to the clouds for empty schemas

* 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

* fix: Old typings, patch before emitting events

* fix: Settings delete core event (#477)

* fix: Added core event handler for settingsDelete

* docs: Updated IncludedEvents

* docs: Correct changelog PR number

* fix: Don't check this.client.shard in both core settings event handlers

* misc: Add IS support for both core settings events handlers

* misc: Much better and faster shard check for both settings events

* src: GatewayDriver#register changes

* fix: I missed this

* misc: Requested changes

* src: Requested change

* fix: Who did this

* fix: settings is not defined in the evals
  • Loading branch information
kyranet committed Nov 11, 2018
1 parent 01751c9 commit 26f7226
Show file tree
Hide file tree
Showing 9 changed files with 48 additions and 65 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ NOTE: For the contributors, you add new entries to this document following this

### Changed

- [[#480](https://github.com/dirigeants/klasa/pull/480)] Changed the entire mechanism for `GatewayDriver#register` to take a `GatewayStorage` instance. (kyranet)
- [[#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)
Expand Down Expand Up @@ -254,6 +255,7 @@ NOTE: For the contributors, you add new entries to this document following this

### Removed

- [[#480](https://github.com/dirigeants/klasa/pull/480)] Removed `Gateway#store`. (kyranet)
- [[#471](https://github.com/dirigeants/klasa/pull/471)] Removed `Gateway#defaults`. Refer to `Schema#defaults` instead. (kyranet)
- [[#426](https://github.com/dirigeants/klasa/pull/426)] Removed `Gateway#getPath`. They're now resolved inside Settings. (kyranet)
- [[#401](https://github.com/dirigeants/klasa/pull/401)] Removed `Settings#waitSync` in favor of `Settings#sync(?false);`. (kyranet)
Expand Down
2 changes: 1 addition & 1 deletion src/commands/Admin/transfer.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ module.exports = class extends Command {
piece.store.load(piece.store.userDirectory, piece.file);
if (this.client.shard) {
await this.client.shard.broadcastEval(`
if (String(this.shard.id) !== '${this.client.shard.id}') this.${piece.store}.load(${piece.store.userDirectory}, ${JSON.stringify(piece.file)});
if (String(this.shard.id) !== '${this.client.shard.id}') this.${piece.store}.load('${piece.store.userDirectory}', ${JSON.stringify(piece.file)});
`);
}
return message.sendLocale('COMMAND_TRANSFER_SUCCESS', [piece.type, piece.name]);
Expand Down
2 changes: 1 addition & 1 deletion src/events/coreSettingsDelete.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ module.exports = class extends Event {
if (String(this.shard.id) !== '${this.client.shard.id}') {
const entry = this.gateways.get('${settings.gateway.name}').get('${settings.id}');
if (entry && entry.existenceStatus) {
this.emit('settingsDelete', settings);
this.emit('settingsDelete', entry);
entry.init(entry, entry.schema);
entry.existenceStatus = false;
}
Expand Down
2 changes: 1 addition & 1 deletion src/events/coreSettingsUpdate.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ module.exports = class extends Event {
if (entry) {
entry._patch(${JSON.stringify(settings)});
entry.existenceStatus = true;
this.emit('settingsSync', settings);
this.emit('settingsSync', entry);
}
}
`);
Expand Down
7 changes: 4 additions & 3 deletions src/lib/Client.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const TaskStore = require('./structures/TaskStore');
const GatewayDriver = require('./settings/gateway/GatewayDriver');

// lib/settings/schema
const Gateway = require('./settings/gateway/Gateway');
const Schema = require('./settings/schema/Schema');

// lib/util
Expand Down Expand Up @@ -267,9 +268,9 @@ class KlasaClient extends Discord.Client {

// Register default gateways
this.gateways
.register('guilds', { ...guilds, schema: guildSchema })
.register('users', { ...users, schema: userSchema })
.register('clientStorage', { ...clientStorage, schema: clientSchema });
.register(new Gateway(this, 'guilds', { schema: guildSchema, ...this.options.gateways.guilds || {} }))
.register(new Gateway(this, 'users', { schema: userSchema, ...this.options.gateways.users || {} }))
.register(new Gateway(this, 'clientStorage', { schema: clientSchema, ...this.options.gateways.clientStorage || {} }));

/**
* The Settings instance that handles this client's settings
Expand Down
19 changes: 7 additions & 12 deletions src/lib/settings/gateway/Gateway.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const GatewayStorage = require('./GatewayStorage');
const Settings = require('../Settings');
const Schema = require('../schema/Schema');
const { Collection } = require('discord.js');

/**
Expand All @@ -12,20 +13,14 @@ class Gateway extends GatewayStorage {

/**
* @since 0.0.1
* @param {GatewayDriver} store The GatewayDriver instance which initiated this instance
* @param {KlasaClient} client The KlasaClient instance which initiated this instance
* @param {string} name The name of this Gateway
* @param {Schema} schema The schema for this gateway
* @param {string} provider The provider's name for this gateway
* @param {Object} [options = {}] The options for this gateway
* @param {Schema} [options.schema = new Schema()] The schema for this gateway
* @param {string} [options.provider = this.client.options.providers.default] The provider's name for this gateway
*/
constructor(store, name, schema, provider) {
super(store.client, name, schema, provider);

/**
* The GatewayDriver that manages this Gateway
* @since 0.0.1
* @type {GatewayDriver}
*/
this.store = store;
constructor(client, name, { schema = new Schema(), provider = client.options.providers.default } = {}) {
super(client, name, schema, provider);

/**
* The cached entries for this Gateway or the external datastore to get the settings from
Expand Down
19 changes: 7 additions & 12 deletions src/lib/settings/gateway/GatewayDriver.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const Gateway = require('./Gateway');
const Schema = require('../schema/Schema');
const GatewayStorage = require('./GatewayStorage');
const Type = require('../../util/Type');
const { Collection } = require('discord.js');

/**
Expand Down Expand Up @@ -53,19 +53,14 @@ class GatewayDriver extends Collection {
/**
* Registers a new Gateway.
* @since 0.5.0
* @param {string} name The name for the new gateway
* @param {GatewayDriverRegisterOptions} [options = {}] The options for the new gateway
* @param {GatewayStorage} gateway The gateway to register
* @returns {this}
* @chainable
*/
register(name, { provider = this.client.options.providers.default, schema = new Schema() } = {}) {
if (typeof name !== 'string') throw new TypeError('You must pass a name for your new gateway and it must be a string.');
if (!(schema instanceof Schema)) throw new TypeError('Schema must be a valid Schema instance.');
if (this.has(name)) throw new Error(`The key '${name}' is taken by another Gateway.`);

if (!(name in this.client.options.gateways)) this.client.options.gateways[name] = {};
const gateway = new Gateway(this, name, schema, provider);
this.set(name, gateway);
register(gateway) {
if (!(gateway instanceof GatewayStorage)) throw new TypeError(`You must pass a GatewayStorage instance, received: ${new Type(gateway)}`);
if (!(gateway.name in this.client.options.gateways)) this.client.options.gateways[gateway.name] = {};
this.set(gateway.name, gateway);
return this;
}

Expand Down
10 changes: 6 additions & 4 deletions src/lib/settings/gateway/GatewayStorage.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ class GatewayStorage {
/**
* @typedef {Object} GatewayJSON
* @property {string} name The name of this gateway
* @property {GatewayDriverRegisterOptions} options The options for this gateway
* @property {string} provider The provider's name
* @property {Object} schema The current schema
*/

Expand Down Expand Up @@ -42,13 +42,15 @@ class GatewayStorage {
* @type {string}
* @readonly
*/
Object.defineProperty(this, 'providerName', { value: provider || this.client.options.providers.default });
Object.defineProperty(this, 'providerName', { value: provider });

/**
* @since 0.5.0
* @name GatewayStorage#schema
* @type {Schema}
* @readonly
*/
this.schema = schema;
Object.defineProperty(this, 'schema', { value: schema, enumerable: true });

/**
* @since 0.5.0
Expand Down Expand Up @@ -118,7 +120,7 @@ class GatewayStorage {
toJSON() {
return {
name: this.name,
options: { provider: this.providerName },
provider: this.providerName,
schema: this.schema.toJSON()
};
}
Expand Down
50 changes: 19 additions & 31 deletions typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -494,9 +494,21 @@ declare module 'klasa' {
private init(folder: SettingsFolder, schema: SchemaFolder): void;
}

export class GatewayDriver extends Collection<string, Gateway> {
private constructor(client: KlasaClient);
public readonly client: KlasaClient;
public ready: boolean;

public register(gateway: GatewayStorage): this;
public init(): Promise<void>;
public sync(input?: string[] | string): Promise<Array<Gateway>>;

public toJSON(): GatewayDriverJSON;
public toString(): string;
}

export class Gateway extends GatewayStorage {
public constructor(store: GatewayDriver, name: string, schema: Schema, options: GatewayOptions);
public store: GatewayDriver;
public constructor(client: KlasaClient, name: string, options?: { schema?: Schema, provider?: string });
public syncQueue: WeakMap<Settings, Promise<Settings>>;
public readonly Settings: Settings;
private cache: Collection<string, Record<string, any> & { settings: Settings }>;
Expand All @@ -517,27 +529,14 @@ declare module 'klasa' {
private readonly _datatypes: ObjectLiteral<Required<QueryBuilderDatatypeOptions>>;
}

export class GatewayDriver extends Collection<string, Gateway> {
private constructor(client: KlasaClient);
public readonly client: KlasaClient;
public ready: boolean;

public register(name: string, options?: GatewayDriverRegisterOptions): this;
public init(): Promise<void>;
public sync(input?: string[] | string): Promise<Array<Gateway>>;

public toJSON(): GatewayDriverJSON;
public toString(): string;
}

export abstract class GatewayStorage {
public constructor(client: KlasaClient, name: string, schema: Schema, provider?: string);
public constructor(client: KlasaClient, name: string, schema?: Schema, provider?: string);
public readonly client: KlasaClient;
public readonly provider: Provider | null;
public readonly providerName: string;
public readonly name: string;
public readonly schema: SchemaFolder;
public ready: boolean;
public schema: SchemaFolder;

public init(): Promise<void>;
public toJSON(): GatewayJSON;
Expand Down Expand Up @@ -1299,11 +1298,7 @@ declare module 'klasa' {
default?: string;
} & ObjectLiteral;

export type KlasaGatewaysOptions = {
clientStorage?: GatewayDriverRegisterOptions;
guilds?: GatewayDriverRegisterOptions;
users?: GatewayDriverRegisterOptions;
} & ObjectLiteral;
export type KlasaGatewaysOptions = Record<string, { provider ?: string }>;

// Parsers
export type ArgResolverCustomMethod = (arg: string, possible: Possible, message: KlasaMessage, params: string[]) => any;
Expand Down Expand Up @@ -1453,9 +1448,9 @@ declare module 'klasa' {
};

export type GatewayJSON = {
options: GatewayOptions;
schema: SchemaFolderAddOptions;
name: string;
provider: string;
schema: SchemaFolderAddOptions;
};

export type QueryBuilderDatatypeOptions = {
Expand All @@ -1472,12 +1467,6 @@ declare module 'klasa' {
| KlasaGuildChannel
| Snowflake;

export type GatewayDriverRegisterOptions = {
provider?: string;
schema?: Schema;
syncArg?: string[] | string | true;
};

export type SchemaFolderAddOptions = SchemaFolderOptions | SchemaPieceOptions;

export type SchemaPieceOptions = {
Expand All @@ -1501,7 +1490,6 @@ declare module 'klasa' {
clientStorage: GatewayJSON;
guilds: GatewayJSON;
users: GatewayJSON;
keys: string[];
ready: boolean;
} & ObjectLiteral<GatewayJSON>;

Expand Down

0 comments on commit 26f7226

Please sign in to comment.