Skip to content

Commit

Permalink
Update to v2.0.24
Browse files Browse the repository at this point in the history
  • Loading branch information
andot committed Nov 18, 2016
1 parent 7ea69b7 commit c26872b
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 15 deletions.
17 changes: 13 additions & 4 deletions dist/hprose.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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

Large diffs are not rendered by default.

55 changes: 53 additions & 2 deletions dist/hprose.src.js
Original file line number Diff line number Diff line change
Expand Up @@ -1948,6 +1948,53 @@ hprose.global = (
return isGenerator(constructor.prototype);
}

function thunkToPromise(fn) {
if (isGeneratorFunction(fn) || isGenerator(fn)) {
return co(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);
Expand Down Expand Up @@ -2021,7 +2068,9 @@ hprose.global = (
future.resolve(ret.value);
}
else {
toPromise(ret.value).then(onFulfilled, onRejected);
(('function' == typeof ret.value) ?
thunkToPromise(ret.value) :
toPromise(ret.value)).then(onFulfilled, onRejected);
}
}

Expand All @@ -2038,7 +2087,7 @@ hprose.global = (
thisArg = thisArg || this;
return all(arguments).then(function(args) {
var result = handler.apply(thisArg, args);
if (isGeneratorFunction(result)) {
if (isGeneratorFunction(result) || isGenerator(result)) {
return co.call(thisArg, result);
}
return result;
Expand Down Expand Up @@ -2184,6 +2233,7 @@ hprose.global = (
settle: { value: settle },
attempt: { value: attempt },
run: { value: run },
thunkify: { value: thunkify },
promisify: { value: promisify },
co: { value: co },
wrap: { value: wrap },
Expand Down Expand Up @@ -2493,6 +2543,7 @@ hprose.global = (

hprose.Future = Future;

hprose.thunkify = thunkify;
hprose.promisify = promisify;
hprose.co = co;
hprose.co.wrap = hprose.wrap = wrap;
Expand Down
17 changes: 13 additions & 4 deletions example/hprose.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit c26872b

Please sign in to comment.