experimenting with AsyncGenerator static methods...

Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
Alex A. Naanou 2022-06-22 10:26:04 +03:00
parent 3a211c9d82
commit 2b6dfb35c2
2 changed files with 30 additions and 14 deletions

View File

@ -124,6 +124,7 @@ Library of JavaScript type extensions, types and utilities.
- [Generator helpers](#generator-helpers)
- [`generator.stoppable(..)`](#generatorstoppable)
- [Async generator extensions](#async-generator-extensions)
- [`generator.AsyncGenerator`](#generatorasyncgenerator)
- [`<async-generator>.then(..)` / `<async-generator>.catch(..)` / `<async-generator>.finally(..)`](#async-generatorthen--async-generatorcatch--async-generatorfinally)
- [`<async-generator>.iter(..)`](#async-generatoriter)
- [`<async-generator>.map(..)` / `<async-generator>.filter(..)` / `<async-generator>.reduce(..)`](#async-generatormap--async-generatorfilter--async-generatorreduce)
@ -2493,6 +2494,8 @@ stoppable(<generator>)
XXX EXPERIMENTAL
#### `generator.AsyncGenerator`
#### `<async-generator>.then(..)` / `<async-generator>.catch(..)` / `<async-generator>.finally(..)`
#### `<async-generator>.iter(..)`

View File

@ -121,9 +121,11 @@ Generator.iter =
//
// makeGenerator(<name>)
// makeGenerator(<name>, <handler>)
// -> <func>
//
// makeGenerator(<name>, <handler>)
// makeGenerator('async', <name>)
// makeGenerator('async', <name>, <handler>)
// -> <func>
//
//
@ -140,14 +142,26 @@ Generator.iter =
// XXX this needs to be of the correct type... (???)
// XXX need to accept generators as handlers...
var makeGenerator = function(name, pre){
var sync = true
if(name == 'async'){
sync = false
var [name, pre] = [...arguments].slice(1) }
return function(...args){
var that = this
return Object.assign(
function*(){
var a = pre ?
pre.call(this, args, ...arguments)
: args
yield* that(...arguments)[name](...a) },
// NOTE: the two branches here are identical, the only
// difference is the async keyword...
sync ?
function*(){
var a = pre ?
pre.call(this, args, ...arguments)
: args
yield* that(...arguments)[name](...a) }
: async function*(){
var a = pre ?
pre.call(this, args, ...arguments)
: args
yield* that(...arguments)[name](...a) },
{ toString: function(){
return [
that.toString(),
@ -461,19 +475,18 @@ ITERATOR_PROTOTYPES
var AsyncGeneratorMixin =
module.AsyncGeneratorMixin =
object.Mixin('AsyncGeneratorMixin', 'soft', {
// XXX TEST...
iter: makeGenerator('async', 'iter'),
map: makeGenerator('async', 'map'),
filter: makeGenerator('async', 'filter'),
reduce: makeGenerator('async', 'reduce'),
})
var AsyncGeneratorProtoMixin =
module.AsyncGeneratorProtoMixin =
object.Mixin('AsyncGeneratorProtoMixin', 'soft', {
// promise...
//
// .then()
// -> promise
//
// .then(resolve[, reject])
// -> promise
//
// NOTE: this makes this await compatible...
// NOTE: this will unwind the generator...
// XXX create an iterator promise???
// XXX should we unwind???
@ -539,7 +552,7 @@ object.Mixin('AsyncGeneratorProtoMixin', 'soft', {
yield* this },
// XXX
// slice
// slice -- not sure if we need this...
// ...
})