coalesced-function.js 993 B

12345678910111213141516171819202122232425262728293031323334
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.withCoalescedInvoke = withCoalescedInvoke;
  6. const globalInvokeCache = new Map();
  7. function withCoalescedInvoke(func) {
  8. return async function(key, args) {
  9. const entry = globalInvokeCache.get(key);
  10. if (entry) {
  11. return entry.then((res)=>({
  12. isOrigin: false,
  13. value: res.value
  14. }));
  15. }
  16. async function __wrapper() {
  17. return await func.apply(undefined, args);
  18. }
  19. const future = __wrapper().then((res)=>{
  20. globalInvokeCache.delete(key);
  21. return {
  22. isOrigin: true,
  23. value: res
  24. };
  25. }).catch((err)=>{
  26. globalInvokeCache.delete(key);
  27. return Promise.reject(err);
  28. });
  29. globalInvokeCache.set(key, future);
  30. return future;
  31. };
  32. }
  33. //# sourceMappingURL=coalesced-function.js.map