Skip to content

Commit 520c0d9

Browse files
working promises
1 parent 6f05569 commit 520c0d9

File tree

4 files changed

+66
-77
lines changed

4 files changed

+66
-77
lines changed

public/evileval.js

+1-6
Original file line numberDiff line numberDiff line change
@@ -50,17 +50,13 @@ define([], function(){
5050
return new Promise(function(resolve, reject){
5151
require([jsAnimPath],
5252
function(animation){
53-
console.log(animation);
5453
var animationClone = Object.create(animation);
5554
canvasAnim.uri = jsAnimPath;
5655
canvasAnim.animation = animationClone;
5756
if(canvasAnim.hasOwnProperty("setup")){
5857
canvasAnim.setup();
5958
}
60-
resolve({canvasAnim:canvasAnim,
61-
position: position,
62-
path: jsAnimPath});
63-
//onLoadCallback(canvasAnim);
59+
resolve(canvasAnim);
6460
},
6561
function(err){
6662
reject(err);
@@ -70,7 +66,6 @@ define([], function(){
7066

7167
loadJsAnimOnCanvasAnim = function(exquis, jsAnimPath, canvasAnim, onLoadCallback){
7268
require([jsAnimPath], function(animation){
73-
console.log(animation);
7469
var animationClone = Object.create(animation);
7570
canvasAnim.uri = jsAnimPath;
7671
canvasAnim.animation = animationClone;

public/exquis.js

-2
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ define(["net",
2727
animationName: animationCode.name,
2828
uri: animationCode.uri,
2929
currentAnimation: null,
30-
3130
borders : function(){
3231
return {
3332
north: context.getImageData(0, 0, context.canvas.width, 1),
@@ -115,7 +114,6 @@ define(["net",
115114
var init = function (exquis, assName, animCodes) {
116115
var container = document.getElementById("container"),
117116
editorController = makeEditorController(exquis);
118-
119117
exquis.assName = assName;
120118

121119
exquis.editorView = editorView(editorController);

public/net.js

+60-64
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,6 @@
11
"use strict";
22

33
define(["iter2d", "evileval"], function(iter2d, evileval){
4-
5-
6-
var loadAnimations2d = function(animationNames, callback ){
7-
8-
9-
var results = [];
10-
var addToResults = function(result, path, position){
11-
var name = /animations\/(\w+)\.js/.exec(path)[1];
12-
13-
if(results[position.row] === undefined){
14-
results[position.row] = [];
15-
}
16-
result.name = name;
17-
results[position.row][position.col] = result;
18-
};
19-
var addToResultsAndCallback = function(result, path, position){
20-
addToResults(result, path, position);
21-
callback(results);
22-
};
23-
24-
var initialPromise = null;
25-
for(var i=0; i<animationNames.length; i++){
26-
var lastrow = i == (animationNames.length - 1);
27-
for(var j=0; j<animationNames[i].length; j++){
28-
var position = {row: i, col: j},
29-
animationName = animationNames[i][j],
30-
lastcol = j == (animationNames[i].length - 1),
31-
handleAnimation = lastcol && lastrow ? addToResultsAndCallback : addToResults;
32-
if(isExternalJs(animationName)){
33-
//TODO this does not work
34-
animationName = /http:.*\/(\w+\.js)/.exec(animationName)[1];
35-
}
36-
//loadAnimation(animationName, handleAnimation, position);
37-
38-
if(initialPromise === null){
39-
initialPromise = evileval.loadJsAnimOnCanvasAnimP(animationName, {});
40-
}else{
41-
initialPromise = initialPromise.then(function(anPaPo){
42-
addToResults(anPaPo.canvasAnim, anPaPo.path, anPaPo.position);
43-
console.log(results);
44-
return evileval.loadJsAnimOnCanvasAnimP(animationName, {});
45-
});
46-
}
47-
}
48-
}
49-
initialPromise.then(function(anPaPo){
50-
addToResults(anPaPo.canvasAnim, anPaPo.path, anPaPo.position);
51-
callback(results);
52-
});
53-
54-
};
554

565
var isExternalJs = function(url){
576
return url.match(/http:\/\//);
@@ -112,9 +61,7 @@ define(["iter2d", "evileval"], function(iter2d, evileval){
11261
ajax.setRequestHeader("Content-type","application/x-www-form-urlencoded");
11362
ajax.onreadystatechange = callback;
11463
ajax.send(params);
115-
11664
};
117-
11865

11966
var makeAnimationFileName = function(animationName){
12067
return "/animations/"+animationName + ".js";
@@ -132,30 +79,79 @@ define(["iter2d", "evileval"], function(iter2d, evileval){
13279
history.pushState({},"...", "/assemblage/" + assName);
13380
}
13481
assemblagePath += assName + ".json";
135-
136-
loadJson(assemblagePath, function(assemblage){
137-
138-
var animationNames = iter2d.map2dArray(assemblage, makeAnimationFileName);
139-
140-
loadAnimations2d(animationNames, function(animCodes){
141-
handleAnimCodes(exquis, assName, animCodes);
82+
HTTPgetJSON(assemblagePath)
83+
.then(function(animationNames){
84+
var animNamesList = animationNames.reduce(function(a, b) {
85+
return a.concat(b);
86+
}),
87+
animPromises = animNamesList.map(function(animName){
88+
var animPath = "/animations/" + animName + ".js";
89+
return evileval.loadJsAnimOnCanvasAnimP(animPath, {});
90+
});
91+
return Promise.all(animPromises);
92+
}).then(function(animCodes){
93+
var animCodes2d = splitarray(animCodes, 3);
94+
handleAnimCodes(exquis, assName, animCodes2d);
14295
});
143-
144-
});
14596
};
146-
97+
98+
var splitarray = function(input, spacing){
99+
var output = [];
100+
for (var i = 0; i < input.length; i += spacing){
101+
output[output.length] = input.slice(i, i + spacing);
102+
}
103+
return output;
104+
};
147105

148106
var loadAnimations = function(exquis, handleJsonAnimations){
149107
var name = window.location.pathname.substr("/assemblage/".length);
150108
loadAssemblage(exquis, name, handleJsonAnimations);
151109
};
110+
111+
var HTTPget = function(url) {
112+
// Return a new promise.
113+
return new Promise(function(resolve, reject) {
114+
// Do the usual XHR stuff
115+
var req = new XMLHttpRequest();
116+
req.open('GET', url);
117+
118+
req.onload = function() {
119+
// This is called even on 404 etc
120+
// so check the status
121+
if (req.status == 200) {
122+
// Resolve the promise with the response text
123+
resolve(req.response);
124+
}
125+
else {
126+
// Otherwise reject with the status text
127+
// which will hopefully be a meaningful error
128+
reject(Error(req.statusText));
129+
}
130+
};
131+
132+
// Handle network errors
133+
req.onerror = function() {
134+
reject(Error("Network Error"));
135+
};
136+
137+
// Make the request
138+
req.send();
139+
});
140+
};
141+
142+
var HTTPgetJSON = function(url) {
143+
return HTTPget(url).then(JSON.parse);
144+
};
152145

153146
return {saveAnimation: saveAnimation,
154147
loadAnimations: loadAnimations,
155148
loadAnimation: loadAnimation,
156149
loadText: loadText,
157150
loadJson: loadJson,
158151
makeAnimationFileUri: makeAnimationFileUri,
159-
saveAssemblage: saveAssemblage};
152+
saveAssemblage: saveAssemblage,
153+
HTTPgetJSON: HTTPgetJSON,
154+
HTTPget: HTTPget
155+
};
160156

161157
});

public/promise.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
function get(url) {
1+
function HTTPget(url) {
22
// Return a new promise.
33
return new Promise(function(resolve, reject) {
44
// Do the usual XHR stuff
@@ -29,20 +29,20 @@ function get(url) {
2929
});
3030
}
3131

32-
function getJSON(url) {
33-
return get(url).then(JSON.parse);
32+
function HTTPgetJSON(url) {
33+
return HTTPget(url).then(JSON.parse);
3434
}
3535

3636
function propro(){
3737

3838

39-
getJSON("/assemblages/assemblageAvecSinus.json")
39+
HTTPgetJSON("/assemblages/assemblageAvecSinus.json")
4040
.then(function(animationNames){
4141
var animNamesList = animationNames.reduce(function(a, b) {
4242
return a.concat(b);
4343
}),
4444
animPromises = animNamesList.map(function(animName){
45-
return get("/animations/" + animName + ".js") ;
45+
return HTTPget("/animations/" + animName + ".js") ;
4646
});
4747
return Promise.all(animPromises);
4848
}).then(function(animNames){

0 commit comments

Comments
 (0)