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:
1.0 KB
|
Rev | Line | |
---|
[d24f17c] | 1 | var _arity =
|
---|
| 2 | /*#__PURE__*/
|
---|
| 3 | require("./internal/_arity.js");
|
---|
| 4 |
|
---|
| 5 | var _curry1 =
|
---|
| 6 | /*#__PURE__*/
|
---|
| 7 | require("./internal/_curry1.js");
|
---|
| 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 |
|
---|
| 29 | var 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 |
|
---|
| 45 | module.exports = once; |
---|
Note:
See
TracBrowser
for help on using the repository browser.