mrthuanvn / fabrication

Automatically exported from code.google.com/p/fabrication
0 stars 0 forks source link

Proxy IoC not working properly? #15

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. Use IoC to injext proxy
2. Use async object (any object that dispatcher ie. COMPLETE event)
3. Listen to COMPLETE event and use injected proxy

Sample source below.

What is the expected output? What do you see instead?

I expect my proxy not to be null, but it is. Below is my trick to remember 
reference to proxy to use it in other methods.

What version of the product are you using? On what operating system?

0.7.4

Please provide any additional information below.

Here is my code:

I use JPEGAyncEncoder as Async object 
(http://www.switchonthecode.com/tutorials/flex-tutorial-an-asynchronous-jpeg-enc
oder)

public class SendSnapshotCommand extends SimpleFabricationCommand {

        [InjectProxy]
        public var snapshotProxy:SnapshotProxy;

        [InjectMediator]
        public var applicationMediator:ApplicationMediator;

        /* SnapshotPorxy instance to remember reference to proxy */
        private var mySnapshotProxy:SnapshotProxy;

        public function SendSnapshotCommand() {
            super();
        }        

        public override function execute(notification:INotification):void {
            super.execute(notification);

            /* Remember snapshot reference */
            mySnapshotProxy = snapshotProxy;

            /* Get snapshot bitmap data */
            var bitmapData:BitmapData = ImageSnapshot.captureBitmapData(applicationMediator.application.mainPanel);

            /* Encode JPEG */
            var jpegEncoder:JPEGAsyncEncoder = new JPEGAsyncEncoder(80);
            jpegEncoder.PixelsPerIteration = 200;

            jpegEncoder.addEventListener(ProgressEvent.PROGRESS, jpegEncoder_PROGRESS);
            jpegEncoder.addEventListener(JPEGAsyncCompleteEvent.JPEGASYNC_COMPLETE, jpegEncoder_JPEGASYNC_COMPLETE);

            jpegEncoder.encode(bitmapData);

            /* Turn on indicator */
            facade.notifyObservers(new IndicatorShowNotification(SHOW_INDICATOR, false, "Wysyłanie obrazka do likwidatora"));

        }

//________________________________________________________________________
//                                  EVENT LISTENERS

        private function jpegEncoder_PROGRESS(event:ProgressEvent):void {

            trace(event.bytesLoaded / event.bytesTotal);
            facade.sendNotification(PROGRESS_INDICATOR, event.bytesLoaded / event.bytesTotal);
        }//jpegEncoder_PROGRESS

        private function jpegEncoder_JPEGASYNC_COMPLETE(event:JPEGAsyncCompleteEvent):void {

            trace("Completed");
            facade.sendNotification(HIDE_INDICATOR);
            mySnapshotProxy.sendSnapshot(liquidatorId, event.ImageData);
        }//jpegEncoder_JPEGASYNC_COMPLETE

    }

Original issue reported on code.google.com by Damian.R...@gmail.com on 5 Aug 2010 at 3:49

GoogleCodeExporter commented 9 years ago
The problem is that after command execution facade calls on command instance 
dispose method. This approach is essential if you want to have proper memory 
management using Fabrication. Commands are created "on the fly" and every 
reference it has inside ( injected references ) should be nulled before command 
is destroyed. So, in your handler, wich is called after Command.dispose you 
don't have access to proxy. Another question is the fact that you should not 
use command this way - commands, by design, are stateless so using events in 
command is not very good idea. If you need it to be done this way the solution 
is to use old-fashioned retrieveProxy method.

Original comment by rafael.s...@gmail.com on 5 Aug 2010 at 5:53

GoogleCodeExporter commented 9 years ago
OK, thank you.

Where should I use event then? In Mediators? 
As I've read Commands should be used for logic as often as possible, am I wrong?
Sometimes it's difficult to decide where to put some actions... command, 
mediator or proxy... :) It's because lack of experience I guess :)

Is there any forum about Fab, so that I could move this topic there?

Original comment by Damian.R...@gmail.com on 5 Aug 2010 at 6:16

GoogleCodeExporter commented 9 years ago
Hi

Commands should manage all application logic, but this means that they are 
responsible of telling all other actors what to do instead of doing it for 
themselves. So, in your example, I'd move all encoding related action into 
proxy - snapshot proxy should be responsible for managing data ( in this case 
encoding and sending snapshot data ). HIDE_INDICATOR data can be there send 
form proxy body.

P.S.

You can check and post here: http://forums.puremvc.org/index.php?board=63.0, 
but if you find something that seems to be a bug - this you should do it as 
you've done this time :)

Original comment by rafael.s...@gmail.com on 5 Aug 2010 at 6:29

GoogleCodeExporter commented 9 years ago
Ok. Thanks again :)

Original comment by Damian.R...@gmail.com on 5 Aug 2010 at 6:35

GoogleCodeExporter commented 9 years ago
YOu can read about this in here http://puremvc.org/content/view/80/188/, too.

Original comment by rafael.s...@gmail.com on 5 Aug 2010 at 6:51