Created
September 17, 2019 03:52
-
-
Save eihigh/d57594a7f44522fc9f45bcebe4766dd5 to your computer and use it in GitHub Desktop.
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
// aco -- Async COmmunication system | |
export interface Action { | |
event: Event; | |
value: any; | |
tag?: string; | |
} | |
let tmp: Action | null = null; | |
export const send = (value: any, tag?: string) => (event: Event) => { | |
const a: Action = { event, value, tag }; | |
if (tmp === null) { | |
tmp = a; | |
return; | |
} | |
const poll = () => { | |
if (tmp === null) { | |
tmp = a; | |
return; | |
} | |
requestAnimationFrame(poll); | |
}; | |
requestAnimationFrame(poll); | |
}; | |
export const recv = async (tag?: string): Promise<Action> => { | |
return new Promise(resolve => { | |
const poll = () => { | |
if (tmp !== null && (tag === undefined ? true : tag === tmp.tag)) { | |
const res = tmp; | |
tmp = null; | |
resolve(res); | |
return; | |
} | |
requestAnimationFrame(poll); | |
}; | |
requestAnimationFrame(poll); | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment