Custom Pipe Messages

Strongly typed messages are used to communicate between cores in PureMVC MultiCore application with Pipes Utility. Below is a simple example of a custom pipe message class, and a JunctionMediator using the custom message. Message names are defined in the message itself which makes the message handling very easy.

ContactMessage.as

package com.mariusht.contactmanager.common.messages
{
    import com.mariusht.contactmanager.common.model.vo.ContactVO;

    import org.puremvc.as3.multicore.utilities.pipes.messages.Message;

    public class ContactMessage extends Message
    {
        protected static const NAME:String = 'ContactMessage';

        public static const ADD_CONTACT:String = NAME + '/message/contact/add';
        public static const CONTACT_ADDED:String = NAME + '/message/contact/added';
        public static const CONTACT_ADD_FAILED:String = NAME + '/message/contact/add/failed';

        public static const UPDATE_CONTACT:String = NAME + '/message/contact/update';
        public static const CONTACT_UPDATED:String = NAME + '/message/contact/updated';
        public static const CONTACT_UPDATE_FAILED:String = NAME + '/message/contact/update/failed';

        public static const REMOVE_CONTACT:String = NAME + '/message/contact/remove';
        public static const CONTACT_REMOVED:String = NAME + '/message/contact/removed';
        public static const CONTACT_REMOVE_FAILED:String = NAME + '/message/contact/remove/failed';

        public function ContactMessage(type:String, contact:ContactVO=nulll)
        {
            // add headers if needed
            super(type, null, contact);
        } 

        public function get contact():ContactVO
        {
            return body as ContactVO;
        }
    }
}

JunctionMediator.as

...
override public function handlePipeMessage(message:IPipeMessage):void
{
    switch(message.getType())
    {
        case ScreenMessage.GET:
            //
            break;
        case ContactMessage.CONTACT_ADDED:
            contactsProxy.addContact( ContactMessage(message).contact );
            break;
        case ContactMessage.CONTACT_UPDATED:
            contactsProxy.updateContact( ContactMessage(message).contact );
            break;
        case ContactMessage.CONTACT_REMOVED:
            contactsProxy.removeContact( ContactMessage(message).contact );
            break;
        case ItemMessage.ITEM_ADDED:
            itemsProxy.addItem( ItemMessage(message).item );
            break;
        case ItemMessage.ITEM_UPDATED:
            itemsProxy.updateItem( ItemMessage(message).item );
            break;
        case ItemMessage.ITEM_REMOVED:
            itemsProxy.removeItem( ItemMessage(message).item );
            break;
    }
}
...

Leave a comment