diff options
Diffstat (limited to 'Source/WebCore/Modules/modern-media-controls/controls/scheduler.js')
-rw-r--r-- | Source/WebCore/Modules/modern-media-controls/controls/scheduler.js | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/Source/WebCore/Modules/modern-media-controls/controls/scheduler.js b/Source/WebCore/Modules/modern-media-controls/controls/scheduler.js new file mode 100644 index 000000000..f85beb037 --- /dev/null +++ b/Source/WebCore/Modules/modern-media-controls/controls/scheduler.js @@ -0,0 +1,66 @@ + +const scheduler = new class +{ + + constructor() + { + this._frameID = -1; + this._layoutCallbacks = new Set; + } + + // Public + + get hasScheduledLayoutCallbacks() + { + return this._frameID !== -1 || this._layoutCallbacks.size > 0; + } + + scheduleLayout(callback) + { + if (typeof callback !== "function") + return; + + this._layoutCallbacks.add(callback); + this._requestFrameIfNeeded(); + } + + unscheduleLayout(callback) + { + if (typeof callback !== "function") + return; + + this._layoutCallbacks.delete(callback); + } + + // Private + + _requestFrameIfNeeded() + { + if (this._frameID === -1 && this._layoutCallbacks.size > 0) + this._frameID = window.requestAnimationFrame(this._frameDidFire.bind(this)); + } + + _frameDidFire() + { + if (typeof scheduler.frameWillFire === "function") + scheduler.frameWillFire(); + + this._layout(); + this._frameID = -1; + this._requestFrameIfNeeded(); + + if (typeof scheduler.frameDidFire === "function") + scheduler.frameDidFire(); + } + + _layout() + { + // Layouts are not re-entrant. + const layoutCallbacks = this._layoutCallbacks; + this._layoutCallbacks = new Set; + + for (let callback of layoutCallbacks) + callback(); + } + +}; |