Skip to content

Instantly share code, notes, and snippets.

@eihigh
Created September 17, 2019 03:52
Show Gist options
  • Save eihigh/d57594a7f44522fc9f45bcebe4766dd5 to your computer and use it in GitHub Desktop.
Save eihigh/d57594a7f44522fc9f45bcebe4766dd5 to your computer and use it in GitHub Desktop.
// 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