@xylabs/timer 6.0.2 → 6.0.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.
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../../src/setTimeoutEx.ts"],
4
- "sourcesContent": ["import { assertEx } from '@xylabs/assert'\n\ninterface TimeoutInfo {\n delay: number\n // eslint-disable-next-line @typescript-eslint/no-unsafe-function-type\n func: Function\n id: string\n}\n\nlet timeouts: TimeoutInfo[] = []\nlet currentTimeout: NodeJS.Timeout | undefined\nlet interval = -1\n\nconst reset = () => {\n interval = -1\n clearTimeout(currentTimeout)\n currentTimeout = undefined\n timeouts = []\n}\n\nconst update = (newTimeouts = timeouts, delayPassed = 0) => {\n // if no more timeouts, set back to initial state\n if (newTimeouts.length <= 0) {\n reset()\n } else {\n const newInterval = Math.min(...newTimeouts.map(timeout => timeout.delay))\n\n if (newInterval === interval && currentTimeout !== undefined) {\n // since nothing changed, just return\n return\n } else {\n clearTimeout(currentTimeout)\n timeouts = newTimeouts.map(timeout => ({\n delay: timeout.delay - delayPassed, func: timeout.func, id: timeout.id,\n }))\n // restart timeout since it needs to be different\n interval = newInterval\n currentTimeout = setTimeout(timerFunc, interval)\n }\n }\n}\n\nconst timerFunc = () => {\n const notFiring = timeouts.filter(timeout => timeout.delay > interval)\n const firing = timeouts.filter(timeout => timeout.delay <= interval)\n\n // call this after getting notFiring and firing since set will change in this call\n update(notFiring, interval)\n\n // trigger the ones that need to be triggered\n for (const timeout of firing) {\n timeout.func()\n }\n}\n\n/**\n * Sets a timeout using an optimized internal timer that coalesces multiple timeouts into a single native timer.\n * @param func - The function to call after the delay.\n * @param delay - The delay in milliseconds (must be >= 0).\n * @returns A unique string ID that can be used with clearTimeoutEx to cancel the timeout.\n */\n// eslint-disable-next-line @typescript-eslint/no-unsafe-function-type\nexport const setTimeoutEx = (func: Function, delay: number) => {\n assertEx(delay >= 0, () => 'delay must be >= 0')\n const id = `${Date.now()}|${Math.random() * 9_999_999_999}`\n timeouts.push({\n delay, func, id,\n })\n update()\n return id\n}\n\n/**\n * Cancels a timeout previously created with setTimeoutEx.\n * @param id - The timeout ID returned by setTimeoutEx.\n */\nexport const clearTimeoutEx = (id: string) => {\n timeouts = timeouts.filter(timeout => timeout.id !== id)\n update(timeouts)\n}\n"],
5
- "mappings": ";AAAA,SAAS,gBAAgB;AASzB,IAAI,WAA0B,CAAC;AAC/B,IAAI;AACJ,IAAI,WAAW;AAEf,IAAM,QAAQ,MAAM;AAClB,aAAW;AACX,eAAa,cAAc;AAC3B,mBAAiB;AACjB,aAAW,CAAC;AACd;AAEA,IAAM,SAAS,CAAC,cAAc,UAAU,cAAc,MAAM;AAE1D,MAAI,YAAY,UAAU,GAAG;AAC3B,UAAM;AAAA,EACR,OAAO;AACL,UAAM,cAAc,KAAK,IAAI,GAAG,YAAY,IAAI,aAAW,QAAQ,KAAK,CAAC;AAEzE,QAAI,gBAAgB,YAAY,mBAAmB,QAAW;AAE5D;AAAA,IACF,OAAO;AACL,mBAAa,cAAc;AAC3B,iBAAW,YAAY,IAAI,cAAY;AAAA,QACrC,OAAO,QAAQ,QAAQ;AAAA,QAAa,MAAM,QAAQ;AAAA,QAAM,IAAI,QAAQ;AAAA,MACtE,EAAE;AAEF,iBAAW;AACX,uBAAiB,WAAW,WAAW,QAAQ;AAAA,IACjD;AAAA,EACF;AACF;AAEA,IAAM,YAAY,MAAM;AACtB,QAAM,YAAY,SAAS,OAAO,aAAW,QAAQ,QAAQ,QAAQ;AACrE,QAAM,SAAS,SAAS,OAAO,aAAW,QAAQ,SAAS,QAAQ;AAGnE,SAAO,WAAW,QAAQ;AAG1B,aAAW,WAAW,QAAQ;AAC5B,YAAQ,KAAK;AAAA,EACf;AACF;AASO,IAAM,eAAe,CAAC,MAAgB,UAAkB;AAC7D,WAAS,SAAS,GAAG,MAAM,oBAAoB;AAC/C,QAAM,KAAK,GAAG,KAAK,IAAI,CAAC,IAAI,KAAK,OAAO,IAAI,UAAa;AACzD,WAAS,KAAK;AAAA,IACZ;AAAA,IAAO;AAAA,IAAM;AAAA,EACf,CAAC;AACD,SAAO;AACP,SAAO;AACT;AAMO,IAAM,iBAAiB,CAAC,OAAe;AAC5C,aAAW,SAAS,OAAO,aAAW,QAAQ,OAAO,EAAE;AACvD,SAAO,QAAQ;AACjB;",
4
+ "sourcesContent": ["import { assertEx } from '@xylabs/assert'\n\ninterface TimeoutInfo {\n delay: number\n func: () => void\n id: string\n}\n\nlet timeouts: TimeoutInfo[] = []\nlet currentTimeout: NodeJS.Timeout | undefined\nlet interval = -1\n\nconst reset = () => {\n interval = -1\n clearTimeout(currentTimeout)\n currentTimeout = undefined\n timeouts = []\n}\n\nconst update = (newTimeouts = timeouts, delayPassed = 0) => {\n // if no more timeouts, set back to initial state\n if (newTimeouts.length <= 0) {\n reset()\n } else {\n const newInterval = Math.min(...newTimeouts.map(timeout => timeout.delay))\n\n if (newInterval === interval && currentTimeout !== undefined) {\n // since nothing changed, just return\n return\n } else {\n clearTimeout(currentTimeout)\n timeouts = newTimeouts.map(timeout => ({\n delay: timeout.delay - delayPassed, func: timeout.func, id: timeout.id,\n }))\n // restart timeout since it needs to be different\n interval = newInterval\n currentTimeout = setTimeout(timerFunc, interval)\n }\n }\n}\n\nconst timerFunc = () => {\n const notFiring = timeouts.filter(timeout => timeout.delay > interval)\n const firing = timeouts.filter(timeout => timeout.delay <= interval)\n\n // call this after getting notFiring and firing since set will change in this call\n update(notFiring, interval)\n\n // trigger the ones that need to be triggered\n for (const timeout of firing) {\n timeout.func()\n }\n}\n\n/**\n * Sets a timeout using an optimized internal timer that coalesces multiple timeouts into a single native timer.\n * @param func - The function to call after the delay.\n * @param delay - The delay in milliseconds (must be >= 0).\n * @returns A unique string ID that can be used with clearTimeoutEx to cancel the timeout.\n */\nexport const setTimeoutEx = (func: () => void, delay: number) => {\n assertEx(delay >= 0, () => 'delay must be >= 0')\n const id = `${Date.now()}|${Math.random() * 9_999_999_999}`\n timeouts.push({\n delay, func, id,\n })\n update()\n return id\n}\n\n/**\n * Cancels a timeout previously created with setTimeoutEx.\n * @param id - The timeout ID returned by setTimeoutEx.\n */\nexport const clearTimeoutEx = (id: string) => {\n timeouts = timeouts.filter(timeout => timeout.id !== id)\n update(timeouts)\n}\n"],
5
+ "mappings": ";AAAA,SAAS,gBAAgB;AAQzB,IAAI,WAA0B,CAAC;AAC/B,IAAI;AACJ,IAAI,WAAW;AAEf,IAAM,QAAQ,MAAM;AAClB,aAAW;AACX,eAAa,cAAc;AAC3B,mBAAiB;AACjB,aAAW,CAAC;AACd;AAEA,IAAM,SAAS,CAAC,cAAc,UAAU,cAAc,MAAM;AAE1D,MAAI,YAAY,UAAU,GAAG;AAC3B,UAAM;AAAA,EACR,OAAO;AACL,UAAM,cAAc,KAAK,IAAI,GAAG,YAAY,IAAI,aAAW,QAAQ,KAAK,CAAC;AAEzE,QAAI,gBAAgB,YAAY,mBAAmB,QAAW;AAE5D;AAAA,IACF,OAAO;AACL,mBAAa,cAAc;AAC3B,iBAAW,YAAY,IAAI,cAAY;AAAA,QACrC,OAAO,QAAQ,QAAQ;AAAA,QAAa,MAAM,QAAQ;AAAA,QAAM,IAAI,QAAQ;AAAA,MACtE,EAAE;AAEF,iBAAW;AACX,uBAAiB,WAAW,WAAW,QAAQ;AAAA,IACjD;AAAA,EACF;AACF;AAEA,IAAM,YAAY,MAAM;AACtB,QAAM,YAAY,SAAS,OAAO,aAAW,QAAQ,QAAQ,QAAQ;AACrE,QAAM,SAAS,SAAS,OAAO,aAAW,QAAQ,SAAS,QAAQ;AAGnE,SAAO,WAAW,QAAQ;AAG1B,aAAW,WAAW,QAAQ;AAC5B,YAAQ,KAAK;AAAA,EACf;AACF;AAQO,IAAM,eAAe,CAAC,MAAkB,UAAkB;AAC/D,WAAS,SAAS,GAAG,MAAM,oBAAoB;AAC/C,QAAM,KAAK,GAAG,KAAK,IAAI,CAAC,IAAI,KAAK,OAAO,IAAI,UAAa;AACzD,WAAS,KAAK;AAAA,IACZ;AAAA,IAAO;AAAA,IAAM;AAAA,EACf,CAAC;AACD,SAAO;AACP,SAAO;AACT;AAMO,IAAM,iBAAiB,CAAC,OAAe;AAC5C,aAAW,SAAS,OAAO,aAAW,QAAQ,OAAO,EAAE;AACvD,SAAO,QAAQ;AACjB;",
6
6
  "names": []
7
7
  }
@@ -4,7 +4,7 @@
4
4
  * @param delay - The delay in milliseconds (must be >= 0).
5
5
  * @returns A unique string ID that can be used with clearTimeoutEx to cancel the timeout.
6
6
  */
7
- export declare const setTimeoutEx: (func: Function, delay: number) => string;
7
+ export declare const setTimeoutEx: (func: () => void, delay: number) => string;
8
8
  /**
9
9
  * Cancels a timeout previously created with setTimeoutEx.
10
10
  * @param id - The timeout ID returned by setTimeoutEx.
@@ -1 +1 @@
1
- {"version":3,"file":"setTimeoutEx.d.ts","sourceRoot":"","sources":["../../src/setTimeoutEx.ts"],"names":[],"mappings":"AAuDA;;;;;GAKG;AAEH,eAAO,MAAM,YAAY,GAAI,MAAM,QAAQ,EAAE,OAAO,MAAM,WAQzD,CAAA;AAED;;;GAGG;AACH,eAAO,MAAM,cAAc,GAAI,IAAI,MAAM,SAGxC,CAAA"}
1
+ {"version":3,"file":"setTimeoutEx.d.ts","sourceRoot":"","sources":["../../src/setTimeoutEx.ts"],"names":[],"mappings":"AAsDA;;;;;GAKG;AACH,eAAO,MAAM,YAAY,GAAI,MAAM,MAAM,IAAI,EAAE,OAAO,MAAM,WAQ3D,CAAA;AAED;;;GAGG;AACH,eAAO,MAAM,cAAc,GAAI,IAAI,MAAM,SAGxC,CAAA"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xylabs/timer",
3
- "version": "6.0.2",
3
+ "version": "6.0.4",
4
4
  "description": "Base functionality used throughout XY Labs TypeScript/JavaScript libraries",
5
5
  "keywords": [
6
6
  "xylabs",
@@ -42,15 +42,15 @@
42
42
  "README.md"
43
43
  ],
44
44
  "dependencies": {
45
- "@xylabs/assert": "~6.0.2"
45
+ "@xylabs/assert": "~6.0.4"
46
46
  },
47
47
  "devDependencies": {
48
48
  "@types/node": "^25.9.1",
49
- "@xylabs/toolchain": "^8.1.1",
50
- "@xylabs/tsconfig": "^8.1.1",
49
+ "@xylabs/toolchain": "^8.1.5",
50
+ "@xylabs/tsconfig": "^8.1.5",
51
51
  "eslint": "^10.4.0",
52
52
  "typescript": "^6.0.3",
53
- "vite": "^8.0.13",
53
+ "vite": "^8.0.14",
54
54
  "vitest": "^4.1.7"
55
55
  },
56
56
  "engines": {