|
1 | | -// Hprose for HTML5 v2.0.23 |
| 1 | +// Hprose for HTML5 v2.0.24 |
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: |
|
1097 | 1097 | * * |
1098 | 1098 | * hprose Future for HTML5. * |
1099 | 1099 | * * |
1100 | | - * LastModified: Oct 21, 2016 * |
| 1100 | + * LastModified: Nov 17, 2016 * |
1101 | 1101 | * Author: Ma Bingyao <andot@hprose.com> * |
1102 | 1102 | * * |
1103 | 1103 | \**********************************************************/ |
|
1142 | 1142 | return 'function' === typeof obj.then; |
1143 | 1143 | } |
1144 | 1144 |
|
1145 | | - function toPromise(obj) { |
1146 | | - return (isFuture(obj) ? obj : value(obj)); |
1147 | | - } |
1148 | | - |
1149 | 1145 | function delayed(duration, value) { |
1150 | 1146 | var computation = (typeof value === 'function') ? |
1151 | 1147 | value : |
|
1276 | 1272 | } |
1277 | 1273 |
|
1278 | 1274 | function attempt(handler/*, arg1, arg2, ... */) { |
| 1275 | + var thisArg = (function() { return this; })(); |
1279 | 1276 | var args = Array.slice(arguments, 1); |
1280 | 1277 | return all(args).then(function(args) { |
1281 | | - return handler.apply(undefined, args); |
| 1278 | + return handler.apply(thisArg, args); |
1282 | 1279 | }); |
1283 | 1280 | } |
1284 | 1281 |
|
|
1289 | 1286 | }); |
1290 | 1287 | } |
1291 | 1288 |
|
| 1289 | + function isGenerator(obj) { |
| 1290 | + return 'function' == typeof obj.next && 'function' == typeof obj['throw']; |
| 1291 | + } |
| 1292 | + |
| 1293 | + function isGeneratorFunction(obj) { |
| 1294 | + if (!obj) { |
| 1295 | + return false; |
| 1296 | + } |
| 1297 | + var constructor = obj.constructor; |
| 1298 | + if (!constructor) { |
| 1299 | + return false; |
| 1300 | + } |
| 1301 | + if ('GeneratorFunction' === constructor.name || |
| 1302 | + 'GeneratorFunction' === constructor.displayName) { |
| 1303 | + return true; |
| 1304 | + } |
| 1305 | + return isGenerator(constructor.prototype); |
| 1306 | + } |
| 1307 | + |
| 1308 | + function thunkToPromise(fn) { |
| 1309 | + var thisArg = (function() { return this; })(); |
| 1310 | + var future = new Future(); |
| 1311 | + fn.call(thisArg, function(err, res) { |
| 1312 | + if (arguments.length < 2) { |
| 1313 | + if (err instanceof Error) { |
| 1314 | + return future.reject(err); |
| 1315 | + } |
| 1316 | + return future.resolve(err); |
| 1317 | + } |
| 1318 | + if (err) { |
| 1319 | + return future.reject(err); |
| 1320 | + } |
| 1321 | + if (arguments.length > 2) { |
| 1322 | + res = Array.slice(arguments, 1); |
| 1323 | + } |
| 1324 | + future.resolve(res); |
| 1325 | + }); |
| 1326 | + return future; |
| 1327 | + } |
| 1328 | + |
| 1329 | + function thunkify(fn) { |
| 1330 | + return function() { |
| 1331 | + var args = Array.slice(arguments, 0); |
| 1332 | + var thisArg = this; |
| 1333 | + var results = new Future(); |
| 1334 | + args.push(function() { |
| 1335 | + thisArg = this; |
| 1336 | + results.resolve(arguments); |
| 1337 | + }); |
| 1338 | + try { |
| 1339 | + fn.apply(this, args); |
| 1340 | + } |
| 1341 | + catch (err) { |
| 1342 | + results.resolve([err]); |
| 1343 | + } |
| 1344 | + return function(done) { |
| 1345 | + results.then(function(results) { |
| 1346 | + done.apply(thisArg, results); |
| 1347 | + }); |
| 1348 | + }; |
| 1349 | + }; |
| 1350 | + } |
| 1351 | + |
| 1352 | + function promisify(fn) { |
| 1353 | + return function() { |
| 1354 | + var args = Array.slice(arguments, 0); |
| 1355 | + var results = new Future(); |
| 1356 | + args.push(function(err, res) { |
| 1357 | + if (arguments.length < 2) { |
| 1358 | + if (err instanceof Error) { |
| 1359 | + return results.reject(err); |
| 1360 | + } |
| 1361 | + return results.resolve(err); |
| 1362 | + } |
| 1363 | + if (err) { |
| 1364 | + return results.reject(err); |
| 1365 | + } |
| 1366 | + if (arguments.length > 2) { |
| 1367 | + res = Array.slice(arguments, 1); |
| 1368 | + } |
| 1369 | + results.resolve(res); |
| 1370 | + }); |
| 1371 | + try { |
| 1372 | + fn.apply(this, args); |
| 1373 | + } |
| 1374 | + catch (err) { |
| 1375 | + results.reject(err); |
| 1376 | + } |
| 1377 | + return results; |
| 1378 | + }; |
| 1379 | + } |
| 1380 | + |
| 1381 | + function toPromise(obj) { |
| 1382 | + if (!obj) { |
| 1383 | + return value(obj); |
| 1384 | + } |
| 1385 | + if (isPromise(obj)) { |
| 1386 | + return obj; |
| 1387 | + } |
| 1388 | + if (isGeneratorFunction(obj) || isGenerator(obj)) { |
| 1389 | + return co(obj); |
| 1390 | + } |
| 1391 | + if ('function' == typeof obj) { |
| 1392 | + return thunkToPromise(obj); |
| 1393 | + } |
| 1394 | + return value(obj); |
| 1395 | + } |
| 1396 | + |
| 1397 | + function co(gen) { |
| 1398 | + var thisArg = (function() { return this; })(); |
| 1399 | + if (typeof gen === 'function') { |
| 1400 | + var args = Array.slice(arguments, 1); |
| 1401 | + gen = gen.apply(thisArg, args); |
| 1402 | + } |
| 1403 | + var future = new Future(); |
| 1404 | + |
| 1405 | + function onFulfilled(res) { |
| 1406 | + try { |
| 1407 | + next(gen.next(res)); |
| 1408 | + } |
| 1409 | + catch (e) { |
| 1410 | + future.reject(e); |
| 1411 | + } |
| 1412 | + } |
| 1413 | + |
| 1414 | + function onRejected(err) { |
| 1415 | + try { |
| 1416 | + next(gen['throw'](err)); |
| 1417 | + } |
| 1418 | + catch (e) { |
| 1419 | + return future.reject(e); |
| 1420 | + } |
| 1421 | + } |
| 1422 | + |
| 1423 | + function next(ret) { |
| 1424 | + if (ret.done) { |
| 1425 | + future.resolve(ret.value); |
| 1426 | + } |
| 1427 | + else { |
| 1428 | + toPromise(ret.value).then(onFulfilled, onRejected); |
| 1429 | + } |
| 1430 | + } |
| 1431 | + |
| 1432 | + if (!gen || typeof gen.next !== 'function') { |
| 1433 | + return future.resolve(gen); |
| 1434 | + } |
| 1435 | + onFulfilled(); |
| 1436 | + |
| 1437 | + return future; |
| 1438 | + } |
| 1439 | + |
1292 | 1440 | function wrap(handler, thisArg) { |
1293 | 1441 | return function() { |
| 1442 | + thisArg = thisArg || this; |
1294 | 1443 | return all(arguments).then(function(args) { |
1295 | | - return handler.apply(thisArg, args); |
| 1444 | + var result = handler.apply(thisArg, args); |
| 1445 | + if (isGeneratorFunction(result)) { |
| 1446 | + return co.call(thisArg, result); |
| 1447 | + } |
| 1448 | + return result; |
1296 | 1449 | }); |
1297 | 1450 | }; |
1298 | 1451 | } |
1299 | 1452 |
|
1300 | 1453 | function forEach(array, callback, thisArg) { |
| 1454 | + thisArg = thisArg || (function() { return this; })(); |
1301 | 1455 | return all(array).then(function(array) { |
1302 | 1456 | return array.forEach(callback, thisArg); |
1303 | 1457 | }); |
1304 | 1458 | } |
1305 | 1459 |
|
1306 | 1460 | function every(array, callback, thisArg) { |
| 1461 | + thisArg = thisArg || (function() { return this; })(); |
1307 | 1462 | return all(array).then(function(array) { |
1308 | 1463 | return array.every(callback, thisArg); |
1309 | 1464 | }); |
1310 | 1465 | } |
1311 | 1466 |
|
1312 | 1467 | function some(array, callback, thisArg) { |
| 1468 | + thisArg = thisArg || (function() { return this; })(); |
1313 | 1469 | return all(array).then(function(array) { |
1314 | 1470 | return array.some(callback, thisArg); |
1315 | 1471 | }); |
1316 | 1472 | } |
1317 | 1473 |
|
1318 | 1474 | function filter(array, callback, thisArg) { |
| 1475 | + thisArg = thisArg || (function() { return this; })(); |
1319 | 1476 | return all(array).then(function(array) { |
1320 | 1477 | return array.filter(callback, thisArg); |
1321 | 1478 | }); |
1322 | 1479 | } |
1323 | 1480 |
|
1324 | 1481 | function map(array, callback, thisArg) { |
| 1482 | + thisArg = thisArg || (function() { return this; })(); |
1325 | 1483 | return all(array).then(function(array) { |
1326 | 1484 | return array.map(callback, thisArg); |
1327 | 1485 | }); |
|
1396 | 1554 | } |
1397 | 1555 |
|
1398 | 1556 | function find(array, predicate, thisArg) { |
| 1557 | + thisArg = thisArg || (function() { return this; })(); |
1399 | 1558 | return all(array).then(function(array) { |
1400 | 1559 | return array.find(predicate, thisArg); |
1401 | 1560 | }); |
1402 | 1561 | } |
1403 | 1562 |
|
1404 | 1563 | function findIndex(array, predicate, thisArg) { |
| 1564 | + thisArg = thisArg || (function() { return this; })(); |
1405 | 1565 | return all(array).then(function(array) { |
1406 | 1566 | return array.findIndex(predicate, thisArg); |
1407 | 1567 | }); |
|
1428 | 1588 | settle: { value: settle }, |
1429 | 1589 | attempt: { value: attempt }, |
1430 | 1590 | run: { value: run }, |
| 1591 | + thunkify: { value: thunkify }, |
| 1592 | + promisify: { value: promisify }, |
| 1593 | + co: { value: co }, |
1431 | 1594 | wrap: { value: wrap }, |
1432 | 1595 | // for array |
1433 | 1596 | forEach: { value: forEach }, |
|
1735 | 1898 |
|
1736 | 1899 | global.hprose.Future = Future; |
1737 | 1900 |
|
| 1901 | + global.hprose.thunkify = thunkify; |
| 1902 | + global.hprose.promisify = promisify; |
| 1903 | + global.hprose.co = co; |
| 1904 | + global.hprose.co.wrap = global.hprose.wrap = wrap; |
| 1905 | + |
1738 | 1906 | function Completer() { |
1739 | 1907 | var future = new Future(); |
1740 | 1908 | Object.defineProperties(this, { |
|
0 commit comments