pi-agy-acp 0.1.2 → 0.1.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/util.ts CHANGED
@@ -90,8 +90,8 @@ export class AsyncQueue<T> {
90
90
  * @returns whether an item is available.
91
91
  */
92
92
  async wait(signal?: AbortSignal, timeoutMs?: number): Promise<boolean> {
93
- if (this.items.length > 0) return true
94
93
  if (signal?.aborted === true) return false
94
+ if (this.items.length > 0) return true
95
95
  return new Promise<boolean>((resolve) => {
96
96
  let timer: ReturnType<typeof setTimeout> | undefined
97
97
  const finish = (value: boolean): void => {
@@ -117,9 +117,10 @@ export class KeyedMutex {
117
117
  /**
118
118
  * Acquire the lock for one key.
119
119
  * @param key - lock key.
120
+ * @param signal - cancels waiting without bypassing earlier lock holders.
120
121
  * @returns the release function.
121
122
  */
122
- async acquire(key: string): Promise<() => void> {
123
+ async acquire(key: string, signal?: AbortSignal): Promise<() => void> {
123
124
  const previous = this.tails.get(key) ?? Promise.resolve()
124
125
  let release!: () => void
125
126
  const current = new Promise<void>((resolve) => {
@@ -127,10 +128,16 @@ export class KeyedMutex {
127
128
  })
128
129
  const tail = previous.then(() => current)
129
130
  this.tails.set(key, tail)
130
- await previous
131
- return () => {
132
- release()
131
+ void tail.then(() => {
133
132
  if (this.tails.get(key) === tail) this.tails.delete(key)
133
+ })
134
+ try {
135
+ await abortable(previous, signal)
136
+ signal?.throwIfAborted()
137
+ return release
138
+ } catch (error) {
139
+ release()
140
+ throw error
134
141
  }
135
142
  }
136
143
  }