ig-types 6.21.0 → 6.22.1
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 +96 -0
- package/README.md +28 -1
- package/package.json +1 -1
package/Promise.js
CHANGED
|
@@ -793,6 +793,65 @@ object.Constructor('ProxyPromise', Promise, {
|
|
|
793
793
|
|
|
794
794
|
|
|
795
795
|
|
|
796
|
+
//---------------------------------------------------------------------
|
|
797
|
+
|
|
798
|
+
// XXX like promise but if a value can be generated sync then this will
|
|
799
|
+
// run in sync otherwise it will fall back to being a promise...
|
|
800
|
+
// ...not sure where to return the sync value...
|
|
801
|
+
// XXX potential problem is recursion (depth) on the sync stage...
|
|
802
|
+
// XXX not sure if we need this..
|
|
803
|
+
var MaybePromice =
|
|
804
|
+
module.MaybePromice =
|
|
805
|
+
object.Constructor('MaybePromise', Promise, {
|
|
806
|
+
// XXX can we get into this without either .__error or .__result ???
|
|
807
|
+
then: function(resolve, reject){
|
|
808
|
+
if(this.hasOwnProperty('__error')){
|
|
809
|
+
return this.constructor.reject(
|
|
810
|
+
reject ?
|
|
811
|
+
reject(this.__error)
|
|
812
|
+
: this.__error) }
|
|
813
|
+
if(this.hasOwnProperty('__result')){
|
|
814
|
+
return this.constructor.resolve(
|
|
815
|
+
resolve(this.__result)) } },
|
|
816
|
+
//catch: function(func){
|
|
817
|
+
//},
|
|
818
|
+
//finally: function(func){
|
|
819
|
+
//},
|
|
820
|
+
|
|
821
|
+
//__error: undefined,
|
|
822
|
+
//__result: undefined,
|
|
823
|
+
__new__: function(context, func){
|
|
824
|
+
var result
|
|
825
|
+
var resolve = function(res){
|
|
826
|
+
result = res }
|
|
827
|
+
var error
|
|
828
|
+
var reject = function(err){
|
|
829
|
+
error = err }
|
|
830
|
+
|
|
831
|
+
try{
|
|
832
|
+
func(resolve, reject)
|
|
833
|
+
}catch(err){
|
|
834
|
+
reject(err) }
|
|
835
|
+
|
|
836
|
+
if(error){
|
|
837
|
+
this.__error = error
|
|
838
|
+
|
|
839
|
+
// async...
|
|
840
|
+
} else if(result instanceof Promise){
|
|
841
|
+
return result }
|
|
842
|
+
|
|
843
|
+
// sync...
|
|
844
|
+
this.__result = result
|
|
845
|
+
// XXX
|
|
846
|
+
var obj = Reflect.construct(
|
|
847
|
+
MaybePromise.__proto__,
|
|
848
|
+
[],
|
|
849
|
+
MaybePromise)
|
|
850
|
+
return obj
|
|
851
|
+
},
|
|
852
|
+
})
|
|
853
|
+
|
|
854
|
+
|
|
796
855
|
//---------------------------------------------------------------------
|
|
797
856
|
|
|
798
857
|
var PromiseMixin =
|
|
@@ -801,6 +860,43 @@ object.Mixin('PromiseMixin', 'soft', {
|
|
|
801
860
|
iter: IterablePromise,
|
|
802
861
|
interactive: InteractivePromise,
|
|
803
862
|
cooperative: CooperativePromise,
|
|
863
|
+
// XXX
|
|
864
|
+
//maybe: MaybePromise,
|
|
865
|
+
|
|
866
|
+
// XXX need error support...
|
|
867
|
+
awaitOrRun: function(data, func){
|
|
868
|
+
data = [...arguments]
|
|
869
|
+
func = data.pop()
|
|
870
|
+
var error
|
|
871
|
+
if(typeof(data.at(-1)) == 'function'){
|
|
872
|
+
error = func
|
|
873
|
+
func = data.pop() }
|
|
874
|
+
// ceck if we need to await...
|
|
875
|
+
return data
|
|
876
|
+
.reduce(function(res, e){
|
|
877
|
+
return res
|
|
878
|
+
|| e instanceof Promise }, false) ?
|
|
879
|
+
// NOTE: we will not reach this on empty data...
|
|
880
|
+
(data.length > 1 ?
|
|
881
|
+
Promise.all(data)
|
|
882
|
+
.then(
|
|
883
|
+
function(res){
|
|
884
|
+
return func(...res) },
|
|
885
|
+
...(error ?
|
|
886
|
+
[error]
|
|
887
|
+
: []))
|
|
888
|
+
: data[0].then(
|
|
889
|
+
func,
|
|
890
|
+
...(error ?
|
|
891
|
+
[error]
|
|
892
|
+
: [])))
|
|
893
|
+
: error ?
|
|
894
|
+
function(){
|
|
895
|
+
try{
|
|
896
|
+
func(...data)
|
|
897
|
+
}catch(err){
|
|
898
|
+
error(err) } }()
|
|
899
|
+
: func(...data) },
|
|
804
900
|
})
|
|
805
901
|
|
|
806
902
|
PromiseMixin(Promise)
|
package/README.md
CHANGED
|
@@ -94,6 +94,8 @@ Library of JavaScript type extensions, types and utilities.
|
|
|
94
94
|
- [Promise proxies](#promise-proxies)
|
|
95
95
|
- [`<promise>.as(..)`](#promiseas)
|
|
96
96
|
- [`<promise-proxy>.<method>(..)`](#promise-proxymethod)
|
|
97
|
+
- [Promise utilities](#promise-utilities)
|
|
98
|
+
- [`Promise.awaitOrRun(..)`](#promiseawaitorrun)
|
|
97
99
|
- [Generator extensions and utilities](#generator-extensions-and-utilities)
|
|
98
100
|
- [The basics](#the-basics)
|
|
99
101
|
- [`generator.Generator`](#generatorgenerator)
|
|
@@ -636,11 +638,17 @@ Compare to arrays ignoring element order and count.
|
|
|
636
638
|
Sort array as a different array.
|
|
637
639
|
```bnf
|
|
638
640
|
<array>.sortAs(<other>)
|
|
641
|
+
<array>.sortAs(<other>, 'head')
|
|
642
|
+
-> <array>
|
|
643
|
+
|
|
644
|
+
<array>.sortAs(<other>, 'tail')
|
|
639
645
|
-> <array>
|
|
640
646
|
```
|
|
641
647
|
|
|
642
648
|
Elements not present in `<other>` retain their relative order and are
|
|
643
|
-
placed after the sorted elements.
|
|
649
|
+
placed after the sorted elements if `'head'` (i.e. _"sorted at head of
|
|
650
|
+
array"_) is passed as second argument (default) and before them if
|
|
651
|
+
`'tail'` (_"sorted at tail"_) is passed.
|
|
644
652
|
|
|
645
653
|
Example:
|
|
646
654
|
```javascript
|
|
@@ -1993,6 +2001,25 @@ the main `<promise>` is resolved.
|
|
|
1993
2001
|
|
|
1994
2002
|
|
|
1995
2003
|
|
|
2004
|
+
### Promise utilities
|
|
2005
|
+
|
|
2006
|
+
#### `Promise.awaitOrRun(..)`
|
|
2007
|
+
|
|
2008
|
+
Await for inputs if any of them is a promise and then run a function with
|
|
2009
|
+
the results, otherwise run the function in sync.
|
|
2010
|
+
|
|
2011
|
+
```dnf
|
|
2012
|
+
Promise.awaitOrRun(<value>, <func>[, <onerror>])
|
|
2013
|
+
Promise.awaitOrRun(<value>, .. , <func>[, <onerror>])
|
|
2014
|
+
-> <promise(value)>
|
|
2015
|
+
-> <value>
|
|
2016
|
+
```
|
|
2017
|
+
|
|
2018
|
+
Note that if the last `<value>` is a function and no `<onerror>` function
|
|
2019
|
+
is given then `.awaitOrRun(..)` will confuse the `<value>` for `<func>`,
|
|
2020
|
+
to avoid this one needs to explicitly pass `null`/`undefined` as `<onerror>`.
|
|
2021
|
+
|
|
2022
|
+
|
|
1996
2023
|
## Generator extensions and utilities
|
|
1997
2024
|
|
|
1998
2025
|
```javascript
|