This is an automated email from the ASF dual-hosted git repository.
harbs pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/royale-asjs.git
The following commit(s) were added to refs/heads/develop by this push:
new 99c0e37 Added throttle
99c0e37 is described below
commit 99c0e37644e8e8c89da2571ff0d013fca1dd084c
Author: Harbs <[email protected]>
AuthorDate: Tue Jan 4 23:25:16 2022 +0200
Added throttle
---
.../projects/Core/src/main/royale/CoreClasses.as | 1 +
.../apache/royale/utils/functional/debounceLong.as | 9 +-
.../royale/utils/functional/debounceShort.as | 5 +-
.../functional/{debounceLong.as => throttle.as} | 20 +--
.../test/royale/flexUnitTests/FunctionalTests.as | 174 +++++++++++++++------
5 files changed, 150 insertions(+), 59 deletions(-)
diff --git a/frameworks/projects/Core/src/main/royale/CoreClasses.as
b/frameworks/projects/Core/src/main/royale/CoreClasses.as
index 289429e..50c3c71 100644
--- a/frameworks/projects/Core/src/main/royale/CoreClasses.as
+++ b/frameworks/projects/Core/src/main/royale/CoreClasses.as
@@ -381,6 +381,7 @@ internal class CoreClasses
import org.apache.royale.utils.functional.debounceLong; debounceLong;
import org.apache.royale.utils.functional.debounceShort; debounceShort;
+ import org.apache.royale.utils.functional.throttle; throttle;
import org.apache.royale.core.TextLineMetrics; TextLineMetrics;
import org.apache.royale.utils.ClassSelectorList; ClassSelectorList;
diff --git
a/frameworks/projects/Core/src/main/royale/org/apache/royale/utils/functional/debounceLong.as
b/frameworks/projects/Core/src/main/royale/org/apache/royale/utils/functional/debounceLong.as
index 31f4c04..b119709 100644
---
a/frameworks/projects/Core/src/main/royale/org/apache/royale/utils/functional/debounceLong.as
+++
b/frameworks/projects/Core/src/main/royale/org/apache/royale/utils/functional/debounceLong.as
@@ -32,7 +32,7 @@ package org.apache.royale.utils.functional
* @productversion Royale 0.9.9
*
*/
- public function debounceLong(method:Function,
delay:Number,thisArg:Object=null):Function
+ public function debounceLong(method:Function, delay:Number):Function
{
var timeoutRef:*;
return function(...args):void
@@ -40,9 +40,12 @@ package org.apache.royale.utils.functional
function callback():void
{
timeoutRef = null;
- method.apply(thisArg,args);
+ //Because of the way Royale binds closures, no
this argument is needed.
+ method.apply(null,args);
}
- clearTimeout(timeoutRef);
+ if(timeoutRef)
+ clearTimeout(timeoutRef);
+
timeoutRef = setTimeout(callback, delay);
}
}
diff --git
a/frameworks/projects/Core/src/main/royale/org/apache/royale/utils/functional/debounceShort.as
b/frameworks/projects/Core/src/main/royale/org/apache/royale/utils/functional/debounceShort.as
index 26c132a..ff480b5 100644
---
a/frameworks/projects/Core/src/main/royale/org/apache/royale/utils/functional/debounceShort.as
+++
b/frameworks/projects/Core/src/main/royale/org/apache/royale/utils/functional/debounceShort.as
@@ -32,15 +32,16 @@ package org.apache.royale.utils.functional
* @productversion Royale 0.9.9
*
*/
- public function debounceShort(method:Function,
delay:Number,thisArg:Object=null):Function
+ public function debounceShort(method:Function, delay:Number):Function
{
var timeoutRef:*;
return function(...args):void
{
function callback():void
{
+ //Because of the way Royale binds closures, no
this argument is needed.
+ method.apply(null,args);
timeoutRef = null;
- method.apply(thisArg,args);
}
if(timeoutRef == null)
{
diff --git
a/frameworks/projects/Core/src/main/royale/org/apache/royale/utils/functional/debounceLong.as
b/frameworks/projects/Core/src/main/royale/org/apache/royale/utils/functional/throttle.as
similarity index 72%
copy from
frameworks/projects/Core/src/main/royale/org/apache/royale/utils/functional/debounceLong.as
copy to
frameworks/projects/Core/src/main/royale/org/apache/royale/utils/functional/throttle.as
index 31f4c04..01cfda0 100644
---
a/frameworks/projects/Core/src/main/royale/org/apache/royale/utils/functional/debounceLong.as
+++
b/frameworks/projects/Core/src/main/royale/org/apache/royale/utils/functional/throttle.as
@@ -23,27 +23,27 @@ package org.apache.royale.utils.functional
import flash.utils.clearTimeout;
}
/**
- * Returns a debounced function to run after a delay.
- * If the function is invoked again within the delay period, the latest
- * invocation of the function will be used and the delay will be reset
to then.
+ * Limits the execution of a function to a maximum of once within the
limit in ms.
+ * It the limit has expired since the last call, the function is
executed immediately.
+ * Otherwise it is not executed at all.
+ *
*
* @royalesuppressexport
* @langversion 3.0
* @productversion Royale 0.9.9
*
*/
- public function debounceLong(method:Function,
delay:Number,thisArg:Object=null):Function
+ public function throttle(method:Function, limit:Number):Function
{
- var timeoutRef:*;
+ var timeStamp:Number = 0;
return function(...args):void
{
- function callback():void
+ var currentTime:Number = new Date().getTime();
+ if(currentTime - timeStamp >= limit)
{
- timeoutRef = null;
- method.apply(thisArg,args);
+ timeStamp = currentTime;
+ method.apply(null,args);
}
- clearTimeout(timeoutRef);
- timeoutRef = setTimeout(callback, delay);
}
}
}
\ No newline at end of file
diff --git
a/frameworks/projects/Core/src/test/royale/flexUnitTests/FunctionalTests.as
b/frameworks/projects/Core/src/test/royale/flexUnitTests/FunctionalTests.as
index 10db0f2..022c4da 100644
--- a/frameworks/projects/Core/src/test/royale/flexUnitTests/FunctionalTests.as
+++ b/frameworks/projects/Core/src/test/royale/flexUnitTests/FunctionalTests.as
@@ -51,70 +51,156 @@ package flexUnitTests
[Test(async,timeout="300")]
public function testDebounceLong():void
{
+ var foo:Foo = new Foo();
+ var thisDebounced:Function = debounceLong(foo.increment,30);
+ setTimeout(function():void{
+ thisDebounced(5);
+ },0);
+ setTimeout(function():void{
+ thisDebounced(4);
+ },5);
+ setTimeout(function():void{
+ thisDebounced(3);
+ },10);
+ setTimeout(function():void{
+ thisDebounced(2);
+ },100);
+ setTimeout(function():void{
+ thisDebounced(1);
+ },110);
+
var value:Number = 0;
- var foo = function(){
- this.value = 0;
- this.increment = function(val:Number){
- this.value+=val;
- }
- }
- var fooObj = new foo();
- var thisDebounced:Function =
debounceLong(fooObj.increment,30,fooObj);
- setTimeout(thisDebounced, 10,5);
- setTimeout(thisDebounced, 10,4);
- setTimeout(thisDebounced, 10,3);
- setTimeout(thisDebounced, 10,2);
- setTimeout(thisDebounced, 10,1);
- function increment(val:Number){
+ function increment(val:Number):void{
value+=val;
+ trace(val);
}
var debounced:Function = debounceLong(increment,30);
- setTimeout(debounced, 10,5);
- setTimeout(debounced, 10,4);
- setTimeout(debounced, 10,3);
- setTimeout(debounced, 10,2);
- setTimeout(debounced, 10,1);
+ setTimeout(function():void{
+ debounced(5);
+ },0);
+ setTimeout(function():void{
+ debounced(4);
+ },5);
+ setTimeout(function():void{
+ debounced(3);
+ },10);
+ setTimeout(function():void{
+ debounced(2);
+ },100);
+ setTimeout(function():void{
+ debounced(1);
+ },110);
Async.delayCall(this, function():void
{
- assertTrue(fooObj.value == 1,"foo value should be incremented
by 1");
- assertTrue(value == 1,"Should be incremented by 1");
- }, 100);
+ assertEquals(foo.value,4,"foo value should be incremented by
4")
+ assertEquals(value,4,"value should be incremented by 4")
+ }, 300);
}
[Test(async,timeout="300")]
public function testDebounceShort():void
{
- var foo = function(){
- this.value = 0;
- this.increment = function(val:Number){
- this.value+=val;
- }
- }
- var fooObj = new foo();
- var thisDebounced:Function =
debounceShort(fooObj.increment,30,fooObj);
- setTimeout(thisDebounced, 10,5);
- setTimeout(thisDebounced, 10,4);
- setTimeout(thisDebounced, 10,3);
- setTimeout(thisDebounced, 10,2);
- setTimeout(thisDebounced, 10,1);
+ var foo:Foo = new Foo();
+ var thisDebounced:Function = debounceShort(foo.increment,30);
+
+ setTimeout(function():void{
+ thisDebounced(5);
+ },0);
+ setTimeout(function():void{
+ thisDebounced(4);
+ },5);
+ setTimeout(function():void{
+ thisDebounced(3);
+ },10);
+ setTimeout(function():void{
+ thisDebounced(2);
+ },100);
+ setTimeout(function():void{
+ thisDebounced(1);
+ },110);
var value:Number = 0;
- function increment(val:Number){
+ function increment(val:Number):void{
value+=val;
}
var debounced:Function = debounceShort(increment,30);
- setTimeout(debounced, 10,5);
- setTimeout(debounced, 10,4);
- setTimeout(debounced, 10,3);
- setTimeout(debounced, 10,2);
- setTimeout(debounced, 10,1);
+
+ setTimeout(function():void{
+ debounced(5);
+ },0);
+ setTimeout(function():void{
+ debounced(4);
+ },5);
+ setTimeout(function():void{
+ debounced(3);
+ },10);
+ setTimeout(function():void{
+ debounced(2);
+ },100);
+ setTimeout(function():void{
+ debounced(1);
+ },110);
Async.delayCall(this, function():void
{
- assertTrue(fooObj.value > 1,"foo value should be greater than
1");
- assertTrue(value > 1,"Should be greater than 1");
- }, 100);
+ assertEquals(foo.value,7,"foo value should be 7")
+ assertEquals(value,7,"value should be 7")
+ }, 300);
+ }
+ [Test(async,timeout="300")]
+ public function testThrottle():void
+ {
+ var foo:Foo = new Foo();
+ var thisThrottled:Function = throttle(foo.increment,30);
+ setTimeout(function():void{
+ thisThrottled(5);
+ },0);
+ setTimeout(function():void{
+ thisThrottled(4);
+ },5);
+ setTimeout(function():void{
+ thisThrottled(3);
+ },10);
+ setTimeout(function():void{
+ thisThrottled(2);
+ },100);
+ setTimeout(function():void{
+ thisThrottled(1);
+ },110);
+ var value:Number = 0;
+ function increment(val:Number):void{
+ value+=val;
+ }
+ var throttled:Function = throttle(increment,30);
+ setTimeout(function():void{
+ throttled(5);
+ },0);
+ setTimeout(function():void{
+ throttled(4);
+ },5);
+ setTimeout(function():void{
+ throttled(3);
+ },10);
+ setTimeout(function():void{
+ throttled(2);
+ },100);
+ setTimeout(function():void{
+ throttled(1);
+ },110);
+ Async.delayCall(this, function():void
+ {
+ assertEquals(foo.value,7,"foo value should be 7");
+ assertEquals(value,7,"value should be 7");
+ }, 300);
}
}
}
+class Foo
+{
+ public var value:Number = 0;
+ public function increment(value:int):void{
+ this.value += value;
+ trace(this.value);
+ }
+}
\ No newline at end of file