-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path_common.js
90 lines (75 loc) · 1.6 KB
/
_common.js
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
"use strict";
class ReconnectSys {
constructor(min = 500, max = null, jitter = true) {
this.min = min;
this.max = max != null ? max : min * 10;
this.jitter = jitter;
this._current = min;
this._timeoutId = null;
this._fails = 0;
}
get fails() {
return this._fails;
}
get current() {
return this._current;
}
get pending() {
return this._timeoutId != null;
}
succeed() {
this.cancel();
this._fails = 0;
this._current = this.min;
}
fail(callback) {
this._fails += 1;
let delay = this._current * 2;
if (this.jitter) {
delay *= Math.random();
}
this._current = Math.min(this._current + delay, this.max);
if (callback != null) {
if (this._timeoutId != null) {
throw new Error("callback already pending");
}
this._timeoutId = setTimeout(() => {
try {
if (callback != null) {
callback();
}
} finally {
this._timeoutId = null;
}
}, this._current);
}
return this._current;
}
cancel() {
if (this._timeoutId != null) {
clearTimeout(this._timeoutId);
this._timeoutId = null;
}
}
}
function doAABBsOverlap(a, b) {
const ax1 = a.position.x + a.size.width;
const bx1 = b.x + b.width;
const ay1 = a.position.y + a.size.height;
const by1 = b.y + b.height;
// Prenda a para b, veja se não está vazio
const cx0 = a.position.x < b.x ? b.x : a.position.x;
const cx1 = ax1 < bx1 ? ax1 : bx1;
if (cx1 - cx0 > 0) {
const cy0 = a.position.y < b.y ? b.y : a.position.y;
const cy1 = ay1 < by1 ? ay1 : by1;
if (cy1 - cy0 > 0) {
return true;
}
}
return false;
}
module.exports = {
ReconnectSys,
doAABBsOverlap
};