HiveBrain v1.2.0
Get Started
← Back to all entries
debugMinor

Add events dispatching for ActionScript framework Robotlegs

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
eventsrobotlegsdispatchingforactionscriptframeworkadd

Problem

I have the following function in a Service class

protected function writeToFile():void{
        var destinationFile:File = File.applicationStorageDirectory.resolvePath(_newPath);

        var fs:FileStream = new FileStream();

        fs.open(destinationFile, FileMode.WRITE);
        fs.writeBytes(_buffer, 0, _buffer.length);

        fs.close();         

    }


this Service class should dispatch a SuccessfulCreateLocalFileEvent when writeToFile is successful and an UnsuccessfulCreateLocalFileEvent if writeToFile is unsuccessful.

I know that FileStream functions open and writeBytes may throw IOError if there is any problems. And all these FileStream functions all have void as return types.

I believe I need try catch, but I am not entirely sure how to do so.

Please advise.

This question is replicated at http://knowledge.robotlegs.org/discussions/questions/924-what-is-best-way-to-write-dispatch-events-in-this-service-class-function Experimenting which site has better answers for such a question.

Solution

FileStream class has its own events which are dispatched when the file operation is successful or not.

http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/filesystem/FileStream.html#eventSummary

A possible refactor of the code is

protected function writeToFile():void{
    _destinationFile = File.applicationStorageDirectory.resolvePath(_newPath);

    var fs:FileStream = new FileStream();

    fs.open(_destinationFile, FileMode.WRITE);
    fs.writeBytes(_buffer, 0, _buffer.length);

    fs.addEventListener(Event.CLOSE, dispatchFileCreatedEvent);

    fs.close(); 

}


Thanks to krasimir of knowledge Robotlegs Community for his help in arriving at this answer.

Code Snippets

protected function writeToFile():void{
    _destinationFile = File.applicationStorageDirectory.resolvePath(_newPath);

    var fs:FileStream = new FileStream();

    fs.open(_destinationFile, FileMode.WRITE);
    fs.writeBytes(_buffer, 0, _buffer.length);

    fs.addEventListener(Event.CLOSE, dispatchFileCreatedEvent);

    fs.close(); 

}

Context

StackExchange Code Review Q#12061, answer score: 3

Revisions (0)

No revisions yet.