|
1 | | -// Hprose for JavaScript v2.0.21 |
| 1 | +// Hprose for JavaScript v2.0.22 |
2 | 2 | // Copyright (c) 2008-2016 http://hprose.com |
3 | 3 | // Hprose is freely distributable under the MIT license. |
4 | 4 | // For all details and documentation: |
|
1733 | 1733 | * * |
1734 | 1734 | * hprose Future for JavaScript. * |
1735 | 1735 | * * |
1736 | | - * LastModified: Oct 21, 2016 * |
| 1736 | + * LastModified: Nov 17, 2016 * |
1737 | 1737 | * Author: Ma Bingyao <andot@hprose.com> * |
1738 | 1738 | * * |
1739 | 1739 | \**********************************************************/ |
|
1781 | 1781 | return 'function' === typeof obj.then; |
1782 | 1782 | } |
1783 | 1783 |
|
1784 | | - function toPromise(obj) { |
1785 | | - return (isFuture(obj) ? obj : value(obj)); |
1786 | | - } |
1787 | | - |
1788 | 1784 | function delayed(duration, value) { |
1789 | 1785 | var computation = (typeof value === 'function') ? |
1790 | 1786 | value : |
|
1915 | 1911 | } |
1916 | 1912 |
|
1917 | 1913 | function attempt(handler/*, arg1, arg2, ... */) { |
| 1914 | + var thisArg = (function() { return this; })(); |
1918 | 1915 | var args = Array.slice(arguments, 1); |
1919 | 1916 | return all(args).then(function(args) { |
1920 | | - return handler.apply(undefined, args); |
| 1917 | + return handler.apply(thisArg, args); |
1921 | 1918 | }); |
1922 | 1919 | } |
1923 | 1920 |
|
|
1928 | 1925 | }); |
1929 | 1926 | } |
1930 | 1927 |
|
| 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 | + |
1931 | 2079 | function wrap(handler, thisArg) { |
1932 | 2080 | return function() { |
| 2081 | + thisArg = thisArg || this; |
1933 | 2082 | 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; |
1935 | 2088 | }); |
1936 | 2089 | }; |
1937 | 2090 | } |
1938 | 2091 |
|
1939 | 2092 | function forEach(array, callback, thisArg) { |
| 2093 | + thisArg = thisArg || (function() { return this; })(); |
1940 | 2094 | return all(array).then(function(array) { |
1941 | 2095 | return array.forEach(callback, thisArg); |
1942 | 2096 | }); |
1943 | 2097 | } |
1944 | 2098 |
|
1945 | 2099 | function every(array, callback, thisArg) { |
| 2100 | + thisArg = thisArg || (function() { return this; })(); |
1946 | 2101 | return all(array).then(function(array) { |
1947 | 2102 | return array.every(callback, thisArg); |
1948 | 2103 | }); |
1949 | 2104 | } |
1950 | 2105 |
|
1951 | 2106 | function some(array, callback, thisArg) { |
| 2107 | + thisArg = thisArg || (function() { return this; })(); |
1952 | 2108 | return all(array).then(function(array) { |
1953 | 2109 | return array.some(callback, thisArg); |
1954 | 2110 | }); |
1955 | 2111 | } |
1956 | 2112 |
|
1957 | 2113 | function filter(array, callback, thisArg) { |
| 2114 | + thisArg = thisArg || (function() { return this; })(); |
1958 | 2115 | return all(array).then(function(array) { |
1959 | 2116 | return array.filter(callback, thisArg); |
1960 | 2117 | }); |
1961 | 2118 | } |
1962 | 2119 |
|
1963 | 2120 | function map(array, callback, thisArg) { |
| 2121 | + thisArg = thisArg || (function() { return this; })(); |
1964 | 2122 | return all(array).then(function(array) { |
1965 | 2123 | return array.map(callback, thisArg); |
1966 | 2124 | }); |
|
2035 | 2193 | } |
2036 | 2194 |
|
2037 | 2195 | function find(array, predicate, thisArg) { |
| 2196 | + thisArg = thisArg || (function() { return this; })(); |
2038 | 2197 | return all(array).then(function(array) { |
2039 | 2198 | return array.find(predicate, thisArg); |
2040 | 2199 | }); |
2041 | 2200 | } |
2042 | 2201 |
|
2043 | 2202 | function findIndex(array, predicate, thisArg) { |
| 2203 | + thisArg = thisArg || (function() { return this; })(); |
2044 | 2204 | return all(array).then(function(array) { |
2045 | 2205 | return array.findIndex(predicate, thisArg); |
2046 | 2206 | }); |
|
2067 | 2227 | settle: { value: settle }, |
2068 | 2228 | attempt: { value: attempt }, |
2069 | 2229 | run: { value: run }, |
| 2230 | + thunkify: { value: thunkify }, |
| 2231 | + promisify: { value: promisify }, |
| 2232 | + co: { value: co }, |
2070 | 2233 | wrap: { value: wrap }, |
2071 | 2234 | // for array |
2072 | 2235 | forEach: { value: forEach }, |
|
2374 | 2537 |
|
2375 | 2538 | global.hprose.Future = Future; |
2376 | 2539 |
|
| 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 | + |
2377 | 2545 | function Completer() { |
2378 | 2546 | var future = new Future(); |
2379 | 2547 | defineProperties(this, { |
|
0 commit comments