ig-types 6.10.1 → 6.11.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/Promise.js +55 -1
- package/README.md +53 -0
- package/package.json +1 -1
package/Promise.js
CHANGED
|
@@ -417,6 +417,58 @@ object.Constructor('CooperativePromise', Promise, {
|
|
|
417
417
|
|
|
418
418
|
//---------------------------------------------------------------------
|
|
419
419
|
|
|
420
|
+
var ProxyPromise =
|
|
421
|
+
module.ProxyPromise =
|
|
422
|
+
object.Constructor('ProxyPromise', Promise, {
|
|
423
|
+
__new__: function(context, constructor){
|
|
424
|
+
var proto = 'prototype' in constructor ?
|
|
425
|
+
constructor.prototype
|
|
426
|
+
: constructor
|
|
427
|
+
var obj = Reflect.construct(
|
|
428
|
+
ProxyPromise.__proto__,
|
|
429
|
+
[function(resolve, reject){
|
|
430
|
+
context.then(resolve)
|
|
431
|
+
context.catch(reject) }],
|
|
432
|
+
ProxyPromise)
|
|
433
|
+
// populate...
|
|
434
|
+
// NOTE: we are not using object.deepKeys(..) here as we need
|
|
435
|
+
// the key origin not to trigger property getters...
|
|
436
|
+
var seen = new Set()
|
|
437
|
+
while(proto != null){
|
|
438
|
+
Object.entries(Object.getOwnPropertyDescriptors(proto))
|
|
439
|
+
.forEach(function([key, value]){
|
|
440
|
+
// skip overloaded keys...
|
|
441
|
+
if(seen.has(key)){
|
|
442
|
+
return }
|
|
443
|
+
// skip non-functions...
|
|
444
|
+
if(typeof(value.value) != 'function'){
|
|
445
|
+
return }
|
|
446
|
+
// skip non-enumerable except for Object.prototype.run(..)...
|
|
447
|
+
if(!(key == 'run'
|
|
448
|
+
&& Object.prototype.run === value.value)
|
|
449
|
+
&& !value.enumerable){
|
|
450
|
+
return }
|
|
451
|
+
// proxy...
|
|
452
|
+
obj[key] = function(...args){
|
|
453
|
+
// XXX should we also .catch(..) here???
|
|
454
|
+
return context.then(function(res){
|
|
455
|
+
return res[key](...args) }) } })
|
|
456
|
+
proto = proto.__proto__ }
|
|
457
|
+
return obj },
|
|
458
|
+
})
|
|
459
|
+
|
|
460
|
+
|
|
461
|
+
|
|
462
|
+
//---------------------------------------------------------------------
|
|
463
|
+
|
|
464
|
+
// XXX EXPEREMENTAL...
|
|
465
|
+
var PromiseProtoMixin =
|
|
466
|
+
module.PromiseProtoMixin =
|
|
467
|
+
object.Mixin('PromiseProtoMixin', 'soft', {
|
|
468
|
+
as: ProxyPromise,
|
|
469
|
+
})
|
|
470
|
+
|
|
471
|
+
|
|
420
472
|
var PromiseMixin =
|
|
421
473
|
module.PromiseMixin =
|
|
422
474
|
object.Mixin('PromiseMixin', 'soft', {
|
|
@@ -425,9 +477,11 @@ object.Mixin('PromiseMixin', 'soft', {
|
|
|
425
477
|
cooperative: CooperativePromise,
|
|
426
478
|
})
|
|
427
479
|
|
|
428
|
-
|
|
429
480
|
PromiseMixin(Promise)
|
|
430
481
|
|
|
482
|
+
// XXX EXPEREMENTAL...
|
|
483
|
+
PromiseProtoMixin(Promise.prototype)
|
|
484
|
+
|
|
431
485
|
|
|
432
486
|
|
|
433
487
|
|
package/README.md
CHANGED
|
@@ -74,6 +74,9 @@ Library of JavaScript type extensions, types and utilities.
|
|
|
74
74
|
- [`<promise-iter>.flat(..)`](#promise-iterflat)
|
|
75
75
|
- [`<promise-iter>.then(..)` / `<promise-iter>.catch(..)` / `<promise-iter>.finally(..)`](#promise-iterthen--promise-itercatch--promise-iterfinally)
|
|
76
76
|
- [Advanced handler](#advanced-handler)
|
|
77
|
+
- [Promise proxies](#promise-proxies)
|
|
78
|
+
- [`<promise>.as(..)`](#promiseas)
|
|
79
|
+
- [`<promise-proxy>.<method>(..)`](#promise-proxymethod)
|
|
77
80
|
- [Generator extensions and utilities](#generator-extensions-and-utilities)
|
|
78
81
|
- [The basics](#the-basics)
|
|
79
82
|
- [`generator.Generator`](#generatorgenerator)
|
|
@@ -1621,6 +1624,56 @@ var p = Promise.iter(
|
|
|
1621
1624
|
```
|
|
1622
1625
|
|
|
1623
1626
|
|
|
1627
|
+
### Promise proxies
|
|
1628
|
+
|
|
1629
|
+
_Promise proxies_ generate a set of prototype methods returning promises that when the parent promise is resolved will resolve to a specific method call.
|
|
1630
|
+
|
|
1631
|
+
Example:
|
|
1632
|
+
```javascript
|
|
1633
|
+
var o = {
|
|
1634
|
+
method: function(...args){
|
|
1635
|
+
console.log('method:', ...args)
|
|
1636
|
+
},
|
|
1637
|
+
}
|
|
1638
|
+
|
|
1639
|
+
var p = Peomise.cooperative().as(o)
|
|
1640
|
+
|
|
1641
|
+
p.method(1, 2, 3) // returns a promise...
|
|
1642
|
+
|
|
1643
|
+
// ...
|
|
1644
|
+
|
|
1645
|
+
// resolving a promise will trigger all the proxy emthod execution, so
|
|
1646
|
+
// here 'method: 1, 2, 3' will get printed...
|
|
1647
|
+
p.set(o)
|
|
1648
|
+
|
|
1649
|
+
```
|
|
1650
|
+
|
|
1651
|
+
#### `<promise>.as(..)`
|
|
1652
|
+
|
|
1653
|
+
Create a promise proxy
|
|
1654
|
+
|
|
1655
|
+
```bnf
|
|
1656
|
+
<promise>.as(<object>)
|
|
1657
|
+
<promise>.as(<constructor>)
|
|
1658
|
+
-> <promise-proxy>
|
|
1659
|
+
```
|
|
1660
|
+
|
|
1661
|
+
A proxy promise will be populated with proxy methods to all the methods of the `<object>` or `<constructor>.prototype`.
|
|
1662
|
+
|
|
1663
|
+
#### `<promise-proxy>.<method>(..)`
|
|
1664
|
+
|
|
1665
|
+
When `<promise>` resolves, call the `.<method>(..)` on the resolved value.
|
|
1666
|
+
|
|
1667
|
+
```bnf
|
|
1668
|
+
<promise-proxy>.<method>(..)
|
|
1669
|
+
-> <method-promise>
|
|
1670
|
+
```
|
|
1671
|
+
|
|
1672
|
+
`<method-promise>` will resolve the the return value of the `<method>` when
|
|
1673
|
+
the main `<promise>` is resolved.
|
|
1674
|
+
|
|
1675
|
+
|
|
1676
|
+
|
|
1624
1677
|
## Generator extensions and utilities
|
|
1625
1678
|
|
|
1626
1679
|
```javascript
|