-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
476 additions
and
55 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,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", | ||
|
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
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 |
---|---|---|
@@ -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: | ||
|
@@ -1733,7 +1733,7 @@ | |
* * | ||
* hprose Future for JavaScript. * | ||
* * | ||
* LastModified: Oct 21, 2016 * | ||
* LastModified: Nov 17, 2016 * | ||
* Author: Ma Bingyao <[email protected]> * | ||
* * | ||
\**********************************************************/ | ||
|
@@ -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 : | ||
|
@@ -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); | ||
}); | ||
} | ||
|
||
|
@@ -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); | ||
}); | ||
|
@@ -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); | ||
}); | ||
|
@@ -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 }, | ||
|
@@ -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, { | ||
|
Oops, something went wrong.