Skip to content

Commit

Permalink
first usable version - wait.for based on generators
Browse files Browse the repository at this point in the history
  • Loading branch information
luciotato committed Dec 6, 2013
1 parent 73965d9 commit 44fd7b0
Show file tree
Hide file tree
Showing 15 changed files with 695 additions and 1,217 deletions.
535 changes: 273 additions & 262 deletions README.md

Large diffs are not rendered by default.

59 changes: 28 additions & 31 deletions package.json
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"
}
}
4 changes: 0 additions & 4 deletions samples/ajaxServer/blogPost.txt

This file was deleted.

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);
});
}
}
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>
1 change: 0 additions & 1 deletion samples/ajaxServer/run-demo

This file was deleted.

1 change: 1 addition & 0 deletions samples/ajaxServer/run.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node --harmony --debug server
Loading

0 comments on commit 44fd7b0

Please sign in to comment.