Created
October 10, 2018 17:53
-
-
Save Gozala/058e9039ac2645e3724689997d0d6899 to your computer and use it in GitHub Desktop.
Flow generator hackery to make it make it infer types
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* @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