-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.js
More file actions
76 lines (69 loc) · 2.31 KB
/
utils.js
File metadata and controls
76 lines (69 loc) · 2.31 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
class ObserverHelper {
constructor() {
this.__resizeCallbacks = new Map();
this.__resizeObserver = new ResizeObserver((entries) => {
entries.forEach((entry) => {
const listeners = this.__resizeCallbacks.get(entry.target);
listeners?.forEach((resizeCallback) => { resizeCallback(entry); });
});
});
}
isSizeChanged = (value1, value2, delta) => !value1 || Math.abs(value2 - value1) > delta;
// eslint-disable-next-line spellcheck/spell-checker
debounce = (func, timeout) => {
let timer;
return (...args) => {
clearTimeout(timer);
timer = setTimeout(() => {
func.apply(this, args);
}, timeout);
};
};
resizeCallback = (component, resizeAction, delta = 1) => (container) => {
const { width, height } = container.contentRect;
if (
this.isSizeChanged(component.__observableWidth, width, delta)
|| this.isSizeChanged(component.__observableHeight, height, delta)
) {
component.__observableHeight = height;
component.__observableWidth = width;
if (width === 0 || height === 0) return;
resizeAction?.apply(component);
}
};
subscribe(component, element, resizeAction, delta, delay) {
if (!resizeAction) {
console.error('Subscription failed. No reisze callback passed');
return;
}
let listeners = this.__resizeCallbacks.get(element);
if (!listeners) {
this.__resizeObserver.observe(element);
listeners = new Map();
}
const newResizeCallback = this.resizeCallback(component, resizeAction, delta);
listeners.set(
component.element().get(0),
// eslint-disable-next-line spellcheck/spell-checker
delay ? this.debounce(newResizeCallback, delay) : newResizeCallback,
);
this.__resizeCallbacks.set(element, listeners);
component.on('disposing', (args) => {
this.unsubscribe(element, args.component.element().get(0));
});
}
unsubscribe(key1, key2) {
const listeners = this.__resizeCallbacks.get(key1);
listeners.delete(key2);
if (listeners.size === 0) {
this.__resizeCallbacks.delete(key1);
this.__resizeObserver.unobserve(key1);
} else {
this.__resizeCallbacks.set(key1, listeners);
}
}
disconnect() {
this.__resizeCallbacks.clear();
this.__resizeObserver.disconnect();
}
}