main
Last change
on this file was d24f17c, checked in by Aleksandar Panovski <apano77@…>, 15 months ago |
Initial commit
|
-
Property mode
set to
100644
|
File size:
999 bytes
|
Line | |
---|
1 | import _arity from "./internal/_arity.js";
|
---|
2 | import _curry1 from "./internal/_curry1.js";
|
---|
3 | /**
|
---|
4 | * Accepts a function `fn` and returns a function that guards invocation of
|
---|
5 | * `fn` such that `fn` can only ever be called once, no matter how many times
|
---|
6 | * the returned function is invoked. The first value calculated is returned in
|
---|
7 | * subsequent invocations.
|
---|
8 | *
|
---|
9 | * @func
|
---|
10 | * @memberOf R
|
---|
11 | * @since v0.1.0
|
---|
12 | * @category Function
|
---|
13 | * @sig (a... -> b) -> (a... -> b)
|
---|
14 | * @param {Function} fn The function to wrap in a call-only-once wrapper.
|
---|
15 | * @return {Function} The wrapped function.
|
---|
16 | * @example
|
---|
17 | *
|
---|
18 | * const addOneOnce = R.once(x => x + 1);
|
---|
19 | * addOneOnce(10); //=> 11
|
---|
20 | * addOneOnce(addOneOnce(50)); //=> 11
|
---|
21 | */
|
---|
22 |
|
---|
23 | var once =
|
---|
24 | /*#__PURE__*/
|
---|
25 | _curry1(function once(fn) {
|
---|
26 | var called = false;
|
---|
27 | var result;
|
---|
28 | return _arity(fn.length, function () {
|
---|
29 | if (called) {
|
---|
30 | return result;
|
---|
31 | }
|
---|
32 |
|
---|
33 | called = true;
|
---|
34 | result = fn.apply(this, arguments);
|
---|
35 | return result;
|
---|
36 | });
|
---|
37 | });
|
---|
38 |
|
---|
39 | export default once; |
---|
Note:
See
TracBrowser
for help on using the repository browser.