index.es.js 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. var P = (o, s, i) => {
  2. if (!s.has(o))
  3. throw TypeError("Cannot " + i);
  4. };
  5. var e = (o, s, i) => (P(o, s, "read from private field"), i ? i.call(o) : s.get(o)), a = (o, s, i) => {
  6. if (s.has(o))
  7. throw TypeError("Cannot add the same private member more than once");
  8. s instanceof WeakSet ? s.add(o) : s.set(o, i);
  9. }, n = (o, s, i, r) => (P(o, s, "write to private field"), r ? r.call(o, i) : s.set(o, i), i);
  10. import { contextNotFound as $, ctxCallOutOfScope as F, timerNotFound as N } from "@milkdown/exception";
  11. class G {
  12. constructor() {
  13. this.sliceMap = /* @__PURE__ */ new Map(), this.get = (s) => {
  14. const i = typeof s == "string" ? [...this.sliceMap.values()].find((r) => r.type.name === s) : this.sliceMap.get(s.id);
  15. if (!i) {
  16. const r = typeof s == "string" ? s : s.name;
  17. throw $(r);
  18. }
  19. return i;
  20. }, this.remove = (s) => {
  21. const i = typeof s == "string" ? [...this.sliceMap.values()].find((r) => r.type.name === s) : this.sliceMap.get(s.id);
  22. i && this.sliceMap.delete(i.type.id);
  23. }, this.has = (s) => typeof s == "string" ? [...this.sliceMap.values()].some((i) => i.type.name === s) : this.sliceMap.has(s.id);
  24. }
  25. }
  26. var u, m, y;
  27. class V {
  28. /// @internal
  29. constructor(s, i, r) {
  30. a(this, u, void 0);
  31. /// @internal
  32. a(this, m, void 0);
  33. a(this, y, void 0);
  34. n(this, u, []), n(this, y, () => {
  35. e(this, u).forEach((t) => t(e(this, m)));
  36. }), this.set = (t) => {
  37. n(this, m, t), e(this, y).call(this);
  38. }, this.get = () => e(this, m), this.update = (t) => {
  39. n(this, m, t(e(this, m))), e(this, y).call(this);
  40. }, this.type = r, n(this, m, i), s.set(r.id, this);
  41. }
  42. /// Add a watcher for changes in the slice.
  43. /// Returns a function to remove the watcher.
  44. on(s) {
  45. return e(this, u).push(s), () => {
  46. n(this, u, e(this, u).filter((i) => i !== s));
  47. };
  48. }
  49. /// Add a one-time watcher for changes in the slice.
  50. /// The watcher will be removed after it is called.
  51. /// Returns a function to remove the watcher.
  52. once(s) {
  53. const i = this.on((r) => {
  54. s(r), i();
  55. });
  56. return i;
  57. }
  58. /// Remove a watcher.
  59. off(s) {
  60. n(this, u, e(this, u).filter((i) => i !== s));
  61. }
  62. /// Remove all watchers.
  63. offAll() {
  64. n(this, u, []);
  65. }
  66. }
  67. u = new WeakMap(), m = new WeakMap(), y = new WeakMap();
  68. class W {
  69. /// Create a slice type with a default value and a name.
  70. /// The name should be unique in the container.
  71. constructor(s, i) {
  72. this.id = Symbol(`Context-${i}`), this.name = i, this._defaultValue = s, this._typeInfo = () => {
  73. throw F();
  74. };
  75. }
  76. /// Create a slice with a container.
  77. /// You can also pass a value to override the default value.
  78. create(s, i = this._defaultValue) {
  79. return new V(s, i, this);
  80. }
  81. }
  82. const H = (o, s) => new W(o, s);
  83. var D, x, R, w, S, f, M, T, j;
  84. class _ {
  85. /// Create an inspector with container, clock and metadata.
  86. constructor(s, i, r) {
  87. /// @internal
  88. a(this, D, void 0);
  89. /// @internal
  90. a(this, x, void 0);
  91. /// @internal
  92. a(this, R, void 0);
  93. a(this, w, void 0);
  94. a(this, S, void 0);
  95. a(this, f, void 0);
  96. a(this, M, void 0);
  97. a(this, T, void 0);
  98. a(this, j, void 0);
  99. n(this, w, /* @__PURE__ */ new Set()), n(this, S, /* @__PURE__ */ new Set()), n(this, f, /* @__PURE__ */ new Map()), n(this, M, /* @__PURE__ */ new Map()), this.read = () => ({
  100. metadata: e(this, D),
  101. injectedSlices: [...e(this, w)].map((t) => ({
  102. name: typeof t == "string" ? t : t.name,
  103. value: e(this, T).call(this, t)
  104. })),
  105. consumedSlices: [...e(this, S)].map((t) => ({
  106. name: typeof t == "string" ? t : t.name,
  107. value: e(this, T).call(this, t)
  108. })),
  109. recordedTimers: [...e(this, f)].map(([t, { duration: h }]) => ({
  110. name: t.name,
  111. duration: h,
  112. status: e(this, j).call(this, t)
  113. })),
  114. waitTimers: [...e(this, M)].map(([t, { duration: h }]) => ({
  115. name: t.name,
  116. duration: h,
  117. status: e(this, j).call(this, t)
  118. }))
  119. }), this.onRecord = (t) => {
  120. e(this, f).set(t, { start: Date.now(), duration: 0 });
  121. }, this.onClear = (t) => {
  122. e(this, f).delete(t);
  123. }, this.onDone = (t) => {
  124. const h = e(this, f).get(t);
  125. h && (h.duration = Date.now() - h.start);
  126. }, this.onWait = (t, h) => {
  127. const v = Date.now();
  128. h.finally(() => {
  129. e(this, M).set(t, { duration: Date.now() - v });
  130. });
  131. }, this.onInject = (t) => {
  132. e(this, w).add(t);
  133. }, this.onRemove = (t) => {
  134. e(this, w).delete(t);
  135. }, this.onUse = (t) => {
  136. e(this, S).add(t);
  137. }, n(this, T, (t) => e(this, x).get(t).get()), n(this, j, (t) => e(this, R).get(t).status), n(this, x, s), n(this, R, i), n(this, D, r);
  138. }
  139. }
  140. D = new WeakMap(), x = new WeakMap(), R = new WeakMap(), w = new WeakMap(), S = new WeakMap(), f = new WeakMap(), M = new WeakMap(), T = new WeakMap(), j = new WeakMap();
  141. var d, l, b, c;
  142. const L = class L {
  143. /// Create a ctx object with container and clock.
  144. constructor(s, i, r) {
  145. /// @internal
  146. a(this, d, void 0);
  147. /// @internal
  148. a(this, l, void 0);
  149. /// @internal
  150. a(this, b, void 0);
  151. /// @internal
  152. a(this, c, void 0);
  153. this.produce = (t) => t && Object.keys(t).length ? new L(e(this, d), e(this, l), { ...t }) : this, this.inject = (t, h) => {
  154. var O;
  155. const v = t.create(e(this, d).sliceMap);
  156. return h != null && v.set(h), (O = e(this, c)) == null || O.onInject(t), this;
  157. }, this.remove = (t) => {
  158. var h;
  159. return e(this, d).remove(t), (h = e(this, c)) == null || h.onRemove(t), this;
  160. }, this.record = (t) => {
  161. var h;
  162. return t.create(e(this, l).store), (h = e(this, c)) == null || h.onRecord(t), this;
  163. }, this.clearTimer = (t) => {
  164. var h;
  165. return e(this, l).remove(t), (h = e(this, c)) == null || h.onClear(t), this;
  166. }, this.isInjected = (t) => e(this, d).has(t), this.isRecorded = (t) => e(this, l).has(t), this.use = (t) => {
  167. var h;
  168. return (h = e(this, c)) == null || h.onUse(t), e(this, d).get(t);
  169. }, this.get = (t) => this.use(t).get(), this.set = (t, h) => this.use(t).set(h), this.update = (t, h) => this.use(t).update(h), this.timer = (t) => e(this, l).get(t), this.done = (t) => {
  170. var h;
  171. this.timer(t).done(), (h = e(this, c)) == null || h.onDone(t);
  172. }, this.wait = (t) => {
  173. var v;
  174. const h = this.timer(t).start();
  175. return (v = e(this, c)) == null || v.onWait(t, h), h;
  176. }, this.waitTimers = async (t) => {
  177. await Promise.all(this.get(t).map((h) => this.wait(h)));
  178. }, n(this, d, s), n(this, l, i), n(this, b, r), r && n(this, c, new _(s, i, r));
  179. }
  180. /// Get metadata of the ctx.
  181. get meta() {
  182. return e(this, b);
  183. }
  184. /// Get the inspector of the ctx.
  185. get inspector() {
  186. return e(this, c);
  187. }
  188. };
  189. d = new WeakMap(), l = new WeakMap(), b = new WeakMap(), c = new WeakMap();
  190. let U = L;
  191. class J {
  192. constructor() {
  193. this.store = /* @__PURE__ */ new Map(), this.get = (s) => {
  194. const i = this.store.get(s.id);
  195. if (!i)
  196. throw N(s.name);
  197. return i;
  198. }, this.remove = (s) => {
  199. this.store.delete(s.id);
  200. }, this.has = (s) => this.store.has(s.id);
  201. }
  202. }
  203. var C, g, E, p, I, k;
  204. class q {
  205. /// @internal
  206. constructor(s, i) {
  207. a(this, C, void 0);
  208. a(this, g, void 0);
  209. /// @internal
  210. a(this, E, void 0);
  211. a(this, p, void 0);
  212. a(this, I, void 0);
  213. a(this, k, void 0);
  214. n(this, C, null), n(this, g, null), n(this, p, "pending"), this.start = () => (e(this, C) ?? n(this, C, new Promise((r, t) => {
  215. n(this, g, (h) => {
  216. h instanceof CustomEvent && h.detail.id === e(this, E) && (n(this, p, "resolved"), e(this, I).call(this), h.stopImmediatePropagation(), r());
  217. }), e(this, k).call(this, () => {
  218. e(this, p) === "pending" && n(this, p, "rejected"), e(this, I).call(this), t(new Error(`Timing ${this.type.name} timeout.`));
  219. }), n(this, p, "pending"), addEventListener(this.type.name, e(this, g));
  220. })), e(this, C)), this.done = () => {
  221. const r = new CustomEvent(this.type.name, { detail: { id: e(this, E) } });
  222. dispatchEvent(r);
  223. }, n(this, I, () => {
  224. e(this, g) && removeEventListener(this.type.name, e(this, g));
  225. }), n(this, k, (r) => {
  226. setTimeout(() => {
  227. r();
  228. }, this.type.timeout);
  229. }), n(this, E, Symbol(i.name)), this.type = i, s.set(i.id, this);
  230. }
  231. /// The status of the timer.
  232. /// Can be `pending`, `resolved` or `rejected`.
  233. get status() {
  234. return e(this, p);
  235. }
  236. }
  237. C = new WeakMap(), g = new WeakMap(), E = new WeakMap(), p = new WeakMap(), I = new WeakMap(), k = new WeakMap();
  238. class A {
  239. /// Create a timer type with a name and a timeout.
  240. /// The name should be unique in the clock.
  241. constructor(s, i = 3e3) {
  242. this.create = (r) => new q(r, this), this.id = Symbol(`Timer-${s}`), this.name = s, this.timeout = i;
  243. }
  244. }
  245. const K = (o, s = 3e3) => new A(o, s);
  246. export {
  247. J as Clock,
  248. G as Container,
  249. U as Ctx,
  250. _ as Inspector,
  251. V as Slice,
  252. W as SliceType,
  253. q as Timer,
  254. A as TimerType,
  255. H as createSlice,
  256. K as createTimer
  257. };
  258. //# sourceMappingURL=index.es.js.map