ig-types 6.24.0 → 6.24.2

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 +46 -7
  2. package/README.md +39 -6
  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
 
@@ -821,14 +860,14 @@ object.Constructor('SyncPromise', Promise, {
821
860
  // NOTE: if func calls resolve(..) with a promise then this will return
822
861
  // that promise...
823
862
  __new__: function(context, func){
824
- var result
863
+ var value
825
864
  var resolve = function(res){
826
- result = res }
865
+ return (value = res) }
827
866
  var rejected
828
867
  var error
829
868
  var reject = function(err){
830
869
  rejected = true
831
- error = err }
870
+ return (error = err) }
832
871
  // call...
833
872
  try{
834
873
  func(resolve, reject)
@@ -836,12 +875,12 @@ object.Constructor('SyncPromise', Promise, {
836
875
  reject(err) }
837
876
  // async...
838
877
  if(!error
839
- && result instanceof Promise){
840
- return result }
878
+ && value instanceof Promise){
879
+ return value }
841
880
  // sync...
842
- var obj = Promise.resolve(result)
881
+ var obj = Promise.resolve(value)
843
882
  obj.__proto__ = this.prototype
844
- obj.value = result
883
+ obj.value = value
845
884
  rejected
846
885
  && (obj.error = error)
847
886
  return obj },
package/README.md CHANGED
@@ -95,8 +95,10 @@ Library of JavaScript type extensions, types and utilities.
95
95
  - [`<promise>.as(..)`](#promiseas)
96
96
  - [`<promise-proxy>.<method>(..)`](#promise-proxymethod)
97
97
  - [Sync/async promise](#syncasync-promise)
98
- - [`Promise.maybe(..)` / `promise.MaybePromice(..)`](#promisemaybe--promisemaybepromice)
99
- - [`<maybe-promise>.value` / `<maybe-promise>.error`](#maybe-promisevalue--maybe-promiseerror)
98
+ - [`Promise.sync(..)` / `promise.SyncPromice(..)`](#promisesync--promisesyncpromice)
99
+ - [`<promise>.sync(..)`](#promisesync)
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`)
100
102
  - [Promise utilities](#promise-utilities)
101
103
  - [`Promise.awaitOrRun(..)`](#promiseawaitorrun)
102
104
  - [Generator extensions and utilities](#generator-extensions-and-utilities)
@@ -2009,15 +2011,46 @@ the main `<promise>` is resolved.
2009
2011
 
2010
2012
  #### `Promise.sync(..)` / `promise.SyncPromice(..)`
2011
2013
 
2012
- XXX
2014
+ ```dnf
2015
+ Promise.sync(<func>)
2016
+ -> <sync-promise>
2013
2017
 
2014
- #### `<sync-promise>.value` / `<sync-promise>.error`
2018
+ <func>(<resolve>, <reject>)
2019
+
2020
+ <resolve>(<value>)
2021
+
2022
+ <reject>(<error>)
2023
+ ```
2024
+
2025
+ Implements the full `Promise` protocol but does it in a sync manner, but
2026
+ the execution of `<func>` is done synchronously. If the value passed to
2027
+ `<resolve>(..)` is a promise this will return that and continue asynchronously
2028
+ otherwise all the promise API (`.then(..)`/`.catch(..)`/...) is run in sync.
2029
+
2030
+ <!--
2031
+ Example:
2032
+ ```javascript
2033
+ ```
2034
+ -->
2015
2035
 
2016
- XXX
2017
2036
 
2018
2037
  #### `<promise>.sync()`
2019
2038
 
2020
- XXX
2039
+ Synchronously return the resolved value if `<sync-promise>` resolved, and
2040
+ if it _rejected_ then re-throw the `<error>`.
2041
+
2042
+
2043
+ #### `<sync-promise>.value` / `<sync-promise>.error`
2044
+
2045
+ `<sync-promise>` attributes that provide access the resolved `.value` and/or
2046
+ rejection `.error`.
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
+
2021
2054
 
2022
2055
 
2023
2056
  ### Promise utilities
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ig-types",
3
- "version": "6.24.0",
3
+ "version": "6.24.2",
4
4
  "description": "Generic JavaScript types and type extensions...",
5
5
  "main": "main.js",
6
6
  "scripts": {