Skip to content

Commit

Permalink
use plimit to allow more concurrent jobs (#126)
Browse files Browse the repository at this point in the history
  • Loading branch information
rorybainfreetrade authored Jan 7, 2022
1 parent c2d2d4f commit 720de47
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ts-firebase-driver-testing",
"version": "1.0.10",
"version": "1.0.11",
"description": "Swap out Firebase as a driver for in-process testing",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand All @@ -20,7 +20,8 @@
"lodash": ">=4",
"object-path": "^0",
"object.entries": "^1",
"object.values": "^1"
"object.values": "^1",
"p-limit": "^3.1.0"
},
"devDependencies": {
"@types/flat": "^5.0.0",
Expand Down
10 changes: 8 additions & 2 deletions src/driver/AsyncJobs.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import _ from "lodash"
import pLimit from "p-limit"
import { sleep } from "../util/sleep"
import { IDatabaseChangePerformanceStats } from "./ChangeObserver/DatabaseChangeObserver"

export interface IAsyncJobs {
shouldDebugJobsCompletePerformance: boolean
pushJob(job: Promise<any>): void

pushJobs(jobs: Array<Promise<any>>): void

jobsComplete(): Promise<void>
}

Expand All @@ -31,9 +34,12 @@ export class AsyncJobs implements IAsyncJobs {
while (this.jobs.length > 0) {
this.randomiseJobsOrder()

const itemsToResolve = this.jobs.slice(0, this.maxConcurrentJobs)
const itemsToResolve = [...this.jobs]

const results = await Promise.all(itemsToResolve)
const limit = pLimit(this.maxConcurrentJobs)
const results = await Promise.all(
itemsToResolve.map((item) => limit(() => item)),
)
changePerformanceStats.push(...results)

// Remove the items we just processed from the front of the job queue
Expand Down

0 comments on commit 720de47

Please sign in to comment.