How to send custom modul messages and avoid nasty switch statements.

Added by alex winx 560 days ago

Hi all
In effort to avoid nasty switch statements as respond of room Module of Module messages I "created/decomplied" CustomEventProducer(EventDispatcher for thoose familiar with AS3).(It will be good in Union API orginal EventProducer to be public instead of private.)
Dispatcher use HashMaps (message code as key and message content as value) which is much faster,readable and usable way then switch statement especially as it grow bigger and bigger with number of messages, and Reflection for execution.
 /**
     * Called when the room receives a room module message.
     * 
     * @param evt The RoomEvent contianing information about the event.
     */
    public void onModuleMessage(RoomEvent evt) 
    {
        Message msg = evt.getMessage();

        //solving nasty switch statement and custom messaging
//        switch(msg.getMessageName())
//        {
//        case code1: 
//            dosmthg
//        break;
//        case code2:
//            dosmthg;
//        }

        dispatcher.dispatchEvent(msg.getMessageName(),evt );
    }
You can use this technique in client side onAttributeChange event.
Instead use of:
switch(attribute.name)
{
 case somthing:
   function handler();
...

much better using signals(http://flashblog.robertpenner.com/2009/09/my-new-as3-event-system-signals.html) or dispatcher system:
 

var dispatcher:EventDispatcher=new EventDispatcher();

dispatcher.dispatchEvent(new Event(attribute.name+"_EVENT",value));

and you have somewhere before
before addEventListener("attributeName_EVENT",function handler())

Replies

RE: How to send custom modul messages and avoid nasty switch statements. - Added by alex winx 560 days ago

// ---- EventProducer or EventDispatcher(in AS3)
private CustomEventProducer dispatcher=new CustomEventProducer();

//register dispatcher for event "TEST"
dispatcher.addEventListener("TESTAMF", this, "TestAmf");
dispatcher.addEventListener("TESTJSON", this, "TestJson");

RE: How to send custom modul messages and avoid nasty switch statements. - Added by alex winx 560 days ago

Hi All

It would be good as when you send message from server:
ctx.getRoom().sendMessage("RoomModulMessage2","message1");
ctx.getRoom().sendMessage("RoomModulMessage1","message2");
on the client side you have:
room.addEventListener("RoomModulMessage2",function handler2());
room.addEventListener("RoomModulMessage1",function handler1());
but when you send
room.sendModuleMessage("MyModuleMessage","message");

you can
ctx.getRoom().addEventListener(RoomEvent.ModuleMessage,java function handler());

and you need to have switch based of msg.name
and you cant
ctx.getRoom().addEventListener("MyModuleMessage",java function handler());
ctx.getRoom().addEventListener("MyModuleMessage1",java function handler());

Respectfull
Alex