-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.js
More file actions
306 lines (257 loc) · 9.56 KB
/
Copy pathutils.js
File metadata and controls
306 lines (257 loc) · 9.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
const ExecutionDelay = class {
#callback;
#delay;
#isThrottle;
#context;
#timeoutId;
#promise;
#resolve;
#reject;
#fulfilled;
#isTimeout = false;
/**
* @param {function} callback - The function to be executed with a delay.
* @param {Object} [options={}] - Options as an object for setting parameters.
* @param {number} [options.delay=1000] - The delay time in milliseconds (default: 1000ms).
* @param {Object | null} [options.context=null] - The context in which the function will be executed (default: null).
* @param {boolean} [options.startNow=false] - Initiates execution immediately upon initialization (default: false).
* @param {boolean} [options.executeNow=false] - Executes the function immediately upon initialization (default: false).
* @param {boolean} [options.isThrottle=false] - Sets whether function calls are throttled (default: false).
* @param {...any} args - Additional arguments to be passed to the function.
*/
constructor(callback, {
delay = 1000,
context = null,
startNow = false,
executeNow = false,
isThrottle = false,
leading = false
} = {}, ...args) {
this.delay = delay;
this.setContext(context);
this.isThrottle = isThrottle;
this.leading = leading;
if (typeof callback == 'function') {
this.setFunction(callback, ...args);
if (executeNow) this.execute();
if (startNow) this.start();
}
}
#arguments = [];
get #args() { return this.#arguments.length > 0 ? this.#arguments : this.#savedArgs; };
set #args(value) { this.#arguments = value; };
#savedArgs = [];
saveArguments = (...args) => {
this.#savedArgs = args;
return { start: this.start, execute: this.execute };
}
nextStart = async (...args) => {
if (this.#promise) {
return new Promise(async (resolve, reject) => {
this.#promise
.then(() => resolve(this.start(...args)))
.catch(reject);
});
}
return this.start(...args);
}
get delay() { return this.#delay; }
set delay(value) {
if (typeof value !== 'number') { throw new TypeError(`The '${value}' is not 'number'`); }
this.#delay = value;
}
get isThrottle() { return this.#isThrottle; }
set isThrottle(value) {
if (typeof value !== 'boolean') { throw new TypeError(`The '${value}' is not 'boolean'`); }
if (value == false) { this.#args = undefined; }
this.#isThrottle = value;
}
get isStarted() { return this.#isTimeout; }
getFunction = () => {
return {
function: this.#callback,
arguments: this.#arguments,
context: this.#context,
savedArgs: this.#savedArgs
}
}
setFunction = (callback, ...args) => {
if (typeof callback != 'function') { throw new TypeError(`The '${callback}' is not a function.`); }
this.#callback = callback;
this.#savedArgs = args;
return { start: this.start, execute: this.execute, setContext: this.setContext }
}
clearArguments = () => { this.#savedArgs = []; }
getContext = () => { return this.#context; }
setContext = (context) => {
if (typeof context != 'object' && context != null) {
throw new TypeError(`The context is '${typeof context}', must be 'object or null.`);
}
this.#context = context;
return { start: this.start, execute: this.execute }
}
#apply = (args = this.#args) => {
let isError = false;
let result;
try {
result = this.#callback.apply(this.#context, args);
} catch (error) {
isError = true;
result = error;
}
isError ? this.#reject?.(result) : this.#resolve?.(result);
return result;
}
#timeout = () => {
this.#isTimeout = false;
if (typeof this.#resolve !== 'function') return;
this.#apply();
this.#resolve = null;
this.#reject = null;
this.#fulfilled = null;
this.#promise = null;
if (this.#isThrottle) this.#createTimeout();
}
#createTimeout = () => {
clearTimeout(this.#timeoutId);
this.#isTimeout = true;
this.#timeoutId = setTimeout(this.#timeout, this.#delay);
}
#createPromise = (needTimeout = false) => {
this.#promise = new Promise((resolve, reject) => {
// this.#fulfilled - function for set promise state to fulfilled with stop().
this.#fulfilled = (message) => { resolve({ causeStops: message }); }
this.#resolve = resolve;
this.#reject = reject;
if (needTimeout) this.#createTimeout();
});
return this.#promise;
}
/**
* Initiates function execution after the specified delay.
* @param {...any} args - Optional arguments to be passed to the function.
* @returns {Promise<Object>} - A promise indicating the completion or an active timer.
*/
start = (...args) => {
if (typeof this.#callback != 'function') { throw new Error('The function is missing.'); }
this.#args = args;
if (this.#isThrottle === true) {
if (this.#isTimeout && this.#promise) return this.#promise;
if (!this.#isTimeout && !this.#promise && this.leading) {
this.#createTimeout();
return Promise.resolve(this.#apply());
}
if (this.#isTimeout && this.#promise === null) {
return this.#createPromise(false);
}
}
if (this.#isThrottle === false) {
if (!this.leading && this.#resolve) {
this.#createTimeout(this.#resolve);
return this.#promise;
} else if (this.leading && !this.#resolve && !this.#isTimeout) {
this.#createTimeout();
return Promise.resolve(this.#apply());
}
}
return this.#createPromise(true);
}
/**
* Executes the function immediately without waiting for the delay.
* @param {...any} args - Optional arguments to be passed to the function.
* @returns {any} - The result of the executed function.
*/
execute = (...args) => {
this.stop("Execute now!");
if (typeof this.#callback != 'function') { throw new Error('The function is missing.'); }
if (args.length > 0) {
return this.#apply(args);
}
return this.#apply(this.#savedArgs);
}
/**
* Stops the execution of the function.
* @param {string} cause - The cause for stopping the execution.
*/
stop = (cause = "Forecd stopp.") => {
clearTimeout(this.#timeoutId);
this.#isTimeout = false;
this.#promise = null;
this.#resolve = null;
this.#reject = null;
if (typeof this.#fulfilled === 'function') {
this.#fulfilled(cause);
this.#fulfilled = undefined;
}
}
}
const CustomEvents = class {
events = new Map();
#listMicroTask = new Set();
#promise = { value: undefined, resolve: undefined }
#isMicroTask = false;
#microTask = () => {
try {
this.#listMicroTask.forEach(type => {
this.events.get(type)?.forEach(listener => listener());
});
} catch (error) {
console.warn(error);
} finally {
this.#isMicroTask = false;
this.#listMicroTask.clear();
this.#promise.resolve();
}
}
on = (type, listener) => {
if (this.events.get(type) === undefined) {
this.events.set(type, new Set());
this.events.get(type).add(listener);
}
this.events.get(type).add(listener);
}
off = (type, listener) => {
if (this.events.size === 1) {
this.events.clear();
return;
}
this.events.get(type)?.delete(listener);
}
/** the event will be execute on microtask */
execute = async (type) => {
if (this.#listMicroTask.has(type)) this.#listMicroTask.delete(type);
this.#listMicroTask.add(type);
if (this.#isMicroTask) return this.#promise;
this.#isMicroTask = true;
queueMicrotask(this.#microTask);
this.#promise.value = new Promise(resolve => this.#promise.resolve = resolve);
return this.#promise.value;
}
has = (type) => { return this.events.has(type); }
}
class MethodInterceptor {
constructor(targetObjects, interceptor) {
const interceptedObjects = {};
for (const objectName in targetObjects) {
interceptedObjects[objectName] = this.#wrapMethods(targetObjects[objectName], interceptor);
}
return interceptedObjects;
}
#wrapMethods(targetObject, interceptor) {
const methodCache = new Map();
return new Proxy(targetObject, {
get(originalObject, property) {
const method = originalObject[property];
if (typeof method !== "function") return method;
if (methodCache.has(property)) return methodCache.get(property);
const wrappedMethod = (...args) => {
return interceptor.apply(originalObject, [method, ...args]);
};
methodCache.set(property, wrappedMethod);
return wrappedMethod;
}
});
}
}
const customEvents = new CustomEvents();
export { ExecutionDelay, MethodInterceptor, customEvents };