@etsoo/shared 1.2.45 → 1.2.46
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -0
- package/lib/cjs/ExtendUtils.d.ts +8 -1
- package/lib/cjs/ExtendUtils.js +99 -23
- package/lib/mjs/ExtendUtils.d.ts +8 -1
- package/lib/mjs/ExtendUtils.js +99 -23
- package/package.json +1 -1
- package/src/ExtendUtils.ts +104 -27
package/README.md
CHANGED
|
@@ -241,8 +241,10 @@ Extend current class/object functioning
|
|
|
241
241
|
|---:|---|
|
|
242
242
|
|applyMixins|Apply mixins to current class|
|
|
243
243
|
|delayedExecutor|Create delayed executor|
|
|
244
|
+
|intervalFor|Repeat interval for callback|
|
|
244
245
|
|promiseHandler|Promise handler to catch error|
|
|
245
246
|
|sleep|Delay promise|
|
|
247
|
+
|waitFor|Wait for condition meets and execute callback|
|
|
246
248
|
|
|
247
249
|
## NumberUtils
|
|
248
250
|
Numbers related utilities
|
package/lib/cjs/ExtendUtils.d.ts
CHANGED
|
@@ -31,8 +31,15 @@ export declare namespace ExtendUtils {
|
|
|
31
31
|
* Wait for condition meets and execute callback
|
|
32
32
|
* requestAnimationFrame to replace setTimeout
|
|
33
33
|
* @param callback Callback
|
|
34
|
-
* @param checkReady Check ready, when it's a number, similar to setTimeout
|
|
34
|
+
* @param checkReady Check ready, when it's a number as miliseconds, similar to setTimeout
|
|
35
35
|
* @returns cancel callback
|
|
36
36
|
*/
|
|
37
37
|
function waitFor(callback: () => void, checkReady: ((spanTime: number) => boolean) | number): () => void;
|
|
38
|
+
/**
|
|
39
|
+
* Repeat interval for callback
|
|
40
|
+
* @param callback Callback
|
|
41
|
+
* @param miliseconds Miliseconds
|
|
42
|
+
* @returns cancel callback
|
|
43
|
+
*/
|
|
44
|
+
function intervalFor(callback: () => void, miliseconds: number): () => void;
|
|
38
45
|
}
|
package/lib/cjs/ExtendUtils.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.ExtendUtils = void 0;
|
|
4
|
+
const hasRequestAnimationFrame = typeof requestAnimationFrame === 'function';
|
|
4
5
|
/**
|
|
5
6
|
* Extend utilities
|
|
6
7
|
*/
|
|
@@ -85,38 +86,113 @@ var ExtendUtils;
|
|
|
85
86
|
* Wait for condition meets and execute callback
|
|
86
87
|
* requestAnimationFrame to replace setTimeout
|
|
87
88
|
* @param callback Callback
|
|
88
|
-
* @param checkReady Check ready, when it's a number, similar to setTimeout
|
|
89
|
+
* @param checkReady Check ready, when it's a number as miliseconds, similar to setTimeout
|
|
89
90
|
* @returns cancel callback
|
|
90
91
|
*/
|
|
91
92
|
function waitFor(callback, checkReady) {
|
|
92
|
-
let startTime;
|
|
93
93
|
let requestID;
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
94
|
+
if (hasRequestAnimationFrame) {
|
|
95
|
+
let lastTime;
|
|
96
|
+
function loop(timestamp) {
|
|
97
|
+
// Cancelled
|
|
98
|
+
if (requestID == null)
|
|
99
|
+
return;
|
|
100
|
+
if (lastTime === undefined) {
|
|
101
|
+
lastTime = timestamp;
|
|
102
|
+
}
|
|
103
|
+
const elapsed = timestamp - lastTime;
|
|
104
|
+
const isReady = typeof checkReady === 'number'
|
|
105
|
+
? elapsed >= checkReady
|
|
106
|
+
: checkReady(elapsed);
|
|
107
|
+
if (isReady) {
|
|
108
|
+
callback();
|
|
109
|
+
}
|
|
110
|
+
else if (requestID != null) {
|
|
111
|
+
// May also be cancelled in callback or somewhere
|
|
112
|
+
requestID = requestAnimationFrame(loop);
|
|
113
|
+
}
|
|
109
114
|
}
|
|
110
|
-
|
|
111
|
-
|
|
115
|
+
requestID = requestAnimationFrame(loop);
|
|
116
|
+
}
|
|
117
|
+
else if (typeof checkReady === 'number') {
|
|
118
|
+
requestID = setTimeout(callback, checkReady);
|
|
119
|
+
}
|
|
120
|
+
else {
|
|
121
|
+
// Bad practice to use setTimeout in this way, only for compatibility
|
|
122
|
+
const ms = 20;
|
|
123
|
+
let spanTime = 0;
|
|
124
|
+
let cr = checkReady;
|
|
125
|
+
function loop() {
|
|
126
|
+
// Cancelled
|
|
127
|
+
if (requestID == null)
|
|
128
|
+
return;
|
|
129
|
+
spanTime += ms;
|
|
130
|
+
if (cr(spanTime)) {
|
|
131
|
+
callback();
|
|
132
|
+
}
|
|
133
|
+
else if (requestID != null) {
|
|
134
|
+
// May also be cancelled in callback or somewhere
|
|
135
|
+
requestID = setTimeout(loop, ms);
|
|
136
|
+
}
|
|
112
137
|
}
|
|
138
|
+
requestID = setTimeout(loop, ms);
|
|
113
139
|
}
|
|
114
|
-
doWait();
|
|
115
140
|
return () => {
|
|
116
|
-
if (requestID)
|
|
117
|
-
|
|
118
|
-
|
|
141
|
+
if (requestID) {
|
|
142
|
+
if (hasRequestAnimationFrame && typeof requestID === 'number') {
|
|
143
|
+
cancelAnimationFrame(requestID);
|
|
144
|
+
}
|
|
145
|
+
else {
|
|
146
|
+
clearTimeout(requestID);
|
|
147
|
+
}
|
|
148
|
+
requestID = undefined;
|
|
149
|
+
}
|
|
119
150
|
};
|
|
120
151
|
}
|
|
121
152
|
ExtendUtils.waitFor = waitFor;
|
|
153
|
+
/**
|
|
154
|
+
* Repeat interval for callback
|
|
155
|
+
* @param callback Callback
|
|
156
|
+
* @param miliseconds Miliseconds
|
|
157
|
+
* @returns cancel callback
|
|
158
|
+
*/
|
|
159
|
+
function intervalFor(callback, miliseconds) {
|
|
160
|
+
let requestID;
|
|
161
|
+
if (hasRequestAnimationFrame) {
|
|
162
|
+
let lastTime;
|
|
163
|
+
function loop(timestamp) {
|
|
164
|
+
// Cancelled
|
|
165
|
+
if (requestID == null)
|
|
166
|
+
return;
|
|
167
|
+
if (lastTime === undefined) {
|
|
168
|
+
lastTime = timestamp;
|
|
169
|
+
}
|
|
170
|
+
const elapsed = timestamp - lastTime;
|
|
171
|
+
if (elapsed >= miliseconds) {
|
|
172
|
+
lastTime = timestamp;
|
|
173
|
+
callback();
|
|
174
|
+
}
|
|
175
|
+
if (requestID != null) {
|
|
176
|
+
// May also be cancelled in callback or somewhere
|
|
177
|
+
requestID = requestAnimationFrame(loop);
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
requestID = requestAnimationFrame(loop);
|
|
181
|
+
}
|
|
182
|
+
else {
|
|
183
|
+
requestID = setInterval(callback, miliseconds);
|
|
184
|
+
}
|
|
185
|
+
return () => {
|
|
186
|
+
if (requestID) {
|
|
187
|
+
if (hasRequestAnimationFrame && typeof requestID === 'number') {
|
|
188
|
+
cancelAnimationFrame(requestID);
|
|
189
|
+
}
|
|
190
|
+
else {
|
|
191
|
+
clearInterval(requestID);
|
|
192
|
+
}
|
|
193
|
+
requestID = undefined;
|
|
194
|
+
}
|
|
195
|
+
};
|
|
196
|
+
}
|
|
197
|
+
ExtendUtils.intervalFor = intervalFor;
|
|
122
198
|
})(ExtendUtils || (exports.ExtendUtils = ExtendUtils = {}));
|
package/lib/mjs/ExtendUtils.d.ts
CHANGED
|
@@ -31,8 +31,15 @@ export declare namespace ExtendUtils {
|
|
|
31
31
|
* Wait for condition meets and execute callback
|
|
32
32
|
* requestAnimationFrame to replace setTimeout
|
|
33
33
|
* @param callback Callback
|
|
34
|
-
* @param checkReady Check ready, when it's a number, similar to setTimeout
|
|
34
|
+
* @param checkReady Check ready, when it's a number as miliseconds, similar to setTimeout
|
|
35
35
|
* @returns cancel callback
|
|
36
36
|
*/
|
|
37
37
|
function waitFor(callback: () => void, checkReady: ((spanTime: number) => boolean) | number): () => void;
|
|
38
|
+
/**
|
|
39
|
+
* Repeat interval for callback
|
|
40
|
+
* @param callback Callback
|
|
41
|
+
* @param miliseconds Miliseconds
|
|
42
|
+
* @returns cancel callback
|
|
43
|
+
*/
|
|
44
|
+
function intervalFor(callback: () => void, miliseconds: number): () => void;
|
|
38
45
|
}
|
package/lib/mjs/ExtendUtils.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
const hasRequestAnimationFrame = typeof requestAnimationFrame === 'function';
|
|
1
2
|
/**
|
|
2
3
|
* Extend utilities
|
|
3
4
|
*/
|
|
@@ -82,38 +83,113 @@ export var ExtendUtils;
|
|
|
82
83
|
* Wait for condition meets and execute callback
|
|
83
84
|
* requestAnimationFrame to replace setTimeout
|
|
84
85
|
* @param callback Callback
|
|
85
|
-
* @param checkReady Check ready, when it's a number, similar to setTimeout
|
|
86
|
+
* @param checkReady Check ready, when it's a number as miliseconds, similar to setTimeout
|
|
86
87
|
* @returns cancel callback
|
|
87
88
|
*/
|
|
88
89
|
function waitFor(callback, checkReady) {
|
|
89
|
-
let startTime;
|
|
90
90
|
let requestID;
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
91
|
+
if (hasRequestAnimationFrame) {
|
|
92
|
+
let lastTime;
|
|
93
|
+
function loop(timestamp) {
|
|
94
|
+
// Cancelled
|
|
95
|
+
if (requestID == null)
|
|
96
|
+
return;
|
|
97
|
+
if (lastTime === undefined) {
|
|
98
|
+
lastTime = timestamp;
|
|
99
|
+
}
|
|
100
|
+
const elapsed = timestamp - lastTime;
|
|
101
|
+
const isReady = typeof checkReady === 'number'
|
|
102
|
+
? elapsed >= checkReady
|
|
103
|
+
: checkReady(elapsed);
|
|
104
|
+
if (isReady) {
|
|
105
|
+
callback();
|
|
106
|
+
}
|
|
107
|
+
else if (requestID != null) {
|
|
108
|
+
// May also be cancelled in callback or somewhere
|
|
109
|
+
requestID = requestAnimationFrame(loop);
|
|
110
|
+
}
|
|
106
111
|
}
|
|
107
|
-
|
|
108
|
-
|
|
112
|
+
requestID = requestAnimationFrame(loop);
|
|
113
|
+
}
|
|
114
|
+
else if (typeof checkReady === 'number') {
|
|
115
|
+
requestID = setTimeout(callback, checkReady);
|
|
116
|
+
}
|
|
117
|
+
else {
|
|
118
|
+
// Bad practice to use setTimeout in this way, only for compatibility
|
|
119
|
+
const ms = 20;
|
|
120
|
+
let spanTime = 0;
|
|
121
|
+
let cr = checkReady;
|
|
122
|
+
function loop() {
|
|
123
|
+
// Cancelled
|
|
124
|
+
if (requestID == null)
|
|
125
|
+
return;
|
|
126
|
+
spanTime += ms;
|
|
127
|
+
if (cr(spanTime)) {
|
|
128
|
+
callback();
|
|
129
|
+
}
|
|
130
|
+
else if (requestID != null) {
|
|
131
|
+
// May also be cancelled in callback or somewhere
|
|
132
|
+
requestID = setTimeout(loop, ms);
|
|
133
|
+
}
|
|
109
134
|
}
|
|
135
|
+
requestID = setTimeout(loop, ms);
|
|
110
136
|
}
|
|
111
|
-
doWait();
|
|
112
137
|
return () => {
|
|
113
|
-
if (requestID)
|
|
114
|
-
|
|
115
|
-
|
|
138
|
+
if (requestID) {
|
|
139
|
+
if (hasRequestAnimationFrame && typeof requestID === 'number') {
|
|
140
|
+
cancelAnimationFrame(requestID);
|
|
141
|
+
}
|
|
142
|
+
else {
|
|
143
|
+
clearTimeout(requestID);
|
|
144
|
+
}
|
|
145
|
+
requestID = undefined;
|
|
146
|
+
}
|
|
116
147
|
};
|
|
117
148
|
}
|
|
118
149
|
ExtendUtils.waitFor = waitFor;
|
|
150
|
+
/**
|
|
151
|
+
* Repeat interval for callback
|
|
152
|
+
* @param callback Callback
|
|
153
|
+
* @param miliseconds Miliseconds
|
|
154
|
+
* @returns cancel callback
|
|
155
|
+
*/
|
|
156
|
+
function intervalFor(callback, miliseconds) {
|
|
157
|
+
let requestID;
|
|
158
|
+
if (hasRequestAnimationFrame) {
|
|
159
|
+
let lastTime;
|
|
160
|
+
function loop(timestamp) {
|
|
161
|
+
// Cancelled
|
|
162
|
+
if (requestID == null)
|
|
163
|
+
return;
|
|
164
|
+
if (lastTime === undefined) {
|
|
165
|
+
lastTime = timestamp;
|
|
166
|
+
}
|
|
167
|
+
const elapsed = timestamp - lastTime;
|
|
168
|
+
if (elapsed >= miliseconds) {
|
|
169
|
+
lastTime = timestamp;
|
|
170
|
+
callback();
|
|
171
|
+
}
|
|
172
|
+
if (requestID != null) {
|
|
173
|
+
// May also be cancelled in callback or somewhere
|
|
174
|
+
requestID = requestAnimationFrame(loop);
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
requestID = requestAnimationFrame(loop);
|
|
178
|
+
}
|
|
179
|
+
else {
|
|
180
|
+
requestID = setInterval(callback, miliseconds);
|
|
181
|
+
}
|
|
182
|
+
return () => {
|
|
183
|
+
if (requestID) {
|
|
184
|
+
if (hasRequestAnimationFrame && typeof requestID === 'number') {
|
|
185
|
+
cancelAnimationFrame(requestID);
|
|
186
|
+
}
|
|
187
|
+
else {
|
|
188
|
+
clearInterval(requestID);
|
|
189
|
+
}
|
|
190
|
+
requestID = undefined;
|
|
191
|
+
}
|
|
192
|
+
};
|
|
193
|
+
}
|
|
194
|
+
ExtendUtils.intervalFor = intervalFor;
|
|
119
195
|
})(ExtendUtils || (ExtendUtils = {}));
|
package/package.json
CHANGED
package/src/ExtendUtils.ts
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import { DelayedExecutorType } from './types/DelayedExecutorType';
|
|
2
2
|
|
|
3
|
+
const hasRequestAnimationFrame = typeof requestAnimationFrame === 'function';
|
|
4
|
+
|
|
3
5
|
/**
|
|
4
6
|
* Extend utilities
|
|
5
7
|
*/
|
|
@@ -89,44 +91,119 @@ export namespace ExtendUtils {
|
|
|
89
91
|
* Wait for condition meets and execute callback
|
|
90
92
|
* requestAnimationFrame to replace setTimeout
|
|
91
93
|
* @param callback Callback
|
|
92
|
-
* @param checkReady Check ready, when it's a number, similar to setTimeout
|
|
94
|
+
* @param checkReady Check ready, when it's a number as miliseconds, similar to setTimeout
|
|
93
95
|
* @returns cancel callback
|
|
94
96
|
*/
|
|
95
97
|
export function waitFor(
|
|
96
98
|
callback: () => void,
|
|
97
99
|
checkReady: ((spanTime: number) => boolean) | number
|
|
98
100
|
) {
|
|
99
|
-
let
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
101
|
+
let requestID: number | undefined | NodeJS.Timeout;
|
|
102
|
+
|
|
103
|
+
if (hasRequestAnimationFrame) {
|
|
104
|
+
let lastTime: number | undefined;
|
|
105
|
+
function loop(timestamp: number) {
|
|
106
|
+
// Cancelled
|
|
107
|
+
if (requestID == null) return;
|
|
108
|
+
|
|
109
|
+
if (lastTime === undefined) {
|
|
110
|
+
lastTime = timestamp;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
const elapsed = timestamp - lastTime;
|
|
114
|
+
|
|
115
|
+
const isReady =
|
|
116
|
+
typeof checkReady === 'number'
|
|
117
|
+
? elapsed >= checkReady
|
|
118
|
+
: checkReady(elapsed);
|
|
119
|
+
|
|
120
|
+
if (isReady) {
|
|
121
|
+
callback();
|
|
122
|
+
} else if (requestID != null) {
|
|
123
|
+
// May also be cancelled in callback or somewhere
|
|
124
|
+
requestID = requestAnimationFrame(loop);
|
|
125
|
+
}
|
|
122
126
|
}
|
|
127
|
+
requestID = requestAnimationFrame(loop);
|
|
128
|
+
} else if (typeof checkReady === 'number') {
|
|
129
|
+
requestID = setTimeout(callback, checkReady);
|
|
130
|
+
} else {
|
|
131
|
+
// Bad practice to use setTimeout in this way, only for compatibility
|
|
132
|
+
const ms = 20;
|
|
133
|
+
let spanTime = 0;
|
|
134
|
+
let cr = checkReady;
|
|
135
|
+
function loop() {
|
|
136
|
+
// Cancelled
|
|
137
|
+
if (requestID == null) return;
|
|
138
|
+
|
|
139
|
+
spanTime += ms;
|
|
140
|
+
|
|
141
|
+
if (cr(spanTime)) {
|
|
142
|
+
callback();
|
|
143
|
+
} else if (requestID != null) {
|
|
144
|
+
// May also be cancelled in callback or somewhere
|
|
145
|
+
requestID = setTimeout(loop, ms);
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
requestID = setTimeout(loop, ms);
|
|
123
149
|
}
|
|
124
150
|
|
|
125
|
-
|
|
151
|
+
return () => {
|
|
152
|
+
if (requestID) {
|
|
153
|
+
if (hasRequestAnimationFrame && typeof requestID === 'number') {
|
|
154
|
+
cancelAnimationFrame(requestID);
|
|
155
|
+
} else {
|
|
156
|
+
clearTimeout(requestID);
|
|
157
|
+
}
|
|
158
|
+
requestID = undefined;
|
|
159
|
+
}
|
|
160
|
+
};
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* Repeat interval for callback
|
|
165
|
+
* @param callback Callback
|
|
166
|
+
* @param miliseconds Miliseconds
|
|
167
|
+
* @returns cancel callback
|
|
168
|
+
*/
|
|
169
|
+
export function intervalFor(callback: () => void, miliseconds: number) {
|
|
170
|
+
let requestID: number | undefined | NodeJS.Timer;
|
|
171
|
+
|
|
172
|
+
if (hasRequestAnimationFrame) {
|
|
173
|
+
let lastTime: number | undefined;
|
|
174
|
+
function loop(timestamp: number) {
|
|
175
|
+
// Cancelled
|
|
176
|
+
if (requestID == null) return;
|
|
177
|
+
|
|
178
|
+
if (lastTime === undefined) {
|
|
179
|
+
lastTime = timestamp;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
const elapsed = timestamp - lastTime;
|
|
183
|
+
if (elapsed >= miliseconds) {
|
|
184
|
+
lastTime = timestamp;
|
|
185
|
+
callback();
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
if (requestID != null) {
|
|
189
|
+
// May also be cancelled in callback or somewhere
|
|
190
|
+
requestID = requestAnimationFrame(loop);
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
requestID = requestAnimationFrame(loop);
|
|
194
|
+
} else {
|
|
195
|
+
requestID = setInterval(callback, miliseconds);
|
|
196
|
+
}
|
|
126
197
|
|
|
127
198
|
return () => {
|
|
128
|
-
if (requestID)
|
|
129
|
-
|
|
199
|
+
if (requestID) {
|
|
200
|
+
if (hasRequestAnimationFrame && typeof requestID === 'number') {
|
|
201
|
+
cancelAnimationFrame(requestID);
|
|
202
|
+
} else {
|
|
203
|
+
clearInterval(requestID);
|
|
204
|
+
}
|
|
205
|
+
requestID = undefined;
|
|
206
|
+
}
|
|
130
207
|
};
|
|
131
208
|
}
|
|
132
209
|
}
|