init project
Some checks failed
No response / noResponse (push) Has been cancelled
CI / Continuous releases (push) Has been cancelled
CI / test-dev (macos-latest) (push) Has been cancelled
CI / test-dev (ubuntu-latest) (push) Has been cancelled
CI / test-dev (windows-latest) (push) Has been cancelled
Maintenance / main (push) Has been cancelled
Scorecards supply-chain security / Scorecards analysis (push) Has been cancelled
CodeQL / Analyze (push) Has been cancelled

This commit is contained in:
how2ice
2025-12-12 14:26:25 +09:00
commit 005cf56baf
43188 changed files with 1079531 additions and 0 deletions

View File

@@ -0,0 +1,58 @@
import waitUntil from './waitUntil.mjs';
class Queue {
pendingEntries = [];
inFlight = 0;
err = null;
constructor(worker, options = {}) {
this.worker = worker;
this.concurrency = options.concurrency || 1;
}
push = (entries) => {
this.pendingEntries = this.pendingEntries.concat(entries);
this.process();
};
process = () => {
const scheduled = this.pendingEntries.splice(0, this.concurrency - this.inFlight);
this.inFlight += scheduled.length;
scheduled.forEach(async (task) => {
try {
await this.worker(task);
} catch (err) {
this.err = err;
} finally {
this.inFlight -= 1;
}
if (this.pendingEntries.length > 0) {
this.process();
}
});
};
wait = (options = {}) =>
waitUntil(
() => {
if (this.err) {
this.pendingEntries = [];
throw this.err;
}
return {
predicate: options.empty
? this.inFlight === 0 && this.pendingEntries.length === 0
: this.concurrency > this.pendingEntries.length,
};
},
{
delay: 50,
},
);
}
export default Queue;

View File

@@ -0,0 +1,3 @@
# Waterfall
A set of utility functions for handling async/await at scale.

View File

@@ -0,0 +1,4 @@
export { default as Queue } from './Queue.mjs';
export { default as retry } from './retry.mjs';
export { default as sleep } from './sleep.mjs';
export { default as waitUntil } from './waitUntil.mjs';

View File

@@ -0,0 +1,7 @@
{
"name": "@mui/internal-waterfall",
"version": "1.0.0",
"private": "true",
"type": "module",
"main": "index.mjs"
}

View File

@@ -0,0 +1,34 @@
// Inspired by https://github.com/zeit/async-retry
// Without the retry dependency (1 kB gzipped +)
async function retry(tryFunction, options = {}) {
const { retries = 3 } = options;
let tries = 0;
let output = null;
let exitErr = null;
const bail = (err) => {
exitErr = err;
};
while (tries < retries) {
tries += 1;
try {
// eslint-disable-next-line no-await-in-loop
output = await tryFunction({ tries, bail });
break;
} catch (err) {
if (tries >= retries) {
throw err;
}
}
}
if (exitErr) {
throw exitErr;
}
return output;
}
export default retry;

View File

@@ -0,0 +1,9 @@
function sleep(duration) {
return new Promise((resolve) => {
setTimeout(() => {
resolve();
}, duration);
});
}
export default sleep;

View File

@@ -0,0 +1,17 @@
import sleep from './sleep.mjs';
export default async function waitUntil(test, options = {}) {
const { delay = 5e3, tries = -1 } = options;
const { predicate, result } = await test();
if (predicate) {
return result;
}
if (tries - 1 === 0) {
throw new Error('tries limit reached');
}
await sleep(delay);
return waitUntil(test, { ...options, tries: tries > 0 ? tries - 1 : tries });
}