From b90a32307906d7ffb1b94064fef2dcdc3e5ccb84 Mon Sep 17 00:00:00 2001 From: Nate-Wessel Date: Thu, 11 Jul 2024 20:12:53 +0000 Subject: [PATCH] implement queue for travel time promises --- frontend/package-lock.json | 32 ++++++++++++++++++++++++++++++++ frontend/package.json | 1 + frontend/src/spatialData.js | 7 +++++-- 3 files changed, 38 insertions(+), 2 deletions(-) diff --git a/frontend/package-lock.json b/frontend/package-lock.json index 3262e35..2c17e81 100644 --- a/frontend/package-lock.json +++ b/frontend/package-lock.json @@ -14,6 +14,7 @@ "@mui/material": "5.x", "express": "^4.18.2", "leaflet": "^1.9.4", + "p-queue": "^8.0.1", "react": "18.x", "react-calendar": "^4.6.1", "react-date-picker": "^8.4.0", @@ -4447,6 +4448,11 @@ "node": ">= 0.6" } }, + "node_modules/eventemitter3": { + "version": "5.0.1", + "resolved": "https://registry.npmjs.org/eventemitter3/-/eventemitter3-5.0.1.tgz", + "integrity": "sha512-GWkBvjiSZK87ELrYOSESUYeVIc9mvLLf/nXalMOS5dYrgZq9o5OVkbZAVM06CVxYsCwH9BDZFPlQTlPA1j4ahA==" + }, "node_modules/events": { "version": "3.3.0", "resolved": "https://registry.npmjs.org/events/-/events-3.3.0.tgz", @@ -6518,6 +6524,32 @@ "node": ">=6" } }, + "node_modules/p-queue": { + "version": "8.0.1", + "resolved": "https://registry.npmjs.org/p-queue/-/p-queue-8.0.1.tgz", + "integrity": "sha512-NXzu9aQJTAzbBqOt2hwsR63ea7yvxJc0PwN/zobNAudYfb1B7R08SzB4TsLeSbUCuG467NhnoT0oO6w1qRO+BA==", + "dependencies": { + "eventemitter3": "^5.0.1", + "p-timeout": "^6.1.2" + }, + "engines": { + "node": ">=18" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, + "node_modules/p-timeout": { + "version": "6.1.2", + "resolved": "https://registry.npmjs.org/p-timeout/-/p-timeout-6.1.2.tgz", + "integrity": "sha512-UbD77BuZ9Bc9aABo74gfXhNvzC9Tx7SxtHSh1fxvx3jTLLYvmVhiQZZrJzqqU0jKbN32kb5VOKiLEQI/3bIjgQ==", + "engines": { + "node": ">=14.16" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, "node_modules/p-try": { "version": "2.2.0", "resolved": "https://registry.npmjs.org/p-try/-/p-try-2.2.0.tgz", diff --git a/frontend/package.json b/frontend/package.json index c295608..79e3ae5 100644 --- a/frontend/package.json +++ b/frontend/package.json @@ -46,6 +46,7 @@ "@mui/material": "5.x", "express": "^4.18.2", "leaflet": "^1.9.4", + "p-queue": "^8.0.1", "react": "18.x", "react-calendar": "^4.6.1", "react-date-picker": "^8.4.0", diff --git a/frontend/src/spatialData.js b/frontend/src/spatialData.js index 6f45eec..b874547 100644 --- a/frontend/src/spatialData.js +++ b/frontend/src/spatialData.js @@ -5,6 +5,9 @@ import { Days } from './days.js' import { HolidayOption } from './holidayOption.js' import { TravelTimeQuery } from './travelTimeQuery.js' import { domain } from './domain.js' +import PQueue from 'p-queue' + +const queue = new PQueue({concurrency: 3}) // instantiated once, this is the data store for all spatial and temporal data export class SpatialData { @@ -116,10 +119,10 @@ export class SpatialData { return [...this.#queries.values()].sort((a,b)=> a.URI < b.URI ? -1 : 1) } fetchAllResults(){ - return Promise.all( + return queue.addAll( this.travelTimeQueries .filter( TTQ => ! TTQ.hasData ) - .map( TTQ => TTQ.fetchData() ) + .map( TTQ => () => TTQ.fetchData() ) ) } }