(concurrency: number)
| 1148 | * A lightweight Promise concurrency limiter. |
| 1149 | */ |
| 1150 | export function pLimit(concurrency: number): Limit { |
| 1151 | if (!Number.isInteger(concurrency) || concurrency <= 0) { |
| 1152 | throw new FirebaseError(`pLimit concurrency must be a positive integer, got ${concurrency}`); |
| 1153 | } |
| 1154 | |
| 1155 | const queue: Array<() => void> = []; |
| 1156 | let activeCount = 0; |
| 1157 | |
| 1158 | const next = () => { |
| 1159 | activeCount--; |
| 1160 | if (queue.length > 0) { |
| 1161 | queue.shift()?.(); |
| 1162 | } |
| 1163 | }; |
| 1164 | |
| 1165 | return <T>(fn: () => Promise<T>): Promise<T> => { |
| 1166 | return new Promise<T>((resolve, reject) => { |
| 1167 | const run = () => { |
| 1168 | activeCount++; |
| 1169 | try { |
| 1170 | Promise.resolve(fn()).then(resolve, reject).finally(next); |
| 1171 | } catch (err) { |
| 1172 | reject(err); |
| 1173 | next(); |
| 1174 | } |
| 1175 | }; |
| 1176 | |
| 1177 | if (activeCount < concurrency) { |
| 1178 | run(); |
| 1179 | } else { |
| 1180 | queue.push(run); |
| 1181 | } |
| 1182 | }); |
| 1183 | }; |
| 1184 | } |
| 1185 | |
| 1186 | /** |
| 1187 | * Calculates the Levenshtein distance between two strings. |
no test coverage detected
searching dependent graphs…