@hyperfrontend/time-utils 0.0.3 → 0.0.5
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/CHANGELOG.md +15 -5
- package/README.md +6 -3
- package/_dependencies/@hyperfrontend/immutable-api-utils/built-in-copy/date/index.cjs.js +10 -0
- package/_dependencies/@hyperfrontend/immutable-api-utils/built-in-copy/date/index.esm.js +8 -0
- package/_dependencies/@hyperfrontend/immutable-api-utils/built-in-copy/error/index.cjs.js +6 -0
- package/_dependencies/@hyperfrontend/immutable-api-utils/built-in-copy/error/index.esm.js +5 -0
- package/_dependencies/@hyperfrontend/immutable-api-utils/built-in-copy/math/index.cjs.js +5 -0
- package/_dependencies/@hyperfrontend/immutable-api-utils/built-in-copy/math/index.esm.js +4 -0
- package/_dependencies/@hyperfrontend/immutable-api-utils/built-in-copy/number/index.cjs.js +4 -0
- package/_dependencies/@hyperfrontend/immutable-api-utils/built-in-copy/number/index.esm.js +5 -0
- package/_dependencies/@hyperfrontend/immutable-api-utils/built-in-copy/object/index.cjs.js +5 -0
- package/_dependencies/@hyperfrontend/immutable-api-utils/built-in-copy/object/index.esm.js +4 -0
- package/_dependencies/@hyperfrontend/immutable-api-utils/built-in-copy/promise/index.cjs.js +6 -0
- package/_dependencies/@hyperfrontend/immutable-api-utils/built-in-copy/promise/index.esm.js +5 -0
- package/_dependencies/@hyperfrontend/immutable-api-utils/built-in-copy/timers/index.cjs.js +18 -0
- package/_dependencies/@hyperfrontend/immutable-api-utils/built-in-copy/timers/index.esm.js +14 -0
- package/bundle/index.iife.js +119 -24
- package/bundle/index.iife.min.js +0 -1
- package/bundle/index.umd.js +119 -24
- package/bundle/index.umd.min.js +0 -1
- package/index.cjs.js +84 -213
- package/index.d.ts +134 -6
- package/index.d.ts.map +1 -1
- package/index.esm.js +67 -196
- package/package.json +12 -7
- package/bundle/index.iife.js.map +0 -1
- package/bundle/index.iife.min.js.map +0 -1
- package/bundle/index.umd.js.map +0 -1
- package/bundle/index.umd.min.js.map +0 -1
- package/create-clock.d.ts +0 -20
- package/create-clock.d.ts.map +0 -1
- package/create-timer.d.ts +0 -18
- package/create-timer.d.ts.map +0 -1
- package/index.cjs.js.map +0 -1
- package/index.esm.js.map +0 -1
- package/normalize-to-base-time-window.d.ts +0 -9
- package/normalize-to-base-time-window.d.ts.map +0 -1
- package/set-interval-callback.d.ts +0 -9
- package/set-interval-callback.d.ts.map +0 -1
- package/sleep.d.ts +0 -8
- package/sleep.d.ts.map +0 -1
package/bundle/index.umd.js
CHANGED
|
@@ -7,22 +7,36 @@
|
|
|
7
7
|
/**
|
|
8
8
|
* Safe copies of Date built-in via factory function and static methods.
|
|
9
9
|
*
|
|
10
|
-
* Since constructors cannot be safely captured via Object.assign, this module
|
|
11
|
-
* provides a factory function that uses Reflect.construct internally.
|
|
12
|
-
*
|
|
13
|
-
* These references are captured at module initialization time to protect against
|
|
14
|
-
* prototype pollution attacks. Import only what you need for tree-shaking.
|
|
15
|
-
*
|
|
16
10
|
* @module @hyperfrontend/immutable-api-utils/built-in-copy/date
|
|
17
11
|
*/
|
|
18
|
-
|
|
12
|
+
/* eslint-disable jsdoc/require-param */
|
|
19
13
|
const _Date = globalThis.Date;
|
|
20
14
|
const _Reflect$2 = globalThis.Reflect;
|
|
15
|
+
/**
|
|
16
|
+
* (Safe copy) Creates a new Date using the captured Date constructor.
|
|
17
|
+
* Use this instead of `new Date()`. Accepts all standard Date constructor signatures.
|
|
18
|
+
*
|
|
19
|
+
* @returns A new Date instance.
|
|
20
|
+
*
|
|
21
|
+
* @example Creating Date instances
|
|
22
|
+
* ```typescript
|
|
23
|
+
* const now = createDate()
|
|
24
|
+
* const fromTimestamp = createDate(1704067200000)
|
|
25
|
+
* const fromString = createDate('2024-01-01T00:00:00Z')
|
|
26
|
+
* const fromParts = createDate(2024, 0, 1, 12, 30, 0) // Jan 1, 2024 12:30:00
|
|
27
|
+
* ```
|
|
28
|
+
*/
|
|
21
29
|
function createDate(...args) {
|
|
22
30
|
return _Reflect$2.construct(_Date, args);
|
|
23
31
|
}
|
|
24
32
|
/**
|
|
25
33
|
* (Safe copy) Returns the number of milliseconds elapsed since January 1, 1970 00:00:00 UTC.
|
|
34
|
+
*
|
|
35
|
+
* @example
|
|
36
|
+
* ```typescript
|
|
37
|
+
* const timestamp = dateNow()
|
|
38
|
+
* // => 1704067200000 (example timestamp)
|
|
39
|
+
* ```
|
|
26
40
|
*/
|
|
27
41
|
const dateNow = _Date.now;
|
|
28
42
|
|
|
@@ -34,7 +48,6 @@
|
|
|
34
48
|
*
|
|
35
49
|
* @module @hyperfrontend/immutable-api-utils/built-in-copy/object
|
|
36
50
|
*/
|
|
37
|
-
// Capture references at module initialization time
|
|
38
51
|
const _Object = globalThis.Object;
|
|
39
52
|
/**
|
|
40
53
|
* (Safe copy) Prevents modification of existing property attributes and values,
|
|
@@ -50,7 +63,6 @@
|
|
|
50
63
|
*
|
|
51
64
|
* @module @hyperfrontend/immutable-api-utils/built-in-copy/timers
|
|
52
65
|
*/
|
|
53
|
-
// Capture references at module initialization time
|
|
54
66
|
const _setTimeout = globalThis.setTimeout;
|
|
55
67
|
const _setInterval = globalThis.setInterval;
|
|
56
68
|
const _clearTimeout = globalThis.clearTimeout;
|
|
@@ -62,6 +74,13 @@
|
|
|
62
74
|
* @param delay - Time in milliseconds before executing.
|
|
63
75
|
* @param args - Additional arguments to pass to the callback.
|
|
64
76
|
* @returns A numeric ID for the timer.
|
|
77
|
+
*
|
|
78
|
+
* @example Setting a timeout
|
|
79
|
+
* ```typescript
|
|
80
|
+
* const timerId = setTimeout(() => console.log('Executed'), 1000)
|
|
81
|
+
* // Pass arguments to callback
|
|
82
|
+
* setTimeout((name, count) => console.log(name, count), 500, 'items', 5)
|
|
83
|
+
* ```
|
|
65
84
|
*/
|
|
66
85
|
const setTimeout = (callback, delay, ...args) => _setTimeout(callback, delay, ...args);
|
|
67
86
|
/**
|
|
@@ -71,12 +90,27 @@
|
|
|
71
90
|
* @param delay - Time in milliseconds between calls.
|
|
72
91
|
* @param args - Additional arguments to pass to the callback.
|
|
73
92
|
* @returns A numeric ID for the interval.
|
|
93
|
+
*
|
|
94
|
+
* @example Setting an interval
|
|
95
|
+
* ```typescript
|
|
96
|
+
* let count = 0
|
|
97
|
+
* const intervalId = setInterval(() => {
|
|
98
|
+
* count++
|
|
99
|
+
* if (count >= 5) clearInterval(intervalId)
|
|
100
|
+
* }, 1000)
|
|
101
|
+
* ```
|
|
74
102
|
*/
|
|
75
103
|
const setInterval = (callback, delay, ...args) => _setInterval(callback, delay, ...args);
|
|
76
104
|
/**
|
|
77
105
|
* (Safe copy) Cancels a timeout previously established by setTimeout.
|
|
78
106
|
*
|
|
79
107
|
* @param id - The identifier of the timeout to cancel.
|
|
108
|
+
*
|
|
109
|
+
* @example Canceling a timeout
|
|
110
|
+
* ```typescript
|
|
111
|
+
* const timerId = setTimeout(() => console.log('Never runs'), 5000)
|
|
112
|
+
* clearTimeout(timerId)
|
|
113
|
+
* ```
|
|
80
114
|
*/
|
|
81
115
|
const clearTimeout = (id) => {
|
|
82
116
|
_clearTimeout(id);
|
|
@@ -85,6 +119,13 @@
|
|
|
85
119
|
* (Safe copy) Cancels a timed, repeating action previously established by setInterval.
|
|
86
120
|
*
|
|
87
121
|
* @param id - The identifier of the interval to cancel.
|
|
122
|
+
*
|
|
123
|
+
* @example Canceling an interval
|
|
124
|
+
* ```typescript
|
|
125
|
+
* const intervalId = setInterval(() => console.log('tick'), 1000)
|
|
126
|
+
* // Stop after some condition
|
|
127
|
+
* clearInterval(intervalId)
|
|
128
|
+
* ```
|
|
88
129
|
*/
|
|
89
130
|
const clearInterval = (id) => {
|
|
90
131
|
_clearInterval(id);
|
|
@@ -99,6 +140,21 @@
|
|
|
99
140
|
*
|
|
100
141
|
* @param interval - Time in milliseconds between each callback invocation (default: 1000ms)
|
|
101
142
|
* @returns A Clock instance with start, stop, subscribe, and unsubscribe methods
|
|
143
|
+
*
|
|
144
|
+
* @example Creating interval clock
|
|
145
|
+
* ```typescript
|
|
146
|
+
* const clock = createClock(1000)
|
|
147
|
+
*
|
|
148
|
+
* const updateDisplay = (currentTime: Date) => {
|
|
149
|
+
* console.log(currentTime.toISOString())
|
|
150
|
+
* }
|
|
151
|
+
*
|
|
152
|
+
* clock.subscribe(updateDisplay)
|
|
153
|
+
* clock.start()
|
|
154
|
+
*
|
|
155
|
+
* // Later: stop receiving updates
|
|
156
|
+
* clock.stop()
|
|
157
|
+
* ```
|
|
102
158
|
*/
|
|
103
159
|
function createClock(interval = 1000) {
|
|
104
160
|
let clockId = null;
|
|
@@ -133,6 +189,24 @@
|
|
|
133
189
|
* @param callback - The function to invoke after the delay
|
|
134
190
|
* @param delay - Time in milliseconds to wait until callback is invoked
|
|
135
191
|
* @returns A Timer instance with pause, resume, and reset methods
|
|
192
|
+
*
|
|
193
|
+
* @example Creating pausable timer
|
|
194
|
+
* ```typescript
|
|
195
|
+
* const timer = createTimer(() => {
|
|
196
|
+
* console.log('Session expired')
|
|
197
|
+
* }, 30_000)
|
|
198
|
+
*
|
|
199
|
+
* timer.resume() // Start the 30-second countdown
|
|
200
|
+
*
|
|
201
|
+
* // User activity detected - pause the timer
|
|
202
|
+
* timer.pause()
|
|
203
|
+
*
|
|
204
|
+
* // User idle again - resume from where we left off
|
|
205
|
+
* timer.resume()
|
|
206
|
+
*
|
|
207
|
+
* // Reset to full 30 seconds on explicit action
|
|
208
|
+
* timer.reset()
|
|
209
|
+
* ```
|
|
136
210
|
*/
|
|
137
211
|
function createTimer(callback, delay) {
|
|
138
212
|
let timerId = null;
|
|
@@ -177,7 +251,6 @@
|
|
|
177
251
|
*
|
|
178
252
|
* @module @hyperfrontend/immutable-api-utils/built-in-copy/error
|
|
179
253
|
*/
|
|
180
|
-
// Capture references at module initialization time
|
|
181
254
|
const _Error = globalThis.Error;
|
|
182
255
|
const _Reflect$1 = globalThis.Reflect;
|
|
183
256
|
/**
|
|
@@ -187,6 +260,13 @@
|
|
|
187
260
|
* @param message - Optional error message.
|
|
188
261
|
* @param options - Optional error options.
|
|
189
262
|
* @returns A new Error instance.
|
|
263
|
+
*
|
|
264
|
+
* @example Creating Error instances
|
|
265
|
+
* ```typescript
|
|
266
|
+
* const error = createError('Operation failed')
|
|
267
|
+
* // With cause for error chaining
|
|
268
|
+
* const wrapped = createError('Request failed', { cause: originalError })
|
|
269
|
+
* ```
|
|
190
270
|
*/
|
|
191
271
|
const createError = (message, options) => _Reflect$1.construct(_Error, [message, options]);
|
|
192
272
|
|
|
@@ -198,7 +278,6 @@
|
|
|
198
278
|
*
|
|
199
279
|
* @module @hyperfrontend/immutable-api-utils/built-in-copy/math
|
|
200
280
|
*/
|
|
201
|
-
// Capture references at module initialization time
|
|
202
281
|
const _Math = globalThis.Math;
|
|
203
282
|
/**
|
|
204
283
|
* (Safe copy) Returns the largest integer less than or equal to a number.
|
|
@@ -213,11 +292,7 @@
|
|
|
213
292
|
*
|
|
214
293
|
* @module @hyperfrontend/immutable-api-utils/built-in-copy/number
|
|
215
294
|
*/
|
|
216
|
-
// Capture references at module initialization time
|
|
217
295
|
const _isNaN = globalThis.isNaN;
|
|
218
|
-
// ============================================================================
|
|
219
|
-
// Global Type Checking (legacy, less strict)
|
|
220
|
-
// ============================================================================
|
|
221
296
|
/**
|
|
222
297
|
* (Safe copy) Global isNaN function (coerces to number first, less strict than Number.isNaN).
|
|
223
298
|
*/
|
|
@@ -229,6 +304,14 @@
|
|
|
229
304
|
* @param time - The Date object to normalize to the nearest time window
|
|
230
305
|
* @param baseTimeWindow - The size of the time window in minutes for normalization
|
|
231
306
|
* @returns A new Date object normalized to the start of the time window
|
|
307
|
+
*
|
|
308
|
+
* @example Normalizing to 15-minute buckets
|
|
309
|
+
* ```typescript
|
|
310
|
+
* // Round timestamps to 15-minute intervals for analytics bucketing
|
|
311
|
+
* const eventTime = new Date('2024-03-15T14:23:45Z')
|
|
312
|
+
* const bucketTime = normalizeToBaseTimeWindow(eventTime, 15)
|
|
313
|
+
* // => 2024-03-15T14:15:00.000Z
|
|
314
|
+
* ```
|
|
232
315
|
*/
|
|
233
316
|
function normalizeToBaseTimeWindow(time, baseTimeWindow) {
|
|
234
317
|
if (!time || !(time instanceof Date) || globalIsNaN(time.getTime())) {
|
|
@@ -249,6 +332,16 @@
|
|
|
249
332
|
* @param callback - The function to invoke at each interval
|
|
250
333
|
* @param interval - Time in milliseconds between each callback invocation
|
|
251
334
|
* @returns A cleanup function that stops the interval when called
|
|
335
|
+
*
|
|
336
|
+
* @example Setting interval with cleanup
|
|
337
|
+
* ```typescript
|
|
338
|
+
* const stopPolling = setIntervalCallback(() => {
|
|
339
|
+
* fetchLatestData()
|
|
340
|
+
* }, 5000)
|
|
341
|
+
*
|
|
342
|
+
* // Later: clean up when component unmounts
|
|
343
|
+
* stopPolling()
|
|
344
|
+
* ```
|
|
252
345
|
*/
|
|
253
346
|
function setIntervalCallback(callback, interval) {
|
|
254
347
|
const timerId = setInterval(callback, interval);
|
|
@@ -256,17 +349,11 @@
|
|
|
256
349
|
}
|
|
257
350
|
|
|
258
351
|
/**
|
|
259
|
-
* Safe
|
|
260
|
-
*
|
|
261
|
-
* Since constructors cannot be safely captured via Object.assign, this module
|
|
262
|
-
* provides factory functions that use Reflect.construct internally.
|
|
263
|
-
*
|
|
264
|
-
* These references are captured at module initialization time to protect against
|
|
265
|
-
* prototype pollution attacks. Import only what you need for tree-shaking.
|
|
352
|
+
* Safe Promise factory and bound static methods.
|
|
266
353
|
*
|
|
267
354
|
* @module @hyperfrontend/immutable-api-utils/built-in-copy/promise
|
|
268
355
|
*/
|
|
269
|
-
|
|
356
|
+
/* eslint-disable workspace/lib-require-jsdoc-example */
|
|
270
357
|
const _Promise = globalThis.Promise;
|
|
271
358
|
const _Reflect = globalThis.Reflect;
|
|
272
359
|
/**
|
|
@@ -313,6 +400,15 @@
|
|
|
313
400
|
*
|
|
314
401
|
* @param milliseconds - The duration to sleep in milliseconds
|
|
315
402
|
* @returns A promise that resolves after the specified duration
|
|
403
|
+
*
|
|
404
|
+
* @example Implementing retry backoff
|
|
405
|
+
* ```typescript
|
|
406
|
+
* async function retryWithBackoff(attempt: number) {
|
|
407
|
+
* const backoffMs = Math.min(1000 * 2 ** attempt, 30_000)
|
|
408
|
+
* await sleep(backoffMs)
|
|
409
|
+
* return fetchResource()
|
|
410
|
+
* }
|
|
411
|
+
* ```
|
|
316
412
|
*/
|
|
317
413
|
function sleep(milliseconds) {
|
|
318
414
|
return createPromise((resolve) => setTimeout(resolve, milliseconds));
|
|
@@ -325,4 +421,3 @@
|
|
|
325
421
|
exports.sleep = sleep;
|
|
326
422
|
|
|
327
423
|
}));
|
|
328
|
-
//# sourceMappingURL=index.umd.js.map
|
package/bundle/index.umd.min.js
CHANGED
|
@@ -1,2 +1 @@
|
|
|
1
1
|
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).HyperfrontendTimeUtils={})}(this,function(e){"use strict";const t=globalThis.Date,l=globalThis.Reflect;function n(...e){return l.construct(t,e)}const i=t.now,o=globalThis.Object.freeze,s=globalThis.setTimeout,r=globalThis.setInterval,a=globalThis.clearTimeout,u=globalThis.clearInterval,c=(e,t,...l)=>s(e,t,...l),f=(e,t,...l)=>r(e,t,...l),b=e=>{u(e)};const T=globalThis.Error,h=globalThis.Reflect,d=(e,t)=>h.construct(T,[e,t]),g=globalThis.Math.floor,m=globalThis.isNaN;const p=globalThis.Promise,v=globalThis.Reflect;p.resolve.bind(p),p.reject.bind(p),p.all.bind(p),p.race.bind(p),p.allSettled.bind(p),p.any.bind(p),p.withResolvers?.bind(p),e.createClock=function(e=1e3){let t=null,l=[];return o({start:()=>{null===t&&(t=f(()=>{const e=n();l.forEach(t=>t(e))},e))},stop:()=>{null!==t&&(b(t),t=null)},subscribe:e=>{l.push(e)},unsubscribe:e=>{l=l.filter(t=>t!==e)},interval:e})},e.createTimer=function(e,t){let l=null,n=null,s=t;const r=()=>{if(null!==l){a(l);const e=i();null!==n&&(s-=e-n),l=null}},u=()=>{null===l&&(n=i(),l=c(()=>{e(),l=null},s))};return o({pause:r,resume:u,reset:(e=t)=>{r(),s=e,u()}})},e.normalizeToBaseTimeWindow=function(e,t){if(!e||!(e instanceof Date)||m(e.getTime()))throw d("Invalid time input");if(t<=0)throw d("Base time window must be positive");const l=e.getTime(),i=60*t*1e3;return n(g(l/i)*i)},e.setIntervalCallback=function(e,t){const l=f(e,t);return()=>b(l)},e.sleep=function(e){return t=t=>c(t,e),v.construct(p,[t]);var t}});
|
|
2
|
-
//# sourceMappingURL=index.umd.min.js.map
|
package/index.cjs.js
CHANGED
|
@@ -1,90 +1,12 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
* prototype pollution attacks. Import only what you need for tree-shaking.
|
|
11
|
-
*
|
|
12
|
-
* @module @hyperfrontend/immutable-api-utils/built-in-copy/date
|
|
13
|
-
*/
|
|
14
|
-
// Capture references at module initialization time
|
|
15
|
-
const _Date = globalThis.Date;
|
|
16
|
-
const _Reflect$2 = globalThis.Reflect;
|
|
17
|
-
function createDate(...args) {
|
|
18
|
-
return _Reflect$2.construct(_Date, args);
|
|
19
|
-
}
|
|
20
|
-
/**
|
|
21
|
-
* (Safe copy) Returns the number of milliseconds elapsed since January 1, 1970 00:00:00 UTC.
|
|
22
|
-
*/
|
|
23
|
-
const dateNow = _Date.now;
|
|
24
|
-
|
|
25
|
-
/**
|
|
26
|
-
* Safe copies of Object built-in methods.
|
|
27
|
-
*
|
|
28
|
-
* These references are captured at module initialization time to protect against
|
|
29
|
-
* prototype pollution attacks. Import only what you need for tree-shaking.
|
|
30
|
-
*
|
|
31
|
-
* @module @hyperfrontend/immutable-api-utils/built-in-copy/object
|
|
32
|
-
*/
|
|
33
|
-
// Capture references at module initialization time
|
|
34
|
-
const _Object = globalThis.Object;
|
|
35
|
-
/**
|
|
36
|
-
* (Safe copy) Prevents modification of existing property attributes and values,
|
|
37
|
-
* and prevents the addition of new properties.
|
|
38
|
-
*/
|
|
39
|
-
const freeze = _Object.freeze;
|
|
40
|
-
|
|
41
|
-
/**
|
|
42
|
-
* Safe copies of Timer/Scheduling built-in functions.
|
|
43
|
-
*
|
|
44
|
-
* These references are captured at module initialization time to protect against
|
|
45
|
-
* prototype pollution attacks. Import only what you need for tree-shaking.
|
|
46
|
-
*
|
|
47
|
-
* @module @hyperfrontend/immutable-api-utils/built-in-copy/timers
|
|
48
|
-
*/
|
|
49
|
-
// Capture references at module initialization time
|
|
50
|
-
const _setTimeout = globalThis.setTimeout;
|
|
51
|
-
const _setInterval = globalThis.setInterval;
|
|
52
|
-
const _clearTimeout = globalThis.clearTimeout;
|
|
53
|
-
const _clearInterval = globalThis.clearInterval;
|
|
54
|
-
/**
|
|
55
|
-
* (Safe copy) Sets a timer which executes a function once the timer expires.
|
|
56
|
-
*
|
|
57
|
-
* @param callback - Function to call when the timer elapses.
|
|
58
|
-
* @param delay - Time in milliseconds before executing.
|
|
59
|
-
* @param args - Additional arguments to pass to the callback.
|
|
60
|
-
* @returns A numeric ID for the timer.
|
|
61
|
-
*/
|
|
62
|
-
const setTimeout = (callback, delay, ...args) => _setTimeout(callback, delay, ...args);
|
|
63
|
-
/**
|
|
64
|
-
* (Safe copy) Repeatedly calls a function with a fixed time delay between each call.
|
|
65
|
-
*
|
|
66
|
-
* @param callback - Function to call at each interval.
|
|
67
|
-
* @param delay - Time in milliseconds between calls.
|
|
68
|
-
* @param args - Additional arguments to pass to the callback.
|
|
69
|
-
* @returns A numeric ID for the interval.
|
|
70
|
-
*/
|
|
71
|
-
const setInterval = (callback, delay, ...args) => _setInterval(callback, delay, ...args);
|
|
72
|
-
/**
|
|
73
|
-
* (Safe copy) Cancels a timeout previously established by setTimeout.
|
|
74
|
-
*
|
|
75
|
-
* @param id - The identifier of the timeout to cancel.
|
|
76
|
-
*/
|
|
77
|
-
const clearTimeout = (id) => {
|
|
78
|
-
_clearTimeout(id);
|
|
79
|
-
};
|
|
80
|
-
/**
|
|
81
|
-
* (Safe copy) Cancels a timed, repeating action previously established by setInterval.
|
|
82
|
-
*
|
|
83
|
-
* @param id - The identifier of the interval to cancel.
|
|
84
|
-
*/
|
|
85
|
-
const clearInterval = (id) => {
|
|
86
|
-
_clearInterval(id);
|
|
87
|
-
};
|
|
3
|
+
const index_cjs_js$2 = require('./_dependencies/@hyperfrontend/immutable-api-utils/built-in-copy/date/index.cjs.js');
|
|
4
|
+
const index_cjs_js = require('./_dependencies/@hyperfrontend/immutable-api-utils/built-in-copy/object/index.cjs.js');
|
|
5
|
+
const index_cjs_js$1 = require('./_dependencies/@hyperfrontend/immutable-api-utils/built-in-copy/timers/index.cjs.js');
|
|
6
|
+
const index_cjs_js$4 = require('./_dependencies/@hyperfrontend/immutable-api-utils/built-in-copy/error/index.cjs.js');
|
|
7
|
+
const index_cjs_js$5 = require('./_dependencies/@hyperfrontend/immutable-api-utils/built-in-copy/math/index.cjs.js');
|
|
8
|
+
const index_cjs_js$3 = require('./_dependencies/@hyperfrontend/immutable-api-utils/built-in-copy/number/index.cjs.js');
|
|
9
|
+
const index_cjs_js$6 = require('./_dependencies/@hyperfrontend/immutable-api-utils/built-in-copy/promise/index.cjs.js');
|
|
88
10
|
|
|
89
11
|
/**
|
|
90
12
|
* Creates an interval loop that invokes one or more subscribed callback functions
|
|
@@ -95,21 +17,36 @@ const clearInterval = (id) => {
|
|
|
95
17
|
*
|
|
96
18
|
* @param interval - Time in milliseconds between each callback invocation (default: 1000ms)
|
|
97
19
|
* @returns A Clock instance with start, stop, subscribe, and unsubscribe methods
|
|
20
|
+
*
|
|
21
|
+
* @example Creating interval clock
|
|
22
|
+
* ```typescript
|
|
23
|
+
* const clock = createClock(1000)
|
|
24
|
+
*
|
|
25
|
+
* const updateDisplay = (currentTime: Date) => {
|
|
26
|
+
* console.log(currentTime.toISOString())
|
|
27
|
+
* }
|
|
28
|
+
*
|
|
29
|
+
* clock.subscribe(updateDisplay)
|
|
30
|
+
* clock.start()
|
|
31
|
+
*
|
|
32
|
+
* // Later: stop receiving updates
|
|
33
|
+
* clock.stop()
|
|
34
|
+
* ```
|
|
98
35
|
*/
|
|
99
36
|
function createClock(interval = 1000) {
|
|
100
37
|
let clockId = null;
|
|
101
38
|
let subscribers = [];
|
|
102
39
|
const start = () => {
|
|
103
40
|
if (clockId === null) {
|
|
104
|
-
clockId = setInterval(() => {
|
|
105
|
-
const currentTime = createDate();
|
|
41
|
+
clockId = index_cjs_js$1.setInterval(() => {
|
|
42
|
+
const currentTime = index_cjs_js$2.createDate();
|
|
106
43
|
subscribers.forEach((subscriber) => subscriber(currentTime));
|
|
107
44
|
}, interval);
|
|
108
45
|
}
|
|
109
46
|
};
|
|
110
47
|
const stop = () => {
|
|
111
48
|
if (clockId !== null) {
|
|
112
|
-
clearInterval(clockId);
|
|
49
|
+
index_cjs_js$1.clearInterval(clockId);
|
|
113
50
|
clockId = null;
|
|
114
51
|
}
|
|
115
52
|
};
|
|
@@ -119,7 +56,7 @@ function createClock(interval = 1000) {
|
|
|
119
56
|
const unsubscribe = (callback) => {
|
|
120
57
|
subscribers = subscribers.filter((subscriber) => subscriber !== callback);
|
|
121
58
|
};
|
|
122
|
-
return freeze({ start, stop, subscribe, unsubscribe, interval });
|
|
59
|
+
return index_cjs_js.freeze({ start, stop, subscribe, unsubscribe, interval });
|
|
123
60
|
}
|
|
124
61
|
|
|
125
62
|
/**
|
|
@@ -129,6 +66,24 @@ function createClock(interval = 1000) {
|
|
|
129
66
|
* @param callback - The function to invoke after the delay
|
|
130
67
|
* @param delay - Time in milliseconds to wait until callback is invoked
|
|
131
68
|
* @returns A Timer instance with pause, resume, and reset methods
|
|
69
|
+
*
|
|
70
|
+
* @example Creating pausable timer
|
|
71
|
+
* ```typescript
|
|
72
|
+
* const timer = createTimer(() => {
|
|
73
|
+
* console.log('Session expired')
|
|
74
|
+
* }, 30_000)
|
|
75
|
+
*
|
|
76
|
+
* timer.resume() // Start the 30-second countdown
|
|
77
|
+
*
|
|
78
|
+
* // User activity detected - pause the timer
|
|
79
|
+
* timer.pause()
|
|
80
|
+
*
|
|
81
|
+
* // User idle again - resume from where we left off
|
|
82
|
+
* timer.resume()
|
|
83
|
+
*
|
|
84
|
+
* // Reset to full 30 seconds on explicit action
|
|
85
|
+
* timer.reset()
|
|
86
|
+
* ```
|
|
132
87
|
*/
|
|
133
88
|
function createTimer(callback, delay) {
|
|
134
89
|
let timerId = null;
|
|
@@ -136,8 +91,8 @@ function createTimer(callback, delay) {
|
|
|
136
91
|
let remaining = delay;
|
|
137
92
|
const pause = () => {
|
|
138
93
|
if (timerId !== null) {
|
|
139
|
-
clearTimeout(timerId);
|
|
140
|
-
const now = dateNow();
|
|
94
|
+
index_cjs_js$1.clearTimeout(timerId);
|
|
95
|
+
const now = index_cjs_js$2.dateNow();
|
|
141
96
|
/* istanbul ignore else - start is always set when timerId is not null */
|
|
142
97
|
if (start !== null) {
|
|
143
98
|
remaining -= now - start;
|
|
@@ -147,8 +102,8 @@ function createTimer(callback, delay) {
|
|
|
147
102
|
};
|
|
148
103
|
const resume = () => {
|
|
149
104
|
if (timerId === null) {
|
|
150
|
-
start = dateNow();
|
|
151
|
-
timerId = setTimeout(() => {
|
|
105
|
+
start = index_cjs_js$2.dateNow();
|
|
106
|
+
timerId = index_cjs_js$1.setTimeout(() => {
|
|
152
107
|
callback();
|
|
153
108
|
timerId = null;
|
|
154
109
|
}, remaining);
|
|
@@ -159,84 +114,35 @@ function createTimer(callback, delay) {
|
|
|
159
114
|
remaining = newDelay;
|
|
160
115
|
resume();
|
|
161
116
|
};
|
|
162
|
-
return freeze({ pause, resume, reset });
|
|
117
|
+
return index_cjs_js.freeze({ pause, resume, reset });
|
|
163
118
|
}
|
|
164
119
|
|
|
165
|
-
/**
|
|
166
|
-
* Safe copies of Error built-ins via factory functions.
|
|
167
|
-
*
|
|
168
|
-
* Since constructors cannot be safely captured via Object.assign, this module
|
|
169
|
-
* provides factory functions that use Reflect.construct internally.
|
|
170
|
-
*
|
|
171
|
-
* These references are captured at module initialization time to protect against
|
|
172
|
-
* prototype pollution attacks. Import only what you need for tree-shaking.
|
|
173
|
-
*
|
|
174
|
-
* @module @hyperfrontend/immutable-api-utils/built-in-copy/error
|
|
175
|
-
*/
|
|
176
|
-
// Capture references at module initialization time
|
|
177
|
-
const _Error = globalThis.Error;
|
|
178
|
-
const _Reflect$1 = globalThis.Reflect;
|
|
179
|
-
/**
|
|
180
|
-
* (Safe copy) Creates a new Error using the captured Error constructor.
|
|
181
|
-
* Use this instead of `new Error()`.
|
|
182
|
-
*
|
|
183
|
-
* @param message - Optional error message.
|
|
184
|
-
* @param options - Optional error options.
|
|
185
|
-
* @returns A new Error instance.
|
|
186
|
-
*/
|
|
187
|
-
const createError = (message, options) => _Reflect$1.construct(_Error, [message, options]);
|
|
188
|
-
|
|
189
|
-
/**
|
|
190
|
-
* Safe copies of Math built-in methods.
|
|
191
|
-
*
|
|
192
|
-
* These references are captured at module initialization time to protect against
|
|
193
|
-
* prototype pollution attacks. Import only what you need for tree-shaking.
|
|
194
|
-
*
|
|
195
|
-
* @module @hyperfrontend/immutable-api-utils/built-in-copy/math
|
|
196
|
-
*/
|
|
197
|
-
// Capture references at module initialization time
|
|
198
|
-
const _Math = globalThis.Math;
|
|
199
|
-
/**
|
|
200
|
-
* (Safe copy) Returns the largest integer less than or equal to a number.
|
|
201
|
-
*/
|
|
202
|
-
const floor = _Math.floor;
|
|
203
|
-
|
|
204
|
-
/**
|
|
205
|
-
* Safe copies of Number built-in methods and constants.
|
|
206
|
-
*
|
|
207
|
-
* These references are captured at module initialization time to protect against
|
|
208
|
-
* prototype pollution attacks. Import only what you need for tree-shaking.
|
|
209
|
-
*
|
|
210
|
-
* @module @hyperfrontend/immutable-api-utils/built-in-copy/number
|
|
211
|
-
*/
|
|
212
|
-
// Capture references at module initialization time
|
|
213
|
-
const _isNaN = globalThis.isNaN;
|
|
214
|
-
// ============================================================================
|
|
215
|
-
// Global Type Checking (legacy, less strict)
|
|
216
|
-
// ============================================================================
|
|
217
|
-
/**
|
|
218
|
-
* (Safe copy) Global isNaN function (coerces to number first, less strict than Number.isNaN).
|
|
219
|
-
*/
|
|
220
|
-
const globalIsNaN = _isNaN;
|
|
221
|
-
|
|
222
120
|
/**
|
|
223
121
|
* Normalizes a given time to the nearest base time window.
|
|
224
122
|
*
|
|
225
123
|
* @param time - The Date object to normalize to the nearest time window
|
|
226
124
|
* @param baseTimeWindow - The size of the time window in minutes for normalization
|
|
227
125
|
* @returns A new Date object normalized to the start of the time window
|
|
126
|
+
*
|
|
127
|
+
* @example Normalizing to 15-minute buckets
|
|
128
|
+
* ```typescript
|
|
129
|
+
* // Round timestamps to 15-minute intervals for analytics bucketing
|
|
130
|
+
* const eventTime = new Date('2024-03-15T14:23:45Z')
|
|
131
|
+
* const bucketTime = normalizeToBaseTimeWindow(eventTime, 15)
|
|
132
|
+
* // => 2024-03-15T14:15:00.000Z
|
|
133
|
+
* ```
|
|
228
134
|
*/
|
|
229
135
|
function normalizeToBaseTimeWindow(time, baseTimeWindow) {
|
|
230
|
-
if (!time || !(time instanceof Date) || globalIsNaN(time.getTime())) {
|
|
231
|
-
throw createError('Invalid time input');
|
|
136
|
+
if (!time || !(time instanceof Date) || index_cjs_js$3.globalIsNaN(time.getTime())) {
|
|
137
|
+
throw index_cjs_js$4.createError('Invalid time input');
|
|
232
138
|
}
|
|
233
139
|
if (baseTimeWindow <= 0) {
|
|
234
|
-
throw createError('Base time window must be positive');
|
|
140
|
+
throw index_cjs_js$4.createError('Base time window must be positive');
|
|
235
141
|
}
|
|
236
142
|
const timeInMs = time.getTime();
|
|
237
143
|
const windowInMs = baseTimeWindow * 60 * 1000;
|
|
238
|
-
const normalizedTimeInMs = floor(timeInMs / windowInMs) * windowInMs;
|
|
239
|
-
return createDate(normalizedTimeInMs);
|
|
144
|
+
const normalizedTimeInMs = index_cjs_js$5.floor(timeInMs / windowInMs) * windowInMs;
|
|
145
|
+
return index_cjs_js$2.createDate(normalizedTimeInMs);
|
|
240
146
|
}
|
|
241
147
|
|
|
242
148
|
/**
|
|
@@ -245,73 +151,39 @@ function normalizeToBaseTimeWindow(time, baseTimeWindow) {
|
|
|
245
151
|
* @param callback - The function to invoke at each interval
|
|
246
152
|
* @param interval - Time in milliseconds between each callback invocation
|
|
247
153
|
* @returns A cleanup function that stops the interval when called
|
|
248
|
-
*/
|
|
249
|
-
function setIntervalCallback(callback, interval) {
|
|
250
|
-
const timerId = setInterval(callback, interval);
|
|
251
|
-
return () => clearInterval(timerId);
|
|
252
|
-
}
|
|
253
|
-
|
|
254
|
-
/**
|
|
255
|
-
* Safe copies of Promise built-in methods via factory functions.
|
|
256
|
-
*
|
|
257
|
-
* Since constructors cannot be safely captured via Object.assign, this module
|
|
258
|
-
* provides factory functions that use Reflect.construct internally.
|
|
259
|
-
*
|
|
260
|
-
* These references are captured at module initialization time to protect against
|
|
261
|
-
* prototype pollution attacks. Import only what you need for tree-shaking.
|
|
262
154
|
*
|
|
263
|
-
* @
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
/**
|
|
269
|
-
* (Safe copy) Creates a new Promise using the captured Promise constructor.
|
|
270
|
-
* Use this instead of `new Promise()`.
|
|
155
|
+
* @example Setting interval with cleanup
|
|
156
|
+
* ```typescript
|
|
157
|
+
* const stopPolling = setIntervalCallback(() => {
|
|
158
|
+
* fetchLatestData()
|
|
159
|
+
* }, 5000)
|
|
271
160
|
*
|
|
272
|
-
*
|
|
273
|
-
*
|
|
274
|
-
|
|
275
|
-
const createPromise = (executor) => _Reflect.construct(_Promise, [executor]);
|
|
276
|
-
/**
|
|
277
|
-
* (Safe copy) Returns a Promise that resolves with the given value.
|
|
278
|
-
*/
|
|
279
|
-
_Promise.resolve.bind(_Promise);
|
|
280
|
-
/**
|
|
281
|
-
* (Safe copy) Returns a Promise that rejects with the given reason.
|
|
282
|
-
*/
|
|
283
|
-
_Promise.reject.bind(_Promise);
|
|
284
|
-
/**
|
|
285
|
-
* (Safe copy) Returns a Promise that resolves when all promises resolve.
|
|
161
|
+
* // Later: clean up when component unmounts
|
|
162
|
+
* stopPolling()
|
|
163
|
+
* ```
|
|
286
164
|
*/
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
_Promise.race.bind(_Promise);
|
|
292
|
-
/**
|
|
293
|
-
* (Safe copy) Returns a Promise that resolves when all promises settle.
|
|
294
|
-
*/
|
|
295
|
-
_Promise.allSettled.bind(_Promise);
|
|
296
|
-
/**
|
|
297
|
-
* (Safe copy) Returns a Promise that resolves with the first fulfilled promise.
|
|
298
|
-
*/
|
|
299
|
-
_Promise.any.bind(_Promise);
|
|
300
|
-
/**
|
|
301
|
-
* (Safe copy) Creates a Promise along with its resolve and reject functions.
|
|
302
|
-
* Note: Available only in ES2024+ environments.
|
|
303
|
-
*/
|
|
304
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
305
|
-
_Promise.withResolvers?.bind(_Promise);
|
|
165
|
+
function setIntervalCallback(callback, interval) {
|
|
166
|
+
const timerId = index_cjs_js$1.setInterval(callback, interval);
|
|
167
|
+
return () => index_cjs_js$1.clearInterval(timerId);
|
|
168
|
+
}
|
|
306
169
|
|
|
307
170
|
/**
|
|
308
171
|
* Pauses execution for a specified duration.
|
|
309
172
|
*
|
|
310
173
|
* @param milliseconds - The duration to sleep in milliseconds
|
|
311
174
|
* @returns A promise that resolves after the specified duration
|
|
175
|
+
*
|
|
176
|
+
* @example Implementing retry backoff
|
|
177
|
+
* ```typescript
|
|
178
|
+
* async function retryWithBackoff(attempt: number) {
|
|
179
|
+
* const backoffMs = Math.min(1000 * 2 ** attempt, 30_000)
|
|
180
|
+
* await sleep(backoffMs)
|
|
181
|
+
* return fetchResource()
|
|
182
|
+
* }
|
|
183
|
+
* ```
|
|
312
184
|
*/
|
|
313
185
|
function sleep(milliseconds) {
|
|
314
|
-
return createPromise((resolve) => setTimeout(resolve, milliseconds));
|
|
186
|
+
return index_cjs_js$6.createPromise((resolve) => index_cjs_js$1.setTimeout(resolve, milliseconds));
|
|
315
187
|
}
|
|
316
188
|
|
|
317
189
|
exports.createClock = createClock;
|
|
@@ -319,4 +191,3 @@ exports.createTimer = createTimer;
|
|
|
319
191
|
exports.normalizeToBaseTimeWindow = normalizeToBaseTimeWindow;
|
|
320
192
|
exports.setIntervalCallback = setIntervalCallback;
|
|
321
193
|
exports.sleep = sleep;
|
|
322
|
-
//# sourceMappingURL=index.cjs.js.map
|