Webpack build for branch custom-structures: f3817e328b

This commit is contained in:
Travis CI 2017-11-20 06:40:54 +00:00
parent 5f7164a540
commit 70bf0dcc0f
2 changed files with 29 additions and 17 deletions

View file

@ -9603,8 +9603,6 @@ module.exports = DMChannel;
/** /**
* Allows for the extension of built-in Discord.js structures that are instantiated by {@link DataStore DataStores}. * Allows for the extension of built-in Discord.js structures that are instantiated by {@link DataStore DataStores}.
* When extending a built-in structure, it is important to both get the class you're extending from here,
* and to set it here afterwards.
*/ */
class Structures { class Structures {
constructor() { constructor() {
@ -9622,32 +9620,46 @@ class Structures {
} }
/** /**
* Replaces a structure class with an extended one. * Extends a structure.
* @param {string} structure Name of the structure to replace * @param {string} structure Name of the structure class to extend
* @param {Function} extended Extended structure class/prototype function to replace with * @param {Function} extender Function that takes the base class to extend as its only parameter and returns the
* extended class/prototype
* @returns {Function} Extended class/prototype returned from the extender
* @example * @example
* const { Structures } = require('discord.js'); * const { Structures } = require('discord.js');
* *
* class CoolGuild extends Structures.get('Guild') { * Structures.extend('Guild', Guild => {
* constructor(client, data) { * class CoolGuild extends Guild {
* super(client, data); * constructor(client, data) {
* this.cool = true; * super(client, data);
* this.cool = true;
* }
* } * }
* }
* *
* Structures.set('Guild', CoolGuild); * return CoolGuild;
* });
*/ */
static set(structure, extended) { static extend(structure, extender) {
if (!structures[structure]) throw new RangeError(`"${structure}" is not a valid extensible structure.`); if (!structures[structure]) throw new RangeError(`"${structure}" is not a valid extensible structure.`);
if (typeof extended !== 'function') { if (typeof extender !== 'function') {
const received = `(received ${typeof extender})`;
throw new TypeError( throw new TypeError(
`"extended" argument must be a structure class/prototype function (received ${typeof extended})` `"extender" argument must be a function that returns the extended structure class/prototype ${received}`
); );
} }
if (Object.getPrototypeOf(extended) !== structures[structure]) {
throw new Error('The class/prototype function provided doesn\'t extend the existing structure class/prototype.'); const extended = extender(structures[structure]);
if (typeof extended !== 'function') {
throw new TypeError('The extender function must return the extended structure class/prototype.');
} }
if (Object.getPrototypeOf(extended) !== structures[structure]) {
throw new Error(
'The class/prototype returned from the extender function must extend the existing structure class/prototype.'
);
}
structures[structure] = extended; structures[structure] = extended;
return extended;
} }
} }

File diff suppressed because one or more lines are too long