@hyperfrontend/time-utils 0.0.4 → 1.0.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/CHANGELOG.md +20 -6
- package/README.md +6 -3
- package/SECURITY.md +50 -15
- 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 +0 -192
- package/bundle/index.iife.min.js +0 -1
- package/bundle/index.umd.js +0 -192
- 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
|
@@ -4,102 +4,29 @@
|
|
|
4
4
|
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.HyperfrontendTimeUtils = {}));
|
|
5
5
|
})(this, (function (exports) { 'use strict';
|
|
6
6
|
|
|
7
|
-
/**
|
|
8
|
-
* Safe copies of Date built-in via factory function and static methods.
|
|
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
|
-
* @module @hyperfrontend/immutable-api-utils/built-in-copy/date
|
|
17
|
-
*/
|
|
18
|
-
// Capture references at module initialization time
|
|
19
7
|
const _Date = globalThis.Date;
|
|
20
8
|
const _Reflect$2 = globalThis.Reflect;
|
|
21
9
|
function createDate(...args) {
|
|
22
10
|
return _Reflect$2.construct(_Date, args);
|
|
23
11
|
}
|
|
24
|
-
/**
|
|
25
|
-
* (Safe copy) Returns the number of milliseconds elapsed since January 1, 1970 00:00:00 UTC.
|
|
26
|
-
*/
|
|
27
12
|
const dateNow = _Date.now;
|
|
28
13
|
|
|
29
|
-
/**
|
|
30
|
-
* Safe copies of Object built-in methods.
|
|
31
|
-
*
|
|
32
|
-
* These references are captured at module initialization time to protect against
|
|
33
|
-
* prototype pollution attacks. Import only what you need for tree-shaking.
|
|
34
|
-
*
|
|
35
|
-
* @module @hyperfrontend/immutable-api-utils/built-in-copy/object
|
|
36
|
-
*/
|
|
37
|
-
// Capture references at module initialization time
|
|
38
14
|
const _Object = globalThis.Object;
|
|
39
|
-
/**
|
|
40
|
-
* (Safe copy) Prevents modification of existing property attributes and values,
|
|
41
|
-
* and prevents the addition of new properties.
|
|
42
|
-
*/
|
|
43
15
|
const freeze = _Object.freeze;
|
|
44
16
|
|
|
45
|
-
/**
|
|
46
|
-
* Safe copies of Timer/Scheduling built-in functions.
|
|
47
|
-
*
|
|
48
|
-
* These references are captured at module initialization time to protect against
|
|
49
|
-
* prototype pollution attacks. Import only what you need for tree-shaking.
|
|
50
|
-
*
|
|
51
|
-
* @module @hyperfrontend/immutable-api-utils/built-in-copy/timers
|
|
52
|
-
*/
|
|
53
|
-
// Capture references at module initialization time
|
|
54
17
|
const _setTimeout = globalThis.setTimeout;
|
|
55
18
|
const _setInterval = globalThis.setInterval;
|
|
56
19
|
const _clearTimeout = globalThis.clearTimeout;
|
|
57
20
|
const _clearInterval = globalThis.clearInterval;
|
|
58
|
-
/**
|
|
59
|
-
* (Safe copy) Sets a timer which executes a function once the timer expires.
|
|
60
|
-
*
|
|
61
|
-
* @param callback - Function to call when the timer elapses.
|
|
62
|
-
* @param delay - Time in milliseconds before executing.
|
|
63
|
-
* @param args - Additional arguments to pass to the callback.
|
|
64
|
-
* @returns A numeric ID for the timer.
|
|
65
|
-
*/
|
|
66
21
|
const setTimeout = (callback, delay, ...args) => _setTimeout(callback, delay, ...args);
|
|
67
|
-
/**
|
|
68
|
-
* (Safe copy) Repeatedly calls a function with a fixed time delay between each call.
|
|
69
|
-
*
|
|
70
|
-
* @param callback - Function to call at each interval.
|
|
71
|
-
* @param delay - Time in milliseconds between calls.
|
|
72
|
-
* @param args - Additional arguments to pass to the callback.
|
|
73
|
-
* @returns A numeric ID for the interval.
|
|
74
|
-
*/
|
|
75
22
|
const setInterval = (callback, delay, ...args) => _setInterval(callback, delay, ...args);
|
|
76
|
-
/**
|
|
77
|
-
* (Safe copy) Cancels a timeout previously established by setTimeout.
|
|
78
|
-
*
|
|
79
|
-
* @param id - The identifier of the timeout to cancel.
|
|
80
|
-
*/
|
|
81
23
|
const clearTimeout = (id) => {
|
|
82
24
|
_clearTimeout(id);
|
|
83
25
|
};
|
|
84
|
-
/**
|
|
85
|
-
* (Safe copy) Cancels a timed, repeating action previously established by setInterval.
|
|
86
|
-
*
|
|
87
|
-
* @param id - The identifier of the interval to cancel.
|
|
88
|
-
*/
|
|
89
26
|
const clearInterval = (id) => {
|
|
90
27
|
_clearInterval(id);
|
|
91
28
|
};
|
|
92
29
|
|
|
93
|
-
/**
|
|
94
|
-
* Creates an interval loop that invokes one or more subscribed callback functions
|
|
95
|
-
* at the specified internal (in milliseconds).
|
|
96
|
-
*
|
|
97
|
-
* Allows you to start or stop the interval loop, much like a stop watch.
|
|
98
|
-
* Allows you to unsubscribe callback functions.
|
|
99
|
-
*
|
|
100
|
-
* @param interval - Time in milliseconds between each callback invocation (default: 1000ms)
|
|
101
|
-
* @returns A Clock instance with start, stop, subscribe, and unsubscribe methods
|
|
102
|
-
*/
|
|
103
30
|
function createClock(interval = 1000) {
|
|
104
31
|
let clockId = null;
|
|
105
32
|
let subscribers = [];
|
|
@@ -126,14 +53,6 @@
|
|
|
126
53
|
return freeze({ start, stop, subscribe, unsubscribe, interval });
|
|
127
54
|
}
|
|
128
55
|
|
|
129
|
-
/**
|
|
130
|
-
* Invokes callback function after the designated time has passed, much like a timer.
|
|
131
|
-
* Allows you to pause, resume, or reset the progress of time tracked.
|
|
132
|
-
*
|
|
133
|
-
* @param callback - The function to invoke after the delay
|
|
134
|
-
* @param delay - Time in milliseconds to wait until callback is invoked
|
|
135
|
-
* @returns A Timer instance with pause, resume, and reset methods
|
|
136
|
-
*/
|
|
137
56
|
function createTimer(callback, delay) {
|
|
138
57
|
let timerId = null;
|
|
139
58
|
let start = null;
|
|
@@ -142,7 +61,6 @@
|
|
|
142
61
|
if (timerId !== null) {
|
|
143
62
|
clearTimeout(timerId);
|
|
144
63
|
const now = dateNow();
|
|
145
|
-
/* istanbul ignore else - start is always set when timerId is not null */
|
|
146
64
|
if (start !== null) {
|
|
147
65
|
remaining -= now - start;
|
|
148
66
|
}
|
|
@@ -166,70 +84,16 @@
|
|
|
166
84
|
return freeze({ pause, resume, reset });
|
|
167
85
|
}
|
|
168
86
|
|
|
169
|
-
/**
|
|
170
|
-
* Safe copies of Error built-ins via factory functions.
|
|
171
|
-
*
|
|
172
|
-
* Since constructors cannot be safely captured via Object.assign, this module
|
|
173
|
-
* provides factory functions that use Reflect.construct internally.
|
|
174
|
-
*
|
|
175
|
-
* These references are captured at module initialization time to protect against
|
|
176
|
-
* prototype pollution attacks. Import only what you need for tree-shaking.
|
|
177
|
-
*
|
|
178
|
-
* @module @hyperfrontend/immutable-api-utils/built-in-copy/error
|
|
179
|
-
*/
|
|
180
|
-
// Capture references at module initialization time
|
|
181
87
|
const _Error = globalThis.Error;
|
|
182
88
|
const _Reflect$1 = globalThis.Reflect;
|
|
183
|
-
/**
|
|
184
|
-
* (Safe copy) Creates a new Error using the captured Error constructor.
|
|
185
|
-
* Use this instead of `new Error()`.
|
|
186
|
-
*
|
|
187
|
-
* @param message - Optional error message.
|
|
188
|
-
* @param options - Optional error options.
|
|
189
|
-
* @returns A new Error instance.
|
|
190
|
-
*/
|
|
191
89
|
const createError = (message, options) => _Reflect$1.construct(_Error, [message, options]);
|
|
192
90
|
|
|
193
|
-
/**
|
|
194
|
-
* Safe copies of Math built-in methods.
|
|
195
|
-
*
|
|
196
|
-
* These references are captured at module initialization time to protect against
|
|
197
|
-
* prototype pollution attacks. Import only what you need for tree-shaking.
|
|
198
|
-
*
|
|
199
|
-
* @module @hyperfrontend/immutable-api-utils/built-in-copy/math
|
|
200
|
-
*/
|
|
201
|
-
// Capture references at module initialization time
|
|
202
91
|
const _Math = globalThis.Math;
|
|
203
|
-
/**
|
|
204
|
-
* (Safe copy) Returns the largest integer less than or equal to a number.
|
|
205
|
-
*/
|
|
206
92
|
const floor = _Math.floor;
|
|
207
93
|
|
|
208
|
-
/**
|
|
209
|
-
* Safe copies of Number built-in methods and constants.
|
|
210
|
-
*
|
|
211
|
-
* These references are captured at module initialization time to protect against
|
|
212
|
-
* prototype pollution attacks. Import only what you need for tree-shaking.
|
|
213
|
-
*
|
|
214
|
-
* @module @hyperfrontend/immutable-api-utils/built-in-copy/number
|
|
215
|
-
*/
|
|
216
|
-
// Capture references at module initialization time
|
|
217
94
|
const _isNaN = globalThis.isNaN;
|
|
218
|
-
// ============================================================================
|
|
219
|
-
// Global Type Checking (legacy, less strict)
|
|
220
|
-
// ============================================================================
|
|
221
|
-
/**
|
|
222
|
-
* (Safe copy) Global isNaN function (coerces to number first, less strict than Number.isNaN).
|
|
223
|
-
*/
|
|
224
95
|
const globalIsNaN = _isNaN;
|
|
225
96
|
|
|
226
|
-
/**
|
|
227
|
-
* Normalizes a given time to the nearest base time window.
|
|
228
|
-
*
|
|
229
|
-
* @param time - The Date object to normalize to the nearest time window
|
|
230
|
-
* @param baseTimeWindow - The size of the time window in minutes for normalization
|
|
231
|
-
* @returns A new Date object normalized to the start of the time window
|
|
232
|
-
*/
|
|
233
97
|
function normalizeToBaseTimeWindow(time, baseTimeWindow) {
|
|
234
98
|
if (!time || !(time instanceof Date) || globalIsNaN(time.getTime())) {
|
|
235
99
|
throw createError('Invalid time input');
|
|
@@ -243,77 +107,22 @@
|
|
|
243
107
|
return createDate(normalizedTimeInMs);
|
|
244
108
|
}
|
|
245
109
|
|
|
246
|
-
/**
|
|
247
|
-
* Creates a repeating interval that invokes a callback function at regular intervals.
|
|
248
|
-
*
|
|
249
|
-
* @param callback - The function to invoke at each interval
|
|
250
|
-
* @param interval - Time in milliseconds between each callback invocation
|
|
251
|
-
* @returns A cleanup function that stops the interval when called
|
|
252
|
-
*/
|
|
253
110
|
function setIntervalCallback(callback, interval) {
|
|
254
111
|
const timerId = setInterval(callback, interval);
|
|
255
112
|
return () => clearInterval(timerId);
|
|
256
113
|
}
|
|
257
114
|
|
|
258
|
-
/**
|
|
259
|
-
* Safe copies of Promise built-in methods via factory functions.
|
|
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.
|
|
266
|
-
*
|
|
267
|
-
* @module @hyperfrontend/immutable-api-utils/built-in-copy/promise
|
|
268
|
-
*/
|
|
269
|
-
// Capture references at module initialization time
|
|
270
115
|
const _Promise = globalThis.Promise;
|
|
271
116
|
const _Reflect = globalThis.Reflect;
|
|
272
|
-
/**
|
|
273
|
-
* (Safe copy) Creates a new Promise using the captured Promise constructor.
|
|
274
|
-
* Use this instead of `new Promise()`.
|
|
275
|
-
*
|
|
276
|
-
* @param executor - The executor function.
|
|
277
|
-
* @returns A new Promise instance.
|
|
278
|
-
*/
|
|
279
117
|
const createPromise = (executor) => _Reflect.construct(_Promise, [executor]);
|
|
280
|
-
/**
|
|
281
|
-
* (Safe copy) Returns a Promise that resolves with the given value.
|
|
282
|
-
*/
|
|
283
118
|
_Promise.resolve.bind(_Promise);
|
|
284
|
-
/**
|
|
285
|
-
* (Safe copy) Returns a Promise that rejects with the given reason.
|
|
286
|
-
*/
|
|
287
119
|
_Promise.reject.bind(_Promise);
|
|
288
|
-
/**
|
|
289
|
-
* (Safe copy) Returns a Promise that resolves when all promises resolve.
|
|
290
|
-
*/
|
|
291
120
|
_Promise.all.bind(_Promise);
|
|
292
|
-
/**
|
|
293
|
-
* (Safe copy) Returns a Promise that resolves/rejects with the first settled promise.
|
|
294
|
-
*/
|
|
295
121
|
_Promise.race.bind(_Promise);
|
|
296
|
-
/**
|
|
297
|
-
* (Safe copy) Returns a Promise that resolves when all promises settle.
|
|
298
|
-
*/
|
|
299
122
|
_Promise.allSettled.bind(_Promise);
|
|
300
|
-
/**
|
|
301
|
-
* (Safe copy) Returns a Promise that resolves with the first fulfilled promise.
|
|
302
|
-
*/
|
|
303
123
|
_Promise.any.bind(_Promise);
|
|
304
|
-
/**
|
|
305
|
-
* (Safe copy) Creates a Promise along with its resolve and reject functions.
|
|
306
|
-
* Note: Available only in ES2024+ environments.
|
|
307
|
-
*/
|
|
308
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
309
124
|
_Promise.withResolvers?.bind(_Promise);
|
|
310
125
|
|
|
311
|
-
/**
|
|
312
|
-
* Pauses execution for a specified duration.
|
|
313
|
-
*
|
|
314
|
-
* @param milliseconds - The duration to sleep in milliseconds
|
|
315
|
-
* @returns A promise that resolves after the specified duration
|
|
316
|
-
*/
|
|
317
126
|
function sleep(milliseconds) {
|
|
318
127
|
return createPromise((resolve) => setTimeout(resolve, milliseconds));
|
|
319
128
|
}
|
|
@@ -325,4 +134,3 @@
|
|
|
325
134
|
exports.sleep = sleep;
|
|
326
135
|
|
|
327
136
|
}));
|
|
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
|