Q to understand Signals BP
Hi all!
I want to understand if I am using signals right (Best Practices).
I have RenderGraphics component in my view components.
It has -- public var frame :Signal = new Signal;
and dispatches it every frame -- frame.dispatch(posX, posY);
In my RenderGraphicsMediator I have:
[Inject] public var enterFrameSignal :EnterFrameSignal;
that is mapped as singleton in context -- injector.mapSingleton(EnterFrameSignal);
What I am doing is when "frame" signal comes from view component I am transmitting it to framework:
view.frame.add(frameUpdated);
private function frameUpdated(posX:int, posY:int):void
{
enterFrameSignal.dispatch(posX, posY);
}
So, RenderGraphics has a good incapsulation (dont depends on EnterFrameSignal class). But I have many signals that I want just to transmit to framework (no additional logic in Mediator) - and for every one I need to create custom Signal class an code in mediator.
Maybe there is any way just to transmit signal from component to framework? (which not ruins BP approach)
Comments are currently closed for this discussion. You can start a new one.
Keyboard shortcuts
Generic
? | Show this help |
---|---|
ESC | Blurs the current field |
Comment Form
r | Focus the comment reply box |
---|---|
^ + ↩ | Submit the comment |
You can use Command ⌘
instead of Control ^
on Mac
1 Posted by Stray on 08 Nov, 2010 10:51 AM
Hi there,
others may have more thoughts, but here are a few:
For enter-frame, a centralised signal (one for the whole app) is a nice approach that cuts down on overhead - Robert Penner has done some benchmarking that shows that it's an efficient approach compared with listening for enter-frame in multiple places. (You might only be doing it in one place anyway). But in this incidence your EnterFrameSignal seems to carry x and y coordinates, so it might not be relevant to centralise it.
You would have the mediator receive the EnterFrameSignal and expose an API on the view to get x and y ... but that seems like it wouldn't fit with what you need.
Another option is to use the mediator to set the signal on the view.
same as you are currently doing in the mediator:
[Inject] public var enterFrameSignal :EnterFrameSignal;
but then also
onRegister():void
{
view.frame = enterFrameSignal;
}
and in view:
public function set frame(frameSignal:Signal):void
{
_frame = frame;
}
Then you just dispatch as normal in your view, but you'd miss out the stage in the mediator where you re-transmit it.
You could even inject the signal into your view, but things can get tangled fast with view-injection.
Personally I'd probably stay with the more verbose implementation because I think it's possible to see the two signals as being different things that currently happen to be well aligned, but this approach above would be very easy to refactor to the original implementation you have below so I think it's a pretty good compromise.
2 Posted by Stray on 08 Nov, 2010 11:43 AM
Of course I meant that you could even 'auto-inject' the signal to the view. What I described is setter injection - you're still injecting the dependency, just through a function rather than the [Inject] auto-magic route.
3 Posted by havarez on 09 Nov, 2010 03:14 PM
Thank you!
Stray closed this discussion on 10 Feb, 2011 06:08 PM.