开发者

How to Fake a AsyncToken return in ActionScript 3

开发者 https://www.devze.com 2022-12-29 18:17 出处:网络
Using Parsley, I have a service that I access through a [Command(selector=\'list\')] public function getRssFeed( msg:RssEvent ):AsyncToken

Using Parsley, I have a service that I access through a [Command(selector='list')] public function getRssFeed( msg:RssEvent ):AsyncToken { return service.list() as AsyncToken; }

when I point to the "Real" RssService, everything works as expected. My problem is when I point to the "Mock" RssService. I can't figure out how to fake a AsyncToken wi开发者_C百科th some dummy data return... does anyone knows how to do this ?


Resolved.............. ;)

 public function list():AsyncToken

     var rssFeed:Array = [rss,rss,rss];
     var token:AsyncToken = createToken(rssFeed);
     token.addResponder(new AsyncResponder(resultHandler, null));
     return token;

  }

  private function resultHandler(event:ResultEvent, token:AsyncToken = null):void
  {
     event.token.dispatchEvent(event);   
  }


  protected function createToken(result:Object):AsyncToken
  {
     var token:AsyncToken = new AsyncToken(null);
     setTimeout(applyResult, Math.random()*500, token, result);
     return token;
  }

  private function applyResult(token:AsyncToken, result:Object):void
  {
     mx_internal:token.setResult(result);
     var event:ResultEvent = new ResultEvent(ResultEvent.RESULT, false, true, result, token);
     mx_internal:token.applyResult(event);
     trace(token);
  }


Using Parsley 3.0, you have a better option with spicefactory asynchronous commands :

public class MockCommand
{
    public var callback:Function;

    public function execute():void
    {
        var timer:Timer = new Timer(500, 1);
        timer.addEventListener(TimerEvent.TIMER_COMPLETE, timer_completeHandler);
        timer.start();
    }

    private function timer_completeHandler(event:TimerEvent):void
    {
        callback(mockResultData);
    }
}

No need for mx_internal import.


Don't forget to add:

use namespace mx_internal;

Otherwise you will get this exception.

[Fault] exception, information=TypeError: Error #1006: setResult is not a function.


I use some sort of a ServiceProxy-pattern which provides basically 3 methods:

  • invokeOperation
  • mockResultInvokeOperation
  • mockFaultInvokeOperation

I extend the ServiceProxy in the following way:

public class SomeService extends ServiceProxy {
    public var useMock:Boolean;

    public function someServiceCall(arg1:Type, arg2:Type, responder:IResponder):AsyncToken {
        if (useMock) {
            mockResultInvokeOperation({some fake result object}, responder);
        }

        return invokeOperation("someServiceCall", [arg1, arg2], responder);
    }

}

Technology-wise I use exactly the same trick as you.

package com.obecto.services.proxy
{
import flash.utils.setTimeout;

import mx.core.mx_internal;
import mx.messaging.messages.RemotingMessage;
import mx.rpc.AbstractOperation;
import mx.rpc.AbstractService;
import mx.rpc.AsyncToken;
import mx.rpc.Fault;
import mx.rpc.IResponder;
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;

public class ServiceProxy
{
    public var service:AbstractService;

    public function invokeOperation(operationName:String, arguments:Array, responder:IResponder):AsyncToken 
    {
        var operation:AbstractOperation = service.getOperation(operationName);
        operation.arguments = arguments;

        var token:AsyncToken = operation.send();
        token.addResponder(responder);
        return token;
    }

    public function mockResultInvokeOperation(mockResult:Object, responder:IResponder):AsyncToken
    {
        var fakeMessage:RemotingMessage = new RemotingMessage();

        var token:AsyncToken = new AsyncToken(fakeMessage);
        token.addResponder(responder);

        setTimeout(
            function(e:ResultEvent = null):void 
            {
                token.mx_internal::applyResult(new ResultEvent(ResultEvent.RESULT, false, true, mockResult));
            }, 1000);

        return token;
    }

    public function mockFaultInvokeOperation(message:String, responder:IResponder):AsyncToken
    {
        var fakeMessage:RemotingMessage = new RemotingMessage();

        var token:AsyncToken = new AsyncToken(fakeMessage);
        token.addResponder(responder);

        setTimeout(
            function(e:ResultEvent = null):void 
            {
                token.mx_internal::applyFault(new FaultEvent(FaultEvent.FAULT, false, true, 
                    new Fault("MOCK_FAULT", message)));
            }, 1000);

        return token;
    }

}
}
0

精彩评论

暂无评论...
验证码 换一张
取 消