Skip to content

Instantly share code, notes, and snippets.

@Gozala
Created October 10, 2018 17:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Gozala/058e9039ac2645e3724689997d0d6899 to your computer and use it in GitHub Desktop.
Save Gozala/058e9039ac2645e3724689997d0d6899 to your computer and use it in GitHub Desktop.
Flow generator hackery to make it make it infer types
/* @flow strict */
type Future<x, a> = () => Task<x, a>
type Task<x, a> = {
@@iterator(): $Iterator<empty, a, empty>;
next(empty): IteratorResult<empty, a>;
return(a): IteratorResult<empty, a>;
throw(x): IteratorResult<empty, a>;
}
class Effect<x, a> {
value:a
isDone:boolean;
@@iterator: () => $Iterator<empty,a,empty>;
constructor(value:a) {
this.value = value;
this.isDone = false
}
// [Symbol.iterator]() {
// return this
// }
get done() {
if (this.isDone) {
return true
} else {
this.isDone = true
return false
}
}
next() {
return this
}
}
type Result <x, a> =
| {ok:true, value:a}
| {ok:false, error:x}
declare function spawn <x, a>(Future<x, a>):Promise<Result<x, a>>
declare function read(string):Task<Error, ArrayBuffer>
declare function write(string, ArrayBuffer):Effect<Error, void>
var out = spawn(function*() {
var buffer = yield * read("foo")
const slice = buffer.slice(10, 20)
const done = yield * write("another/path", slice)
return buffer
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment