@zygout/eris 0.19.0 → 0.19.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +20 -20
- package/README.md +71 -80
- package/esm.mjs +72 -72
- package/index.d.ts +4407 -4397
- package/index.js +75 -75
- package/lib/Client.js +4708 -4683
- package/lib/Constants.d.ts +990 -988
- package/lib/Constants.js +1163 -1161
- package/lib/command/Command.js +552 -552
- package/lib/command/CommandClient.js +460 -460
- package/lib/errors/DiscordHTTPError.js +63 -63
- package/lib/errors/DiscordRESTError.js +77 -77
- package/lib/gateway/Shard.js +3029 -3029
- package/lib/gateway/ShardManager.js +164 -164
- package/lib/rest/Endpoints.js +172 -172
- package/lib/rest/RequestHandler.js +459 -459
- package/lib/structures/ApplicationCommand.js +125 -125
- package/lib/structures/AutoModerationRule.js +143 -143
- package/lib/structures/AutocompleteInteraction.js +80 -80
- package/lib/structures/Base.js +81 -81
- package/lib/structures/CategoryChannel.js +77 -77
- package/lib/structures/Channel.js +98 -98
- package/lib/structures/CommandInteraction.js +473 -473
- package/lib/structures/ComponentInteraction.js +538 -538
- package/lib/structures/DMChannel.js +278 -278
- package/lib/structures/Entitlement.js +77 -77
- package/lib/structures/ExtendedUser.js +46 -46
- package/lib/structures/ForumChannel.js +24 -24
- package/lib/structures/GroupChannel.js +106 -106
- package/lib/structures/Guild.js +1677 -1677
- package/lib/structures/GuildAuditLogEntry.js +190 -190
- package/lib/structures/GuildChannel.js +128 -128
- package/lib/structures/GuildIntegration.js +99 -99
- package/lib/structures/GuildPreview.js +97 -97
- package/lib/structures/GuildScheduledEvent.js +176 -176
- package/lib/structures/GuildTemplate.js +76 -76
- package/lib/structures/GuildTextableChannel.js +260 -260
- package/lib/structures/Interaction.js +133 -133
- package/lib/structures/Invite.js +143 -143
- package/lib/structures/Lobby.js +108 -108
- package/lib/structures/LobbyMember.js +59 -59
- package/lib/structures/MediaChannel.js +216 -216
- package/lib/structures/Member.js +371 -371
- package/lib/structures/Message.js +708 -708
- package/lib/structures/ModalSubmitInteraction.js +478 -478
- package/lib/structures/NewsChannel.js +34 -34
- package/lib/structures/NewsThreadChannel.js +12 -12
- package/lib/structures/Permission.js +80 -80
- package/lib/structures/PermissionOverwrite.js +32 -32
- package/lib/structures/PingInteraction.js +39 -39
- package/lib/structures/PrivateThreadChannel.js +25 -25
- package/lib/structures/PublicThreadChannel.js +26 -26
- package/lib/structures/Role.js +163 -163
- package/lib/structures/SKU.js +80 -80
- package/lib/structures/SoundboardSound.js +79 -79
- package/lib/structures/StageChannel.js +59 -59
- package/lib/structures/StageInstance.js +55 -55
- package/lib/structures/Subscription.js +69 -69
- package/lib/structures/TextChannel.js +202 -202
- package/lib/structures/ThreadChannel.js +143 -143
- package/lib/structures/ThreadMember.js +59 -59
- package/lib/structures/UnavailableGuild.js +26 -26
- package/lib/structures/UnknownInteraction.js +502 -502
- package/lib/structures/User.js +296 -296
- package/lib/structures/VoiceChannel.js +166 -166
- package/lib/structures/VoiceState.js +84 -84
- package/lib/util/BrowserWebSocket.js +76 -76
- package/lib/util/Bucket.js +89 -89
- package/lib/util/Collection.js +202 -202
- package/lib/util/MultipartData.js +70 -70
- package/lib/util/Opus.js +33 -33
- package/lib/util/SequentialBucket.js +83 -83
- package/lib/util/emitDeprecation.js +26 -26
- package/lib/voice/Piper.js +300 -300
- package/lib/voice/SharedStream.js +238 -238
- package/lib/voice/VoiceConnection.js +1440 -1440
- package/lib/voice/VoiceConnectionManager.js +151 -151
- package/lib/voice/VoiceDataStream.js +22 -22
- package/lib/voice/streams/BaseTransformer.js +40 -40
- package/lib/voice/streams/DCAOpusTransformer.js +78 -78
- package/lib/voice/streams/FFmpegDuplex.js +192 -192
- package/lib/voice/streams/FFmpegOggTransformer.js +35 -35
- package/lib/voice/streams/FFmpegPCMTransformer.js +26 -26
- package/lib/voice/streams/OggOpusTransformer.js +108 -108
- package/lib/voice/streams/PCMOpusTransformer.js +61 -61
- package/lib/voice/streams/VolumeTransformer.js +50 -50
- package/lib/voice/streams/WebmOpusTransformer.js +258 -258
- package/package.json +83 -82
package/lib/util/Bucket.js
CHANGED
|
@@ -1,89 +1,89 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
const util = require("util");
|
|
4
|
-
const Base = require("../structures/Base");
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* Handle ratelimiting something
|
|
8
|
-
* @prop {Number} interval How long (in ms) to wait between clearing used tokens
|
|
9
|
-
* @prop {Number} lastReset Timestamp of last token clearing
|
|
10
|
-
* @prop {Number} lastSend Timestamp of last token consumption
|
|
11
|
-
* @prop {Number} tokenLimit The max number tokens the bucket can consume per interval
|
|
12
|
-
* @prop {Number} tokens How many tokens the bucket has consumed in this interval
|
|
13
|
-
*/
|
|
14
|
-
class Bucket {
|
|
15
|
-
/**
|
|
16
|
-
* Construct a Bucket
|
|
17
|
-
* @arg {Number} tokenLimit The max number of tokens the bucket can consume per interval
|
|
18
|
-
* @arg {Number} interval How long (in ms) to wait between clearing used tokens
|
|
19
|
-
* @arg {Object} [options] Optional parameters
|
|
20
|
-
* @arg {Object} options.latencyRef A latency reference object
|
|
21
|
-
* @arg {Number} options.latencyRef.latency Interval between consuming tokens
|
|
22
|
-
* @arg {Number} options.reservedTokens How many tokens to reserve for priority operations
|
|
23
|
-
*/
|
|
24
|
-
constructor(tokenLimit, interval, options = {}) {
|
|
25
|
-
this.tokenLimit = tokenLimit;
|
|
26
|
-
this.interval = interval;
|
|
27
|
-
this.latencyRef = options.latencyRef || { latency: 0 };
|
|
28
|
-
this.lastReset = this.tokens = this.lastSend = 0;
|
|
29
|
-
this.reservedTokens = options.reservedTokens || 0;
|
|
30
|
-
this._queue = [];
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
check() {
|
|
34
|
-
if (this.timeout || this._queue.length === 0) {
|
|
35
|
-
return;
|
|
36
|
-
}
|
|
37
|
-
if (this.lastReset + this.interval + this.tokenLimit * this.latencyRef.latency < Date.now()) {
|
|
38
|
-
this.lastReset = Date.now();
|
|
39
|
-
this.tokens = Math.max(0, this.tokens - this.tokenLimit);
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
let val;
|
|
43
|
-
let tokensAvailable = this.tokens < this.tokenLimit;
|
|
44
|
-
let unreservedTokensAvailable = this.tokens < (this.tokenLimit - this.reservedTokens);
|
|
45
|
-
while (this._queue.length > 0 && (unreservedTokensAvailable || (tokensAvailable && this._queue[0].priority))) {
|
|
46
|
-
this.tokens++;
|
|
47
|
-
tokensAvailable = this.tokens < this.tokenLimit;
|
|
48
|
-
unreservedTokensAvailable = this.tokens < (this.tokenLimit - this.reservedTokens);
|
|
49
|
-
const item = this._queue.shift();
|
|
50
|
-
val = this.latencyRef.latency - Date.now() + this.lastSend;
|
|
51
|
-
if (this.latencyRef.latency === 0 || val <= 0) {
|
|
52
|
-
item.func();
|
|
53
|
-
this.lastSend = Date.now();
|
|
54
|
-
} else {
|
|
55
|
-
setTimeout(() => {
|
|
56
|
-
item.func();
|
|
57
|
-
}, val);
|
|
58
|
-
this.lastSend = Date.now() + val;
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
if (this._queue.length > 0 && !this.timeout) {
|
|
63
|
-
this.timeout = setTimeout(() => {
|
|
64
|
-
this.timeout = null;
|
|
65
|
-
this.check();
|
|
66
|
-
}, this.tokens < this.tokenLimit ? this.latencyRef.latency : Math.max(0, this.lastReset + this.interval + this.tokenLimit * this.latencyRef.latency - Date.now()));
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
/**
|
|
71
|
-
* Queue something in the Bucket
|
|
72
|
-
* @arg {Function} func A callback to call when a token can be consumed
|
|
73
|
-
* @arg {Boolean} [priority=false] Whether or not the callback should use reserved tokens
|
|
74
|
-
*/
|
|
75
|
-
queue(func, priority = false) {
|
|
76
|
-
if (priority) {
|
|
77
|
-
this._queue.unshift({ func, priority });
|
|
78
|
-
} else {
|
|
79
|
-
this._queue.push({ func, priority });
|
|
80
|
-
}
|
|
81
|
-
this.check();
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
[util.inspect.custom]() {
|
|
85
|
-
return Base.prototype[util.inspect.custom].call(this);
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
module.exports = Bucket;
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const util = require("util");
|
|
4
|
+
const Base = require("../structures/Base");
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Handle ratelimiting something
|
|
8
|
+
* @prop {Number} interval How long (in ms) to wait between clearing used tokens
|
|
9
|
+
* @prop {Number} lastReset Timestamp of last token clearing
|
|
10
|
+
* @prop {Number} lastSend Timestamp of last token consumption
|
|
11
|
+
* @prop {Number} tokenLimit The max number tokens the bucket can consume per interval
|
|
12
|
+
* @prop {Number} tokens How many tokens the bucket has consumed in this interval
|
|
13
|
+
*/
|
|
14
|
+
class Bucket {
|
|
15
|
+
/**
|
|
16
|
+
* Construct a Bucket
|
|
17
|
+
* @arg {Number} tokenLimit The max number of tokens the bucket can consume per interval
|
|
18
|
+
* @arg {Number} interval How long (in ms) to wait between clearing used tokens
|
|
19
|
+
* @arg {Object} [options] Optional parameters
|
|
20
|
+
* @arg {Object} options.latencyRef A latency reference object
|
|
21
|
+
* @arg {Number} options.latencyRef.latency Interval between consuming tokens
|
|
22
|
+
* @arg {Number} options.reservedTokens How many tokens to reserve for priority operations
|
|
23
|
+
*/
|
|
24
|
+
constructor(tokenLimit, interval, options = {}) {
|
|
25
|
+
this.tokenLimit = tokenLimit;
|
|
26
|
+
this.interval = interval;
|
|
27
|
+
this.latencyRef = options.latencyRef || { latency: 0 };
|
|
28
|
+
this.lastReset = this.tokens = this.lastSend = 0;
|
|
29
|
+
this.reservedTokens = options.reservedTokens || 0;
|
|
30
|
+
this._queue = [];
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
check() {
|
|
34
|
+
if (this.timeout || this._queue.length === 0) {
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
if (this.lastReset + this.interval + this.tokenLimit * this.latencyRef.latency < Date.now()) {
|
|
38
|
+
this.lastReset = Date.now();
|
|
39
|
+
this.tokens = Math.max(0, this.tokens - this.tokenLimit);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
let val;
|
|
43
|
+
let tokensAvailable = this.tokens < this.tokenLimit;
|
|
44
|
+
let unreservedTokensAvailable = this.tokens < (this.tokenLimit - this.reservedTokens);
|
|
45
|
+
while (this._queue.length > 0 && (unreservedTokensAvailable || (tokensAvailable && this._queue[0].priority))) {
|
|
46
|
+
this.tokens++;
|
|
47
|
+
tokensAvailable = this.tokens < this.tokenLimit;
|
|
48
|
+
unreservedTokensAvailable = this.tokens < (this.tokenLimit - this.reservedTokens);
|
|
49
|
+
const item = this._queue.shift();
|
|
50
|
+
val = this.latencyRef.latency - Date.now() + this.lastSend;
|
|
51
|
+
if (this.latencyRef.latency === 0 || val <= 0) {
|
|
52
|
+
item.func();
|
|
53
|
+
this.lastSend = Date.now();
|
|
54
|
+
} else {
|
|
55
|
+
setTimeout(() => {
|
|
56
|
+
item.func();
|
|
57
|
+
}, val);
|
|
58
|
+
this.lastSend = Date.now() + val;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
if (this._queue.length > 0 && !this.timeout) {
|
|
63
|
+
this.timeout = setTimeout(() => {
|
|
64
|
+
this.timeout = null;
|
|
65
|
+
this.check();
|
|
66
|
+
}, this.tokens < this.tokenLimit ? this.latencyRef.latency : Math.max(0, this.lastReset + this.interval + this.tokenLimit * this.latencyRef.latency - Date.now()));
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Queue something in the Bucket
|
|
72
|
+
* @arg {Function} func A callback to call when a token can be consumed
|
|
73
|
+
* @arg {Boolean} [priority=false] Whether or not the callback should use reserved tokens
|
|
74
|
+
*/
|
|
75
|
+
queue(func, priority = false) {
|
|
76
|
+
if (priority) {
|
|
77
|
+
this._queue.unshift({ func, priority });
|
|
78
|
+
} else {
|
|
79
|
+
this._queue.push({ func, priority });
|
|
80
|
+
}
|
|
81
|
+
this.check();
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
[util.inspect.custom]() {
|
|
85
|
+
return Base.prototype[util.inspect.custom].call(this);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
module.exports = Bucket;
|
package/lib/util/Collection.js
CHANGED
|
@@ -1,202 +1,202 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Hold a bunch of something
|
|
5
|
-
* @extends Map
|
|
6
|
-
* @prop {Class} baseObject The base class for all items
|
|
7
|
-
* @prop {Number?} limit Max number of items to hold
|
|
8
|
-
*/
|
|
9
|
-
class Collection extends Map {
|
|
10
|
-
/**
|
|
11
|
-
* Construct a Collection
|
|
12
|
-
* @arg {Class} baseObject The base class for all items
|
|
13
|
-
* @arg {Number} [limit] Max number of items to hold
|
|
14
|
-
*/
|
|
15
|
-
constructor(baseObject, limit) {
|
|
16
|
-
super();
|
|
17
|
-
this.baseObject = baseObject;
|
|
18
|
-
this.limit = limit;
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
/**
|
|
22
|
-
* Update an object
|
|
23
|
-
* @arg {Object} obj The updated object data
|
|
24
|
-
* @arg {String} obj.id The ID of the object
|
|
25
|
-
* @arg {Class} [extra] An extra parameter the constructor may need
|
|
26
|
-
* @arg {Boolean} [replace] Whether to replace an existing object with the same ID
|
|
27
|
-
* @returns {Class} The updated object
|
|
28
|
-
*/
|
|
29
|
-
update(obj, extra, replace) {
|
|
30
|
-
if (!obj.id && obj.id !== 0) {
|
|
31
|
-
throw new Error("Missing object id");
|
|
32
|
-
}
|
|
33
|
-
const item = this.get(obj.id);
|
|
34
|
-
if (!item) {
|
|
35
|
-
return this.add(obj, extra, replace);
|
|
36
|
-
}
|
|
37
|
-
item.update(obj, extra);
|
|
38
|
-
return item;
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
/**
|
|
42
|
-
* Add an object
|
|
43
|
-
* @arg {Object} obj The object data
|
|
44
|
-
* @arg {String} obj.id The ID of the object
|
|
45
|
-
* @arg {Class} [extra] An extra parameter the constructor may need
|
|
46
|
-
* @arg {Boolean} [replace] Whether to replace an existing object with the same ID
|
|
47
|
-
* @returns {Class} The existing or newly created object
|
|
48
|
-
*/
|
|
49
|
-
add(obj, extra, replace) {
|
|
50
|
-
if (this.limit === 0) {
|
|
51
|
-
return (obj instanceof this.baseObject || obj.constructor.name === this.baseObject.name) ? obj : new this.baseObject(obj, extra);
|
|
52
|
-
}
|
|
53
|
-
if (obj.id == null) {
|
|
54
|
-
throw new Error("Missing object id");
|
|
55
|
-
}
|
|
56
|
-
const existing = this.get(obj.id);
|
|
57
|
-
if (existing && !replace) {
|
|
58
|
-
return existing;
|
|
59
|
-
}
|
|
60
|
-
if (!(obj instanceof this.baseObject || obj.constructor.name === this.baseObject.name)) {
|
|
61
|
-
obj = new this.baseObject(obj, extra);
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
this.set(obj.id, obj);
|
|
65
|
-
|
|
66
|
-
if (this.limit && this.size > this.limit) {
|
|
67
|
-
const iter = this.keys();
|
|
68
|
-
while (this.size > this.limit) {
|
|
69
|
-
this.delete(iter.next().value);
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
return obj;
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
/**
|
|
76
|
-
* Returns true if all elements satisfy the condition
|
|
77
|
-
* @arg {Function} func A function that takes an object and returns true or false
|
|
78
|
-
* @returns {Boolean} Whether or not all elements satisfied the condition
|
|
79
|
-
*/
|
|
80
|
-
every(func) {
|
|
81
|
-
for (const item of this.values()) {
|
|
82
|
-
if (!func(item)) {
|
|
83
|
-
return false;
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
return true;
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
/**
|
|
90
|
-
* Return all the objects that make the function evaluate true
|
|
91
|
-
* @arg {Function} func A function that takes an object and returns true if it matches
|
|
92
|
-
* @returns {Array<Class>} An array containing all the objects that matched
|
|
93
|
-
*/
|
|
94
|
-
filter(func) {
|
|
95
|
-
const arr = [];
|
|
96
|
-
for (const item of this.values()) {
|
|
97
|
-
if (func(item)) {
|
|
98
|
-
arr.push(item);
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
|
-
return arr;
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
/**
|
|
105
|
-
* Return the first object to make the function evaluate true
|
|
106
|
-
* @arg {Function} func A function that takes an object and returns true if it matches
|
|
107
|
-
* @returns {Class?} The first matching object, or undefined if no match
|
|
108
|
-
*/
|
|
109
|
-
find(func) {
|
|
110
|
-
for (const item of this.values()) {
|
|
111
|
-
if (func(item)) {
|
|
112
|
-
return item;
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
return undefined;
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
/**
|
|
119
|
-
* Return an array with the results of applying the given function to each element
|
|
120
|
-
* @arg {Function} func A function that takes an object and returns something
|
|
121
|
-
* @returns {Array} An array containing the results
|
|
122
|
-
*/
|
|
123
|
-
map(func) {
|
|
124
|
-
const arr = [];
|
|
125
|
-
for (const item of this.values()) {
|
|
126
|
-
arr.push(func(item));
|
|
127
|
-
}
|
|
128
|
-
return arr;
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
/**
|
|
132
|
-
* Get a random object from the Collection
|
|
133
|
-
* @returns {Class?} The random object, or undefined if there is no match
|
|
134
|
-
*/
|
|
135
|
-
random() {
|
|
136
|
-
const index = Math.floor(Math.random() * this.size);
|
|
137
|
-
const iter = this.values();
|
|
138
|
-
for (let i = 0; i < index; ++i) {
|
|
139
|
-
iter.next();
|
|
140
|
-
}
|
|
141
|
-
return iter.next().value;
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
/**
|
|
145
|
-
* Returns a value resulting from applying a function to every element of the collection
|
|
146
|
-
* @arg {Function} func A function that takes the previous value and the next item and returns a new value
|
|
147
|
-
* @arg {any} [initialValue] The initial value passed to the function
|
|
148
|
-
* @returns {any} The final result
|
|
149
|
-
*/
|
|
150
|
-
reduce(func, initialValue) {
|
|
151
|
-
const iter = this.values();
|
|
152
|
-
let val;
|
|
153
|
-
let result = initialValue === undefined ? iter.next().value : initialValue;
|
|
154
|
-
while ((val = iter.next().value) !== undefined) {
|
|
155
|
-
result = func(result, val);
|
|
156
|
-
}
|
|
157
|
-
return result;
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
/**
|
|
161
|
-
* Remove an object
|
|
162
|
-
* @arg {Object} obj The object
|
|
163
|
-
* @arg {String} obj.id The ID of the object
|
|
164
|
-
* @returns {Class?} The removed object, or null if nothing was removed
|
|
165
|
-
*/
|
|
166
|
-
remove(obj) {
|
|
167
|
-
const item = this.get(obj.id);
|
|
168
|
-
if (!item) {
|
|
169
|
-
return null;
|
|
170
|
-
}
|
|
171
|
-
this.delete(obj.id);
|
|
172
|
-
return item;
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
/**
|
|
176
|
-
* Returns true if at least one element satisfies the condition
|
|
177
|
-
* @arg {Function} func A function that takes an object and returns true or false
|
|
178
|
-
* @returns {Boolean} Whether or not at least one element satisfied the condition
|
|
179
|
-
*/
|
|
180
|
-
some(func) {
|
|
181
|
-
for (const item of this.values()) {
|
|
182
|
-
if (func(item)) {
|
|
183
|
-
return true;
|
|
184
|
-
}
|
|
185
|
-
}
|
|
186
|
-
return false;
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
toString() {
|
|
190
|
-
return `[Collection<${this.baseObject.name}>]`;
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
toJSON() {
|
|
194
|
-
const json = {};
|
|
195
|
-
for (const item of this.values()) {
|
|
196
|
-
json[item.id] = item;
|
|
197
|
-
}
|
|
198
|
-
return json;
|
|
199
|
-
}
|
|
200
|
-
}
|
|
201
|
-
|
|
202
|
-
module.exports = Collection;
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Hold a bunch of something
|
|
5
|
+
* @extends Map
|
|
6
|
+
* @prop {Class} baseObject The base class for all items
|
|
7
|
+
* @prop {Number?} limit Max number of items to hold
|
|
8
|
+
*/
|
|
9
|
+
class Collection extends Map {
|
|
10
|
+
/**
|
|
11
|
+
* Construct a Collection
|
|
12
|
+
* @arg {Class} baseObject The base class for all items
|
|
13
|
+
* @arg {Number} [limit] Max number of items to hold
|
|
14
|
+
*/
|
|
15
|
+
constructor(baseObject, limit) {
|
|
16
|
+
super();
|
|
17
|
+
this.baseObject = baseObject;
|
|
18
|
+
this.limit = limit;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Update an object
|
|
23
|
+
* @arg {Object} obj The updated object data
|
|
24
|
+
* @arg {String} obj.id The ID of the object
|
|
25
|
+
* @arg {Class} [extra] An extra parameter the constructor may need
|
|
26
|
+
* @arg {Boolean} [replace] Whether to replace an existing object with the same ID
|
|
27
|
+
* @returns {Class} The updated object
|
|
28
|
+
*/
|
|
29
|
+
update(obj, extra, replace) {
|
|
30
|
+
if (!obj.id && obj.id !== 0) {
|
|
31
|
+
throw new Error("Missing object id");
|
|
32
|
+
}
|
|
33
|
+
const item = this.get(obj.id);
|
|
34
|
+
if (!item) {
|
|
35
|
+
return this.add(obj, extra, replace);
|
|
36
|
+
}
|
|
37
|
+
item.update(obj, extra);
|
|
38
|
+
return item;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Add an object
|
|
43
|
+
* @arg {Object} obj The object data
|
|
44
|
+
* @arg {String} obj.id The ID of the object
|
|
45
|
+
* @arg {Class} [extra] An extra parameter the constructor may need
|
|
46
|
+
* @arg {Boolean} [replace] Whether to replace an existing object with the same ID
|
|
47
|
+
* @returns {Class} The existing or newly created object
|
|
48
|
+
*/
|
|
49
|
+
add(obj, extra, replace) {
|
|
50
|
+
if (this.limit === 0) {
|
|
51
|
+
return (obj instanceof this.baseObject || obj.constructor.name === this.baseObject.name) ? obj : new this.baseObject(obj, extra);
|
|
52
|
+
}
|
|
53
|
+
if (obj.id == null) {
|
|
54
|
+
throw new Error("Missing object id");
|
|
55
|
+
}
|
|
56
|
+
const existing = this.get(obj.id);
|
|
57
|
+
if (existing && !replace) {
|
|
58
|
+
return existing;
|
|
59
|
+
}
|
|
60
|
+
if (!(obj instanceof this.baseObject || obj.constructor.name === this.baseObject.name)) {
|
|
61
|
+
obj = new this.baseObject(obj, extra);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
this.set(obj.id, obj);
|
|
65
|
+
|
|
66
|
+
if (this.limit && this.size > this.limit) {
|
|
67
|
+
const iter = this.keys();
|
|
68
|
+
while (this.size > this.limit) {
|
|
69
|
+
this.delete(iter.next().value);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
return obj;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Returns true if all elements satisfy the condition
|
|
77
|
+
* @arg {Function} func A function that takes an object and returns true or false
|
|
78
|
+
* @returns {Boolean} Whether or not all elements satisfied the condition
|
|
79
|
+
*/
|
|
80
|
+
every(func) {
|
|
81
|
+
for (const item of this.values()) {
|
|
82
|
+
if (!func(item)) {
|
|
83
|
+
return false;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
return true;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Return all the objects that make the function evaluate true
|
|
91
|
+
* @arg {Function} func A function that takes an object and returns true if it matches
|
|
92
|
+
* @returns {Array<Class>} An array containing all the objects that matched
|
|
93
|
+
*/
|
|
94
|
+
filter(func) {
|
|
95
|
+
const arr = [];
|
|
96
|
+
for (const item of this.values()) {
|
|
97
|
+
if (func(item)) {
|
|
98
|
+
arr.push(item);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
return arr;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* Return the first object to make the function evaluate true
|
|
106
|
+
* @arg {Function} func A function that takes an object and returns true if it matches
|
|
107
|
+
* @returns {Class?} The first matching object, or undefined if no match
|
|
108
|
+
*/
|
|
109
|
+
find(func) {
|
|
110
|
+
for (const item of this.values()) {
|
|
111
|
+
if (func(item)) {
|
|
112
|
+
return item;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
return undefined;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Return an array with the results of applying the given function to each element
|
|
120
|
+
* @arg {Function} func A function that takes an object and returns something
|
|
121
|
+
* @returns {Array} An array containing the results
|
|
122
|
+
*/
|
|
123
|
+
map(func) {
|
|
124
|
+
const arr = [];
|
|
125
|
+
for (const item of this.values()) {
|
|
126
|
+
arr.push(func(item));
|
|
127
|
+
}
|
|
128
|
+
return arr;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* Get a random object from the Collection
|
|
133
|
+
* @returns {Class?} The random object, or undefined if there is no match
|
|
134
|
+
*/
|
|
135
|
+
random() {
|
|
136
|
+
const index = Math.floor(Math.random() * this.size);
|
|
137
|
+
const iter = this.values();
|
|
138
|
+
for (let i = 0; i < index; ++i) {
|
|
139
|
+
iter.next();
|
|
140
|
+
}
|
|
141
|
+
return iter.next().value;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* Returns a value resulting from applying a function to every element of the collection
|
|
146
|
+
* @arg {Function} func A function that takes the previous value and the next item and returns a new value
|
|
147
|
+
* @arg {any} [initialValue] The initial value passed to the function
|
|
148
|
+
* @returns {any} The final result
|
|
149
|
+
*/
|
|
150
|
+
reduce(func, initialValue) {
|
|
151
|
+
const iter = this.values();
|
|
152
|
+
let val;
|
|
153
|
+
let result = initialValue === undefined ? iter.next().value : initialValue;
|
|
154
|
+
while ((val = iter.next().value) !== undefined) {
|
|
155
|
+
result = func(result, val);
|
|
156
|
+
}
|
|
157
|
+
return result;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* Remove an object
|
|
162
|
+
* @arg {Object} obj The object
|
|
163
|
+
* @arg {String} obj.id The ID of the object
|
|
164
|
+
* @returns {Class?} The removed object, or null if nothing was removed
|
|
165
|
+
*/
|
|
166
|
+
remove(obj) {
|
|
167
|
+
const item = this.get(obj.id);
|
|
168
|
+
if (!item) {
|
|
169
|
+
return null;
|
|
170
|
+
}
|
|
171
|
+
this.delete(obj.id);
|
|
172
|
+
return item;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* Returns true if at least one element satisfies the condition
|
|
177
|
+
* @arg {Function} func A function that takes an object and returns true or false
|
|
178
|
+
* @returns {Boolean} Whether or not at least one element satisfied the condition
|
|
179
|
+
*/
|
|
180
|
+
some(func) {
|
|
181
|
+
for (const item of this.values()) {
|
|
182
|
+
if (func(item)) {
|
|
183
|
+
return true;
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
return false;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
toString() {
|
|
190
|
+
return `[Collection<${this.baseObject.name}>]`;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
toJSON() {
|
|
194
|
+
const json = {};
|
|
195
|
+
for (const item of this.values()) {
|
|
196
|
+
json[item.id] = item;
|
|
197
|
+
}
|
|
198
|
+
return json;
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
module.exports = Collection;
|