once.js 401 B

12345678910111213141516171819202122232425
  1. 'use strict';
  2. const internals = {
  3. wrapped: Symbol('wrapped')
  4. };
  5. module.exports = function (method) {
  6. if (method[internals.wrapped]) {
  7. return method;
  8. }
  9. let once = false;
  10. const wrappedFn = function (...args) {
  11. if (!once) {
  12. once = true;
  13. method(...args);
  14. }
  15. };
  16. wrappedFn[internals.wrapped] = true;
  17. return wrappedFn;
  18. };