-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
first usable version - wait.for based on generators
- Loading branch information
Showing
15 changed files
with
695 additions
and
1,217 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,29 @@ | ||
{ | ||
"name": "wait.for-es6" | ||
,"version": "0.0.1" | ||
,"description": "Sequential programming for node.js -and the browser-. End of callback hell - Original Wait.for, implemented using upcoming javascript/ES6-Harmony generators" | ||
|
||
,"author": { | ||
"name": "Lucio Tato" | ||
,"email": "[email protected]" | ||
,"url":"http://github.com/luciotato" | ||
} | ||
,"keywords": [ | ||
"fiber", "fibers", "generator", "coroutine", "thread", "sycn", "async" | ||
,"parallel", "worker", "future", "promise" | ||
,"wait","Wait.for","callback hell","piramyd of doom"] | ||
|
||
,"homepage": "http://github.com/luciotato/waitfor-ES6" | ||
,"license" : "Creative Commons, MIT" | ||
,"bugs": "http://github.com/luciotato/waitfor-ES6/issues" | ||
,"repository": { | ||
"type": "git" | ||
,"url": "git://github.com/luciotato/waitfor-ES6.git" | ||
} | ||
,"main": "waitfor.js" | ||
, "engines": { | ||
"node": ">=0.11.6" | ||
} | ||
,"scripts": { | ||
"test": "node --harmony tests" | ||
,"parallel": "node --harmony parallel-tests" | ||
,"demo": "node --harmony waitfor-demo" | ||
} | ||
{ | ||
"name" : "wait.for-es6", | ||
"version" : "0.0.1", | ||
"description" : "Sequential programming for node.js -and the browser-. End of callback hell - Original Wait.for, implemented using upcoming javascript/ES6-Harmony generators", | ||
"author" : { | ||
"name" : "Lucio Tato", | ||
"email" : "[email protected]", | ||
"url" : "http://github.com/luciotato" | ||
}, | ||
"keywords" : [ "fiber", "fibers", "generator", "coroutine", "thread", "sycn", "async", "parallel", "worker", "future", "promise", "wait", "Wait.for", "callback hell", "piramyd of doom" ], | ||
"homepage" : "http://github.com/luciotato/waitfor-ES6", | ||
"license" : "Creative Commons, MIT", | ||
"bugs" : { | ||
"url" : "http://github.com/luciotato/waitfor-ES6/issues" | ||
}, | ||
"repository" : { | ||
"type" : "git", | ||
"url" : "git://github.com/luciotato/waitfor-ES6.git" | ||
}, | ||
"main" : "./waitfor.js", | ||
"engines" : { | ||
"node" : ">=0.11.6" | ||
}, | ||
"scripts" : { | ||
"test" : "node --harmony tests", | ||
"parallel" : "node --harmony parallel-tests", | ||
"demo" : "node --harmony waitfor-demo" | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
166 changes: 83 additions & 83 deletions
166
samples/ajaxServer/waitfor-demo-client.js → samples/ajaxServer/client.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,83 +1,83 @@ | ||
//ON THE BROWSER | ||
//--- | ||
|
||
function appendHtml(el, str) { | ||
var div = document.createElement('div'); | ||
div.innerHTML = str; | ||
if (div.children.length > 0) { | ||
newItem = div.children[0]; | ||
el.appendChild(newItem); | ||
return newItem; | ||
} | ||
} | ||
|
||
//--------------------------------- | ||
function new_XMLHttpRequest() { | ||
var ref = null; | ||
if (window.XMLHttpRequest) { | ||
ref = new XMLHttpRequest(); | ||
} else if (window.ActiveXObject) { // Older IE. | ||
ref = new ActiveXObject("MSXML2.XMLHTTP.3.0"); | ||
} | ||
if (!ref) { | ||
throw {status: 505, responseText: 'Failure to create XMLHttpRequest'}; | ||
} | ||
return ref; | ||
} | ||
|
||
//--------------------------------- | ||
function ajaxGetJson(url, callback) { | ||
|
||
if (typeof callback !== 'function') { | ||
throw('callback should be a function(err,data)'); | ||
} | ||
|
||
try { | ||
var xmlhttp = new_XMLHttpRequest(); | ||
xmlhttp.onreadystatechange = function() { | ||
if (xmlhttp.readyState === 4) { | ||
if (xmlhttp.status !== 200) { | ||
return callback(new Error("status:" + xmlhttp.status + " " + xmlhttp.statusText)); | ||
} | ||
try { | ||
var response = JSON.parse(xmlhttp.responseText); // parseo json | ||
} catch (ex) { | ||
return callback(new Error(ex.message + '\n' + xmlhttp.responseText.substr(0, 600))); // data no era JSON | ||
} | ||
|
||
if (response.err) return callback(response.err); // server err | ||
return callback(null, response.data); // callback OK | ||
} | ||
; | ||
}; | ||
|
||
xmlhttp.open("GET", url, true); //async | ||
xmlhttp.setRequestHeader('content-type', 'applicattion/json'); | ||
xmlhttp.send(); | ||
return xmlhttp; | ||
} | ||
catch (e) { | ||
//call the callback on next chance | ||
return setTimeout(function(){callback(e, null)},0); | ||
} | ||
} | ||
|
||
//--------------------------------- | ||
function launch(){ | ||
var p = document.getElementById('parallel'); | ||
while(p.firstChild) p.removeChild(p.firstChild); | ||
|
||
var newDivs=[]; | ||
for (var n=0;n<10;n++){ | ||
newDivs.push(appendHtml(p,'<div class=node-req>'+n+': started</div>')); | ||
}; | ||
|
||
for (n=0;n<10;n++){ | ||
console.log('get',n); | ||
ajaxGetJson("/longAsyncOper/"+n,function(err,data){ | ||
if (err) return newDivs[err.order|0].innerHTML="ERR " + err.message; | ||
console.log(data); | ||
newDivs[data.order].innerHTML= JSON.stringify(data.content); | ||
}); | ||
} | ||
} | ||
//ON THE BROWSER | ||
//--- | ||
|
||
function appendHtml(el, str) { | ||
var div = document.createElement('div'); | ||
div.innerHTML = str; | ||
if (div.children.length > 0) { | ||
newItem = div.children[0]; | ||
el.appendChild(newItem); | ||
return newItem; | ||
} | ||
} | ||
|
||
//--------------------------------- | ||
function new_XMLHttpRequest() { | ||
var ref = null; | ||
if (window.XMLHttpRequest) { | ||
ref = new XMLHttpRequest(); | ||
} else if (window.ActiveXObject) { // Older IE. | ||
ref = new ActiveXObject("MSXML2.XMLHTTP.3.0"); | ||
} | ||
if (!ref) { | ||
throw {status: 505, responseText: 'Failure to create XMLHttpRequest'}; | ||
} | ||
return ref; | ||
} | ||
|
||
//--------------------------------- | ||
function ajaxGetJson(url, callback) { | ||
|
||
if (typeof callback !== 'function') { | ||
throw('callback should be a function(err,data)'); | ||
} | ||
|
||
try { | ||
var xmlhttp = new_XMLHttpRequest(); | ||
xmlhttp.onreadystatechange = function() { | ||
if (xmlhttp.readyState === 4) { | ||
if (xmlhttp.status !== 200) { | ||
return callback(new Error("status:" + xmlhttp.status + " " + xmlhttp.statusText)); | ||
} | ||
try { | ||
var response = JSON.parse(xmlhttp.responseText); // parseo json | ||
} catch (ex) { | ||
return callback(new Error(ex.message + '\n' + xmlhttp.responseText.substr(0, 600))); // data no era JSON | ||
} | ||
|
||
if (response.err) return callback(response.err); // server err | ||
return callback(null, response.data); // callback OK | ||
} | ||
; | ||
}; | ||
|
||
xmlhttp.open("GET", url, true); //async | ||
xmlhttp.setRequestHeader('content-type', 'applicattion/json'); | ||
xmlhttp.send(); | ||
return xmlhttp; | ||
} | ||
catch (e) { | ||
//call the callback on next chance | ||
return setTimeout(function(){callback(e, null)},0); | ||
} | ||
} | ||
|
||
//--------------------------------- | ||
function launch(){ | ||
var p = document.getElementById('parallel'); | ||
while(p.firstChild) p.removeChild(p.firstChild); | ||
var cant = 20; | ||
var newDivs=[]; | ||
for (var n=0;n<cant;n++){ | ||
newDivs.push(appendHtml(p,'<div class=node-req>'+n+': started</div>')); | ||
}; | ||
|
||
for (n=0;n<cant;n++){ | ||
console.log('get',n); | ||
ajaxGetJson("/longAsyncOper/"+n,function(err,data){ | ||
if (err) return newDivs[err.order|0].innerHTML="ERR " + err.message; | ||
console.log(data); | ||
newDivs[data.order].innerHTML= JSON.stringify(data.content); | ||
}); | ||
} | ||
} |
33 changes: 13 additions & 20 deletions
33
samples/ajaxServer/blogTemplate.html → samples/ajaxServer/page.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,13 @@ | ||
<html> | ||
|
||
<head> | ||
</head> | ||
|
||
<style> | ||
{{ css }} | ||
</style> | ||
|
||
<body> | ||
{{ content }} | ||
</body> | ||
|
||
<button onclick=launch()>Launch Ajax Requests</button> | ||
<div id=parallel> | ||
</div> | ||
|
||
<script src="waitfor-demo-client.js"></script> | ||
|
||
</html> | ||
<html> | ||
|
||
<head> | ||
</head> | ||
|
||
<body> | ||
<button onclick=launch()>Launch Ajax Requests</button> | ||
<div id=parallel> | ||
</div> | ||
|
||
<script src="client.js"></script> | ||
|
||
</html> |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
node --harmony --debug server |
Oops, something went wrong.