Allow One Function Call 只呼叫一次有回傳的fn
Given a function fn
, return a new function that is identical to the original function except that it ensures fn
is called at most once.
fn
.undefined
.
Example 1:
Input: fn = (a,b,c) => (a + b + c), calls = [[1,2,3],[2,3,6]]
Output: [{"calls":1,"value":6}]
Explanation:
const onceFn = once(fn);
onceFn(1, 2, 3); // 6
onceFn(2, 3, 6); // undefined, fn was not called
Example 2:
Input: fn = (a,b,c) => (a * b * c), calls = [[5,7,4],[2,3,6],[4,6,8]]
Output: [{"calls":1,"value":140}]
Explanation:
const onceFn = once(fn);
onceFn(5, 7, 4); // 140
onceFn(2, 3, 6); // undefined, fn was not called
onceFn(4, 6, 8); // undefined, fn was not called
Constraints:
1 <= calls.length <= 10
1 <= calls[i].length <= 100
2 <= JSON.stringify(calls).length <= 1000
解法:
這是講一個函式傳入另一個函式,這個函式要控管他呼叫只能在第一次有回傳結果後續都是undefined
type Fn = (...args: any[]) => any;
const once = <T extends Fn>(fn: T): ((...args: Parameters<T>) => ReturnType<T> | undefined) => {
let isCall = false;
return (...args) => {
if(isCall){
return undefined;
}else {
isCall = true;
return fn(...args);
}
}
}