Skip to content
This repository has been archived by the owner on Apr 27, 2024. It is now read-only.

Commit

Permalink
misc: Updated for settings' branch breaking changes
Browse files Browse the repository at this point in the history
  • Loading branch information
kyranet committed Apr 17, 2019
1 parent 0c1c030 commit fbf7e9f
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 43 deletions.
8 changes: 3 additions & 5 deletions src/lib/Client.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,10 @@ module.exports = class extends Client {

static [Client.plugin]() {
mergeDefault(CLIENT, this.options);
const { members } = this.options.gateways;
const memberSchema = 'schema' in members ? members.schema : this.constructor.defaultMemberSchema;

this.gateways.members = new MemberGateway(this.gateways, 'members', memberSchema, members.provider || this.options.providers.default);
this.gateways.keys.add('members');
this.gateways._queue.push(this.gateways.members.init.bind(this.gateways.members));
const { members } = this.options.gateways;
members.schema = 'schema' in members ? members.schema : this.constructor.defaultMemberSchema;
this.gateways.register(new MemberGateway(this, 'members', members));
}

};
2 changes: 1 addition & 1 deletion src/lib/extensions/KlasaMember.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ module.exports = Structures.extend('GuildMember', GuildMember => {
* @since 0.0.1
* @type {external:Settings}
*/
this.settings = this.client.gateways.members.create([this.guild.id, this.id]);
this.settings = this.client.gateways.get('members').acquire([this.guild.id, this.id], this);
}

/**
Expand Down
56 changes: 24 additions & 32 deletions src/lib/settings/MemberGateway.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
const { GatewayStorage, Settings, util: { getIdentifier } } = require('klasa');
const { Collection } = require('discord.js');

/**
* The Gateway class that manages the data input, parsing, and output, of an entire database, while keeping a cache system sync with the changes.
Expand All @@ -9,27 +8,19 @@ class MemberGateway extends GatewayStorage {

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

/**
* The GatewayDriver that manages this Gateway
* @since 0.0.1
* @type {external:GatewayDriver}
*/
this.store = store;
constructor(client, name, options) {
super(client, name, options);

/**
* The synchronization queue for all Settings instances
* @since 0.0.1
* @type {external:Collection<string, Promise<external:Settings>>}
* @type {WeakMap<string, Promise<external:Settings>>}
*/
this.syncQueue = new Collection();
this.syncMap = new WeakMap();

/**
* @since 0.0.1
Expand All @@ -39,17 +30,6 @@ class MemberGateway extends GatewayStorage {
Object.defineProperty(this, '_synced', { value: false, writable: true });
}

/**
* The Settings that this class should make.
* @since 0.0.1
* @type {external:Settings}
* @readonly
* @private
*/
get Settings() {
return Settings;
}

/**
* The ID length for all entries.
* @since 0.0.1
Expand All @@ -62,10 +42,21 @@ class MemberGateway extends GatewayStorage {
return 37;
}

/**
* Gets an entry from the cache or creates one if it does not exist
* @since 0.5.0
* @param {string|string[]} id The id for this instance
* @param {*} target The target that holds a Settings instance of the holder for the new one
* @returns {external:Settings}
*/
acquire(id, target) {
return this.get(id) || this.create(id, target);
}

/**
* Get a Settings entry from this gateway
* @since 0.0.1
* @param {string|string[]} id The id for this instance
* @param {string|string[]} id The id for the instance to retrieve
* @returns {?external:Settings}
*/
get(id) {
Expand All @@ -84,15 +75,16 @@ class MemberGateway extends GatewayStorage {
* Create a new Settings for this gateway
* @since 0.0.1
* @param {string|string[]} id The id for this instance
* @param {Object<string, *>} [data={}] The data for this Settings instance
* @param {any} target The holder for this Settings instance
* @returns {external:Settings}
*/
create(id, data = {}) {
create(id, target) {
const [guildID, memberID] = typeof id === 'string' ? id.split('.') : id;

const entry = this.get([guildID, memberID]);
if (entry) return entry;

const settings = new this.Settings(this, { id: `${guildID}.${memberID}`, ...data });
const settings = new Settings(this, target, `${guildID}.${memberID}`);
if (this._synced) settings.sync();
return settings;
}
Expand All @@ -106,7 +98,7 @@ class MemberGateway extends GatewayStorage {
async sync(input = this.client.guilds.reduce((keys, guild) => keys.concat(guild.members.map(member => member.settings.id)), [])) {
if (Array.isArray(input)) {
if (!this._synced) this._synced = true;
const entries = await this.provider.getAll(this.type, input);
const entries = await this.provider.getAll(this.name, input);
for (const entry of entries) {
if (!entry) continue;

Expand Down
16 changes: 11 additions & 5 deletions typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,6 @@ declare module 'klasa-member-gateway' {

export { MemberGatewayClient as Client };

export class KlasaMember extends GuildMember {
public settings: Settings;
public toJSON(): KlasaMemberJSON;
}

export class MemberGateway extends GatewayStorage {
public store: GatewayDriver;
public syncQueue: Collection<string, Promise<Settings>>;
Expand All @@ -51,4 +46,15 @@ declare module 'klasa-member-gateway' {
roles: Array<Snowflake>;
settings: Settings;
};

}

declare module 'discord.js' {

import { Settings } from 'klasa';

export interface GuildMember {
public settings: Settings;
}

}

0 comments on commit fbf7e9f

Please sign in to comment.