Hello.
This code used to compile but now it’s giving a “This expression’s type contains type variables that can’t be generalized” error.
Also…is there a proper way to get the js iterator without using raw?
Thanks
type _iterable<'a>
type _iterator<'a> = unit => Promise.t<('a, bool)>
let _getIterator :_iterable<'a> => _iterator<'a> = %raw(`
function (iterable) {
if(iterable[Symbol.iterator]) {
let iterator = iterable[Symbol.iterator]()
return () => {
let {value, done} = iterator.next()
return Promise.resolve([value, done])
}
} else if(iterable[Symbol.asyncIterator]){
let iterator = iterable[Symbol.asyncIterator]()
return () => iterator.next().then(({value, done}) => [value, done])
}
}
`)
let fromIterable = (iterable :_iterable<'a>) :ReStream_Source.readable<'a> => {
let iterator: _iterator<'a> = _getIterator(iterable)
let done = ref(false)
(sig :ReStream_Source.signal<'a>) => switch sig {
| Pull(cb) when done.contents => cb(End)
| Pull(cb) => {
iterator()
-> Promise.then(((value, _done)) => {
if(_done) {
done := true
cb(End)
} else {
cb(Data(value))
}
Promise.resolve()
})
-> Promise.catch(err => {
cb(Error(err))
Promise.resolve()
})
-> ignore
}
| Abort => {
done := true
}
}
}