Warrior Tang (tangaroa) wrote,
Warrior Tang

How to use PureMVC's Observer class in Haxe

// Example use of the Observer pattern implementation provided by PureMVC
// Requires puremvc's haxe package unzipped to %HAXE_LIBRARY_PATH%
// Save as observe.hx and compile as: 
// haxe Observe -main Observe -neko out.n
// Run as:
// neko out.n

// import org.puremvc.haxe.ImportAll; // This doesn't include Observer or Notification.
import org.puremvc.haxe.patterns.observer.Observer;
import org.puremvc.haxe.patterns.observer.Notification;

class MyNoteData {
	// Example of app-specific data that may be passed in a Note.
	static public var special_data = 0x1234;
	static public var more_data = 0x1234;

	public function new(); 

	public function toString():String{
		// A toString function is needed for debugging.
		// Otherwise your program will crash in Notification.hx
		// if you try to trace the notification to see what it is.
		return "special data";

class MyNote extends Notification {
	// A custom note created for the application. 
	public function new(){
		// Create the notification with its members:
		// First arg: The notification's name is a string.
		// Second arg: The body is custom data sent with the notification.
		// Third arg: The type is a second string you can use to distinguish types of notifications. 
		super("myNote", new MyNoteData(), "myNoteType");
		// super("myNote") should work if you only need one string
		// to describe any of your notifications. 

class ASDF extends Observer {
	// The app's custom extension of Observer. 
	public static var noteDefault:Notification = new MyNote(); 

	public function new(a,b){

	public function doSomething():Void{
		// The app's custom logic that may trigger a notification

class Whatever { 
	// The app's class that needs to be notified when an Observer does something.
	public var foo:ASDF; // Observer
	public var i:Int; // A counter. 

	public function new(){
		i = 0;

		// Launch the Observer with...
		// First arg: the notifyMethod is a function to call. 
		// Second arg: the notifyContext is the class to send the notifyMethod's signal to.
 		foo = new ASDF(iup, this); // Should run this.iup() on notification

	public function iup(arg:Notification){
		// A function that is called on notification.
		// The received argument is the notification object. 
		i += 1;
		neko.Lib.println("Arg is:\n{\n" + arg + "\n}");

class Observe {

	static function main (){
 		var a = new Whatever();
 		var b = new Whatever();
		neko.Lib.println(a.i); // Should be 0 (Just initialized)
		neko.Lib.println(a.i);  // Should be 2 (notified twice)

		neko.Lib.println(b.i); // Should be 0 (not touched)

		// Output should be "020"


I don't normally deploy code with names like that, but this is just a hack and understanding what is happening is more important than what I name the classes.

Publishing this because "Dynamic -> Void" is insufficient documentation and I could not find any usage examples.

I read that Observer is supposed to be an internal class not meant for public use, but this is what I found when I looked for an Observer class for Haxe, and it works.

If Observer is supposed to be internal, there might be some other signaling method that is supposed to be better for third-party use but I haven't found it yet. Only after writing this all out I discover Robert Penner's Signals library and the Haxe signaling library ... will have to look into them.


  • Post a new comment


    default userpic

    Your IP address will be recorded 

    When you submit the form an invisible reCAPTCHA check will be performed.
    You must follow the Privacy Policy and Google Terms of use.