@t007/utils 0.0.13 → 0.0.14
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/dist/index.cjs +59 -0
- package/dist/index.d.cts +21 -1
- package/dist/index.d.ts +21 -1
- package/dist/index.js +51 -0
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -23,8 +23,11 @@ __export(index_exports, {
|
|
|
23
23
|
VIRTUAL_RESOURCE: () => VIRTUAL_RESOURCE,
|
|
24
24
|
assignEl: () => assignEl,
|
|
25
25
|
bindAllMethods: () => bindAllMethods,
|
|
26
|
+
bindCleanupToSignal: () => bindCleanupToSignal,
|
|
27
|
+
breath: () => breath,
|
|
26
28
|
clamp: () => clamp,
|
|
27
29
|
createEl: () => createEl,
|
|
30
|
+
deepBreath: () => deepBreath,
|
|
28
31
|
guardAllMethods: () => guardAllMethods,
|
|
29
32
|
guardMethod: () => guardMethod,
|
|
30
33
|
inBoolArrOpt: () => inBoolArrOpt,
|
|
@@ -41,9 +44,14 @@ __export(index_exports, {
|
|
|
41
44
|
isSameURL: () => isSameURL,
|
|
42
45
|
isStr: () => isStr,
|
|
43
46
|
isSym: () => isSym,
|
|
47
|
+
limited: () => limited,
|
|
44
48
|
loadResource: () => loadResource,
|
|
49
|
+
mockAsync: () => mockAsync,
|
|
45
50
|
onAllMethods: () => onAllMethods,
|
|
46
51
|
removeScrollAssist: () => removeScrollAssist,
|
|
52
|
+
requestAnimationFrame: () => requestAnimationFrame2,
|
|
53
|
+
setInterval: () => setInterval,
|
|
54
|
+
setTimeout: () => setTimeout2,
|
|
47
55
|
uid: () => uid
|
|
48
56
|
});
|
|
49
57
|
module.exports = __toCommonJS(index_exports);
|
|
@@ -147,6 +155,49 @@ function isSameURL(src1, src2) {
|
|
|
147
155
|
}
|
|
148
156
|
}
|
|
149
157
|
|
|
158
|
+
// src/core/fn.ts
|
|
159
|
+
function setTimeout2(handler, timeout, ...args) {
|
|
160
|
+
const sig = args[0] instanceof AbortSignal ? args.shift() : void 0;
|
|
161
|
+
if (sig?.aborted) return -1;
|
|
162
|
+
const w = args[0] instanceof Window ? args.shift() : window;
|
|
163
|
+
if (!sig) return w.setTimeout(handler, timeout, ...args);
|
|
164
|
+
const id = w.setTimeout(() => (sig.removeEventListener("abort", kill), isStr(handler) ? new Function(handler) : handler(...args)), timeout), kill = () => w.clearTimeout(id);
|
|
165
|
+
return sig.addEventListener("abort", kill, { once: true }), id;
|
|
166
|
+
}
|
|
167
|
+
function setInterval(handler, timeout, ...args) {
|
|
168
|
+
const sig = args[0] instanceof AbortSignal ? args.shift() : void 0;
|
|
169
|
+
if (sig?.aborted) return -1;
|
|
170
|
+
const w = args[0] instanceof Window ? args.shift() : window, id = w.setInterval(handler, timeout, ...args);
|
|
171
|
+
return sig?.addEventListener("abort", () => w.clearInterval(id), { once: true }), id;
|
|
172
|
+
}
|
|
173
|
+
function requestAnimationFrame2(callback, sig, w = window) {
|
|
174
|
+
if (sig?.aborted) return -1;
|
|
175
|
+
if (!sig) return w.requestAnimationFrame(callback);
|
|
176
|
+
const id = w.requestAnimationFrame((t) => (sig.removeEventListener("abort", kill), callback(t))), kill = () => w.cancelAnimationFrame(id);
|
|
177
|
+
return sig.addEventListener("abort", kill, { once: true }), id;
|
|
178
|
+
}
|
|
179
|
+
function limited(FN_KEY, fn, opts = {}) {
|
|
180
|
+
let count = 0, { key, maxTimes: max = 1 } = isStr(opts) ? { key: opts } : opts;
|
|
181
|
+
const getReg = () => JSON.parse(localStorage.getItem(FN_KEY) || "{}"), setReg = (r) => localStorage.setItem(FN_KEY, JSON.stringify(r));
|
|
182
|
+
const handle = (...args) => {
|
|
183
|
+
if (!key) return count++ < max ? fn(...args) : void 0;
|
|
184
|
+
const r = getReg(), c = r[key] || 0;
|
|
185
|
+
return c < max ? (r[key] = c + 1, setReg(r), fn(...args)) : void 0;
|
|
186
|
+
};
|
|
187
|
+
handle.left = max - (handle.count = count);
|
|
188
|
+
handle.reset = () => (count = 0, key && ((r) => (delete r[key], setReg(r)))(getReg()));
|
|
189
|
+
handle.block = () => (count = max, key && ((r) => (r[key] = max, setReg(r)))(getReg()));
|
|
190
|
+
return handle;
|
|
191
|
+
}
|
|
192
|
+
var mockAsync = (timeout = 250) => new Promise((resolve) => setTimeout2(resolve, timeout));
|
|
193
|
+
var breath = (w = window) => new Promise((res) => w.requestAnimationFrame(res));
|
|
194
|
+
var deepBreath = (w = window) => new Promise((res) => w.requestAnimationFrame(() => w.requestAnimationFrame(res)));
|
|
195
|
+
function bindCleanupToSignal(cleanup, signal) {
|
|
196
|
+
signal?.aborted ? cleanup() : signal?.addEventListener("abort", cleanup, { once: true });
|
|
197
|
+
if (signal && !signal.aborted) cleanup = (() => (signal.removeEventListener("abort", cleanup), cleanup()));
|
|
198
|
+
return cleanup;
|
|
199
|
+
}
|
|
200
|
+
|
|
150
201
|
// src/mixins/methodist.ts
|
|
151
202
|
function onAllMethods(owner, callback, skipNestedOwn = true, nested = false) {
|
|
152
203
|
let proto = owner;
|
|
@@ -284,8 +335,11 @@ if (isDef(window)) {
|
|
|
284
335
|
VIRTUAL_RESOURCE,
|
|
285
336
|
assignEl,
|
|
286
337
|
bindAllMethods,
|
|
338
|
+
bindCleanupToSignal,
|
|
339
|
+
breath,
|
|
287
340
|
clamp,
|
|
288
341
|
createEl,
|
|
342
|
+
deepBreath,
|
|
289
343
|
guardAllMethods,
|
|
290
344
|
guardMethod,
|
|
291
345
|
inBoolArrOpt,
|
|
@@ -302,8 +356,13 @@ if (isDef(window)) {
|
|
|
302
356
|
isSameURL,
|
|
303
357
|
isStr,
|
|
304
358
|
isSym,
|
|
359
|
+
limited,
|
|
305
360
|
loadResource,
|
|
361
|
+
mockAsync,
|
|
306
362
|
onAllMethods,
|
|
307
363
|
removeScrollAssist,
|
|
364
|
+
requestAnimationFrame,
|
|
365
|
+
setInterval,
|
|
366
|
+
setTimeout,
|
|
308
367
|
uid
|
|
309
368
|
});
|
package/dist/index.d.cts
CHANGED
|
@@ -67,6 +67,26 @@ declare function clamp(min: number | undefined, val: number, max?: number): numb
|
|
|
67
67
|
declare function uid(prefix?: string): string;
|
|
68
68
|
declare function isSameURL(src1: unknown, src2: unknown): boolean;
|
|
69
69
|
|
|
70
|
+
declare function setTimeout(handler: TimerHandler, timeout?: number, ...args: any[]): number;
|
|
71
|
+
declare function setInterval(handler: TimerHandler, timeout?: number, ...args: any[]): number;
|
|
72
|
+
declare function requestAnimationFrame(callback: FrameRequestCallback, sig?: AbortSignal, w?: Window & typeof globalThis): number;
|
|
73
|
+
interface LimitedOptions {
|
|
74
|
+
key?: string /** Key for localStorage persistence (if omitted, uses session-only) */;
|
|
75
|
+
maxTimes?: number /** Max times to call (default: 1) */;
|
|
76
|
+
}
|
|
77
|
+
interface LimitedHandle<T extends (...args: any[]) => any> {
|
|
78
|
+
(...args: Parameters<T>): ReturnType<T> | void;
|
|
79
|
+
count: number;
|
|
80
|
+
left: number;
|
|
81
|
+
reset: () => void;
|
|
82
|
+
block: () => void;
|
|
83
|
+
}
|
|
84
|
+
declare function limited<T extends (...args: any[]) => any>(FN_KEY: string, fn: T, opts?: LimitedOptions | string): LimitedHandle<T>;
|
|
85
|
+
declare const mockAsync: (timeout?: number) => Promise<void>;
|
|
86
|
+
declare const breath: (w?: Window & typeof globalThis) => Promise<unknown>;
|
|
87
|
+
declare const deepBreath: (w?: Window & typeof globalThis) => Promise<unknown>;
|
|
88
|
+
declare function bindCleanupToSignal<Cb extends () => any>(cleanup: Cb, signal?: AbortSignal): Cb;
|
|
89
|
+
|
|
70
90
|
type Dataset = Record<string, string | number>;
|
|
71
91
|
type Style = Partial<CSSStyleDeclaration>;
|
|
72
92
|
type ResourceType = "style" | "script" | string;
|
|
@@ -93,4 +113,4 @@ declare function bindAllMethods(owner: any): void;
|
|
|
93
113
|
declare function guardAllMethods(owner: any, guardFn?: (fn: Function) => Function, bound?: boolean): void;
|
|
94
114
|
declare function guardMethod<T extends Function>(fn: T, onError?: (e: any) => void): T;
|
|
95
115
|
|
|
96
|
-
export { type Dataset, type LoadResourceOptions, type ResourceType, type ScrollAssistControl, type Style, VIRTUAL_RESOURCE, assignEl, bindAllMethods, clamp, createEl, guardAllMethods, guardMethod, inBoolArrOpt, initScrollAssist, initVScrollerator, isArr, isBool, isDef, isFunc, isIter, isNum, isObj, isPOJO, isSameURL, isStr, isSym, loadResource, onAllMethods, removeScrollAssist, uid };
|
|
116
|
+
export { type Dataset, type LoadResourceOptions, type ResourceType, type ScrollAssistControl, type Style, VIRTUAL_RESOURCE, assignEl, bindAllMethods, bindCleanupToSignal, breath, clamp, createEl, deepBreath, guardAllMethods, guardMethod, inBoolArrOpt, initScrollAssist, initVScrollerator, isArr, isBool, isDef, isFunc, isIter, isNum, isObj, isPOJO, isSameURL, isStr, isSym, limited, loadResource, mockAsync, onAllMethods, removeScrollAssist, requestAnimationFrame, setInterval, setTimeout, uid };
|
package/dist/index.d.ts
CHANGED
|
@@ -67,6 +67,26 @@ declare function clamp(min: number | undefined, val: number, max?: number): numb
|
|
|
67
67
|
declare function uid(prefix?: string): string;
|
|
68
68
|
declare function isSameURL(src1: unknown, src2: unknown): boolean;
|
|
69
69
|
|
|
70
|
+
declare function setTimeout(handler: TimerHandler, timeout?: number, ...args: any[]): number;
|
|
71
|
+
declare function setInterval(handler: TimerHandler, timeout?: number, ...args: any[]): number;
|
|
72
|
+
declare function requestAnimationFrame(callback: FrameRequestCallback, sig?: AbortSignal, w?: Window & typeof globalThis): number;
|
|
73
|
+
interface LimitedOptions {
|
|
74
|
+
key?: string /** Key for localStorage persistence (if omitted, uses session-only) */;
|
|
75
|
+
maxTimes?: number /** Max times to call (default: 1) */;
|
|
76
|
+
}
|
|
77
|
+
interface LimitedHandle<T extends (...args: any[]) => any> {
|
|
78
|
+
(...args: Parameters<T>): ReturnType<T> | void;
|
|
79
|
+
count: number;
|
|
80
|
+
left: number;
|
|
81
|
+
reset: () => void;
|
|
82
|
+
block: () => void;
|
|
83
|
+
}
|
|
84
|
+
declare function limited<T extends (...args: any[]) => any>(FN_KEY: string, fn: T, opts?: LimitedOptions | string): LimitedHandle<T>;
|
|
85
|
+
declare const mockAsync: (timeout?: number) => Promise<void>;
|
|
86
|
+
declare const breath: (w?: Window & typeof globalThis) => Promise<unknown>;
|
|
87
|
+
declare const deepBreath: (w?: Window & typeof globalThis) => Promise<unknown>;
|
|
88
|
+
declare function bindCleanupToSignal<Cb extends () => any>(cleanup: Cb, signal?: AbortSignal): Cb;
|
|
89
|
+
|
|
70
90
|
type Dataset = Record<string, string | number>;
|
|
71
91
|
type Style = Partial<CSSStyleDeclaration>;
|
|
72
92
|
type ResourceType = "style" | "script" | string;
|
|
@@ -93,4 +113,4 @@ declare function bindAllMethods(owner: any): void;
|
|
|
93
113
|
declare function guardAllMethods(owner: any, guardFn?: (fn: Function) => Function, bound?: boolean): void;
|
|
94
114
|
declare function guardMethod<T extends Function>(fn: T, onError?: (e: any) => void): T;
|
|
95
115
|
|
|
96
|
-
export { type Dataset, type LoadResourceOptions, type ResourceType, type ScrollAssistControl, type Style, VIRTUAL_RESOURCE, assignEl, bindAllMethods, clamp, createEl, guardAllMethods, guardMethod, inBoolArrOpt, initScrollAssist, initVScrollerator, isArr, isBool, isDef, isFunc, isIter, isNum, isObj, isPOJO, isSameURL, isStr, isSym, loadResource, onAllMethods, removeScrollAssist, uid };
|
|
116
|
+
export { type Dataset, type LoadResourceOptions, type ResourceType, type ScrollAssistControl, type Style, VIRTUAL_RESOURCE, assignEl, bindAllMethods, bindCleanupToSignal, breath, clamp, createEl, deepBreath, guardAllMethods, guardMethod, inBoolArrOpt, initScrollAssist, initVScrollerator, isArr, isBool, isDef, isFunc, isIter, isNum, isObj, isPOJO, isSameURL, isStr, isSym, limited, loadResource, mockAsync, onAllMethods, removeScrollAssist, requestAnimationFrame, setInterval, setTimeout, uid };
|
package/dist/index.js
CHANGED
|
@@ -97,6 +97,49 @@ function isSameURL(src1, src2) {
|
|
|
97
97
|
}
|
|
98
98
|
}
|
|
99
99
|
|
|
100
|
+
// src/core/fn.ts
|
|
101
|
+
function setTimeout2(handler, timeout, ...args) {
|
|
102
|
+
const sig = args[0] instanceof AbortSignal ? args.shift() : void 0;
|
|
103
|
+
if (sig?.aborted) return -1;
|
|
104
|
+
const w = args[0] instanceof Window ? args.shift() : window;
|
|
105
|
+
if (!sig) return w.setTimeout(handler, timeout, ...args);
|
|
106
|
+
const id = w.setTimeout(() => (sig.removeEventListener("abort", kill), isStr(handler) ? new Function(handler) : handler(...args)), timeout), kill = () => w.clearTimeout(id);
|
|
107
|
+
return sig.addEventListener("abort", kill, { once: true }), id;
|
|
108
|
+
}
|
|
109
|
+
function setInterval(handler, timeout, ...args) {
|
|
110
|
+
const sig = args[0] instanceof AbortSignal ? args.shift() : void 0;
|
|
111
|
+
if (sig?.aborted) return -1;
|
|
112
|
+
const w = args[0] instanceof Window ? args.shift() : window, id = w.setInterval(handler, timeout, ...args);
|
|
113
|
+
return sig?.addEventListener("abort", () => w.clearInterval(id), { once: true }), id;
|
|
114
|
+
}
|
|
115
|
+
function requestAnimationFrame2(callback, sig, w = window) {
|
|
116
|
+
if (sig?.aborted) return -1;
|
|
117
|
+
if (!sig) return w.requestAnimationFrame(callback);
|
|
118
|
+
const id = w.requestAnimationFrame((t) => (sig.removeEventListener("abort", kill), callback(t))), kill = () => w.cancelAnimationFrame(id);
|
|
119
|
+
return sig.addEventListener("abort", kill, { once: true }), id;
|
|
120
|
+
}
|
|
121
|
+
function limited(FN_KEY, fn, opts = {}) {
|
|
122
|
+
let count = 0, { key, maxTimes: max = 1 } = isStr(opts) ? { key: opts } : opts;
|
|
123
|
+
const getReg = () => JSON.parse(localStorage.getItem(FN_KEY) || "{}"), setReg = (r) => localStorage.setItem(FN_KEY, JSON.stringify(r));
|
|
124
|
+
const handle = (...args) => {
|
|
125
|
+
if (!key) return count++ < max ? fn(...args) : void 0;
|
|
126
|
+
const r = getReg(), c = r[key] || 0;
|
|
127
|
+
return c < max ? (r[key] = c + 1, setReg(r), fn(...args)) : void 0;
|
|
128
|
+
};
|
|
129
|
+
handle.left = max - (handle.count = count);
|
|
130
|
+
handle.reset = () => (count = 0, key && ((r) => (delete r[key], setReg(r)))(getReg()));
|
|
131
|
+
handle.block = () => (count = max, key && ((r) => (r[key] = max, setReg(r)))(getReg()));
|
|
132
|
+
return handle;
|
|
133
|
+
}
|
|
134
|
+
var mockAsync = (timeout = 250) => new Promise((resolve) => setTimeout2(resolve, timeout));
|
|
135
|
+
var breath = (w = window) => new Promise((res) => w.requestAnimationFrame(res));
|
|
136
|
+
var deepBreath = (w = window) => new Promise((res) => w.requestAnimationFrame(() => w.requestAnimationFrame(res)));
|
|
137
|
+
function bindCleanupToSignal(cleanup, signal) {
|
|
138
|
+
signal?.aborted ? cleanup() : signal?.addEventListener("abort", cleanup, { once: true });
|
|
139
|
+
if (signal && !signal.aborted) cleanup = (() => (signal.removeEventListener("abort", cleanup), cleanup()));
|
|
140
|
+
return cleanup;
|
|
141
|
+
}
|
|
142
|
+
|
|
100
143
|
// src/mixins/methodist.ts
|
|
101
144
|
function onAllMethods(owner, callback, skipNestedOwn = true, nested = false) {
|
|
102
145
|
let proto = owner;
|
|
@@ -233,8 +276,11 @@ export {
|
|
|
233
276
|
VIRTUAL_RESOURCE,
|
|
234
277
|
assignEl,
|
|
235
278
|
bindAllMethods,
|
|
279
|
+
bindCleanupToSignal,
|
|
280
|
+
breath,
|
|
236
281
|
clamp,
|
|
237
282
|
createEl,
|
|
283
|
+
deepBreath,
|
|
238
284
|
guardAllMethods,
|
|
239
285
|
guardMethod,
|
|
240
286
|
inBoolArrOpt,
|
|
@@ -251,8 +297,13 @@ export {
|
|
|
251
297
|
isSameURL,
|
|
252
298
|
isStr,
|
|
253
299
|
isSym,
|
|
300
|
+
limited,
|
|
254
301
|
loadResource,
|
|
302
|
+
mockAsync,
|
|
255
303
|
onAllMethods,
|
|
256
304
|
removeScrollAssist,
|
|
305
|
+
requestAnimationFrame2 as requestAnimationFrame,
|
|
306
|
+
setInterval,
|
|
307
|
+
setTimeout2 as setTimeout,
|
|
257
308
|
uid
|
|
258
309
|
};
|
package/package.json
CHANGED