Compare commits
3 Commits
5ccbb2ef1c
...
3c6d69c343
Author | SHA1 | Date | |
---|---|---|---|
3c6d69c343 | |||
1ff71d099e | |||
ae9016fbf6 |
@ -34,7 +34,15 @@ function initialize() {
|
||||
}
|
||||
initialize();
|
||||
|
||||
function withTimeout<T>(promise: Promise<T>, ms: number, cb: () => Error) {
|
||||
/**
|
||||
* Awaits a promise with a timeout.
|
||||
*
|
||||
* @param promise the promise to await
|
||||
* @param ms the timeout in milliseconds
|
||||
* @param cb a callback to call when the timeout is reached. The promise is
|
||||
* rejected with whatever gets returned here.
|
||||
*/
|
||||
function withTimeout<T>(promise: Promise<T>, ms: number, cb: () => any) {
|
||||
let timeout: NodeJS.Timeout;
|
||||
return new Promise<T>((resolve, reject) => {
|
||||
timeout = setTimeout(() => {
|
||||
@ -49,13 +57,12 @@ export async function generate(
|
||||
regionHeight: number,
|
||||
clues: number
|
||||
): Promise<Sudoku> {
|
||||
const proxy = available.shift();
|
||||
const proxy = available.pop();
|
||||
if (!proxy) {
|
||||
throw new Error("No workers available right now. Please try again.");
|
||||
}
|
||||
|
||||
try {
|
||||
const puzzle: number[] = await withTimeout(
|
||||
const puzzle = await withTimeout<number[]>(
|
||||
proxy.generate(regionWidth, regionHeight, clues),
|
||||
TIMEOUT,
|
||||
() => {
|
||||
@ -65,7 +72,7 @@ export async function generate(
|
||||
}
|
||||
);
|
||||
|
||||
available.unshift(proxy);
|
||||
available.push(proxy);
|
||||
prettyPrint(regionWidth, regionHeight, puzzle);
|
||||
return {
|
||||
regionWidth,
|
||||
@ -73,7 +80,4 @@ export async function generate(
|
||||
size: (regionWidth * regionHeight) ** 2,
|
||||
cells: puzzle,
|
||||
};
|
||||
} catch (err) {
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user