-
Notifications
You must be signed in to change notification settings - Fork 94
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(cy): serialize resize observer calls
Prevent crashes in Chrome due to the resize observer warnings. See cypress-io/cypress#27415 (comment) Signed-off-by: Max <[email protected]>
- Loading branch information
1 parent
f840ae0
commit 14ad7ab
Showing
2 changed files
with
45 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// store real observer | ||
const RealResizeObserver = ResizeObserver; | ||
|
||
let queueFlushTimeout; | ||
let queue = []; | ||
|
||
/** | ||
* ResizeObserver wrapper with "enforced batches" | ||
*/ | ||
export default class ResizeObserverPolyfill { | ||
constructor(callback) { | ||
this.callback = callback; | ||
this.observer = new RealResizeObserver(this.check.bind(this)); | ||
} | ||
|
||
observe(element) { | ||
this.observer.observe(element); | ||
} | ||
|
||
unobserve(element) { | ||
this.observer.unobserve(element); | ||
} | ||
|
||
disconnect() { | ||
this.observer.disconnect(); | ||
} | ||
|
||
check(entries) { | ||
// remove previous invocations of "self" | ||
queue = queue.filter((x) => x.cb !== this.callback); | ||
// put a new one | ||
queue.push({ cb: this.callback, args: entries }); | ||
// trigger update | ||
if (!queueFlushTimeout) { | ||
queueFlushTimeout = requestAnimationFrame(() => { | ||
queueFlushTimeout = undefined; | ||
const q = queue; | ||
queue = []; | ||
q.forEach(({ cb, args }) => cb(args)); | ||
}, 0); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters