Here’s another fun one: Let’s say you have a class
public class Foo{ public function Foo(){ } public function doSomethingIntense(intensity:Number):void{ } public function somthingElse():void{ if(fooBaz){ doSomethingIntense(); } else if(fooBar){ doSomeOtherThing(); doSomethingIntense(); } } } Now, you really should pull doSomethingIntense() out into a requestAnimationFrame. But every time you invoke doSomethingIntense() having to wrap it in requestAnimationFrame with a function is annoying and error prone. With delayFunction you can do the following: public function Foo(){ delayedIntenseThing = delayFunction(doSomethingIntense); } private var delayedIntenseThing:Function And then you can just call delayedIntenseThing() the same way you’d call doSomethingIntense(). It’s at times like this that I really wish ActionScript had typed functions with signatures... Harbs > On Jan 6, 2022, at 1:28 AM, ha...@apache.org wrote: > > 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 63bad6b Another fun function > 63bad6b is described below > > commit 63bad6b4ab5e4af2953fedc34fa01a301c50052e > Author: Harbs <ha...@in-tools.com> > AuthorDate: Thu Jan 6 01:28:22 2022 +0200 > > Another fun function > --- > .../projects/Core/src/main/royale/CoreClasses.as | 1 + > .../royale/utils/functional/delayFunction.as | 69 ++++++++++++++++++++++ > .../test/royale/flexUnitTests/FunctionalTests.as | 34 +++++++++++ > 3 files changed, 104 insertions(+) > > diff --git a/frameworks/projects/Core/src/main/royale/CoreClasses.as > b/frameworks/projects/Core/src/main/royale/CoreClasses.as > index 48189b5..e3da6a1 100644 > --- a/frameworks/projects/Core/src/main/royale/CoreClasses.as > +++ b/frameworks/projects/Core/src/main/royale/CoreClasses.as > @@ -383,6 +383,7 @@ internal class CoreClasses > import org.apache.royale.utils.functional.debounceShort; debounceShort; > import org.apache.royale.utils.functional.throttle; throttle; > import org.apache.royale.utils.functional.animateFunction; > animateFunction; > + import org.apache.royale.utils.functional.delayFunction; delayFunction; > > 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/delayFunction.as > > b/frameworks/projects/Core/src/main/royale/org/apache/royale/utils/functional/delayFunction.as > new file mode 100644 > index 0000000..44367f1 > --- /dev/null > +++ > b/frameworks/projects/Core/src/main/royale/org/apache/royale/utils/functional/delayFunction.as > @@ -0,0 +1,69 @@ > +//////////////////////////////////////////////////////////////////////////////// > +// > +// Licensed to the Apache Software Foundation (ASF) under one or more > +// contributor license agreements. See the NOTICE file distributed with > +// this work for additional information regarding copyright ownership. > +// The ASF licenses this file to You under the Apache License, Version 2.0 > +// (the "License"); you may not use this file except in compliance with > +// the License. You may obtain a copy of the License at > +// > +// http://www.apache.org/licenses/LICENSE-2.0 > +// > +// Unless required by applicable law or agreed to in writing, software > +// distributed under the License is distributed on an "AS IS" BASIS, > +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. > +// See the License for the specific language governing permissions and > +// limitations under the License. > +// > +//////////////////////////////////////////////////////////////////////////////// > +package org.apache.royale.utils.functional > +{ > + COMPILE::SWF{ > + import flash.utils.setTimeout; > + import flash.utils.clearTimeout; > + } > + /** > + * Returns a function which when run will invoke the wrapped function > after a delay. > + * Defaults to 0 which will execute at the next opportune time. > + * In JS this uses requestAnimationFrame > + * > + * @royalesuppressexport > + * @langversion 3.0 > + * @productversion Royale 0.9.9 > + * > + */ > + public function delayFunction(method:Function, delay:Number=0):Function > + { > + return function(...args):void > + { > + > + function callback():void > + { > + method.apply(null,args); > + } > + > + COMPILE::SWF > + { > + setTimeout(function():void{ > + callback(); > + },delay); > + } > + > + COMPILE::JS > + { > + if(delay) > + { > + setTimeout(function():void{ > + requestAnimationFrame(callback); > + },delay); > + } > + else > + { > + requestAnimationFrame(callback); > + } > + > + } > + > + } > + } > +} > \ 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 75f320c..f17c1a7 100644 > --- > a/frameworks/projects/Core/src/test/royale/flexUnitTests/FunctionalTests.as > +++ > b/frameworks/projects/Core/src/test/royale/flexUnitTests/FunctionalTests.as > @@ -27,6 +27,7 @@ package flexUnitTests > import org.apache.royale.utils.functional.*; > import org.apache.royale.utils.functional.animateFunction; > import org.apache.royale.test.asserts.assertTrue; > + import org.apache.royale.test.asserts.assertEquals; > > public class FunctionalTests > { > @@ -226,6 +227,39 @@ package flexUnitTests > assertTrue(savedValue<3,"value should be 2"); > }, 300); > } > + public function testDelay():void > + { > + var foo:Foo = new Foo(); > + var delayedThis:Function = delayFunction(foo.increment,20); > + > + delayedThis(1); > + > + var firstThisValue:Number = foo.value; > + > + var savedThisValue:Number; > + setTimeout(function():void{ > + savedThisValue = foo.value; > + },75); > + > + var value:Number = 0; > + function increment(val:Number):void{ > + value+=val; > + } > + var delayed:Function = delayFunction(increment,20); > + delayed(1); > + var firstValue:Number = value; > + var savedValue:Number; > + setTimeout(function():void{ > + savedValue = value; > + },75); > + Async.delayCall(this, function():void > + { > + assertEquals(firstThisValue,0,"foo value should be 0") > + assertEquals(savedThisValue,1,"foo value should be 1") > + assertEquals(firstValue,0,"value should be 0") > + assertEquals(savedValue,1,"value should be 1") > + }, 200); > + } > > > }