Skip to content
Browse files

Test for injection bound signals in JS

  • Loading branch information...
1 parent 0bab2f5 commit 2a05c120fc2172005c1cecf46fb1a28df3481814 @dionjwa dionjwa committed
View
59 demo/testJSSignalInjection/JSSignalTester.hx
@@ -0,0 +1,59 @@
+package;
+
+class JSSignalTester
+{
+ public function new ()
+ {
+ var foo = new Foo();
+ var bar = new Bar();
+
+ foo.signaler.bind(bar.listener);
+ foo.signaler.bind(Reflect.field(bar, "listener"), bar);
+
+ fire();
+ }
+
+ public static function main()
+ {
+ new JSSignalTester();
+ }
+
+}
+
+class Foo
+{
+ public var signaler :Signaler<String>;
+
+ public function new ()
+ {
+ signaler = new DirectSignaler(this);
+ }
+
+ public function fire () :Void
+ {
+ signaler.dispatch("firing");
+ }
+
+ public function toString () :String
+ {
+ return "Foo";
+ }
+}
+
+class Bar
+{
+ public function new ()
+ {
+ }
+
+ public function listener (signalText :String) :Void
+ {
+ trace(signalText + " recieved on " + this);
+ }
+
+ public function toString () :String
+ {
+ return "Bar";
+ }
+
+}
View
34 lib/hsl-pico/hsl/haxe/DirectSignaler.hx
@@ -69,9 +69,15 @@ class DirectSignaler<Datatype> implements Signaler<Datatype> {
}
bubblingTargets.add(value);
}
+ #if js
+ public function bind(listener:Datatype -> Dynamic, ?context :Dynamic):Bond {
+ return sentinel.add(new RegularBond(listener, context));
+ }
+ #else
public function bind(listener:Datatype -> Dynamic):Bond {
return sentinel.add(new RegularBond(listener));
}
+ #end
public function bindAdvanced(listener:Signal<Datatype> -> Dynamic):Bond {
return sentinel.add(new AdvancedBond<Datatype>(listener));
}
@@ -290,16 +296,34 @@ private class SentinelBond<Datatype> extends LinkedBond<Datatype> {
*/
private class RegularBond<Datatype> extends LinkedBond<Datatype> {
private var listener:Datatype -> Void;
+ #if js
+ private var context :Dynamic;
+
+ public function new(listener:Datatype -> Void, ?context :Dynamic):Void {
+ super();
+ this.context = context;
+ this.listener = listener;
+ }
+ #else
public function new(listener:Datatype -> Void):Void {
super();
this.listener = listener;
}
+ #end
public override function callListener(data:Datatype, currentTarget:Subject, origin:Subject, propagationStatus:Int):Int {
if (false == halted) {
// #if js
// Reflect.callMethod(origin,
// #else
+ #if js
+ if (context != null) {
+ untyped listener.call(context, data);
+ } else {
+ listener(data);
+ }
+ #else
listener(data);
+ #end
// #end
}
return propagationStatus;
@@ -314,6 +338,13 @@ private class RegularBond<Datatype> extends LinkedBond<Datatype> {
return Std.is(value, RegularBond) && Reflect.compareMethods((untyped value).listener, listener);
#end
}
+
+ #if js
+ public override function destroy():Void {
+ super.destroy();
+ context = null;
+ }
+ #end
}
/**
* A niladic bond is a bond that is created in result of a call to the bindVoid method.
@@ -379,7 +410,8 @@ private class AdvancedBond<Datatype> extends LinkedBond<Datatype> {
#end
}
}
-class PropagationStatus {
+class PropagationStatus
+{
public static inline var IMMEDIATELY_STOPPED:Int = 1;
public static inline var STOPPED:Int = 2;
public static inline var UNDISTURBED:Int = 3;
View
5 lib/hsl-pico/hsl/haxe/Signaler.hx
@@ -93,7 +93,12 @@ interface Signaler<Datatype> {
* system, though the bindAdvanced method of this library has more similarities to addEventListener.
* </li></ul>
*/
+
+ #if js
+ public function bind(listener:Datatype -> Dynamic, ?context :Dynamic):Bond;
+ #else
public function bind(listener:Datatype -> Dynamic):Bond;
+ #end
/**
* Binds this signaler to a listener function that accepts a signal, and returns nothing. Returns the bond between the
* signaler. The bond can be removed by calling either the unbindAdvanced method of the signaler, or the destroy method of
View
13 src/com/pblabs/engine/injection/ComponentInjector.hx
@@ -97,17 +97,10 @@ class ComponentInjector extends Injector
// return cast(signalProp, Signaler<Dynamic>).bind(Reflect.field(obj, listenerMethodName));
}
- #if flash
- return signaller.bind(Reflect.field(obj, listenerMethodName));
- #elseif js
- var listener = Reflect.field(obj, listenerMethodName);
- var f = function (arg :Dynamic) :Void {
- listener(arg);
- }
- return signaller.bind(f);
+ #if js
+ return signaller.bind(Reflect.field(obj, listenerMethodName), obj);
#else
- Log.error("Platform untested with injection bound signals");
- return null;
+ return signaller.bind(Reflect.field(obj, listenerMethodName));
#end
}

0 comments on commit 2a05c12

Please sign in to comment.
Something went wrong with that request. Please try again.