Skip to content

Instantly share code, notes, and snippets.

@vrobel
Created September 29, 2011 00:02
Show Gist options
  • Save vrobel/1249637 to your computer and use it in GitHub Desktop.
Save vrobel/1249637 to your computer and use it in GitHub Desktop.
Promise based service
package com.blackmoondev.services {
import flash.events.ErrorEvent;
import flash.utils.Dictionary;
import org.robotlegs.oil.async.Promise;
public class BasePromiseService {
protected var loaders:Dictionary = new Dictionary();
protected var promises:Dictionary = new Dictionary();
/**
* Save a promise for given loader.
* If promise not provided - new one is created
* @param loader - loader related to a promise
* @param promise - optional promise
* @return new or provided promise
*/
protected function savePromise(loader:*, promise:Promise = null):Promise {
promise ||= new Promise;
if(loader == null)
throw new ArgumentError('Loader must be a non-null object');
if (loaders[promise]) {
switchLoader(loaders[promise], loader);
} else {
promises[loader] = promise;
loaders[promise] = loader;
}
return promise;
}
protected function getPromise(loader:*):Promise {
return promises[loader];
}
protected function releasePromise(promise:Promise):void
{
const loader:* = loaders[promise];
delete promises[loader];
delete loaders[promise];
}
private function switchLoader(oldLoader:*, newLoader:*):void {
if(oldLoader === newLoader)
return;
const p:Promise = promises[oldLoader];
promises[newLoader] = p;
delete promises[oldLoader];
loaders[p] = newLoader;
}
protected function onErrorEventDefaultHandler(e:ErrorEvent):void {
handleError(e.currentTarget, e.type + ' :: ' + e.text)
}
protected function defaultPromiseToResultProcessor(p:Promise):*
{
return p.result
}
protected function handleError(loader:*, error:*):void {
const p:Promise = promises[loader]
if(p) {
releasePromise(p);
p.handleError(error);
}
}
protected function handleResult(loader:*, result:*):void {
const p:Promise = promises[loader]
if(p) {
releasePromise(p)
p.handleResult(result);
}
}
}
}
package com.blackmoondev.services {
import flash.events.Event;
import flash.events.IOErrorEvent;
import flash.net.FileReference;
import org.robotlegs.oil.async.Promise;
public class BrowseForFilesService extends BasePromiseService implements IBrowseForFilesService {
public function browseForFile(typeFilters:Array = null):Promise {
var fr:FileReference = new FileReference();
fr.addEventListener(Event.CANCEL, onCancel);
fr.addEventListener(Event.SELECT, onSelect);
fr.browse(typeFilters);
return savePromise(fr)
}
private function onSelect(e:Event):void {
var fr:FileReference = e.currentTarget as FileReference;
fr.addEventListener(Event.COMPLETE, onLoadComplete);
fr.addEventListener(IOErrorEvent.IO_ERROR, onErrorEventDefaultHandler);
fr.load();
}
private function onLoadComplete(e:Event):void {
var fr:FileReference = e.currentTarget as FileReference;
handleResult(fr, fr)
}
private function onCancel(e:Event):void {
handleError(e.currentTarget, e.type);
}
override protected function releasePromise(promise:Promise):void {
var fr:FileReference = loaders[promise];
fr.removeEventListener(Event.CANCEL, onCancel);
fr.removeEventListener(Event.SELECT, onSelect);
fr.removeEventListener(Event.COMPLETE, onLoadComplete);
fr.removeEventListener(IOErrorEvent.IO_ERROR, onErrorEventDefaultHandler);
super.releasePromise(promise);
}
}
}
package com.blackmoondev.services {
import org.robotlegs.oil.async.Promise;
public interface IBrowseForFilesService {
function browseForFile(typeFilters:Array = null):Promise
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment