Skip to content

Commit 6f2b8f4

Browse files
committed
Added thunkify, promisify, co
1 parent ce3c35f commit 6f2b8f4

9 files changed

Lines changed: 1030 additions & 24 deletions

File tree

bower.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"author": "Ma Bingyao <andot@hprose.com>",
33
"name": "hprose-html5",
4-
"version": "2.0.23",
4+
"version": "2.0.24",
55
"description": "Hprose is a High Performance Remote Object Service Engine.",
66
"keywords": [
77
"hprose",

dist/hprose-html5.js

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/hprose-html5.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/hprose-html5.src.js

Lines changed: 176 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Hprose for HTML5 v2.0.23
1+
// Hprose for HTML5 v2.0.24
22
// Copyright (c) 2008-2016 http://hprose.com
33
// Hprose is freely distributable under the MIT license.
44
// For all details and documentation:
@@ -1097,7 +1097,7 @@
10971097
* *
10981098
* hprose Future for HTML5. *
10991099
* *
1100-
* LastModified: Oct 21, 2016 *
1100+
* LastModified: Nov 17, 2016 *
11011101
* Author: Ma Bingyao <andot@hprose.com> *
11021102
* *
11031103
\**********************************************************/
@@ -1142,10 +1142,6 @@
11421142
return 'function' === typeof obj.then;
11431143
}
11441144

1145-
function toPromise(obj) {
1146-
return (isFuture(obj) ? obj : value(obj));
1147-
}
1148-
11491145
function delayed(duration, value) {
11501146
var computation = (typeof value === 'function') ?
11511147
value :
@@ -1276,9 +1272,10 @@
12761272
}
12771273

12781274
function attempt(handler/*, arg1, arg2, ... */) {
1275+
var thisArg = (function() { return this; })();
12791276
var args = Array.slice(arguments, 1);
12801277
return all(args).then(function(args) {
1281-
return handler.apply(undefined, args);
1278+
return handler.apply(thisArg, args);
12821279
});
12831280
}
12841281

@@ -1289,39 +1286,200 @@
12891286
});
12901287
}
12911288

1289+
function isGenerator(obj) {
1290+
return 'function' == typeof obj.next && 'function' == typeof obj['throw'];
1291+
}
1292+
1293+
function isGeneratorFunction(obj) {
1294+
if (!obj) {
1295+
return false;
1296+
}
1297+
var constructor = obj.constructor;
1298+
if (!constructor) {
1299+
return false;
1300+
}
1301+
if ('GeneratorFunction' === constructor.name ||
1302+
'GeneratorFunction' === constructor.displayName) {
1303+
return true;
1304+
}
1305+
return isGenerator(constructor.prototype);
1306+
}
1307+
1308+
function thunkToPromise(fn) {
1309+
var thisArg = (function() { return this; })();
1310+
var future = new Future();
1311+
fn.call(thisArg, function(err, res) {
1312+
if (arguments.length < 2) {
1313+
if (err instanceof Error) {
1314+
return future.reject(err);
1315+
}
1316+
return future.resolve(err);
1317+
}
1318+
if (err) {
1319+
return future.reject(err);
1320+
}
1321+
if (arguments.length > 2) {
1322+
res = Array.slice(arguments, 1);
1323+
}
1324+
future.resolve(res);
1325+
});
1326+
return future;
1327+
}
1328+
1329+
function thunkify(fn) {
1330+
return function() {
1331+
var args = Array.slice(arguments, 0);
1332+
var thisArg = this;
1333+
var results = new Future();
1334+
args.push(function() {
1335+
thisArg = this;
1336+
results.resolve(arguments);
1337+
});
1338+
try {
1339+
fn.apply(this, args);
1340+
}
1341+
catch (err) {
1342+
results.resolve([err]);
1343+
}
1344+
return function(done) {
1345+
results.then(function(results) {
1346+
done.apply(thisArg, results);
1347+
});
1348+
};
1349+
};
1350+
}
1351+
1352+
function promisify(fn) {
1353+
return function() {
1354+
var args = Array.slice(arguments, 0);
1355+
var results = new Future();
1356+
args.push(function(err, res) {
1357+
if (arguments.length < 2) {
1358+
if (err instanceof Error) {
1359+
return results.reject(err);
1360+
}
1361+
return results.resolve(err);
1362+
}
1363+
if (err) {
1364+
return results.reject(err);
1365+
}
1366+
if (arguments.length > 2) {
1367+
res = Array.slice(arguments, 1);
1368+
}
1369+
results.resolve(res);
1370+
});
1371+
try {
1372+
fn.apply(this, args);
1373+
}
1374+
catch (err) {
1375+
results.reject(err);
1376+
}
1377+
return results;
1378+
};
1379+
}
1380+
1381+
function toPromise(obj) {
1382+
if (!obj) {
1383+
return value(obj);
1384+
}
1385+
if (isPromise(obj)) {
1386+
return obj;
1387+
}
1388+
if (isGeneratorFunction(obj) || isGenerator(obj)) {
1389+
return co(obj);
1390+
}
1391+
if ('function' == typeof obj) {
1392+
return thunkToPromise(obj);
1393+
}
1394+
return value(obj);
1395+
}
1396+
1397+
function co(gen) {
1398+
var thisArg = (function() { return this; })();
1399+
if (typeof gen === 'function') {
1400+
var args = Array.slice(arguments, 1);
1401+
gen = gen.apply(thisArg, args);
1402+
}
1403+
var future = new Future();
1404+
1405+
function onFulfilled(res) {
1406+
try {
1407+
next(gen.next(res));
1408+
}
1409+
catch (e) {
1410+
future.reject(e);
1411+
}
1412+
}
1413+
1414+
function onRejected(err) {
1415+
try {
1416+
next(gen['throw'](err));
1417+
}
1418+
catch (e) {
1419+
return future.reject(e);
1420+
}
1421+
}
1422+
1423+
function next(ret) {
1424+
if (ret.done) {
1425+
future.resolve(ret.value);
1426+
}
1427+
else {
1428+
toPromise(ret.value).then(onFulfilled, onRejected);
1429+
}
1430+
}
1431+
1432+
if (!gen || typeof gen.next !== 'function') {
1433+
return future.resolve(gen);
1434+
}
1435+
onFulfilled();
1436+
1437+
return future;
1438+
}
1439+
12921440
function wrap(handler, thisArg) {
12931441
return function() {
1442+
thisArg = thisArg || this;
12941443
return all(arguments).then(function(args) {
1295-
return handler.apply(thisArg, args);
1444+
var result = handler.apply(thisArg, args);
1445+
if (isGeneratorFunction(result)) {
1446+
return co.call(thisArg, result);
1447+
}
1448+
return result;
12961449
});
12971450
};
12981451
}
12991452

13001453
function forEach(array, callback, thisArg) {
1454+
thisArg = thisArg || (function() { return this; })();
13011455
return all(array).then(function(array) {
13021456
return array.forEach(callback, thisArg);
13031457
});
13041458
}
13051459

13061460
function every(array, callback, thisArg) {
1461+
thisArg = thisArg || (function() { return this; })();
13071462
return all(array).then(function(array) {
13081463
return array.every(callback, thisArg);
13091464
});
13101465
}
13111466

13121467
function some(array, callback, thisArg) {
1468+
thisArg = thisArg || (function() { return this; })();
13131469
return all(array).then(function(array) {
13141470
return array.some(callback, thisArg);
13151471
});
13161472
}
13171473

13181474
function filter(array, callback, thisArg) {
1475+
thisArg = thisArg || (function() { return this; })();
13191476
return all(array).then(function(array) {
13201477
return array.filter(callback, thisArg);
13211478
});
13221479
}
13231480

13241481
function map(array, callback, thisArg) {
1482+
thisArg = thisArg || (function() { return this; })();
13251483
return all(array).then(function(array) {
13261484
return array.map(callback, thisArg);
13271485
});
@@ -1396,12 +1554,14 @@
13961554
}
13971555

13981556
function find(array, predicate, thisArg) {
1557+
thisArg = thisArg || (function() { return this; })();
13991558
return all(array).then(function(array) {
14001559
return array.find(predicate, thisArg);
14011560
});
14021561
}
14031562

14041563
function findIndex(array, predicate, thisArg) {
1564+
thisArg = thisArg || (function() { return this; })();
14051565
return all(array).then(function(array) {
14061566
return array.findIndex(predicate, thisArg);
14071567
});
@@ -1428,6 +1588,9 @@
14281588
settle: { value: settle },
14291589
attempt: { value: attempt },
14301590
run: { value: run },
1591+
thunkify: { value: thunkify },
1592+
promisify: { value: promisify },
1593+
co: { value: co },
14311594
wrap: { value: wrap },
14321595
// for array
14331596
forEach: { value: forEach },
@@ -1735,6 +1898,11 @@
17351898

17361899
global.hprose.Future = Future;
17371900

1901+
global.hprose.thunkify = thunkify;
1902+
global.hprose.promisify = promisify;
1903+
global.hprose.co = co;
1904+
global.hprose.co.wrap = global.hprose.wrap = wrap;
1905+
17381906
function Completer() {
17391907
var future = new Future();
17401908
Object.defineProperties(this, {

gulpfile.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ gulp.task('concat', ['clear'], function() {
4141
});
4242

4343
gulp.task('uglify', ['concat'], function() {
44-
return gulp.src(['dist/hprose-html5.src.js'])
44+
return gulp.src(['dist/hprose-html5.src.js',
45+
'utils/regenerator-runtime.js'])
4546
.pipe(concat('hprose-html5.js'))
4647
.pipe(uglify())
4748
.pipe(gulp.dest('dist'));

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "hprose-html5",
33
"filename": "hprose-html5.js",
4-
"version": "2.0.23",
4+
"version": "2.0.24",
55
"description": "Hprose is a High Performance Remote Object Service Engine.",
66
"homepage": "https://github.com/andot/hprose",
77
"keywords": [

src/CopyRight.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Hprose for HTML5 v2.0.23
1+
// Hprose for HTML5 v2.0.24
22
// Copyright (c) 2008-2016 http://hprose.com
33
// Hprose is freely distributable under the MIT license.
44
// For all details and documentation:

0 commit comments

Comments
 (0)