socket-function 0.98.0 → 0.99.0

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "socket-function",
3
- "version": "0.98.0",
3
+ "version": "0.99.0",
4
4
  "main": "index.js",
5
5
  "license": "MIT",
6
6
  "dependencies": {
@@ -411,7 +411,7 @@ declare global {
411
411
  }
412
412
 
413
413
  let baseController = new RequireControllerBase();
414
- /** Sets the root directory that imports should be resolved from. Defaults to "." */
414
+ /** @deprecated, not needed, as this defaults to ".", which is a lot easier to reason about anyways. */
415
415
  export function setRequireBootRequire(dir: string) {
416
416
  dir = path.resolve(dir);
417
417
  dir = dir.replaceAll("\\", "/");
package/src/misc.ts CHANGED
@@ -406,4 +406,42 @@ export function removeFromSortedList<T>(list: T[], map: (val: T) => string | num
406
406
  let index = binarySearchIndex(list.length, i => compare(map(list[i]), searchValue));
407
407
  if (index < 0) return;
408
408
  list.splice(index, 1);
409
- }
409
+ }
410
+
411
+
412
+
413
+ export function timeoutToError<T>(time: number, p: Promise<T>, err: () => Error) {
414
+ return new Promise<T>((resolve, reject) => {
415
+ let timeout = setTimeout(() => reject(err()), time);
416
+ p.then(resolve, reject).finally(() => clearTimeout(timeout));
417
+ });
418
+ }
419
+
420
+ // NOTE: Both errors and timeouts are converted to undefined
421
+ export function timeoutToUndefined<T>(time: number, p: Promise<T>) {
422
+ return new Promise<T | undefined>((resolve, reject) => {
423
+ let timeout = setTimeout(() => {
424
+ console.error(`timeoutToUndefined timed out after ${time}`);
425
+ resolve(undefined);
426
+ }, time);
427
+ p.then(resolve,
428
+ (err) => {
429
+ console.error(`timeoutToUndefined error: ${err.stack}`);
430
+ resolve(undefined);
431
+ }
432
+ ).finally(() => clearTimeout(timeout));
433
+ });
434
+ }
435
+ export function timeoutToUndefinedSilent<T>(time: number, p: Promise<T>) {
436
+ return new Promise<T | undefined>((resolve, reject) => {
437
+ let timeout = setTimeout(() => {
438
+ resolve(undefined);
439
+ }, time);
440
+ p.then(
441
+ resolve,
442
+ (err) => {
443
+ resolve(undefined);
444
+ }
445
+ ).finally(() => clearTimeout(timeout));
446
+ });
447
+ }