@xylabs/timer 5.0.82 → 5.0.84

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/README.md CHANGED
@@ -38,12 +38,16 @@ Base functionality used throughout XY Labs TypeScript/JavaScript libraries
38
38
  function clearTimeoutEx(id): void;
39
39
  ```
40
40
 
41
+ Cancels a timeout previously created with setTimeoutEx.
42
+
41
43
  ## Parameters
42
44
 
43
45
  ### id
44
46
 
45
47
  `string`
46
48
 
49
+ The timeout ID returned by setTimeoutEx.
50
+
47
51
  ## Returns
48
52
 
49
53
  `void`
@@ -58,20 +62,28 @@ function clearTimeoutEx(id): void;
58
62
  function setTimeoutEx(func, delay): string;
59
63
  ```
60
64
 
65
+ Sets a timeout using an optimized internal timer that coalesces multiple timeouts into a single native timer.
66
+
61
67
  ## Parameters
62
68
 
63
69
  ### func
64
70
 
65
71
  `Function`
66
72
 
73
+ The function to call after the delay.
74
+
67
75
  ### delay
68
76
 
69
77
  `number`
70
78
 
79
+ The delay in milliseconds (must be >= 0).
80
+
71
81
  ## Returns
72
82
 
73
83
  `string`
74
84
 
85
+ A unique string ID that can be used with clearTimeoutEx to cancel the timeout.
86
+
75
87
 
76
88
  Part of [sdk-js](https://www.npmjs.com/package/@xyo-network/sdk-js)
77
89
 
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/setTimeoutEx.ts"],"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// 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\nexport const clearTimeoutEx = (id: string) => {\n timeouts = timeouts.filter(timeout => timeout.id !== id)\n update(timeouts)\n}\n"],"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;AAGO,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;AAEO,IAAM,iBAAiB,CAAC,OAAe;AAC5C,aAAW,SAAS,OAAO,aAAW,QAAQ,OAAO,EAAE;AACvD,SAAO,QAAQ;AACjB;","names":[]}
1
+ {"version":3,"sources":["../../src/setTimeoutEx.ts"],"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"],"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;","names":[]}
@@ -1,3 +1,13 @@
1
+ /**
2
+ * Sets a timeout using an optimized internal timer that coalesces multiple timeouts into a single native timer.
3
+ * @param func - The function to call after the delay.
4
+ * @param delay - The delay in milliseconds (must be >= 0).
5
+ * @returns A unique string ID that can be used with clearTimeoutEx to cancel the timeout.
6
+ */
1
7
  export declare const setTimeoutEx: (func: Function, delay: number) => string;
8
+ /**
9
+ * Cancels a timeout previously created with setTimeoutEx.
10
+ * @param id - The timeout ID returned by setTimeoutEx.
11
+ */
2
12
  export declare const clearTimeoutEx: (id: string) => void;
3
13
  //# sourceMappingURL=setTimeoutEx.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"setTimeoutEx.d.ts","sourceRoot":"","sources":["../../src/setTimeoutEx.ts"],"names":[],"mappings":"AAwDA,eAAO,MAAM,YAAY,GAAI,MAAM,QAAQ,EAAE,OAAO,MAAM,WAQzD,CAAA;AAED,eAAO,MAAM,cAAc,GAAI,IAAI,MAAM,SAGxC,CAAA"}
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"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xylabs/timer",
3
- "version": "5.0.82",
3
+ "version": "5.0.84",
4
4
  "description": "Base functionality used throughout XY Labs TypeScript/JavaScript libraries",
5
5
  "keywords": [
6
6
  "xylabs",
@@ -43,11 +43,11 @@
43
43
  "!**/*.test.*"
44
44
  ],
45
45
  "dependencies": {
46
- "@xylabs/assert": "~5.0.82"
46
+ "@xylabs/assert": "~5.0.84"
47
47
  },
48
48
  "devDependencies": {
49
- "@xylabs/ts-scripts-yarn3": "~7.4.11",
50
- "@xylabs/tsconfig": "~7.4.11",
49
+ "@xylabs/ts-scripts-yarn3": "~7.4.13",
50
+ "@xylabs/tsconfig": "~7.4.13",
51
51
  "typescript": "~5.9.3",
52
52
  "vitest": "^4.0.18"
53
53
  },