mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-17 20:13:30 +01:00
Merge branch 'master' of https://github.com/hydrabolt/discord.js
This commit is contained in:
@@ -3,12 +3,51 @@ const DMChannel = require('../structures/DMChannel');
|
|||||||
const GroupDMChannel = require('../structures/GroupDMChannel');
|
const GroupDMChannel = require('../structures/GroupDMChannel');
|
||||||
const Constants = require('../util/Constants');
|
const Constants = require('../util/Constants');
|
||||||
|
|
||||||
|
const kLru = Symbol('LRU');
|
||||||
|
const lruable = ['group', 'dm'];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Stores channels.
|
* Stores channels.
|
||||||
* @private
|
* @private
|
||||||
* @extends {DataStore}
|
* @extends {DataStore}
|
||||||
*/
|
*/
|
||||||
class ChannelStore extends DataStore {
|
class ChannelStore extends DataStore {
|
||||||
|
constructor(iterable, options = {}) {
|
||||||
|
super(iterable);
|
||||||
|
|
||||||
|
if (options.lru) {
|
||||||
|
const lru = this[kLru] = [];
|
||||||
|
lru.add = item => {
|
||||||
|
lru.remove(item);
|
||||||
|
lru.unshift(item);
|
||||||
|
while (lru.length > options.lru) this.remove(lru[lru.length - 1]);
|
||||||
|
};
|
||||||
|
lru.remove = item => {
|
||||||
|
const index = lru.indexOf(item);
|
||||||
|
if (index > -1) lru.splice(index, 1);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
get(key, peek = false) {
|
||||||
|
const item = super.get(key);
|
||||||
|
if (!item || !lruable.includes(item.type)) return item;
|
||||||
|
if (!peek && this[kLru]) this[kLru].add(key);
|
||||||
|
return item;
|
||||||
|
}
|
||||||
|
|
||||||
|
set(key, val) {
|
||||||
|
if (this[kLru] && lruable.includes(val.type)) this[kLru].add(key);
|
||||||
|
return super.set(key, val);
|
||||||
|
}
|
||||||
|
|
||||||
|
delete(key) {
|
||||||
|
const item = this.get(key, true);
|
||||||
|
if (!item) return false;
|
||||||
|
if (this[kLru] && lruable.includes(item.type)) this[kLru].remove(key);
|
||||||
|
return super.delete(key);
|
||||||
|
}
|
||||||
|
|
||||||
create(data, guild, cache = true) {
|
create(data, guild, cache = true) {
|
||||||
const existing = this.get(data.id);
|
const existing = this.get(data.id);
|
||||||
if (existing) return existing;
|
if (existing) return existing;
|
||||||
|
|||||||
Reference in New Issue
Block a user