ig-types 6.24.1 → 6.24.3

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.
Files changed (3) hide show
  1. package/Promise.js +61 -20
  2. package/README.md +7 -0
  3. package/package.json +1 -1
package/Promise.js CHANGED
@@ -795,6 +795,34 @@ object.Constructor('ProxyPromise', Promise, {
795
795
 
796
796
  //---------------------------------------------------------------------
797
797
 
798
+ var syncAllProxy =
799
+ function(name){
800
+ return function(lst){
801
+ var sync = true
802
+ for(var e of lst){
803
+ if(e instanceof Promise
804
+ && !(e instanceof SyncPromise)){
805
+ sync = false
806
+ break } }
807
+ return sync ?
808
+ this.resolve(lst)
809
+ : Promise[name](lst) } }
810
+
811
+ // XXX REVISE/TEST...
812
+ var syncAnyProxy =
813
+ function(name){
814
+ return function(lst){
815
+ for(var e of lst){
816
+ if(e instanceof SyncPromise
817
+ && !('error' in e)){
818
+ return e }
819
+ if(!(e instanceof Promise)){
820
+ return this.resolve(e) } }
821
+ return Promise[name](list) } }
822
+
823
+
824
+ // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
825
+
798
826
  // XXX EXPEREMENTAL...
799
827
  // XXX DOCS...
800
828
  // XXX like promise but if a value can be generated sync then this will
@@ -803,6 +831,17 @@ object.Constructor('ProxyPromise', Promise, {
803
831
  var SyncPromise =
804
832
  module.SyncPromise =
805
833
  object.Constructor('SyncPromise', Promise, {
834
+ // NOTE: we need to overload these as the builtin versions sneak-in
835
+ // async-ness before we can catch it in .__new__(..)
836
+ resolve: function(value){
837
+ return new this(function(resolve){ resolve(value) }) },
838
+ reject: function(error){
839
+ return new this(function(_, reject){ reject(error) }) },
840
+ all: syncAllProxy('all'),
841
+ allSettled: syncAllProxy('allSettled'),
842
+ any: syncAnyProxy('any'),
843
+ race: syncAnyProxy('race'),
844
+ },{
806
845
  //value: undefined,
807
846
  //error: undefined,
808
847
 
@@ -810,25 +849,28 @@ object.Constructor('SyncPromise', Promise, {
810
849
  // mode and thus set .value and possibly .error, soe there is no
811
850
  // need to check for .value...
812
851
  then: function(resolve, reject){
813
- if(this.hasOwnProperty('error')){
814
- return this.constructor.reject(
815
- reject ?
816
- reject(this.error)
817
- : this.error) }
818
- return this.constructor.resolve(
819
- resolve(this.value)) },
852
+ return this.hasOwnProperty('error') ?
853
+ this.constructor.reject(
854
+ reject ?
855
+ reject(this.error)
856
+ : this.error)
857
+ : resolve ?
858
+ this.constructor.resolve(
859
+ resolve(this.value))
860
+ // XXX should we return a copy???
861
+ : this },
820
862
 
821
863
  // NOTE: if func calls resolve(..) with a promise then this will return
822
864
  // that promise...
823
865
  __new__: function(context, func){
824
- var result
866
+ var value
825
867
  var resolve = function(res){
826
- result = res }
868
+ return (value = res) }
827
869
  var rejected
828
870
  var error
829
871
  var reject = function(err){
830
872
  rejected = true
831
- error = err }
873
+ return (error = err) }
832
874
  // call...
833
875
  try{
834
876
  func(resolve, reject)
@@ -836,12 +878,12 @@ object.Constructor('SyncPromise', Promise, {
836
878
  reject(err) }
837
879
  // async...
838
880
  if(!error
839
- && result instanceof Promise){
840
- return result }
881
+ && value instanceof Promise){
882
+ return value }
841
883
  // sync...
842
- var obj = Promise.resolve(result)
884
+ var obj = Promise.resolve(value)
843
885
  obj.__proto__ = this.prototype
844
- obj.value = result
886
+ obj.value = value
845
887
  rejected
846
888
  && (obj.error = error)
847
889
  return obj },
@@ -856,10 +898,8 @@ object.Mixin('PromiseMixin', 'soft', {
856
898
  iter: IterablePromise,
857
899
  interactive: InteractivePromise,
858
900
  cooperative: CooperativePromise,
859
- // XXX EXPEREMENTAL...
860
901
  sync: SyncPromise,
861
-
862
- // XXX need error support...
902
+ // XXX should this be implemented via SyncPromise???
863
903
  awaitOrRun: function(data, func){
864
904
  data = [...arguments]
865
905
  func = data.pop()
@@ -904,11 +944,12 @@ object.Mixin('PromiseProtoMixin', 'soft', {
904
944
  as: ProxyPromise,
905
945
  iter: function(handler=undefined){
906
946
  return IterablePromise(this, handler) },
907
- // XXX revise...
908
- sync: function(){
947
+ sync: function(error='throw'){
909
948
  if(this instanceof SyncPromise){
910
949
  if('error' in this){
911
- throw this.error }
950
+ if(typeof(error) != 'function'){
951
+ throw this.error }
952
+ return error(this.error) }
912
953
  return this.value }
913
954
  return this },
914
955
  })
package/README.md CHANGED
@@ -98,6 +98,7 @@ Library of JavaScript type extensions, types and utilities.
98
98
  - [`Promise.sync(..)` / `promise.SyncPromice(..)`](#promisesync--promisesyncpromice)
99
99
  - [`<promise>.sync(..)`](#promisesync)
100
100
  - [`<sync-promise>.value` / `<sync-promise>.error`](#sync-promisevalue--sync-promiseerror)
101
+ - [`Promise.sync.all(..)` / `Promise.sync.allSettled(..)` / `Promise.sync.any(..)` / `Promise.sync.race(..)`](promisesyncall--promisesyncallsettled--promisesyncany--promisesyncrace`)
101
102
  - [Promise utilities](#promise-utilities)
102
103
  - [`Promise.awaitOrRun(..)`](#promiseawaitorrun)
103
104
  - [Generator extensions and utilities](#generator-extensions-and-utilities)
@@ -2045,6 +2046,12 @@ if it _rejected_ then re-throw the `<error>`.
2045
2046
  rejection `.error`.
2046
2047
 
2047
2048
 
2049
+ #### `Promise.sync.all(..)` / `Promise.sync.allSettled(..)` / `Promise.sync.any(..)` / `Promise.sync.race(..)`
2050
+
2051
+ Equivalents to `Promise`'s version but will run sync if the relevant
2052
+ items in the input are either non-promises or `<sync-promise>`s.
2053
+
2054
+
2048
2055
 
2049
2056
  ### Promise utilities
2050
2057
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ig-types",
3
- "version": "6.24.1",
3
+ "version": "6.24.3",
4
4
  "description": "Generic JavaScript types and type extensions...",
5
5
  "main": "main.js",
6
6
  "scripts": {