UNPKG

1.03 kBJavaScriptView Raw
1var _arity =
2/*#__PURE__*/
3require("./internal/_arity");
4
5var _curry1 =
6/*#__PURE__*/
7require("./internal/_curry1");
8/**
9 * Accepts a function `fn` and returns a function that guards invocation of
10 * `fn` such that `fn` can only ever be called once, no matter how many times
11 * the returned function is invoked. The first value calculated is returned in
12 * subsequent invocations.
13 *
14 * @func
15 * @memberOf R
16 * @since v0.1.0
17 * @category Function
18 * @sig (a... -> b) -> (a... -> b)
19 * @param {Function} fn The function to wrap in a call-only-once wrapper.
20 * @return {Function} The wrapped function.
21 * @example
22 *
23 * const addOneOnce = R.once(x => x + 1);
24 * addOneOnce(10); //=> 11
25 * addOneOnce(addOneOnce(50)); //=> 11
26 */
27
28
29var once =
30/*#__PURE__*/
31_curry1(function once(fn) {
32 var called = false;
33 var result;
34 return _arity(fn.length, function () {
35 if (called) {
36 return result;
37 }
38
39 called = true;
40 result = fn.apply(this, arguments);
41 return result;
42 });
43});
44
45module.exports = once;
\No newline at end of file