Skip to content

Commit

Permalink
More Promize
Browse files Browse the repository at this point in the history
  • Loading branch information
dmouton committed Oct 19, 2017
1 parent 5ec612c commit b4857b1
Show file tree
Hide file tree
Showing 9 changed files with 165 additions and 92 deletions.
24 changes: 19 additions & 5 deletions Main.hx
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package ;

import js.html.Image;
import org.tamina.net.AssetCompositeLoader;
import org.tamina.net.AssetsSequenceLoader;
import org.tamina.net.AssetURL;
import org.tamina.html.component.HTMLComponentEvent.HTMLComponentEventType;
import test.html.view.TestComponent;
Expand Down Expand Up @@ -75,14 +78,25 @@ typedef MainEvent = Event<String>;
NumberUtils.toFixed(0, 0);
ObjectUtils.merge({}, {});
UID.getUID();
var url = new URL("http://test.com");
var url = new URL("https://cdn.heidi.tech/partners/6/designs/1844052/249990b2-51f5-4d3a-89d6-5796ee87d402.png");
trace(url.scheme);
var l = new ImageLoader();
l.load(url);
/*new XMLLoader();
new AssetCompositeLoader();*/
l.load(url).then(function(image:Image){
Browser.document.body.appendChild(image);
}).catchError(function(error:Error){
trace(error.message);
});

var xmlLoader = new XMLLoader();
xmlLoader.load(new URL("https://raw.githubusercontent.com/bryanlittlefield/Magento-local.xml-Template/master/local.xml")).then(function(value){trace(value);}).catchError(function(value){trace(value);});

var assetLoader = new AssetLoader();
assetLoader.load(new AssetURL("toto.js")).then(function(value){trace(value);}).catchError(function(value){trace(value);});
assetLoader.load(new AssetURL("toto.js")).then(function(value){trace(value);}).catchError(function(value){trace(value.message);});

var assetListLoader = new AssetsSequenceLoader();
assetListLoader.load([new AssetURL("https://code.jquery.com/jquery-3.2.1.slim.min.js"),new AssetURL("https://code.createjs.com/easeljs-0.8.2.min.js")]).then(function(value){trace(value);}).catchError(function(value){trace(value);});

var compositeLoader = new AssetCompositeLoader();

var myComponent:TestComponent = HTMLApplication.createInstance(TestComponent);
myComponent.addEventListener(HTMLComponentEventType.CREATION_COMPLETE, myComponent_creationCompleteHandler);
Expand Down
23 changes: 17 additions & 6 deletions org/tamina/io/ImageLoader.hx
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package org.tamina.io;

import js.Promise;
import org.tamina.events.html.ImageEvent;
import haxe.Timer;
import js.Browser;
import js.html.Image;
import org.tamina.net.URL;
import js.Error;

/**
* The ImageLoader class downloads image data from a URL. It is useful for downloading images files, to be used in a dynamic, data-driven application.<br>
Expand Down Expand Up @@ -34,6 +35,9 @@ class ImageLoader {

private var _image:Image;
private var _toDataURL:Bool;
private var _url:URL;
private var _resolve:Image->Void;
private var _reject:Error->Void;

private function get_image():Image {
return _image;
Expand Down Expand Up @@ -64,17 +68,24 @@ class ImageLoader {
* @method load
* @param url {URL} A string representing the attribute's name
*/
public function load(url:URL):Void {
public function load(url:URL):Promise<Image> {
_url = url;
_image.addEventListener(ImageEvent.LOAD, imageLoadHandler, false);
_image.addEventListener(ImageEvent.ERROR, imageLoadErrorHandler, false);
_image.src = url.path;
return new Promise<Image>(start);
}

private function start(resolve:Image->Void,reject:Error->Void):Void{
_resolve = resolve;
_reject = reject;
_image.src = _url.path;
}

private function imageLoadErrorHandler(event:js.html.Event):Void {
_image.removeEventListener(ImageEvent.LOAD, imageLoadHandler);
_image.removeEventListener(ImageEvent.ERROR, imageLoadErrorHandler);

//error.dispatch();
_reject(new Error("Error while loading : " + _url.path));
}

private function imageLoadHandler(event:js.html.Event):Void {
Expand All @@ -90,9 +101,9 @@ class ImageLoader {

var outputImage = new Image();
outputImage.src = canvas.toDataURL();
//complete.dispatch(outputImage);
_resolve(outputImage);
} else {
//complete.dispatch(_image);
_resolve(_image);
}
}
}
42 changes: 26 additions & 16 deletions org/tamina/net/AssetCompositeLoader.hx
Original file line number Diff line number Diff line change
@@ -1,47 +1,57 @@
package org.tamina.net;

import js.Promise;
import org.tamina.log.QuickLogger;
import js.Error;

class AssetCompositeLoader {

private var _pool:Array<GroupURL>;
private var _resolve:Bool->Void;
private var _reject:Array<Error>->Void;
private var _errors:Array<Error>;

public function new() {
_pool = new Array<GroupURL>();
_errors = new Array<Error>();
}

public function add(group:GroupURL):Void{
_pool.push(group);
}

public function start():Void{
loadNextGroup();
public function start():Promise<Bool>{
return new Promise(loadNextGroup);
}

private function loadNextGroup():Void {
private function loadNextGroup(resolve:Bool->Void,reject:Array<Error>->Void):Void {
_resolve = resolve;
_reject = reject;
if (_pool.length > 0) {
var g = _pool.shift();
if(g.loadingType == AssetLoadingType.SEQUENCE){
var loader = new AssetListLoader();
loader.completeSignal.add(assetCompleteHandler);
loader.errorSignal.add(assetErrorHandler);
loader.load(g.toArray());
var loader = new AssetsSequenceLoader();
loader.load(g.toArray()).then(assetCompleteHandler).catchError(assetErrorHandler);
} else {
var loader = new AssetParallelLoader();
loader.completeSignal.add(assetCompleteHandler);
loader.errorSignal.add(assetErrorHandler);
loader.load(g.toArray());
loader.load(g.toArray()).then(assetCompleteHandler).catchError(assetErrorHandler);
}
} else {
QuickLogger.info("ALL ASSETS LOADED");
//completeSignal.dispatch();
if(_errors.length == 0){
L.info("ALL ASSETS LOADED");
_resolve(true);
} else {
_reject(_errors);
}
}
}

private function assetCompleteHandler():Void {
loadNextGroup();
private function assetCompleteHandler(value:Bool):Void {
loadNextGroup(_resolve,_reject);
}

private function assetErrorHandler():Void {
loadNextGroup();
private function assetErrorHandler(errors:Array<Error>):Void {
_errors = _errors.concat(errors);
loadNextGroup(_resolve,_reject);
}
}
35 changes: 0 additions & 35 deletions org/tamina/net/AssetListLoader.hx

This file was deleted.

14 changes: 7 additions & 7 deletions org/tamina/net/AssetLoader.hx
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package org.tamina.net;

import org.tamina.net.AssetType;
import js.html.Event;
import org.tamina.net.AssetType;
import js.Promise;
import haxe.ds.Either;
import js.Browser;
import js.html.Element;
import js.html.ScriptElement;
import js.html.LinkElement;
import js.Error;

import org.tamina.log.QuickLogger;

Expand All @@ -18,7 +18,7 @@ class AssetLoader {
private var _asset:Element;
private var _cache:AssetLoaderCache;
private var _resolve:Bool->Void;
private var _reject:Bool->Void;
private var _reject:Error->Void;

public function new() {
_cache = new AssetLoaderCache();
Expand All @@ -31,7 +31,7 @@ class AssetLoader {
return new Promise(loadPromise);
}

private function loadPromise(resolve:Bool->Void,reject:Bool->Void):Void{
private function loadPromise(resolve:Bool->Void,reject:Error->Void):Void{
_resolve = resolve;
_reject = reject;
if ( _cache.isLoaded(_url) ) {
Expand Down Expand Up @@ -69,15 +69,15 @@ class AssetLoader {
}
}

private function loadCompleteHandler(?event:Dynamic):Void {
private function loadCompleteHandler(?event:Event):Void {
QuickLogger.info('asset loaded');
_cache.addLoadedAsset(_url);
_cache.removeLoadingAsset(_asset);
_resolve(true);
}

private function loadErrorHandler(event:Dynamic):Void {
private function loadErrorHandler(event:Event):Void {
_cache.removeLoadingAsset(_asset);
_reject(false);
_reject(new Error("Error while loading " + _url.path));
}
}
30 changes: 16 additions & 14 deletions org/tamina/net/AssetParallelLoader.hx
Original file line number Diff line number Diff line change
@@ -1,43 +1,45 @@
package org.tamina.net;

import js.Promise;
import js.Error;
import org.tamina.log.QuickLogger;

class AssetParallelLoader {
class AssetParallelLoader extends BaseAssetsLoader{

private var _pool:Array<AssetURL>;
private var _remainingAssetNumber:Int=0;

public function new() {
_pool = new Array<AssetURL>();
super();
}

public function load(assets:Array<AssetURL>):Void {
override public function load(assets:Array<AssetURL>):Promise<Bool> {
_pool = assets;
_remainingAssetNumber = _pool.length;
start();
return new Promise(start);
}

private function start():Void{
private function start(resolve:Bool->Void,reject:Array<Error>->Void):Void{
_resolve = resolve;
_reject = reject;
for(i in 0..._pool.length){
var l = new AssetLoader();
l.completeSignal.add(assetCompleteHandler);
l.errorSignal.add(assetErrorHandler);
l.load(_pool[i]);
l.load(_pool[i]).then(assetCompleteHandler).catchError(assetErrorHandler);
}
}

private function assetCompleteHandler():Void {
private function assetCompleteHandler(value:Bool):Void {
_remainingAssetNumber--;
if(_remainingAssetNumber == 0){
//completeSignal.dispatch();
end();
}
}

private function assetErrorHandler():Void {
QuickLogger.error('error while loading asset');
private function assetErrorHandler(error:Error):Void {
L.error('error while loading asset');
_remainingAssetNumber--;
_errors.push(error);
if(_remainingAssetNumber == 0){
//completeSignal.dispatch();
end();
}
}
}
35 changes: 35 additions & 0 deletions org/tamina/net/AssetsSequenceLoader.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package org.tamina.net;

import js.Promise;
import js.Error;

class AssetsSequenceLoader extends BaseAssetsLoader {

public function new() {
super();
}

override public function load(assets:Array<AssetURL>):Promise<Bool> {
_pool = assets;
return new Promise(loadNextAsset);
}

private function assetCompleteHandler(value:Bool):Void {
loadNextAsset(_resolve,_reject);
}

private function assetErrorHandler(error:Error):Void {
_errors.push(error);
loadNextAsset(_resolve,_reject);
}

private function loadNextAsset(resolve:Bool->Void,reject:Array<Error>->Void):Void {
_resolve = resolve;
_reject = reject;
if (_pool.length > 0) {
_loader.load(_pool.shift()).then(assetCompleteHandler).catchError(assetErrorHandler);
} else {
end();
}
}
}
32 changes: 32 additions & 0 deletions org/tamina/net/BaseAssetsLoader.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package org.tamina.net;

import js.Promise;
import js.Error;

class BaseAssetsLoader {

private var _pool:Array<AssetURL>;
private var _loader:AssetLoader;
private var _resolve:Bool->Void;
private var _reject:Array<Error>->Void;
private var _errors:Array<Error>;

public function new() {
_pool = new Array<AssetURL>();
_loader = new AssetLoader();
_errors = new Array<Error>();
}

public function load(assets:Array<AssetURL>):Promise<Bool> {
_pool = assets;
return null;
}

private function end():Void{
if(_errors.length == 0){
_resolve(true);
} else {
_reject(_errors);
}
}
}
Loading

0 comments on commit b4857b1

Please sign in to comment.