- Revision
- 226755
- Author
- [email protected]
- Date
- 2018-01-10 19:36:29 -0800 (Wed, 10 Jan 2018)
Log Message
Web Inspector: Canvas tab: throttle recording slider updates
https://bugs.webkit.org/show_bug.cgi?id=180839
<rdar://problem/36057849>
Reviewed by Joseph Pecoraro
Source/WebInspectorUI:
* UserInterface/Base/Utilities.js:
Add Object.throttle and Function.cancelThrottle. Repeated calls to a
function on a throttled object are delayed, so that the function isn't
invoked more frequently than the specified delay value.
For a description of throttling behavior see:
- http://www.chrislondon.co/throttling-vs-debouncing
- http://benalman.com/projects/jquery-throttle-debounce-plugin
* UserInterface/Views/RecordingContentView.js:
(WI.RecordingContentView.prototype.updateActionIndex):
Throttle frequency of canvas snapshot creation to 200ms.
(WI.RecordingContentView.prototype.hidden):
Prevent trailing edge call after hiding the view.
LayoutTests:
* inspector/unit-tests/throttle-expected.txt: Added.
* inspector/unit-tests/throttle.html: Added.
Added function throttling tests.
Modified Paths
Added Paths
Diff
Modified: trunk/LayoutTests/ChangeLog (226754 => 226755)
--- trunk/LayoutTests/ChangeLog 2018-01-11 03:30:25 UTC (rev 226754)
+++ trunk/LayoutTests/ChangeLog 2018-01-11 03:36:29 UTC (rev 226755)
@@ -1,3 +1,15 @@
+2018-01-10 Matt Baker <[email protected]>
+
+ Web Inspector: Canvas tab: throttle recording slider updates
+ https://bugs.webkit.org/show_bug.cgi?id=180839
+ <rdar://problem/36057849>
+
+ Reviewed by Joseph Pecoraro
+
+ * inspector/unit-tests/throttle-expected.txt: Added.
+ * inspector/unit-tests/throttle.html: Added.
+ Added function throttling tests.
+
2018-01-10 Wenson Hsieh <[email protected]>
REGRESSION(r222507): Composition highlight doesn't render when using IME
Added: trunk/LayoutTests/inspector/unit-tests/throttle-expected.txt (0 => 226755)
--- trunk/LayoutTests/inspector/unit-tests/throttle-expected.txt (rev 0)
+++ trunk/LayoutTests/inspector/unit-tests/throttle-expected.txt 2018-01-11 03:36:29 UTC (rev 226755)
@@ -0,0 +1,18 @@
+Test throttle proxy support.
+
+
+== Running test suite: Throttle
+-- Running test case: Basic throttle
+Call throttled function every 1 ms for 110 ms.
+PASS: All calls delayed at least 50 ms.
+
+-- Running test case: Throttle proxy uniqueness
+PASS: Each call to throttle should return a new proxy.
+
+-- Running test case: Throttled function arguments
+PASS: Trailing call invoked with most recent arguments.
+
+-- Running test case: Cancel throttle
+Canceled throttled function.
+PASS: Throttled function canceled.
+
Added: trunk/LayoutTests/inspector/unit-tests/throttle.html (0 => 226755)
--- trunk/LayoutTests/inspector/unit-tests/throttle.html (rev 0)
+++ trunk/LayoutTests/inspector/unit-tests/throttle.html 2018-01-11 03:36:29 UTC (rev 226755)
@@ -0,0 +1,131 @@
+<!doctype html>
+<html>
+<head>
+<script src=""
+<script>
+function test()
+{
+ let suite = InspectorTest.createAsyncSuite("Throttle");
+
+ suite.addTestCase({
+ name: "Basic throttle",
+ test(resolve, reject) {
+ const delay = 50;
+ const duration = 110;
+ const maximumNumberOfCalls = Math.ceil(duration / delay) + 1; // Add one for the leading edge. Math.ceil accounts for the trailing edge.
+
+ let lastCallTime = NaN;
+ let numberOfCalls = 0;
+
+ let object = {
+ test(isTrailing) {
+ let now = Date.now();
+
+ if (++numberOfCalls > maximumNumberOfCalls) {
+ reject("Throttled function called too many times.");
+ return;
+ }
+
+ if (!isNaN(lastCallTime)) {
+ let actualDelay = now - lastCallTime;
+ if (actualDelay < delay) {
+ InspectorTest.fail(`Delay should be at least ${delay} ms. Was ${actualDelay} ms.`);
+ reject();
+ return;
+ }
+ }
+
+ lastCallTime = now;
+
+ if (isTrailing) {
+ InspectorTest.pass(`All calls delayed at least ${delay} ms.`);
+ resolve();
+ }
+ }
+ };
+
+ InspectorTest.log(`Call throttled function every 1 ms for ${duration} ms.`);
+
+ let throttleProxy = object.throttle(delay);
+ throttleProxy.test();
+
+ for (let i = 1; i <= duration; i++)
+ setTimeout(() => { throttleProxy.test(i === duration); }, i);
+ }
+ });
+
+ suite.addTestCase({
+ name: "Throttle proxy uniqueness",
+ test(resolve, reject) {
+ let object = {};
+ let firstProxy = object.throttle(1);
+ let secondProxy = object.throttle(1);
+ InspectorTest.expectNotEqual(firstProxy, secondProxy, "Each call to throttle should return a new proxy.");
+ resolve();
+ }
+ });
+
+ suite.addTestCase({
+ name: "Throttled function arguments",
+ test(resolve, reject) {
+ let values = [1, 2, 3];
+ let isLeadingCall = true;
+
+ let object = {
+ test(value) {
+ if (isLeadingCall) {
+ isLeadingCall = false;
+ return;
+ }
+
+ let expected = values.lastValue;
+ InspectorTest.expectEqual(value, expected, "Trailing call invoked with most recent arguments.");
+ resolve();
+ }
+ };
+
+ let i = 0;
+ let throttleProxy = object.throttle(50);
+ for (let value of values)
+ setTimeout(() => { throttleProxy.test(value); }, i++);
+ }
+ });
+
+ suite.addTestCase({
+ name: "Cancel throttle",
+ test(resolve, reject) {
+ let canceled = false;
+
+ let object = {
+ test() {
+ if (!canceled)
+ return;
+ InspectorTest.fail("Called throttled function after cancel.");
+ reject();
+ }
+ };
+
+ let throttleProxy = object.throttle(20);
+ throttleProxy.test();
+ throttleProxy.test();
+
+ object.test.cancelThrottle();
+ canceled = true;
+
+ InspectorTest.log("Canceled throttled function.");
+
+ setTimeout(() => {
+ InspectorTest.pass("Throttled function canceled.");
+ resolve();
+ }, 50);
+ }
+ });
+
+ suite.runTestCasesAndFinish();
+}
+</script>
+</head>
+<body _onload_="runTest()">
+ <p>Test throttle proxy support.</p>
+</body>
+</html>
Modified: trunk/Source/WebInspectorUI/ChangeLog (226754 => 226755)
--- trunk/Source/WebInspectorUI/ChangeLog 2018-01-11 03:30:25 UTC (rev 226754)
+++ trunk/Source/WebInspectorUI/ChangeLog 2018-01-11 03:36:29 UTC (rev 226755)
@@ -1,3 +1,26 @@
+2018-01-10 Matt Baker <[email protected]>
+
+ Web Inspector: Canvas tab: throttle recording slider updates
+ https://bugs.webkit.org/show_bug.cgi?id=180839
+ <rdar://problem/36057849>
+
+ Reviewed by Joseph Pecoraro
+
+ * UserInterface/Base/Utilities.js:
+ Add Object.throttle and Function.cancelThrottle. Repeated calls to a
+ function on a throttled object are delayed, so that the function isn't
+ invoked more frequently than the specified delay value.
+
+ For a description of throttling behavior see:
+ - http://www.chrislondon.co/throttling-vs-debouncing
+ - http://benalman.com/projects/jquery-throttle-debounce-plugin
+
+ * UserInterface/Views/RecordingContentView.js:
+ (WI.RecordingContentView.prototype.updateActionIndex):
+ Throttle frequency of canvas snapshot creation to 200ms.
+ (WI.RecordingContentView.prototype.hidden):
+ Prevent trailing edge call after hiding the view.
+
2018-01-10 Joseph Pecoraro <[email protected]>
Web Inspector: Should not try to autocomplete subsections of a string
Modified: trunk/Source/WebInspectorUI/UserInterface/Base/Utilities.js (226754 => 226755)
--- trunk/Source/WebInspectorUI/UserInterface/Base/Utilities.js 2018-01-11 03:30:25 UTC (rev 226754)
+++ trunk/Source/WebInspectorUI/UserInterface/Base/Utilities.js 2018-01-11 03:36:29 UTC (rev 226755)
@@ -1481,6 +1481,62 @@
return this[requestAnimationFrameProxySymbol];
}
});
+
+ const throttleTimeoutSymbol = Symbol("throttle-timeout");
+
+ Object.defineProperty(Object.prototype, "throttle",
+ {
+ value(delay)
+ {
+ console.assert(delay >= 0);
+
+ let lastFireTime = NaN;
+ let mostRecentArguments = null;
+
+ return new Proxy(this, {
+ get(target, property, receiver) {
+ return (...args) => {
+ let original = target[property];
+ console.assert(typeof original === "function");
+ mostRecentArguments = args;
+
+ function performWork() {
+ lastFireTime = Date.now();
+ original[throttleTimeoutSymbol] = undefined;
+ original.apply(target, mostRecentArguments);
+ }
+
+ if (isNaN(lastFireTime)) {
+ performWork();
+ return;
+ }
+
+ let remaining = delay - (Date.now() - lastFireTime);
+ if (remaining <= 0) {
+ original.cancelThrottle();
+ performWork();
+ return;
+ }
+
+ if (!original[throttleTimeoutSymbol])
+ original[throttleTimeoutSymbol] = setTimeout(performWork, remaining);
+ };
+ }
+ });
+ }
+ });
+
+ Object.defineProperty(Function.prototype, "cancelThrottle",
+ {
+ value()
+ {
+ if (!this[throttleTimeoutSymbol])
+ return;
+
+ clearTimeout(this[throttleTimeoutSymbol]);
+ this[throttleTimeoutSymbol] = undefined;
+ }
+ });
})();
function appendWebInspectorSourceURL(string)
Modified: trunk/Source/WebInspectorUI/UserInterface/Views/RecordingContentView.js (226754 => 226755)
--- trunk/Source/WebInspectorUI/UserInterface/Views/RecordingContentView.js 2018-01-11 03:30:25 UTC (rev 226754)
+++ trunk/Source/WebInspectorUI/UserInterface/Views/RecordingContentView.js 2018-01-11 03:36:29 UTC (rev 226755)
@@ -34,6 +34,7 @@
this._index = NaN;
this._snapshots = [];
this._initialContent = null;
+ this._throttler = this.throttle(200);
this.element.classList.add("recording", this.representedObject.type);
@@ -113,14 +114,12 @@
this._updateSliderValue();
if (this.representedObject.type === WI.Recording.Type.Canvas2D)
- this._generateContentCanvas2D(index, actions, options);
+ this._throttler._generateContentCanvas2D(index, actions, options);
else if (this.representedObject.type === WI.Recording.Type.CanvasWebGL)
- this._generateContentCanvasWebGL(index, actions, options);
+ this._throttler._generateContentCanvasWebGL(index, actions, options);
});
}
- // Protected
-
shown()
{
super.shown();
@@ -134,7 +133,16 @@
}
}
+ hidden()
+ {
+ super.hidden();
+ this._generateContentCanvas2D.cancelThrottle();
+ this._generateContentCanvasWebGL.cancelThrottle();
+ }
+
+ // Protected
+
get supportsSave()
{
return true;