-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bbf2008
commit 6355c4e
Showing
1 changed file
with
56 additions
and
1 deletion.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,56 @@ | ||
console.log('hello') | ||
export type Result<T, E> = { type: 'ok'; value: T } | { type: 'error'; value: E } | ||
|
||
export type Status = 'created' | 'pending' | 'done' | ||
|
||
export class Future<T> { | ||
constructor( | ||
public fn: (done: (result: T) => void) => void, | ||
public thenFn?: (result: T) => void, | ||
public status: Status = 'created' | ||
) {} | ||
|
||
map(mapFn: (result: T) => void): Future<T> { | ||
this.thenFn = mapFn | ||
return this | ||
} | ||
} | ||
|
||
export class Runtime { | ||
private queue: Future<any>[] = [] | ||
private pending: Future<any>[] = [] | ||
|
||
loop(): void { | ||
if (this.queue.length !== 0) { | ||
const future = this.queue.splice(0, 1)[0] | ||
future.status = 'pending' | ||
this.pending.push(future) | ||
future.fn(res => { | ||
future.thenFn?.(res) | ||
future.status = 'done' | ||
this.pending.splice(this.pending.indexOf(future), 1) | ||
}) | ||
} | ||
if (this.queue.length !== 0 || this.pending.length !== 0) { | ||
setTimeout(() => this.loop(), 10) | ||
} | ||
} | ||
|
||
spawn(future: Future<any>): void { | ||
this.queue.push(future) | ||
} | ||
} | ||
export const runtime = new Runtime() | ||
|
||
runtime.spawn( | ||
new Future<void>(done => { | ||
main() | ||
done() | ||
}) | ||
) | ||
runtime.loop() | ||
|
||
function main() { | ||
console.log('Hello, World!') | ||
const f = new Future(res => setTimeout(() => res(5), 1000)).map(res => console.log('res', res)) | ||
runtime.spawn(f) | ||
} |