Skip to content

Commit 0b927c0

Browse files
Address feedback
- Inline the code called once - Add support for `animationThrottle` option
1 parent 2038cb6 commit 0b927c0

File tree

1 file changed

+11
-16
lines changed

1 file changed

+11
-16
lines changed

src/element-style-observer.js

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -111,18 +111,27 @@ export default class ElementStyleObserver {
111111
async handleEvent (event) {
112112
if (event.type === "animationstart") {
113113
let animation = document.getAnimations().find(a => a.effect.target === this.target);
114-
let properties = this.getObservedAnimationProperties(animation);
114+
115+
// Get the observed properties that are being animated by the animation
116+
let keyframes = animation.effect.getKeyframes();
117+
let properties = new Set(keyframes.flatMap(frame =>
118+
Object.keys(frame).filter(prop => !["offset", "easing", "composite", "computedOffset"].includes(prop))
119+
));
120+
properties = [...properties].filter(property => this.properties.has(property));
121+
115122
if (properties.length === 0) {
116123
return;
117124
}
118125
else {
119-
let interval = setInterval(() => this.#handleChanges(properties), 16); // ~60fps
126+
let interval = setInterval(() => this.#handleChanges(properties), this.options.animationThrottle ?? 16); // 16ms ~ 60fps
120127

121128
let cleanup = event => {
122129
if (event.animationName === animation.animationName) {
123130
clearInterval(interval);
124131
this.target.removeEventListener("animationend", cleanup);
125132
this.target.removeEventListener("animationcancel", cleanup);
133+
134+
// Pick up the recent changes
126135
this.#handleChanges(properties);
127136
}
128137
};
@@ -286,20 +295,6 @@ export default class ElementStyleObserver {
286295
this.updateTransitionProperties();
287296
}
288297

289-
/**
290-
* Get the observed properties that are being animated by an animation.
291-
* @param {Animation} animation
292-
* @returns {string[]}
293-
*/
294-
getObservedAnimationProperties (animation) {
295-
let keyframes = animation.effect.getKeyframes();
296-
let properties = new Set(keyframes.flatMap(frame =>
297-
Object.keys(frame).filter(prop => !["offset", "easing", "composite", "computedOffset"].includes(prop))
298-
));
299-
300-
return [...properties].filter(property => this.properties.has(property));
301-
}
302-
303298
/**
304299
* Stop observing a target for changes to one or more CSS properties.
305300
* @param { string | string[] } [properties] Properties to stop observing. Defaults to all observed properties.

0 commit comments

Comments
 (0)