forked from discordjs/discord.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LimitedCollection.js
33 lines (28 loc) · 873 Bytes
/
LimitedCollection.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
'use strict';
const Collection = require('./Collection.js');
/**
* A Collection which holds a max amount of entries. The first key is deleted if the Collection has
* reached max size.
* @extends {Collection}
* @param {number} [maxSize=0] The maximum size of the Collection
* @param {Iterable} [iterable=null] Optional entries passed to the Map constructor.
*/
class LimitedCollection extends Collection {
constructor(maxSize = 0, iterable = null) {
super(iterable);
/**
* The max size of the Collection.
* @type {number}
*/
this.maxSize = maxSize;
}
set(key, value) {
if (this.maxSize === 0) return this;
if (this.size >= this.maxSize && !this.has(key)) this.delete(this.firstKey());
return super.set(key, value);
}
static get [Symbol.species]() {
return Collection;
}
}
module.exports = LimitedCollection;