UNPKG

@ceramicnetwork/core

Version:

Typescript implementation of the Ceramic protocol

51 lines 1.51 kB
import { PriorityQueue } from '@datastructures-js/priority-queue'; export class Query { constructor(timestamp, streamID, queryID) { this.timestamp = timestamp; this.streamID = streamID; this.queryID = queryID; } } const compareQueryTimestamps = (a, b) => { if (a.timestamp > b.timestamp) { return 1; } if (a.timestamp < b.timestamp) { return -1; } return -1; }; export class OutstandingQueries { constructor() { this.queryQueue = new PriorityQueue(compareQueryTimestamps); this.queryMap = new Map(); this._minutesThreshold = 1; } add(id, query) { this._cleanUpExpiredQueries(); if (this.queryMap.get(id) == undefined) { this.queryMap.set(id, query); this.queryQueue.enqueue(query); } else { this.remove(query); this.queryMap.set(id, query); this.queryQueue.enqueue(query); } } remove(topQuery) { this.queryMap.delete(topQuery.queryID); this.queryQueue.dequeue(); } _isExpired(query) { const diffMs = Date.now() - query?.timestamp; const differenceInMinutes = Math.floor(diffMs / 1000 / 60); return differenceInMinutes > this._minutesThreshold; } _cleanUpExpiredQueries() { while (this._isExpired(this.queryQueue.front())) { this.remove(this.queryQueue.front()); } } } //# sourceMappingURL=outstanding-queries.js.map