Skip to content
Tom Byrne edited this page Feb 14, 2015 · 2 revisions

Sometimes when using @inject to inject multiple properties, it's helpful to know when all of the properties have been injected. This can be done by adding the @promise metadata to a method with a list of the properties that should be watched for. The method will be called immediately after all properties get set.

Notice that it is possible to use non-injected variables in your promises (active in the below example). At compile-time these variables will be wrapped in getter/setters to facilitate the watching process (or the setter will be amended if it already has one).

	class MainPlayer extends AbstractTrait{
		
		public var active:Bool;
		
		@inject({asc:true})
		private var gamepad:IInputDevice;
		
		@inject
		private var controller:CharacterMotionControl;
	
		public function new(){
			super();
		}
		
		@promise("active", "gamepad", "controller")
		public function onPromiseMet(met:Bool):Void{
			if(met){
				trace("Both traits have been added and are ready to bind together");
				controller.setInput(gamepad);
			}else{
				trace("One of the traits is about to be removed, unbind them now");
				controller.setInput(null);
			}
		}
	}

How promises behave

  • The first call to your bound method will be called as soon as all applicable variables have been injected, the met parameter will always be true on the first call.
  • If any of the properties get set to another value, then the method will be called twice, once before the property is committed with met=false, then again after the value has been set with met=true. This will really only happen when promises are pointing to non-injected properties (which are set outside of the composure framework).
  • If any of the applicable traits are removed (i.e. the property is set to null) then the method will be called again with met=false

Clone this wiki locally