Skip to content

Commit 436784f

Browse files
perf(Components): Hash table (#10890)
refactor(Components): hash table Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
1 parent 320e3a6 commit 436784f

File tree

1 file changed

+12
-24
lines changed

1 file changed

+12
-24
lines changed

packages/discord.js/src/util/Components.js

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -68,30 +68,7 @@ const { ComponentType } = require('discord-api-types/v10');
6868
* @ignore
6969
*/
7070
function createComponent(data) {
71-
if (data instanceof Component) {
72-
return data;
73-
}
74-
75-
switch (data.type) {
76-
case ComponentType.ActionRow:
77-
return new ActionRow(data);
78-
case ComponentType.Button:
79-
return new ButtonComponent(data);
80-
case ComponentType.StringSelect:
81-
return new StringSelectMenuComponent(data);
82-
case ComponentType.TextInput:
83-
return new TextInputComponent(data);
84-
case ComponentType.UserSelect:
85-
return new UserSelectMenuComponent(data);
86-
case ComponentType.RoleSelect:
87-
return new RoleSelectMenuComponent(data);
88-
case ComponentType.MentionableSelect:
89-
return new MentionableSelectMenuComponent(data);
90-
case ComponentType.ChannelSelect:
91-
return new ChannelSelectMenuComponent(data);
92-
default:
93-
return new Component(data);
94-
}
71+
return data instanceof Component ? data : new (ComponentTypeToClass[data.type] ?? Component)(data);
9572
}
9673

9774
exports.createComponent = createComponent;
@@ -105,3 +82,14 @@ const { RoleSelectMenuComponent } = require('../structures/RoleSelectMenuCompone
10582
const { StringSelectMenuComponent } = require('../structures/StringSelectMenuComponent.js');
10683
const { TextInputComponent } = require('../structures/TextInputComponent.js');
10784
const { UserSelectMenuComponent } = require('../structures/UserSelectMenuComponent.js');
85+
86+
const ComponentTypeToClass = {
87+
[ComponentType.ActionRow]: ActionRow,
88+
[ComponentType.Button]: ButtonComponent,
89+
[ComponentType.StringSelect]: StringSelectMenuComponent,
90+
[ComponentType.TextInput]: TextInputComponent,
91+
[ComponentType.UserSelect]: UserSelectMenuComponent,
92+
[ComponentType.RoleSelect]: RoleSelectMenuComponent,
93+
[ComponentType.MentionableSelect]: MentionableSelectMenuComponent,
94+
[ComponentType.ChannelSelect]: ChannelSelectMenuComponent,
95+
};

0 commit comments

Comments
 (0)