Last active
May 6, 2017 01:49
-
-
Save typoerr/435e1b8c62ad9a5e202b51b5eda1b3df to your computer and use it in GitHub Desktop.
This file contains hidden or 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
/* | |
* 指定count回目の呼び出しでcallbackを叩く関数 | |
*/ | |
export function onlyThatTime(n: number, callback: () => any): () => void; | |
export function onlyThatTime<A>(n: number, callback: (a: A) => any, init?: [A]): (a: A) => void; | |
export function onlyThatTime<A1, A2>(n: number, callback: (a1: A1, a2: A2) => any, init?: [A1, A2]): (a1: A1, a2: A2) => void; | |
export function onlyThatTime<A1, A2, A3>(n: number, callback: (a1: A1, a2: A2, a3: A3) => any, init?: [A1, A2, A3]): (a1: A1, a2: A2, a3: A3) => void; | |
export function onlyThatTime(n: number, callback: Function, init: any[] = []): Function { | |
let called = false; | |
let count = 0; | |
return (n <= 0) ? constant(awaiter)(callback.apply(null, init)) : awaiter; | |
function awaiter() { | |
count = count + 1; | |
if (called) return; | |
if (n === count) { | |
called = true; | |
callback.apply(null, arguments); | |
} | |
} | |
} | |
export function constant<T>(v: T) { | |
return (..._x: any[]) => v; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment