From: Jérôme Benoit Date: Mon, 29 May 2023 15:13:26 +0000 (+0200) Subject: refactor: use mnemonist queue implementation instead of homebrew one X-Git-Tag: v1.2.15~6 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=5983297441ae1af1d5a6e9ecc38ba04f8777724b;p=e-mobility-charging-stations-simulator.git refactor: use mnemonist queue implementation instead of homebrew one Signed-off-by: Jérôme Benoit --- diff --git a/rollup.config.mjs b/rollup.config.mjs index be507238..2e1e811a 100644 --- a/rollup.config.mjs +++ b/rollup.config.mjs @@ -32,7 +32,7 @@ export default { 'http-status-codes', 'just-clone', 'just-merge', - 'mnemonist/lru-map-with-delete.js', + 'mnemonist', 'moment', 'mongodb', 'node:async_hooks', diff --git a/src/charging-station/SharedLRUCache.ts b/src/charging-station/SharedLRUCache.ts index ba0a47d5..f3d3274d 100644 --- a/src/charging-station/SharedLRUCache.ts +++ b/src/charging-station/SharedLRUCache.ts @@ -1,4 +1,4 @@ -import LRUCache from 'mnemonist/lru-map-with-delete.js'; +import { LRUCacheWithDelete as LRUCache } from 'mnemonist'; import { Bootstrap } from './Bootstrap'; import type { ChargingStationConfiguration, ChargingStationTemplate } from '../types'; diff --git a/src/utils/AsyncLock.ts b/src/utils/AsyncLock.ts index f00ee568..6b16b0f8 100644 --- a/src/utils/AsyncLock.ts +++ b/src/utils/AsyncLock.ts @@ -1,6 +1,6 @@ // Partial Copyright Jerome Benoit. 2021-2023. All Rights Reserved. -import { Queue } from './Queue'; +import { Queue } from 'mnemonist'; export enum AsyncLockType { configuration = 'configuration', diff --git a/src/utils/Queue.ts b/src/utils/Queue.ts deleted file mode 100644 index c13a7bda..00000000 --- a/src/utils/Queue.ts +++ /dev/null @@ -1,70 +0,0 @@ -// Copyright Jerome Benoit. 2021-2023. All Rights Reserved. - -/** - * Queue - * - * @typeParam T - Type of queue items. - */ -export class Queue { - private items: Record; - private head: number; - private tail: number; - - public constructor() { - this.items = {}; - this.head = 0; - this.tail = 0; - } - - /** - * Get the size of the queue. - * - * @returns The size of the queue. - * @readonly - */ - public get size(): number { - return this.tail - this.head; - } - - /** - * Enqueue an item. - * - * @param item - Item to enqueue. - * @returns The new size of the queue. - */ - public enqueue(item: T): number { - this.items[this.tail] = item; - this.tail++; - return this.size; - } - - /** - * Dequeue an item. - * - * @returns The dequeued item or `undefined` if the queue is empty. - */ - public dequeue(): T | undefined { - if (this.size <= 0) { - return undefined; - } - const item = this.items[this.head]; - // eslint-disable-next-line @typescript-eslint/no-dynamic-delete - delete this.items[this.head]; - this.head++; - if (this.head === this.tail) { - this.head = 0; - this.tail = 0; - } - return item; - } - - /** - * Peek at the first item. - */ - public peek(): T | undefined { - if (this.size <= 0) { - return undefined; - } - return this.items[this.head]; - } -} diff --git a/test/utils/Queue.test.ts b/test/utils/Queue.test.ts deleted file mode 100644 index babce1ed..00000000 --- a/test/utils/Queue.test.ts +++ /dev/null @@ -1,53 +0,0 @@ -/* eslint-disable @typescript-eslint/no-unsafe-member-access */ -import { expect } from 'expect'; - -import { Queue } from '../../src/utils/Queue'; - -describe('Queue test suite', () => { - it('Verify enqueue() behavior', () => { - const queue = new Queue(); - let rtSize = queue.enqueue(1); - expect(queue.size).toBe(1); - expect(rtSize).toBe(queue.size); - expect((queue as any).head).toBe(0); - expect((queue as any).tail).toBe(1); - expect((queue as any).items).toStrictEqual({ 0: 1 }); - rtSize = queue.enqueue(2); - expect(queue.size).toBe(2); - expect(rtSize).toBe(queue.size); - expect((queue as any).head).toBe(0); - expect((queue as any).tail).toBe(2); - expect((queue as any).items).toStrictEqual({ 0: 1, 1: 2 }); - rtSize = queue.enqueue(3); - expect(queue.size).toBe(3); - expect(rtSize).toBe(queue.size); - expect((queue as any).head).toBe(0); - expect((queue as any).tail).toBe(3); - expect((queue as any).items).toStrictEqual({ 0: 1, 1: 2, 2: 3 }); - }); - - it('Verify dequeue() behavior', () => { - const queue = new Queue(); - queue.enqueue(1); - queue.enqueue(2); - queue.enqueue(3); - let rtItem = queue.dequeue(); - expect(queue.size).toBe(2); - expect(rtItem).toBe(1); - expect((queue as any).head).toBe(1); - expect((queue as any).tail).toBe(3); - expect((queue as any).items).toStrictEqual({ 1: 2, 2: 3 }); - rtItem = queue.dequeue(); - expect(queue.size).toBe(1); - expect(rtItem).toBe(2); - expect((queue as any).head).toBe(2); - expect((queue as any).tail).toBe(3); - expect((queue as any).items).toStrictEqual({ 2: 3 }); - rtItem = queue.dequeue(); - expect(queue.size).toBe(0); - expect(rtItem).toBe(3); - expect((queue as any).head).toBe(0); - expect((queue as any).tail).toBe(0); - expect((queue as any).items).toStrictEqual({}); - }); -});