Skip to content

Instantly share code, notes, and snippets.

@zaynyatyi
Last active October 11, 2016 17:40
Show Gist options
  • Select an option

  • Save zaynyatyi/1a09179c8bf7eb0916845ea6a0294737 to your computer and use it in GitHub Desktop.

Select an option

Save zaynyatyi/1a09179c8bf7eb0916845ea6a0294737 to your computer and use it in GitHub Desktop.
Typed message in hexMachina illustration
package test.message;
/**
* ...
* @author Heorhiy Kharvat
*/
class MessageData
{
public var id:Int;
public var message:String;
public function new(id:Int, message:String)
{
this.id = id;
this.message = message;
}
}
package test.module.welcome.view;
import test.message.MessageData;
import hex.event.MessageType;
import hex.view.viewhelper.ViewHelper;
/**
* ...
* @author Heorhiy Kharvat
*/
class OtherViewHelper extends ViewHelper<IOtherView>
{
public function new()
{
super();
}
override function _initialize() : Void
{
super._initialize();
dispatcher.addHandlerTyped(WelcomeModuleMessage.TYPED_TEST, {}, handleTyped);
dispatcher.addHandlerTyped(WelcomeModuleMessage.COMPLEX_TYPED_TEST, {}, handleComplex);
//dispatcher.addHandlerTyped(WelcomeModuleMessage.TYPED_TEST, {}, handleDummy); <-- will fire compilation error
//dispatcher.addHandlerTyped(WelcomeModuleMessage.COMPLEX_TYPED_TEST, {}, handleDummy); <-- will fire compilation error
}
// we can be sure here will be Int if we will use typed dispatches
function handleTyped(id:Int):Void
{
trace(id);
}
// we can easily handle data here
function handleComplex(data:MessageData):Void
{
trace(data.id, data.message);
}
function handleDummy(something:String):Void
{
}
}
package test.module.welcome.view;
import hex.event.MessageType;
import hex.view.viewhelper.ViewHelper;
/**
* ...
* @author Heorhiy Kharvat
*/
class SomeViewHelper extends ViewHelper<ISomeView>
{
public function new()
{
super();
}
function triggerDispatches():Void
{
dispatcher.dispatchTyped(WelcomeModuleMessage.TYPED_TEST, 1);
dispatcher.dispatchTyped(WelcomeModuleMessage.COMPLEX_TYPED_TEST, new MessageData(1, "test"));
//dispatcher.dispatchTyped(WelcomeModuleMessage.COMPLEX_TYPED_TEST, "string data"); <-- will fire compilation error
}
}
package test.message;
import hex.event.MessageType;
import hex.event.TypedMessageType;
/**
* ...
* @author Heorhiy Kharvat
*/
class TestModuleMessage
{
static public var SIMPLE_TYPED_TEST = new TypedMessageType<Int>("simple_typed_test");
static public var COMPLEX_TYPED_TEST = new TypedMessageType<MessageData>("simple_typed_test");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment