once.js 585 B

12345678910111213141516171819
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.once = void 0;
  4. const instances = new WeakMap();
  5. /**
  6. * A class method decorator that limits a method to be called only once. All
  7. * subsequent calls will return the result of the first call.
  8. */
  9. function once(fn, context) {
  10. return function (...args) {
  11. let map = instances.get(this);
  12. if (!map)
  13. instances.set(this, (map = new WeakMap()));
  14. if (!map.has(fn))
  15. map.set(fn, fn.apply(this, args));
  16. return map.get(fn);
  17. };
  18. }
  19. exports.once = once;