Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 328 lines (280 sloc) 10.746 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
package
{
    import flash.display.*;
    import flash.events.*;
    import flash.text.*;
    import flash.utils.*;

    import com.unfocus.signalslite.SignalLite;
import com.unfocus.signalslite.SignalTyped;
import com.unfocus.signalslite.SlotLite;

import org.osflash.signals.*;

    [SWF(backgroundColor=0xEEEADB,frameRate=1000)]

    /**
* A test of various callback techniques
* @author Jackson Dunstan
*/
    public class CallbacksTest extends Sprite// implements Runnable
    {
        public var signal:Signal;
public var signalLite:SignalLite;
public var runnableSignal:RunnableSignal;
        public var funcs:Vector.<Function> = new Vector.<Function>();
        public var runnables:Vector.<Runnable> = new Vector.<Runnable>();

public var genericSignal:GenericSignal;
 
        public function CallbacksTest()
        {
            var logger:TextField = new TextField();
            logger.autoSize = TextFieldAutoSize.LEFT;
            addChild(logger);
 
            this.signal = new Signal();
this.signalLite = new SignalLite();
this.runnableSignal = new RunnableSignal();

this.genericSignal = new GenericSignal( KickableSlot );
 
            var i:int;
 
            const FUNC_CALL_REPS:int = 100000;
            const NUM_LISTENERS:int = 10;
            const EVENT_TYPE:String = "test";
            var runnable:Runnable = new MyRunnable();
            var func:Function = runnable.run;
 
            // Single call
 
            var beforeTime:int = getTimer();
            for (i = 0; i < FUNC_CALL_REPS; ++i)
            {
                func();
            }
            logger.appendText("Func call time: " + (getTimer()-beforeTime) + "\n");
 
            beforeTime = getTimer();
            for (i = 0; i < FUNC_CALL_REPS; ++i)
            {
                runnable.run();
            }
            logger.appendText("Runnable call time: " + (getTimer()-beforeTime) + "\n");
 
            addEventListener(EVENT_TYPE, onEvent0);
            beforeTime = getTimer();
            for (i = 0; i < FUNC_CALL_REPS; ++i)
            {
                dispatchEvent(new Event(EVENT_TYPE));
            }
            logger.appendText("Event (1 listener) time: " + (getTimer()-beforeTime) + "\n");
            removeEventListener(EVENT_TYPE, onEvent0);
 
            this.signal.add(onSignal0);
            beforeTime = getTimer();
            for (i = 0; i < FUNC_CALL_REPS; ++i)
            {
                this.signal.dispatch();
            }
            logger.appendText("Signal (1 listener) time: " + (getTimer()-beforeTime) + "\n");
            this.signal.remove(onSignal0);

            this.signalLite.add(onSignal0);
            beforeTime = getTimer();
            for (i = 0; i < FUNC_CALL_REPS; ++i)
            {
                this.signalLite.dispatch();
            }
            logger.appendText("SignalLite (1 listener) time: " + (getTimer()-beforeTime) + "\n");
            this.signalLite.remove(onSignal0);

this.runnableSignal.addSlot(slot0);
            beforeTime = getTimer();
            for (i = 0; i < FUNC_CALL_REPS; ++i)
            {
                this.runnableSignal.dispatchRunnable();
            }
            logger.appendText("RunnableSignal (1 listener) time: " + (getTimer()-beforeTime) + "\n\n");
            this.runnableSignal.removeSlot(slot0);

            // Calling a list
 
            for (i = 0; i < NUM_LISTENERS; ++i)
            {
                this.funcs.push(func);
            }
            beforeTime = getTimer();
            for (i = 0; i < FUNC_CALL_REPS; ++i)
            {
                dispatchFuncs(this.funcs);
            }
            logger.appendText("Func call (" + NUM_LISTENERS + " listeners) time: " + (getTimer()-beforeTime) + "\n");
 
            for (i = 0; i < NUM_LISTENERS; ++i)
            {
                this.runnables.push(runnable);
            }
            beforeTime = getTimer();
            for (i = 0; i < FUNC_CALL_REPS; ++i)
            {
                dispatchRunnables(this.runnables);
            }
            logger.appendText("Runnable call (" + NUM_LISTENERS + " listeners) time: " + (getTimer()-beforeTime) + "\n");
 
            for (i = 0; i < NUM_LISTENERS; ++i)
            {
                addEventListener(EVENT_TYPE, this["onEvent"+i]);
            }
            beforeTime = getTimer();
            for (i = 0; i < FUNC_CALL_REPS; ++i)
            {
                dispatchEvent(new Event(EVENT_TYPE));
            }
            logger.appendText("Event (" + NUM_LISTENERS + " listeners) time: " + (getTimer()-beforeTime) + "\n");
 
            for (i = 0; i < NUM_LISTENERS; ++i)
            {
                this.signal.add(this["onSignal"+i]);
            }

            beforeTime = getTimer();
            for (i = 0; i < FUNC_CALL_REPS; ++i)
            {
                this.signal.dispatch();
            }
            logger.appendText("Signal (" + NUM_LISTENERS + " listeners) time: " + (getTimer() - beforeTime) + "\n");

            for (i = 0; i < NUM_LISTENERS; ++i)
            {
                this.signal.remove(this["onSignal"+i]);
            }

            for (i = 0; i < NUM_LISTENERS; ++i)
            {
                this.signalLite.add(this["onSignal"+i]);
            }

            beforeTime = getTimer();
            for (i = 0; i < FUNC_CALL_REPS; ++i)
            {
                this.signalLite.dispatch();
            }
            logger.appendText("SignalLite (" + NUM_LISTENERS + " listeners) time: " + (getTimer() - beforeTime) + "\n");

            for (i = 0; i < NUM_LISTENERS; ++i)
            {
                this.signalLite.remove(this["onSignal"+i]);
            }

for (i = 0; i < NUM_LISTENERS; ++i)
            {
                this.runnableSignal.addSlot(this["slot"+i]);
            }

beforeTime = getTimer();
            for (i = 0; i < FUNC_CALL_REPS; ++i)
            {
                this.runnableSignal.dispatchRunnable();
            }
            logger.appendText("RunnableSignal (" + NUM_LISTENERS + " listeners) time: " + (getTimer() - beforeTime) + "\n\n");

// GenericSignal - very fast on AVM2/JIT (almost as fast as RunnableSignal). Crazy slow on iOS/AOT!!
for (i = 0; i < NUM_LISTENERS; ++i)
            {
                this.genericSignal.addSlot(this["kslot"+i]);
            }
beforeTime = getTimer();
for (i = 0; i < FUNC_CALL_REPS; ++i)
{
this.genericSignal.dispatch();
}
            logger.appendText("GenericSignal (" + NUM_LISTENERS + " listeners) time: " + (getTimer() - beforeTime) + "\n");
        }
 
        private function dispatchFuncs(funcs:Vector.<Function>): void
        {
            var len:int = funcs.length;
            for (var i:int = 0; i < len; ++i)
            {
                funcs[i]();
            }
        }
 
        private function dispatchRunnables(runnables:Vector.<Runnable>): void
        {
            var len:int = runnables.length;
            for (var i:int = 0; i < len; ++i)
            {
                Runnable(runnables[i]).run();
            }
        }
 
        private function onEvent0(ev:Event): void {}
        private function onEvent1(ev:Event): void {}
        private function onEvent2(ev:Event): void {}
        private function onEvent3(ev:Event): void {}
        private function onEvent4(ev:Event): void {}
        private function onEvent5(ev:Event): void {}
        private function onEvent6(ev:Event): void {}
        private function onEvent7(ev:Event): void {}
        private function onEvent8(ev:Event): void {}
        private function onEvent9(ev:Event): void {}
 
        private function onSignal0(): void {}
        private function onSignal1(): void {}
        private function onSignal2(): void {}
        private function onSignal3(): void {}
        private function onSignal4(): void {}
        private function onSignal5(): void {}
        private function onSignal6(): void {}
        private function onSignal7(): void {}
        private function onSignal8(): void {}
        private function onSignal9(): void {}

private var slot0:RunnableSlot = new RunnableSlot();
private var slot1:RunnableSlot = new RunnableSlot();
private var slot2:RunnableSlot = new RunnableSlot();
private var slot3:RunnableSlot = new RunnableSlot();
private var slot4:RunnableSlot = new RunnableSlot();
private var slot5:RunnableSlot = new RunnableSlot();
private var slot6:RunnableSlot = new RunnableSlot();
private var slot7:RunnableSlot = new RunnableSlot();
private var slot8:RunnableSlot = new RunnableSlot();
private var slot9:RunnableSlot = new RunnableSlot();

private var kslot0:KickableSlot = new KickableSlot( new GetKicked() );
private var kslot1:KickableSlot = new KickableSlot( new GetKicked() );
private var kslot2:KickableSlot = new KickableSlot( new GetKicked() );
private var kslot3:KickableSlot = new KickableSlot( new GetKicked() );
private var kslot4:KickableSlot = new KickableSlot( new GetKicked() );
private var kslot5:KickableSlot = new KickableSlot( new GetKicked() );
private var kslot6:KickableSlot = new KickableSlot( new GetKicked() );
private var kslot7:KickableSlot = new KickableSlot( new GetKicked() );
private var kslot8:KickableSlot = new KickableSlot( new GetKicked() );
private var kslot9:KickableSlot = new KickableSlot( new GetKicked() );


public function run():void {}
    }
}

import com.unfocus.signalslite.*;

internal interface Runnable
{
    function run(): void;
}

internal class MyRunnable implements Runnable
{
    public function run(): void {}
}

// Example SignalTyped
internal class RunnableSignal extends SignalTyped
{
public function RunnableSignal() {
last = first = new RunnableSlot;
}

public function dispatchRunnable():void
{
var node:RunnableSlot = first as RunnableSlot;
while ( node = (node.next as RunnableSlot) ) {
node.runnable.run();
}
}
}

// A custom slot that will store the runnable object.
internal class RunnableSlot extends SlotLite {
public var runnable:Runnable = new MyRunnable;
}

internal class GenericSignal extends SignalTyped
{
protected var cls:Class;
function GenericSignal( aCls:Class ) {
last = first = new aCls( null );
cls = aCls;
}

internal function dispatch():void
{
var node:* = first;
while ( node = (node.next) ) {
node.kickable.gotKicked();
}
}
}
internal interface IGetKicked
{
function gotKicked():void;
}
internal class GetKicked implements IGetKicked {
public function gotKicked():void {}
}
internal class KickableSlot extends SlotLite
{
internal var kickable:IGetKicked;

function KickableSlot( kickable:IGetKicked )
{
this.kickable = kickable;
}
}
Something went wrong with that request. Please try again.