-
Notifications
You must be signed in to change notification settings - Fork 10
/
did-resize.js
60 lines (46 loc) · 1.57 KB
/
did-resize.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
import { setModifierManager, capabilities } from '@ember/modifier';
import { assert } from '@ember/debug';
import { debounce as runloopDebounce, cancel } from '@ember/runloop';
const SERVICE_NAME = 'service:did-resize-detector';
export default setModifierManager(
(owner) => ({
capabilities: capabilities('3.13'),
createModifier(factory) {
return new factory.class();
},
installModifier(instance, element, { positional: [ callback ], named: { debounce = 0 } }) {
instance.element = element;
instance.debounce = debounce;
instance.setupListener(owner, callback);
},
updateModifier(instance, { positional: [callback], named: { debounce = 0 }}) {
instance.destroyListener(owner);
instance.debounce = debounce;
instance.setupListener(owner, callback);
},
destroyModifier(instance) {
instance.destroyListener(owner);
}
}),
class DidResizeModifier {
setupListener(owner, callback) {
assert(
`ember-did-resize-modifier: '${callback}' is not a valid callback. Provide a function.`,
typeof callback === 'function'
);
let cb = (...args) => {
if (this.debounce !== 0) {
this.debounceId = runloopDebounce(callback, ...args, this.debounce);
} else {
callback(...args);
}
};
owner.lookup(SERVICE_NAME).setup(this.element, cb, { callOnAdd: false });
this.callback = cb;
}
destroyListener(owner) {
cancel(this.debounceId);
owner.lookup(SERVICE_NAME).teardown(this.element, this.callback);
}
}
);