Skip to content

Commit 30cd9a8

Browse files
committed
Update to 2.0.22
1 parent c68901b commit 30cd9a8

9 files changed

Lines changed: 476 additions & 55 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",
4-
"version": "2.0.21",
4+
"version": "2.0.22",
55
"description": "Hprose is a High Performance Remote Object Service Engine.",
66
"keywords": [
77
"hprose",

dist/hprose.js

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

dist/hprose.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.src.js

Lines changed: 176 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Hprose for JavaScript v2.0.21
1+
// Hprose for JavaScript v2.0.22
22
// Copyright (c) 2008-2016 http://hprose.com
33
// Hprose is freely distributable under the MIT license.
44
// For all details and documentation:
@@ -1733,7 +1733,7 @@
17331733
* *
17341734
* hprose Future for JavaScript. *
17351735
* *
1736-
* LastModified: Oct 21, 2016 *
1736+
* LastModified: Nov 17, 2016 *
17371737
* Author: Ma Bingyao <andot@hprose.com> *
17381738
* *
17391739
\**********************************************************/
@@ -1781,10 +1781,6 @@
17811781
return 'function' === typeof obj.then;
17821782
}
17831783

1784-
function toPromise(obj) {
1785-
return (isFuture(obj) ? obj : value(obj));
1786-
}
1787-
17881784
function delayed(duration, value) {
17891785
var computation = (typeof value === 'function') ?
17901786
value :
@@ -1915,9 +1911,10 @@
19151911
}
19161912

19171913
function attempt(handler/*, arg1, arg2, ... */) {
1914+
var thisArg = (function() { return this; })();
19181915
var args = Array.slice(arguments, 1);
19191916
return all(args).then(function(args) {
1920-
return handler.apply(undefined, args);
1917+
return handler.apply(thisArg, args);
19211918
});
19221919
}
19231920

@@ -1928,39 +1925,200 @@
19281925
});
19291926
}
19301927

1928+
function isGenerator(obj) {
1929+
return 'function' == typeof obj.next && 'function' == typeof obj['throw'];
1930+
}
1931+
1932+
function isGeneratorFunction(obj) {
1933+
if (!obj) {
1934+
return false;
1935+
}
1936+
var constructor = obj.constructor;
1937+
if (!constructor) {
1938+
return false;
1939+
}
1940+
if ('GeneratorFunction' === constructor.name ||
1941+
'GeneratorFunction' === constructor.displayName) {
1942+
return true;
1943+
}
1944+
return isGenerator(constructor.prototype);
1945+
}
1946+
1947+
function thunkToPromise(fn) {
1948+
var thisArg = (function() { return this; })();
1949+
var future = new Future();
1950+
fn.call(thisArg, function(err, res) {
1951+
if (arguments.length < 2) {
1952+
if (err instanceof Error) {
1953+
return future.reject(err);
1954+
}
1955+
return future.resolve(err);
1956+
}
1957+
if (err) {
1958+
return future.reject(err);
1959+
}
1960+
if (arguments.length > 2) {
1961+
res = Array.slice(arguments, 1);
1962+
}
1963+
future.resolve(res);
1964+
});
1965+
return future;
1966+
}
1967+
1968+
function thunkify(fn) {
1969+
return function() {
1970+
var args = Array.slice(arguments, 0);
1971+
var thisArg = this;
1972+
var results = new Future();
1973+
args.push(function() {
1974+
thisArg = this;
1975+
results.resolve(arguments);
1976+
});
1977+
try {
1978+
fn.apply(this, args);
1979+
}
1980+
catch (err) {
1981+
results.resolve([err]);
1982+
}
1983+
return function(done) {
1984+
results.then(function(results) {
1985+
done.apply(thisArg, results);
1986+
});
1987+
};
1988+
};
1989+
}
1990+
1991+
function promisify(fn) {
1992+
return function() {
1993+
var args = Array.slice(arguments, 0);
1994+
var results = new Future();
1995+
args.push(function(err, res) {
1996+
if (arguments.length < 2) {
1997+
if (err instanceof Error) {
1998+
return results.reject(err);
1999+
}
2000+
return results.resolve(err);
2001+
}
2002+
if (err) {
2003+
return results.reject(err);
2004+
}
2005+
if (arguments.length > 2) {
2006+
res = Array.slice(arguments, 1);
2007+
}
2008+
results.resolve(res);
2009+
});
2010+
try {
2011+
fn.apply(this, args);
2012+
}
2013+
catch (err) {
2014+
results.reject(err);
2015+
}
2016+
return results;
2017+
};
2018+
}
2019+
2020+
function toPromise(obj) {
2021+
if (!obj) {
2022+
return value(obj);
2023+
}
2024+
if (isPromise(obj)) {
2025+
return obj;
2026+
}
2027+
if (isGeneratorFunction(obj) || isGenerator(obj)) {
2028+
return co(obj);
2029+
}
2030+
if ('function' == typeof obj) {
2031+
return thunkToPromise(obj);
2032+
}
2033+
return value(obj);
2034+
}
2035+
2036+
function co(gen) {
2037+
var thisArg = (function() { return this; })();
2038+
if (typeof gen === 'function') {
2039+
var args = Array.slice(arguments, 1);
2040+
gen = gen.apply(thisArg, args);
2041+
}
2042+
var future = new Future();
2043+
2044+
function onFulfilled(res) {
2045+
try {
2046+
next(gen.next(res));
2047+
}
2048+
catch (e) {
2049+
future.reject(e);
2050+
}
2051+
}
2052+
2053+
function onRejected(err) {
2054+
try {
2055+
next(gen['throw'](err));
2056+
}
2057+
catch (e) {
2058+
return future.reject(e);
2059+
}
2060+
}
2061+
2062+
function next(ret) {
2063+
if (ret.done) {
2064+
future.resolve(ret.value);
2065+
}
2066+
else {
2067+
toPromise(ret.value).then(onFulfilled, onRejected);
2068+
}
2069+
}
2070+
2071+
if (!gen || typeof gen.next !== 'function') {
2072+
return future.resolve(gen);
2073+
}
2074+
onFulfilled();
2075+
2076+
return future;
2077+
}
2078+
19312079
function wrap(handler, thisArg) {
19322080
return function() {
2081+
thisArg = thisArg || this;
19332082
return all(arguments).then(function(args) {
1934-
return handler.apply(thisArg, args);
2083+
var result = handler.apply(thisArg, args);
2084+
if (isGeneratorFunction(result)) {
2085+
return co.call(thisArg, result);
2086+
}
2087+
return result;
19352088
});
19362089
};
19372090
}
19382091

19392092
function forEach(array, callback, thisArg) {
2093+
thisArg = thisArg || (function() { return this; })();
19402094
return all(array).then(function(array) {
19412095
return array.forEach(callback, thisArg);
19422096
});
19432097
}
19442098

19452099
function every(array, callback, thisArg) {
2100+
thisArg = thisArg || (function() { return this; })();
19462101
return all(array).then(function(array) {
19472102
return array.every(callback, thisArg);
19482103
});
19492104
}
19502105

19512106
function some(array, callback, thisArg) {
2107+
thisArg = thisArg || (function() { return this; })();
19522108
return all(array).then(function(array) {
19532109
return array.some(callback, thisArg);
19542110
});
19552111
}
19562112

19572113
function filter(array, callback, thisArg) {
2114+
thisArg = thisArg || (function() { return this; })();
19582115
return all(array).then(function(array) {
19592116
return array.filter(callback, thisArg);
19602117
});
19612118
}
19622119

19632120
function map(array, callback, thisArg) {
2121+
thisArg = thisArg || (function() { return this; })();
19642122
return all(array).then(function(array) {
19652123
return array.map(callback, thisArg);
19662124
});
@@ -2035,12 +2193,14 @@
20352193
}
20362194

20372195
function find(array, predicate, thisArg) {
2196+
thisArg = thisArg || (function() { return this; })();
20382197
return all(array).then(function(array) {
20392198
return array.find(predicate, thisArg);
20402199
});
20412200
}
20422201

20432202
function findIndex(array, predicate, thisArg) {
2203+
thisArg = thisArg || (function() { return this; })();
20442204
return all(array).then(function(array) {
20452205
return array.findIndex(predicate, thisArg);
20462206
});
@@ -2067,6 +2227,9 @@
20672227
settle: { value: settle },
20682228
attempt: { value: attempt },
20692229
run: { value: run },
2230+
thunkify: { value: thunkify },
2231+
promisify: { value: promisify },
2232+
co: { value: co },
20702233
wrap: { value: wrap },
20712234
// for array
20722235
forEach: { value: forEach },
@@ -2374,6 +2537,11 @@
23742537

23752538
global.hprose.Future = Future;
23762539

2540+
global.hprose.thunkify = thunkify;
2541+
global.hprose.promisify = promisify;
2542+
global.hprose.co = co;
2543+
global.hprose.co.wrap = global.hprose.wrap = wrap;
2544+
23772545
function Completer() {
23782546
var future = new Future();
23792547
defineProperties(this, {

0 commit comments

Comments
 (0)