Skip to content

Commit

Permalink
Merge pull request #483 from uploadcare/fix/dont-use-private-class-fi…
Browse files Browse the repository at this point in the history
…elds

fix(upload-client/queue): don't use private class fields due to bad support of bundling tools
  • Loading branch information
nd0ut authored Oct 18, 2023
2 parents dcbfcda + 36a1d7e commit 945e470
Showing 1 changed file with 25 additions and 25 deletions.
50 changes: 25 additions & 25 deletions packages/upload-client/src/tools/Queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,37 @@ type Resolver = (value: unknown) => void
type Rejector = (error: unknown) => void

export class Queue {
#concurrency = 1
#pending: Task[] = []
#running = 0
#resolvers: WeakMap<Task, Resolver> = new WeakMap()
#rejectors: WeakMap<Task, Rejector> = new WeakMap()
private _concurrency = 1
private _pending: Task[] = []
private _running = 0
private _resolvers: Map<Task, Resolver> = new Map()
private _rejectors: Map<Task, Rejector> = new Map()

constructor(concurrency: number) {
this.#concurrency = concurrency
this._concurrency = concurrency
}

#run() {
const tasksLeft = this.#concurrency - this.#running
private _run() {
const tasksLeft = this._concurrency - this._running
for (let i = 0; i < tasksLeft; i++) {
const task = this.#pending.shift()
const task = this._pending.shift()
if (!task) {
return
}
const resolver = this.#resolvers.get(task)
const rejector = this.#rejectors.get(task)
const resolver = this._resolvers.get(task)
const rejector = this._rejectors.get(task)
if (!resolver || !rejector)
throw new Error(
'Unexpected behavior: resolver or rejector is undefined'
)
this.#running += 1
this._running += 1

task()
.finally(() => {
this.#resolvers.delete(task)
this.#rejectors.delete(task)
this.#running -= 1
this.#run()
this._resolvers.delete(task)
this._rejectors.delete(task)
this._running -= 1
this._run()
})
.then((value) => resolver(value))
.catch((error) => rejector(error))
Expand All @@ -42,28 +42,28 @@ export class Queue {

add<T>(task: Task<T>): Promise<T> {
return new Promise((resolve, reject) => {
this.#resolvers.set(task, resolve as Resolver)
this.#rejectors.set(task, reject as Rejector)
this._resolvers.set(task, resolve as Resolver)
this._rejectors.set(task, reject as Rejector)

this.#pending.push(task)
this.#run()
this._pending.push(task)
this._run()
}) as Promise<T>
}

get pending() {
return this.#pending.length
return this._pending.length
}

get running() {
return this.#running
return this._running
}

set concurrency(value: number) {
this.#concurrency = value
this.#run()
this._concurrency = value
this._run()
}

get concurrency() {
return this.#concurrency
return this._concurrency
}
}

0 comments on commit 945e470

Please sign in to comment.