{"version":3,"sources":["https://lively-kernel.org/lively4/swt21-debugging/src/client/lang/lang-zone.js"],"names":["Dexie","cop","runZoned","withZone","exposeGlobal","incrementExpectedAwaits","newPSD","decrementExpectedAwaits","Promise","global","self","key","value","Object","defineProperty","configurable","enumerable","writable","every","func","Error","globalZone","zone","PSD","parent","Zone","root","current","scopeFunc","zoneProps","returnValue","call","then","e","console","warn","toString","stack","fn","zoneValues","layer","window","refineClass","Response","text","rest","resolve","proceed","json","ZonifyNativePromisesLayer","beGlobal"],"mappings":";;;;;;AACOA,W;;AAEKC,S;;;;;;;;;;;AAuFHC,mD;;;;;;;;;;;;;8BA7BOC,yC;;;;;;;;;;;;;;AAnDPC,uD;;;;;;;;;;;;;AATFJ,gD;;;;;;;;;;;;;AAEKC,8C;;;;;;;;;AAGZ,YAAM,EAAEI,uBAAF,EAA2BC,MAA3B,EAAmCC,uBAAnC,KAA+DP,MAAMQ,OAA3E;;;;;;;;AAAyCD,kE;;;;;;;;;;;;;AAARD,iD;;;;;;;;;;;;;AAAzBD,kE;;;;;;;AAER,YAAMI,SAASC,IAAf;;;;;;;;AAAMD,iD;;;;;;;AAEN,eAASL,YAAT,CAAsBO,GAAtB,EAA2BC,KAA3B,EAAkC;AAChCC,eAAOC,cAAP,CAAsBL,MAAtB,EAA8BE,GAA9B,EAAmC;AACjCI,wBAAc,IADmB;AAEjCC,sBAAY,IAFqB;;AAIjCJ,eAJiC;AAKjCK,oBAAU;AALuB,SAAnC;AAOD;;AAED;;;;;;;;;;;AAWA;AACA,UAAI,CAAC,CAACZ,uBAAD,EAA0BE,uBAA1B,EAAmDW,KAAnD,CAAyDC,QAAQ,OAAOA,IAAP,KAAgB,UAAjF,CAAL,EAAmG;AACjG,cAAM,IAAIC,KAAJ,CAAU,4FAAV,CAAN;AACD;;AAED,YAAMC,aAAa,CAAC,MAAM;AACxB,YAAIC,OAAOtB,MAAMQ,OAAN,CAAce,GAAzB;;AAEA,eAAO,CAACD,KAAKb,MAAb,EAAqB;A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b,EAAyBF,QAAzB;;AAEA;AACAD,UAAIuC,KAAJ,CAAUC,MAAV,EAAkB,2BAAlB,EAA+CC,WAA/C,CAA2DC,QAA3D,EAAqE;AACnEC,aAAK,GAAGC,IAAR,EAAc;AACZ,iBAAOrC,QAAQsC,OAAR,CAAgB7C,IAAI8C,OAAJ,CAAY,GAAGF,IAAf,CAAhB,CAAP;AACD,SAHkE;;AAKnEG,aAAK,GAAGH,IAAR,EAAc;AACZ,iBAAOrC,QAAQsC,OAAR,CAAgB7C,IAAI8C,OAAJ,CAAY,GAAGF,IAAf,CAAhB,CAAP;AACD;AAPkE,OAArE;AASAI,gCAA0BC,QAA1B","file":"lang-zone.js","sourcesContent":["import 'lang';\nimport Dexie from \"src/external/dexie3.js\";\n\nimport * as cop from \"src/client/ContextJS/src/contextjs.js\"\n\n\nconst { incrementExpectedAwaits, newPSD, decrementExpectedAwaits } = Dexie.Promise;\n\nconst global = self;\n\nfunction exposeGlobal(key, value) {\n  Object.defineProperty(global, key, {\n    configurable: true,\n    enumerable: true,\n    \n    value,\n    writable: true,\n  });\n}\n\n/*MD\n## ZONES\n\n- a **minimal interface** to zones intended to be as compliant as possible to Dart's [Zone concept](https://dart.dev/articles/archive/zones)\n- intended to be exchangeable in case of future standard or library advancements\n- <span style='background: red'>!only modify if you know what you are doing and understand the **long-term consequences**!</span>\n- currently implemented as a lightweight wrapper around [Dexie.Promise](https://dexie.org/docs/Promise/Promise.PSD)\n  - Caution: we <span style='background: red'>monkey-patched Dexie</span> to access `incrementExpectedAwaits` and `decrementExpectedAwaits` to correctly detect native await on primitives\n\nMD*/\n\n// check if `incrementExpectedAwaits` and `decrementExpectedAwaits` are available\nif (![incrementExpectedAwaits, decrementExpectedAwaits].every(func => typeof func === 'function')) {\n  throw new Error('Dexie not monkey-patched to expose `incrementExpectedAwaits` and `decrementExpectedAwaits`');\n}\n\nconst globalZone = (() => {\n  let zone = Dexie.Promise.PSD;\n  \n  while (!zone.global) {\n    if (zone.parent && zone.parent !== zone) {\n      zone = zone.parent;\n    } else {\n      throw new Error('could not initialize `zones`: no global zone found');\n    }\n  }\n\n  \n  return zone;\n})();\n\n\n\nconst Zone = {\n\n  get root() { return globalZone; },\n\n  get current() { return Dexie.Promise.PSD; }\n\n};\n\nexport function withZone(scopeFunc, zoneProps = {}) {\n  let returnValue;\n  try {\n    incrementExpectedAwaits();\n\n    newPSD(() => {\n      returnValue = scopeFunc.call();\n    }, zoneProps);\n    \n    return returnValue;\n  } finally {\n    if (returnValue && typeof returnValue.then === 'function') {\n      (async () => {\n        try {\n          await returnValue;\n        } catch(e) {\n          // #TODO why are we catching errors here?\n          console.warn(\"Error in withZone: \", e.toString(), e.stack ? e.stack.toString() : \"no stack\")\n          throw e\n        } finally {\n          decrementExpectedAwaits();\n        }\n      })();\n    } else {\n      decrementExpectedAwaits();\n    }\n  }\n}\n\nfunction runZoned(fn, { zoneValues } = {} ) {\n  return withZone(fn, zoneValues);\n}\n\nexposeGlobal('Zone', Zone);\nexposeGlobal('runZoned', runZoned);\n\n// ensure zones in known native promise calls\ncop.layer(window, \"ZonifyNativePromisesLayer\").refineClass(Response, {\n  text(...rest) {\n    return Promise.resolve(cop.proceed(...rest))\n  },\n  \n  json(...rest) {\n    return Promise.resolve(cop.proceed(...rest))\n  }\n})\nZonifyNativePromisesLayer.beGlobal()\n"]}