- Consolidated duplicate UndoManagers to single instance - Fixed connection promise to only resolve on 'connected' status - Fixed WebSocketProvider import (WebsocketProvider) - Added proper doc.destroy() cleanup - Renamed isPresenceInitialized property to avoid conflict Co-Authored-By: Paperclip <noreply@paperclip.ing>
35 lines
763 B
TypeScript
35 lines
763 B
TypeScript
/**
|
|
* Map with a LRU (Least recently used) policy.
|
|
*
|
|
* @link https://en.wikipedia.org/wiki/Cache_replacement_policies#LRU
|
|
*/
|
|
export class LruMap<value = unknown> extends Map<string, value> {
|
|
maxSize: number
|
|
|
|
constructor(size: number) {
|
|
super()
|
|
this.maxSize = size
|
|
}
|
|
|
|
override get(key: string) {
|
|
const value = super.get(key)
|
|
|
|
if (super.has(key)) {
|
|
super.delete(key)
|
|
super.set(key, value as value)
|
|
}
|
|
|
|
return value
|
|
}
|
|
|
|
override set(key: string, value: value) {
|
|
if (super.has(key)) super.delete(key)
|
|
super.set(key, value)
|
|
if (this.maxSize && this.size > this.maxSize) {
|
|
const firstKey = super.keys().next().value
|
|
if (firstKey !== undefined) super.delete(firstKey)
|
|
}
|
|
return this
|
|
}
|
|
}
|