Skip to content

Commit

Permalink
Update to 2.0.22
Browse files Browse the repository at this point in the history
  • Loading branch information
andot committed Nov 17, 2016
1 parent c68901b commit 30cd9a8
Show file tree
Hide file tree
Showing 9 changed files with 476 additions and 55 deletions.
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"author": "Ma Bingyao <[email protected]>",
"name": "hprose",
"version": "2.0.21",
"version": "2.0.22",
"description": "Hprose is a High Performance Remote Object Service Engine.",
"keywords": [
"hprose",
Expand Down
112 changes: 98 additions & 14 deletions dist/hprose.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/hprose.min.js

Large diffs are not rendered by default.

184 changes: 176 additions & 8 deletions dist/hprose.src.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Hprose for JavaScript v2.0.21
// Hprose for JavaScript v2.0.22
// Copyright (c) 2008-2016 http://hprose.com
// Hprose is freely distributable under the MIT license.
// For all details and documentation:
Expand Down Expand Up @@ -1733,7 +1733,7 @@
* *
* hprose Future for JavaScript. *
* *
* LastModified: Oct 21, 2016 *
* LastModified: Nov 17, 2016 *
* Author: Ma Bingyao <[email protected]> *
* *
\**********************************************************/
Expand Down Expand Up @@ -1781,10 +1781,6 @@
return 'function' === typeof obj.then;
}

function toPromise(obj) {
return (isFuture(obj) ? obj : value(obj));
}

function delayed(duration, value) {
var computation = (typeof value === 'function') ?
value :
Expand Down Expand Up @@ -1915,9 +1911,10 @@
}

function attempt(handler/*, arg1, arg2, ... */) {
var thisArg = (function() { return this; })();
var args = Array.slice(arguments, 1);
return all(args).then(function(args) {
return handler.apply(undefined, args);
return handler.apply(thisArg, args);
});
}

Expand All @@ -1928,39 +1925,200 @@
});
}

function isGenerator(obj) {
return 'function' == typeof obj.next && 'function' == typeof obj['throw'];
}

function isGeneratorFunction(obj) {
if (!obj) {
return false;
}
var constructor = obj.constructor;
if (!constructor) {
return false;
}
if ('GeneratorFunction' === constructor.name ||
'GeneratorFunction' === constructor.displayName) {
return true;
}
return isGenerator(constructor.prototype);
}

function thunkToPromise(fn) {
var thisArg = (function() { return this; })();
var future = new Future();
fn.call(thisArg, function(err, res) {
if (arguments.length < 2) {
if (err instanceof Error) {
return future.reject(err);
}
return future.resolve(err);
}
if (err) {
return future.reject(err);
}
if (arguments.length > 2) {
res = Array.slice(arguments, 1);
}
future.resolve(res);
});
return future;
}

function thunkify(fn) {
return function() {
var args = Array.slice(arguments, 0);
var thisArg = this;
var results = new Future();
args.push(function() {
thisArg = this;
results.resolve(arguments);
});
try {
fn.apply(this, args);
}
catch (err) {
results.resolve([err]);
}
return function(done) {
results.then(function(results) {
done.apply(thisArg, results);
});
};
};
}

function promisify(fn) {
return function() {
var args = Array.slice(arguments, 0);
var results = new Future();
args.push(function(err, res) {
if (arguments.length < 2) {
if (err instanceof Error) {
return results.reject(err);
}
return results.resolve(err);
}
if (err) {
return results.reject(err);
}
if (arguments.length > 2) {
res = Array.slice(arguments, 1);
}
results.resolve(res);
});
try {
fn.apply(this, args);
}
catch (err) {
results.reject(err);
}
return results;
};
}

function toPromise(obj) {
if (!obj) {
return value(obj);
}
if (isPromise(obj)) {
return obj;
}
if (isGeneratorFunction(obj) || isGenerator(obj)) {
return co(obj);
}
if ('function' == typeof obj) {
return thunkToPromise(obj);
}
return value(obj);
}

function co(gen) {
var thisArg = (function() { return this; })();
if (typeof gen === 'function') {
var args = Array.slice(arguments, 1);
gen = gen.apply(thisArg, args);
}
var future = new Future();

function onFulfilled(res) {
try {
next(gen.next(res));
}
catch (e) {
future.reject(e);
}
}

function onRejected(err) {
try {
next(gen['throw'](err));
}
catch (e) {
return future.reject(e);
}
}

function next(ret) {
if (ret.done) {
future.resolve(ret.value);
}
else {
toPromise(ret.value).then(onFulfilled, onRejected);
}
}

if (!gen || typeof gen.next !== 'function') {
return future.resolve(gen);
}
onFulfilled();

return future;
}

function wrap(handler, thisArg) {
return function() {
thisArg = thisArg || this;
return all(arguments).then(function(args) {
return handler.apply(thisArg, args);
var result = handler.apply(thisArg, args);
if (isGeneratorFunction(result)) {
return co.call(thisArg, result);
}
return result;
});
};
}

function forEach(array, callback, thisArg) {
thisArg = thisArg || (function() { return this; })();
return all(array).then(function(array) {
return array.forEach(callback, thisArg);
});
}

function every(array, callback, thisArg) {
thisArg = thisArg || (function() { return this; })();
return all(array).then(function(array) {
return array.every(callback, thisArg);
});
}

function some(array, callback, thisArg) {
thisArg = thisArg || (function() { return this; })();
return all(array).then(function(array) {
return array.some(callback, thisArg);
});
}

function filter(array, callback, thisArg) {
thisArg = thisArg || (function() { return this; })();
return all(array).then(function(array) {
return array.filter(callback, thisArg);
});
}

function map(array, callback, thisArg) {
thisArg = thisArg || (function() { return this; })();
return all(array).then(function(array) {
return array.map(callback, thisArg);
});
Expand Down Expand Up @@ -2035,12 +2193,14 @@
}

function find(array, predicate, thisArg) {
thisArg = thisArg || (function() { return this; })();
return all(array).then(function(array) {
return array.find(predicate, thisArg);
});
}

function findIndex(array, predicate, thisArg) {
thisArg = thisArg || (function() { return this; })();
return all(array).then(function(array) {
return array.findIndex(predicate, thisArg);
});
Expand All @@ -2067,6 +2227,9 @@
settle: { value: settle },
attempt: { value: attempt },
run: { value: run },
thunkify: { value: thunkify },
promisify: { value: promisify },
co: { value: co },
wrap: { value: wrap },
// for array
forEach: { value: forEach },
Expand Down Expand Up @@ -2374,6 +2537,11 @@

global.hprose.Future = Future;

global.hprose.thunkify = thunkify;
global.hprose.promisify = promisify;
global.hprose.co = co;
global.hprose.co.wrap = global.hprose.wrap = wrap;

function Completer() {
var future = new Future();
defineProperties(this, {
Expand Down
Loading

0 comments on commit 30cd9a8

Please sign in to comment.