once.js 478 B

123456789101112131415161718192021
  1. /**
  2. * Copyright (c) Facebook, Inc. and its affiliates.
  3. *
  4. * This source code is licensed under the MIT license found in the
  5. * LICENSE file in the root directory of this source tree.
  6. */
  7. /**
  8. * This replicates lodash's once functionality for our purposes.
  9. */
  10. module.exports = function(func) {
  11. let called = false;
  12. let result;
  13. return function(...args) {
  14. if (called) {
  15. return result;
  16. }
  17. called = true;
  18. return result = func.apply(this, args);
  19. };
  20. };