@adviser/cement 0.0.0-jsr-t1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (320) hide show
  1. package/LICENSE +201 -0
  2. package/README.md +39 -0
  3. package/base-sys-abstraction-BkEiLHl0.d.ts +193 -0
  4. package/base-sys-abstraction-Qj7pkY1N.d.cts +193 -0
  5. package/chunk-7KFVMTOS.js +311 -0
  6. package/chunk-7KFVMTOS.js.map +1 -0
  7. package/chunk-GES3MUGV.js +92 -0
  8. package/chunk-GES3MUGV.js.map +1 -0
  9. package/chunk-Q65HLCNL.js +601 -0
  10. package/chunk-Q65HLCNL.js.map +1 -0
  11. package/chunk-WMMUXBDX.js +87 -0
  12. package/chunk-WMMUXBDX.js.map +1 -0
  13. package/index-Q3phXzYr.d.cts +75 -0
  14. package/index-tIGZMHTc.d.ts +75 -0
  15. package/index.cjs +2593 -0
  16. package/index.cjs.map +1 -0
  17. package/index.d.cts +532 -0
  18. package/index.d.ts +532 -0
  19. package/index.js +1533 -0
  20. package/index.js.map +1 -0
  21. package/node/index.cjs +924 -0
  22. package/node/index.cjs.map +1 -0
  23. package/node/index.d.cts +65 -0
  24. package/node/index.d.ts +65 -0
  25. package/node/index.js +398 -0
  26. package/node/index.js.map +1 -0
  27. package/package.json +81 -0
  28. package/src/base-sys-abstraction.test.ts +95 -0
  29. package/src/base-sys-abstraction.ts +242 -0
  30. package/src/bin2text.test.ts +59 -0
  31. package/src/bin2text.ts +47 -0
  32. package/src/crypto.test.ts +15 -0
  33. package/src/crypto.ts +125 -0
  34. package/src/file-service.ts +24 -0
  35. package/src/future.test.ts +32 -0
  36. package/src/future.ts +27 -0
  37. package/src/index.ts +22 -0
  38. package/src/jsr.json +20 -0
  39. package/src/log-level-impl.ts +87 -0
  40. package/src/log-writer-impl.ts +58 -0
  41. package/src/logger-impl.ts +498 -0
  42. package/src/logger.test.ts +1132 -0
  43. package/src/logger.ts +208 -0
  44. package/src/node/deno-file-service.ts +92 -0
  45. package/src/node/deno-sys-abstraction.ts +133 -0
  46. package/src/node/index.ts +4 -0
  47. package/src/node/mock-file-service.ts +45 -0
  48. package/src/node/node-file-service.ts +91 -0
  49. package/src/node/node-sys-abstraction.ts +121 -0
  50. package/src/option.ts +60 -0
  51. package/src/resolve-once.test.ts +321 -0
  52. package/src/resolve-once.ts +179 -0
  53. package/src/result.test.ts +102 -0
  54. package/src/result.ts +165 -0
  55. package/src/runtime.ts +36 -0
  56. package/src/sys-abstraction.ts +53 -0
  57. package/src/sys-env.test.ts +53 -0
  58. package/src/sys-env.ts +216 -0
  59. package/src/test/log-write-stream.ts +95 -0
  60. package/src/test/mock-logger.ts +40 -0
  61. package/src/time.ts +20 -0
  62. package/src/tracer.test.ts +314 -0
  63. package/src/tracer.ts +222 -0
  64. package/src/txt-en-decoder.ts +21 -0
  65. package/src/uri.test.ts +155 -0
  66. package/src/uri.ts +421 -0
  67. package/src/utils/console-write-stream.ts +72 -0
  68. package/src/utils/fanout-write-stream.ts +32 -0
  69. package/src/utils/index.ts +6 -0
  70. package/src/utils/rebuffer.ts +75 -0
  71. package/src/utils/stream-map.ts +67 -0
  72. package/src/utils/stream2string.ts +47 -0
  73. package/src/utils/string2stream.ts +14 -0
  74. package/src/version.ts +3 -0
  75. package/src/web/index.ts +1 -0
  76. package/src/web/web-sys-abstraction.ts +80 -0
  77. package/ts/LICENSE +201 -0
  78. package/ts/README.md +39 -0
  79. package/ts/base-sys-abstraction.d.ts +84 -0
  80. package/ts/base-sys-abstraction.d.ts.map +1 -0
  81. package/ts/base-sys-abstraction.js +178 -0
  82. package/ts/base-sys-abstraction.js.map +1 -0
  83. package/ts/base-sys-abstraction.test.d.ts +2 -0
  84. package/ts/base-sys-abstraction.test.d.ts.map +1 -0
  85. package/ts/base-sys-abstraction.test.js +82 -0
  86. package/ts/base-sys-abstraction.test.js.map +1 -0
  87. package/ts/bin2text.d.ts +3 -0
  88. package/ts/bin2text.d.ts.map +1 -0
  89. package/ts/bin2text.js +43 -0
  90. package/ts/bin2text.js.map +1 -0
  91. package/ts/bin2text.test.d.ts +2 -0
  92. package/ts/bin2text.test.d.ts.map +1 -0
  93. package/ts/bin2text.test.js +51 -0
  94. package/ts/bin2text.test.js.map +1 -0
  95. package/ts/crypto.d.ts +76 -0
  96. package/ts/crypto.d.ts.map +1 -0
  97. package/ts/crypto.js +22 -0
  98. package/ts/crypto.js.map +1 -0
  99. package/ts/crypto.test.d.ts +2 -0
  100. package/ts/crypto.test.d.ts.map +1 -0
  101. package/ts/crypto.test.js +14 -0
  102. package/ts/crypto.test.js.map +1 -0
  103. package/ts/file-service.d.ts +17 -0
  104. package/ts/file-service.d.ts.map +1 -0
  105. package/ts/file-service.js +2 -0
  106. package/ts/file-service.js.map +1 -0
  107. package/ts/future.d.ts +8 -0
  108. package/ts/future.d.ts.map +1 -0
  109. package/ts/future.js +38 -0
  110. package/ts/future.js.map +1 -0
  111. package/ts/future.test.d.ts +2 -0
  112. package/ts/future.test.d.ts.map +1 -0
  113. package/ts/future.test.js +28 -0
  114. package/ts/future.test.js.map +1 -0
  115. package/ts/index.d.ts +23 -0
  116. package/ts/index.d.ts.map +1 -0
  117. package/ts/index.js +23 -0
  118. package/ts/index.js.map +1 -0
  119. package/ts/log-level-impl.d.ts +14 -0
  120. package/ts/log-level-impl.d.ts.map +1 -0
  121. package/ts/log-level-impl.js +72 -0
  122. package/ts/log-level-impl.js.map +1 -0
  123. package/ts/log-writer-impl.d.ts +10 -0
  124. package/ts/log-writer-impl.d.ts.map +1 -0
  125. package/ts/log-writer-impl.js +45 -0
  126. package/ts/log-writer-impl.js.map +1 -0
  127. package/ts/logger-impl.d.ts +71 -0
  128. package/ts/logger-impl.d.ts.map +1 -0
  129. package/ts/logger-impl.js +412 -0
  130. package/ts/logger-impl.js.map +1 -0
  131. package/ts/logger.d.ts +84 -0
  132. package/ts/logger.d.ts.map +1 -0
  133. package/ts/logger.js +114 -0
  134. package/ts/logger.js.map +1 -0
  135. package/ts/logger.test.d.ts +2 -0
  136. package/ts/logger.test.d.ts.map +1 -0
  137. package/ts/logger.test.js +1023 -0
  138. package/ts/logger.test.js.map +1 -0
  139. package/ts/node/deno-file-service.d.ts +17 -0
  140. package/ts/node/deno-file-service.d.ts.map +1 -0
  141. package/ts/node/deno-file-service.js +65 -0
  142. package/ts/node/deno-file-service.js.map +1 -0
  143. package/ts/node/deno-sys-abstraction.d.ts +22 -0
  144. package/ts/node/deno-sys-abstraction.d.ts.map +1 -0
  145. package/ts/node/deno-sys-abstraction.js +101 -0
  146. package/ts/node/deno-sys-abstraction.js.map +1 -0
  147. package/ts/node/index.d.ts +5 -0
  148. package/ts/node/index.d.ts.map +1 -0
  149. package/ts/node/index.js +5 -0
  150. package/ts/node/index.js.map +1 -0
  151. package/ts/node/mock-file-service.d.ts +11 -0
  152. package/ts/node/mock-file-service.d.ts.map +1 -0
  153. package/ts/node/mock-file-service.js +34 -0
  154. package/ts/node/mock-file-service.js.map +1 -0
  155. package/ts/node/mock-file-service.test.d.ts +2 -0
  156. package/ts/node/mock-file-service.test.d.ts.map +1 -0
  157. package/ts/node/mock-file-service.test.js +31 -0
  158. package/ts/node/mock-file-service.test.js.map +1 -0
  159. package/ts/node/node-file-service.d.ts +16 -0
  160. package/ts/node/node-file-service.d.ts.map +1 -0
  161. package/ts/node/node-file-service.js +71 -0
  162. package/ts/node/node-file-service.js.map +1 -0
  163. package/ts/node/node-sys-abstraction.d.ts +22 -0
  164. package/ts/node/node-sys-abstraction.d.ts.map +1 -0
  165. package/ts/node/node-sys-abstraction.js +99 -0
  166. package/ts/node/node-sys-abstraction.js.map +1 -0
  167. package/ts/node/node-sys-abstraction.test.d.ts +2 -0
  168. package/ts/node/node-sys-abstraction.test.d.ts.map +1 -0
  169. package/ts/node/node-sys-abstraction.test.js +87 -0
  170. package/ts/node/node-sys-abstraction.test.js.map +1 -0
  171. package/ts/option.d.ts +25 -0
  172. package/ts/option.d.ts.map +1 -0
  173. package/ts/option.js +47 -0
  174. package/ts/option.js.map +1 -0
  175. package/ts/resolve-once.d.ts +46 -0
  176. package/ts/resolve-once.d.ts.map +1 -0
  177. package/ts/resolve-once.js +152 -0
  178. package/ts/resolve-once.js.map +1 -0
  179. package/ts/resolve-once.test.d.ts +2 -0
  180. package/ts/resolve-once.test.d.ts.map +1 -0
  181. package/ts/resolve-once.test.js +283 -0
  182. package/ts/resolve-once.test.js.map +1 -0
  183. package/ts/result.d.ts +34 -0
  184. package/ts/result.d.ts.map +1 -0
  185. package/ts/result.js +85 -0
  186. package/ts/result.js.map +1 -0
  187. package/ts/result.test.d.ts +2 -0
  188. package/ts/result.test.d.ts.map +1 -0
  189. package/ts/result.test.js +79 -0
  190. package/ts/result.test.js.map +1 -0
  191. package/ts/runtime.d.ts +8 -0
  192. package/ts/runtime.d.ts.map +1 -0
  193. package/ts/runtime.js +26 -0
  194. package/ts/runtime.js.map +1 -0
  195. package/ts/sys-abstraction.d.ts +36 -0
  196. package/ts/sys-abstraction.d.ts.map +1 -0
  197. package/ts/sys-abstraction.js +31 -0
  198. package/ts/sys-abstraction.js.map +1 -0
  199. package/ts/sys-env.d.ts +48 -0
  200. package/ts/sys-env.d.ts.map +1 -0
  201. package/ts/sys-env.js +176 -0
  202. package/ts/sys-env.js.map +1 -0
  203. package/ts/sys-env.test.d.ts +2 -0
  204. package/ts/sys-env.test.d.ts.map +1 -0
  205. package/ts/sys-env.test.js +51 -0
  206. package/ts/sys-env.test.js.map +1 -0
  207. package/ts/test/log-write-stream.d.ts +27 -0
  208. package/ts/test/log-write-stream.d.ts.map +1 -0
  209. package/ts/test/log-write-stream.js +74 -0
  210. package/ts/test/log-write-stream.js.map +1 -0
  211. package/ts/test/mock-logger.d.ts +14 -0
  212. package/ts/test/mock-logger.d.ts.map +1 -0
  213. package/ts/test/mock-logger.js +29 -0
  214. package/ts/test/mock-logger.js.map +1 -0
  215. package/ts/test/mock-logger.test.d.ts +2 -0
  216. package/ts/test/mock-logger.test.d.ts.map +1 -0
  217. package/ts/test/mock-logger.test.js +63 -0
  218. package/ts/test/mock-logger.test.js.map +1 -0
  219. package/ts/test/test-exit-handler.d.ts +2 -0
  220. package/ts/test/test-exit-handler.d.ts.map +1 -0
  221. package/ts/test/test-exit-handler.js +57 -0
  222. package/ts/test/test-exit-handler.js.map +1 -0
  223. package/ts/time.d.ts +13 -0
  224. package/ts/time.d.ts.map +1 -0
  225. package/ts/time.js +14 -0
  226. package/ts/time.js.map +1 -0
  227. package/ts/tracer.d.ts +59 -0
  228. package/ts/tracer.d.ts.map +1 -0
  229. package/ts/tracer.js +148 -0
  230. package/ts/tracer.js.map +1 -0
  231. package/ts/tracer.test.d.ts +2 -0
  232. package/ts/tracer.test.d.ts.map +1 -0
  233. package/ts/tracer.test.js +311 -0
  234. package/ts/tracer.test.js.map +1 -0
  235. package/ts/txt-en-decoder.d.ts +10 -0
  236. package/ts/txt-en-decoder.d.ts.map +1 -0
  237. package/ts/txt-en-decoder.js +15 -0
  238. package/ts/txt-en-decoder.js.map +1 -0
  239. package/ts/uri.d.ts +67 -0
  240. package/ts/uri.d.ts.map +1 -0
  241. package/ts/uri.js +283 -0
  242. package/ts/uri.js.map +1 -0
  243. package/ts/uri.test.d.ts +2 -0
  244. package/ts/uri.test.d.ts.map +1 -0
  245. package/ts/uri.test.js +119 -0
  246. package/ts/uri.test.js.map +1 -0
  247. package/ts/utils/console-write-stream.d.ts +21 -0
  248. package/ts/utils/console-write-stream.d.ts.map +1 -0
  249. package/ts/utils/console-write-stream.js +62 -0
  250. package/ts/utils/console-write-stream.js.map +1 -0
  251. package/ts/utils/fanout-write-stream.d.ts +12 -0
  252. package/ts/utils/fanout-write-stream.d.ts.map +1 -0
  253. package/ts/utils/fanout-write-stream.js +24 -0
  254. package/ts/utils/fanout-write-stream.js.map +1 -0
  255. package/ts/utils/index.d.ts +7 -0
  256. package/ts/utils/index.d.ts.map +1 -0
  257. package/ts/utils/index.js +7 -0
  258. package/ts/utils/index.js.map +1 -0
  259. package/ts/utils/rebuffer.d.ts +3 -0
  260. package/ts/utils/rebuffer.d.ts.map +1 -0
  261. package/ts/utils/rebuffer.js +60 -0
  262. package/ts/utils/rebuffer.js.map +1 -0
  263. package/ts/utils/rebuffer.test.d.ts +2 -0
  264. package/ts/utils/rebuffer.test.d.ts.map +1 -0
  265. package/ts/utils/rebuffer.test.js +77 -0
  266. package/ts/utils/rebuffer.test.js.map +1 -0
  267. package/ts/utils/stream-map.d.ts +9 -0
  268. package/ts/utils/stream-map.d.ts.map +1 -0
  269. package/ts/utils/stream-map.js +62 -0
  270. package/ts/utils/stream-map.js.map +1 -0
  271. package/ts/utils/stream-map.test.d.ts +2 -0
  272. package/ts/utils/stream-map.test.d.ts.map +1 -0
  273. package/ts/utils/stream-map.test.js +87 -0
  274. package/ts/utils/stream-map.test.js.map +1 -0
  275. package/ts/utils/stream-test-helper.d.ts +17 -0
  276. package/ts/utils/stream-test-helper.d.ts.map +1 -0
  277. package/ts/utils/stream-test-helper.js +37 -0
  278. package/ts/utils/stream-test-helper.js.map +1 -0
  279. package/ts/utils/stream2string.d.ts +3 -0
  280. package/ts/utils/stream2string.d.ts.map +1 -0
  281. package/ts/utils/stream2string.js +48 -0
  282. package/ts/utils/stream2string.js.map +1 -0
  283. package/ts/utils/stream2string.test.d.ts +2 -0
  284. package/ts/utils/stream2string.test.d.ts.map +1 -0
  285. package/ts/utils/stream2string.test.js +29 -0
  286. package/ts/utils/stream2string.test.js.map +1 -0
  287. package/ts/utils/string2stream.d.ts +4 -0
  288. package/ts/utils/string2stream.d.ts.map +1 -0
  289. package/ts/utils/string2stream.js +13 -0
  290. package/ts/utils/string2stream.js.map +1 -0
  291. package/ts/utils/string2stream.test.d.ts +2 -0
  292. package/ts/utils/string2stream.test.d.ts.map +1 -0
  293. package/ts/utils/string2stream.test.js +6 -0
  294. package/ts/utils/string2stream.test.js.map +1 -0
  295. package/ts/version.d.ts +2 -0
  296. package/ts/version.d.ts.map +1 -0
  297. package/ts/version.js +4 -0
  298. package/ts/version.js.map +1 -0
  299. package/ts/web/index.d.ts +2 -0
  300. package/ts/web/index.d.ts.map +1 -0
  301. package/ts/web/index.js +2 -0
  302. package/ts/web/index.js.map +1 -0
  303. package/ts/web/web-sys-abstraction.d.ts +4 -0
  304. package/ts/web/web-sys-abstraction.d.ts.map +1 -0
  305. package/ts/web/web-sys-abstraction.js +64 -0
  306. package/ts/web/web-sys-abstraction.js.map +1 -0
  307. package/txt-en-decoder-CZYJUju2.d.cts +11 -0
  308. package/txt-en-decoder-CZYJUju2.d.ts +11 -0
  309. package/utils/index.cjs +341 -0
  310. package/utils/index.cjs.map +1 -0
  311. package/utils/index.d.cts +2 -0
  312. package/utils/index.d.ts +2 -0
  313. package/utils/index.js +32 -0
  314. package/utils/index.js.map +1 -0
  315. package/web/index.cjs +593 -0
  316. package/web/index.cjs.map +1 -0
  317. package/web/index.d.cts +6 -0
  318. package/web/index.d.ts +6 -0
  319. package/web/index.js +9 -0
  320. package/web/index.js.map +1 -0
@@ -0,0 +1,283 @@
1
+ import { KeyedResolvOnce, ResolveOnce, ResolveSeq } from "@adviser/cement";
2
+ describe("resolve-once", () => {
3
+ it("sequence", async () => {
4
+ const once = new ResolveOnce();
5
+ const reallyOnce = vi.fn(async () => {
6
+ return new Promise((resolve) => {
7
+ setTimeout(() => {
8
+ resolve(42);
9
+ }, 100);
10
+ });
11
+ });
12
+ const start = Date.now();
13
+ const fn = () => once.once(async () => reallyOnce());
14
+ expect(reallyOnce).toHaveBeenCalledTimes(0);
15
+ expect(await fn()).toBe(42);
16
+ expect(reallyOnce).toHaveBeenCalledTimes(1);
17
+ expect(await fn()).toBe(42);
18
+ expect(reallyOnce).toHaveBeenCalledTimes(1);
19
+ expect(await fn()).toBe(42);
20
+ expect(reallyOnce).toHaveBeenCalledTimes(1);
21
+ const diff = Date.now() - start;
22
+ expect(diff).toBeGreaterThanOrEqual(99);
23
+ expect(diff).toBeLessThan(150);
24
+ });
25
+ it("parallel", async () => {
26
+ const once = new ResolveOnce();
27
+ const reallyOnce = vi.fn(async () => {
28
+ return new Promise((resolve) => {
29
+ setTimeout(() => {
30
+ resolve(42);
31
+ }, 100);
32
+ });
33
+ });
34
+ const fn = () => once.once(async () => reallyOnce());
35
+ const start = Date.now();
36
+ expect(await Promise.all(Array(100)
37
+ .fill(fn)
38
+ .map((fn) => fn()))).toEqual(Array(100).fill(42));
39
+ expect(reallyOnce).toHaveBeenCalledTimes(1);
40
+ const diff = Date.now() - start;
41
+ expect(diff).toBeGreaterThanOrEqual(99);
42
+ expect(diff).toBeLessThan(150);
43
+ });
44
+ it("works with void", async () => {
45
+ const once = new ResolveOnce();
46
+ const reallyOnce = vi.fn(async () => {
47
+ return new Promise((resolve) => {
48
+ setTimeout(() => {
49
+ resolve();
50
+ }, 100);
51
+ });
52
+ });
53
+ const fn = () => once.once(async () => reallyOnce());
54
+ const start = Date.now();
55
+ expect(await Promise.all(Array(100)
56
+ .fill(fn)
57
+ .map((fn) => fn()))).toEqual(Array(100).fill(undefined));
58
+ expect(reallyOnce).toHaveBeenCalledTimes(1);
59
+ const diff = Date.now() - start;
60
+ expect(diff).toBeGreaterThanOrEqual(99);
61
+ expect(diff).toBeLessThan(150);
62
+ });
63
+ it("throws", async () => {
64
+ const once = new ResolveOnce();
65
+ const reallyOnce = vi.fn(async () => {
66
+ return new Promise((rs, rj) => {
67
+ setTimeout(() => {
68
+ rj(new Error("nope"));
69
+ }, 100);
70
+ });
71
+ });
72
+ const fn = () => once.once(async () => reallyOnce());
73
+ const start = Date.now();
74
+ await new Promise((rs) => {
75
+ for (let i = 0; i < 100; i++) {
76
+ fn()
77
+ .then(() => {
78
+ assert.fail("should not happen");
79
+ })
80
+ .catch((e) => {
81
+ expect(e).toEqual(new Error("nope"));
82
+ expect(reallyOnce).toHaveBeenCalledTimes(1);
83
+ if (i === 99) {
84
+ rs(undefined);
85
+ }
86
+ });
87
+ }
88
+ });
89
+ const diff = Date.now() - start;
90
+ expect(diff).toBeGreaterThanOrEqual(99);
91
+ expect(diff).toBeLessThan(150);
92
+ });
93
+ it("preserves order", async () => {
94
+ const once = new ResolveOnce();
95
+ const reallyOnce = vi.fn(async () => {
96
+ return new Promise((resolve) => {
97
+ setTimeout(() => {
98
+ resolve(42);
99
+ }, 100);
100
+ });
101
+ });
102
+ let order = 0;
103
+ const fn = async () => {
104
+ const o = order++;
105
+ const ret = await once.once(async () => reallyOnce());
106
+ return `${o}:${ret}`;
107
+ };
108
+ const start = Date.now();
109
+ expect(await Promise.all(Array(100)
110
+ .fill(fn)
111
+ .map((fn) => fn()))).toEqual(Array(100)
112
+ .fill(undefined)
113
+ .map((_, i) => `${i}:42`));
114
+ expect(reallyOnce).toHaveBeenCalledTimes(1);
115
+ const diff = Date.now() - start;
116
+ expect(diff).toBeGreaterThanOrEqual(99);
117
+ expect(diff).toBeLessThan(150);
118
+ });
119
+ it("preserves call order to resolv order", async () => {
120
+ const once = new ResolveOnce();
121
+ const reallyOnce = vi.fn(async () => {
122
+ return new Promise((resolve) => {
123
+ setTimeout(() => {
124
+ resolve(42);
125
+ }, 100);
126
+ });
127
+ });
128
+ const start = Date.now();
129
+ const orderFn = vi.fn();
130
+ const fns = Array(100)
131
+ .fill(0)
132
+ .map((_, i) => {
133
+ return once
134
+ .once(() => reallyOnce())
135
+ .then((once) => {
136
+ orderFn(i, once);
137
+ return `${i}:${once}`;
138
+ });
139
+ });
140
+ expect(await Promise.all(fns)).toEqual(Array(100)
141
+ .fill(undefined)
142
+ .map((_, i) => `${i}:42`));
143
+ expect(reallyOnce).toHaveBeenCalledTimes(1);
144
+ const diff = Date.now() - start;
145
+ expect(diff).toBeGreaterThanOrEqual(99);
146
+ expect(diff).toBeLessThan(150);
147
+ expect(orderFn).toHaveBeenCalledTimes(100);
148
+ expect(orderFn.mock.calls.map(([i]) => i)).toEqual(Array(100)
149
+ .fill(0)
150
+ .map((_, i) => i));
151
+ });
152
+ it("reset", async () => {
153
+ const once = new ResolveOnce();
154
+ const orderFn = vi.fn(async () => 42);
155
+ once.once(orderFn);
156
+ once.once(orderFn);
157
+ once.once(orderFn);
158
+ once.reset();
159
+ once.once(orderFn);
160
+ once.once(orderFn);
161
+ once.reset();
162
+ once.once(orderFn);
163
+ once.once(orderFn);
164
+ once.reset();
165
+ expect(orderFn).toHaveBeenCalledTimes(3);
166
+ });
167
+ it("keyed", async () => {
168
+ const keyed = new KeyedResolvOnce();
169
+ const a_orderFn = vi.fn(async () => 42);
170
+ const b_orderFn = vi.fn(async () => 42);
171
+ for (let i = 0; i < 5; i++) {
172
+ keyed.get("a").once(a_orderFn);
173
+ keyed.get(() => "a").once(a_orderFn);
174
+ keyed.get("b").once(b_orderFn);
175
+ keyed.get(() => "b").once(b_orderFn);
176
+ expect(a_orderFn).toHaveBeenCalledTimes(i + 1);
177
+ expect(b_orderFn).toHaveBeenCalledTimes(i + 1);
178
+ keyed.reset();
179
+ }
180
+ });
181
+ it("keyed with pass ctx", async () => {
182
+ const keyed = new KeyedResolvOnce();
183
+ const a_orderFn = vi.fn(async (key) => key);
184
+ const b_orderFn = vi.fn(async (key) => key);
185
+ await Promise.all([
186
+ keyed.get("a").once(a_orderFn),
187
+ keyed.get(() => "a").once(a_orderFn),
188
+ keyed.get("b").once(b_orderFn),
189
+ keyed.get(() => "b").once(b_orderFn),
190
+ ]);
191
+ expect(a_orderFn).toHaveBeenCalledTimes(1);
192
+ expect(a_orderFn).toHaveBeenCalledWith("a");
193
+ expect(b_orderFn).toHaveBeenCalledTimes(1);
194
+ expect(b_orderFn).toHaveBeenCalledWith("b");
195
+ });
196
+ it("keyed asyncGet", async () => {
197
+ const keyed = new KeyedResolvOnce();
198
+ const a_orderFn = vi.fn(async (key) => key);
199
+ const b_orderFn = vi.fn(async (key) => key);
200
+ await Promise.all([
201
+ keyed
202
+ .asyncGet(async () => {
203
+ await new Promise((resolve) => setTimeout(resolve, 100));
204
+ return "a";
205
+ })
206
+ .then((resolveOnce) => {
207
+ resolveOnce.once(a_orderFn);
208
+ }),
209
+ keyed
210
+ .asyncGet(async () => {
211
+ await new Promise((resolve) => setTimeout(resolve, 50));
212
+ return "b";
213
+ })
214
+ .then((resolveOnce) => {
215
+ resolveOnce.once(b_orderFn);
216
+ }),
217
+ ]);
218
+ expect(a_orderFn).toHaveBeenCalledTimes(1);
219
+ expect(a_orderFn).toHaveBeenCalledWith("a");
220
+ expect(b_orderFn).toHaveBeenCalledTimes(1);
221
+ expect(b_orderFn).toHaveBeenCalledWith("b");
222
+ });
223
+ function shuffle(array) {
224
+ let currentIndex = array.length;
225
+ while (currentIndex != 0) {
226
+ const randomIndex = Math.floor(Math.random() * currentIndex);
227
+ currentIndex--;
228
+ [array[currentIndex], array[randomIndex]] = [array[randomIndex], array[currentIndex]];
229
+ }
230
+ return array;
231
+ }
232
+ it("ResolveSeq", async () => {
233
+ const seq = new ResolveSeq();
234
+ let enter = 0;
235
+ let leave = 0;
236
+ const actions = Array(10)
237
+ .fill(0)
238
+ .map((_, i) => {
239
+ return seq.add(async () => {
240
+ expect(enter++).toBe(i);
241
+ await new Promise((resolve) => setTimeout(resolve, i * 3));
242
+ await new Promise((resolve) => setTimeout(resolve, i * 2));
243
+ expect(leave++).toBe(i);
244
+ expect(leave).toBe(enter);
245
+ return i;
246
+ }, i);
247
+ });
248
+ const ret = await Promise.all(shuffle(actions));
249
+ expect(ret.length).toBe(10);
250
+ expect(enter).toBe(10);
251
+ expect(leave).toBe(10);
252
+ });
253
+ it("with promise", async () => {
254
+ const once = new ResolveOnce();
255
+ let val = 42;
256
+ const fn = async () => {
257
+ return new Promise((resolve) => {
258
+ setTimeout(() => {
259
+ resolve(val++);
260
+ }, 10);
261
+ });
262
+ };
263
+ expect(await once.once(fn)).toBe(42);
264
+ expect(await once.once(fn)).toBe(42);
265
+ });
266
+ it("without promise", () => {
267
+ const once = new ResolveOnce();
268
+ let val = 42;
269
+ const fn = () => val++;
270
+ expect(once.once(fn)).toBe(42);
271
+ expect(once.once(fn)).toBe(42);
272
+ });
273
+ it("without promise but exception", () => {
274
+ const once = new ResolveOnce();
275
+ let val = 42;
276
+ const fn = () => {
277
+ throw new Error(`nope ${val++}`);
278
+ };
279
+ expect(() => once.once(fn)).toThrowError("nope 42");
280
+ expect(() => once.once(fn)).toThrowError("nope 42");
281
+ });
282
+ });
283
+ //# sourceMappingURL=resolve-once.test.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"resolve-once.test.js","sourceRoot":"","sources":["../../src/resolve-once.test.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,WAAW,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAC;AAE3E,QAAQ,CAAC,cAAc,EAAE,GAAG,EAAE;IAC5B,EAAE,CAAC,UAAU,EAAE,KAAK,IAAI,EAAE;QACxB,MAAM,IAAI,GAAG,IAAI,WAAW,EAAU,CAAC;QAEvC,MAAM,UAAU,GAAG,EAAE,CAAC,EAAE,CAAC,KAAK,IAAI,EAAE;YAClC,OAAO,IAAI,OAAO,CAAS,CAAC,OAAO,EAAE,EAAE;gBACrC,UAAU,CAAC,GAAG,EAAE;oBACd,OAAO,CAAC,EAAE,CAAC,CAAC;gBACd,CAAC,EAAE,GAAG,CAAC,CAAC;YACV,CAAC,CAAC,CAAC;QACL,CAAC,CAAC,CAAC;QACH,MAAM,KAAK,GAAG,IAAI,CAAC,GAAG,EAAE,CAAC;QACzB,MAAM,EAAE,GAAG,GAAoB,EAAE,CAAC,IAAI,CAAC,IAAI,CAAC,KAAK,IAAI,EAAE,CAAC,UAAU,EAAE,CAAC,CAAC;QACtE,MAAM,CAAC,UAAU,CAAC,CAAC,qBAAqB,CAAC,CAAC,CAAC,CAAC;QAC5C,MAAM,CAAC,MAAM,EAAE,EAAE,CAAC,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;QAC5B,MAAM,CAAC,UAAU,CAAC,CAAC,qBAAqB,CAAC,CAAC,CAAC,CAAC;QAC5C,MAAM,CAAC,MAAM,EAAE,EAAE,CAAC,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;QAC5B,MAAM,CAAC,UAAU,CAAC,CAAC,qBAAqB,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,IAAI,CAAC,EAAE,CAAC,CAAC;QAC/B,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;IACjC,CAAC,CAAC,CAAC;IAEH,EAAE,CAAC,+BAA+B,EAAE,GAAG,EAAE;QACvC,MAAM,IAAI,GAAG,IAAI,WAAW,EAAU,CAAC;QACvC,IAAI,GAAG,GAAG,EAAE,CAAC;QACb,MAAM,EAAE,GAAG,GAAoB,EAAE;YAC/B,MAAM,IAAI,KAAK,CAAC,QAAQ,GAAG,EAAE,EAAE,CAAC,CAAC;QACnC,CAAC,CAAC;QACF,MAAM,CAAC,GAAG,EAAE,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC,CAAC,YAAY,CAAC,SAAS,CAAC,CAAC;QACpD,MAAM,CAAC,GAAG,EAAE,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC,CAAC,YAAY,CAAC,SAAS,CAAC,CAAC;IACtD,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC"}
package/ts/result.d.ts ADDED
@@ -0,0 +1,34 @@
1
+ export declare abstract class Result<T, E = Error> {
2
+ static Ok<T = void>(t: T): Result<T, Error>;
3
+ static Err<T, E extends Error = Error>(t: E | string): Result<T, E>;
4
+ static Is<T>(t: unknown): t is Result<T>;
5
+ isOk(): boolean;
6
+ isErr(): boolean;
7
+ Ok(): T;
8
+ Err(): E;
9
+ abstract is_ok(): boolean;
10
+ abstract is_err(): boolean;
11
+ abstract unwrap(): T;
12
+ abstract unwrap_err(): E;
13
+ }
14
+ export declare class ResultOK<T> extends Result<T, Error> {
15
+ private _t;
16
+ constructor(t: T);
17
+ is_ok(): boolean;
18
+ is_err(): boolean;
19
+ unwrap_err(): Error;
20
+ unwrap(): T;
21
+ }
22
+ export declare class ResultError<T extends Error> extends Result<never, T> {
23
+ private _error;
24
+ constructor(t: T);
25
+ is_ok(): boolean;
26
+ is_err(): boolean;
27
+ unwrap(): never;
28
+ unwrap_err(): T;
29
+ }
30
+ export type WithoutResult<T> = T extends Result<infer U> ? U : T;
31
+ type WithResult<T> = T extends Promise<infer U> ? Promise<Result<U>> : Result<T>;
32
+ export declare function exception2Result<FN extends () => Promise<T> | T, T>(fn: FN): WithResult<ReturnType<FN>>;
33
+ export {};
34
+ //# sourceMappingURL=result.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"result.d.ts","sourceRoot":"","sources":["../../src/result.ts"],"names":[],"mappings":"AAAA,8BAAsB,MAAM,CAAC,CAAC,EAAE,CAAC,GAAG,KAAK;IACvC,MAAM,CAAC,EAAE,CAAC,CAAC,GAAG,IAAI,EAAE,CAAC,EAAE,CAAC,GAAG,MAAM,CAAC,CAAC,EAAE,KAAK,CAAC;IAG3C,MAAM,CAAC,GAAG,CAAC,CAAC,EAAE,CAAC,SAAS,KAAK,GAAG,KAAK,EAAE,CAAC,EAAE,CAAC,GAAG,MAAM,GAAG,MAAM,CAAC,CAAC,EAAE,CAAC,CAAC;IAMnE,MAAM,CAAC,EAAE,CAAC,CAAC,EAAE,CAAC,EAAE,OAAO,GAAG,CAAC,IAAI,MAAM,CAAC,CAAC,CAAC;IAcxC,IAAI,IAAI,OAAO;IAGf,KAAK,IAAI,OAAO;IAIhB,EAAE,IAAI,CAAC;IAGP,GAAG,IAAI,CAAC;IAIR,QAAQ,CAAC,KAAK,IAAI,OAAO;IACzB,QAAQ,CAAC,MAAM,IAAI,OAAO;IAC1B,QAAQ,CAAC,MAAM,IAAI,CAAC;IACpB,QAAQ,CAAC,UAAU,IAAI,CAAC;CACzB;AAED,qBAAa,QAAQ,CAAC,CAAC,CAAE,SAAQ,MAAM,CAAC,CAAC,EAAE,KAAK,CAAC;IAC/C,OAAO,CAAC,EAAE,CAAI;gBACF,CAAC,EAAE,CAAC;IAIhB,KAAK,IAAI,OAAO;IAGhB,MAAM,IAAI,OAAO;IAGjB,UAAU,IAAI,KAAK;IAGnB,MAAM,IAAI,CAAC;CAGZ;AAED,qBAAa,WAAW,CAAC,CAAC,SAAS,KAAK,CAAE,SAAQ,MAAM,CAAC,KAAK,EAAE,CAAC,CAAC;IAChE,OAAO,CAAC,MAAM,CAAI;gBACN,CAAC,EAAE,CAAC;IAIhB,KAAK,IAAI,OAAO;IAGhB,MAAM,IAAI,OAAO;IAGjB,MAAM,IAAI,KAAK;IAGf,UAAU,IAAI,CAAC;CAGhB;AAED,MAAM,MAAM,aAAa,CAAC,CAAC,IAAI,CAAC,SAAS,MAAM,CAAC,MAAM,CAAC,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;AAGjE,KAAK,UAAU,CAAC,CAAC,IAAI,CAAC,SAAS,OAAO,CAAC,MAAM,CAAC,CAAC,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,GAAG,MAAM,CAAC,CAAC,CAAC,CAAC;AAEjF,wBAAgB,gBAAgB,CAAC,EAAE,SAAS,MAAM,OAAO,CAAC,CAAC,CAAC,GAAG,CAAC,EAAE,CAAC,EAAE,EAAE,EAAE,EAAE,GAAG,UAAU,CAAC,UAAU,CAAC,EAAE,CAAC,CAAC,CAUvG"}
package/ts/result.js ADDED
@@ -0,0 +1,85 @@
1
+ export class Result {
2
+ static Ok(t) {
3
+ return new ResultOK(t);
4
+ }
5
+ static Err(t) {
6
+ if (typeof t === "string") {
7
+ return new ResultError(new Error(t));
8
+ }
9
+ return new ResultError(t);
10
+ }
11
+ static Is(t) {
12
+ if (!t) {
13
+ return false;
14
+ }
15
+ if (t instanceof Result) {
16
+ return true;
17
+ }
18
+ const rt = t;
19
+ if ([typeof rt.is_ok, typeof rt.is_err, typeof rt.unwrap, typeof rt.unwrap_err].every((x) => x === "function")) {
20
+ return true;
21
+ }
22
+ return false;
23
+ }
24
+ isOk() {
25
+ return this.is_ok();
26
+ }
27
+ isErr() {
28
+ return this.is_err();
29
+ }
30
+ Ok() {
31
+ return this.unwrap();
32
+ }
33
+ Err() {
34
+ return this.unwrap_err();
35
+ }
36
+ }
37
+ export class ResultOK extends Result {
38
+ constructor(t) {
39
+ super();
40
+ this._t = t;
41
+ }
42
+ is_ok() {
43
+ return true;
44
+ }
45
+ is_err() {
46
+ return false;
47
+ }
48
+ unwrap_err() {
49
+ throw new Error("Result is Ok");
50
+ }
51
+ unwrap() {
52
+ return this._t;
53
+ }
54
+ }
55
+ export class ResultError extends Result {
56
+ constructor(t) {
57
+ super();
58
+ this._error = t;
59
+ }
60
+ is_ok() {
61
+ return false;
62
+ }
63
+ is_err() {
64
+ return true;
65
+ }
66
+ unwrap() {
67
+ throw new Error(`Result is Err: ${this._error}`);
68
+ }
69
+ unwrap_err() {
70
+ return this._error;
71
+ }
72
+ }
73
+ export function exception2Result(fn) {
74
+ try {
75
+ const res = fn();
76
+ if (res instanceof Promise) {
77
+ return res.then((value) => Result.Ok(value)).catch((e) => Result.Err(e));
78
+ }
79
+ return Result.Ok(res);
80
+ }
81
+ catch (e) {
82
+ return Result.Err(e);
83
+ }
84
+ }
85
+ //# sourceMappingURL=result.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"result.js","sourceRoot":"","sources":["../../src/result.ts"],"names":[],"mappings":"AAAA,MAAM,OAAgB,MAAM;IAC1B,MAAM,CAAC,EAAE,CAAW,CAAI;QACtB,OAAO,IAAI,QAAQ,CAAC,CAAC,CAAC,CAAC;IACzB,CAAC;IACD,MAAM,CAAC,GAAG,CAA6B,CAAa;QAClD,IAAI,OAAO,CAAC,KAAK,QAAQ,EAAE,CAAC;YAC1B,OAAO,IAAI,WAAW,CAAC,IAAI,KAAK,CAAC,CAAC,CAAM,CAAC,CAAC;QAC5C,CAAC;QACD,OAAO,IAAI,WAAW,CAAC,CAAC,CAAC,CAAC;IAC5B,CAAC;IACD,MAAM,CAAC,EAAE,CAAI,CAAU;QACrB,IAAI,CAAC,CAAC,EAAE,CAAC;YACP,OAAO,KAAK,CAAC;QACf,CAAC;QACD,IAAI,CAAC,YAAY,MAAM,EAAE,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,IAAI,KAAK,CAAC,kBAAkB,IAAI,CAAC,MAAM,EAAE,CAAC,CAAC;IACnD,CAAC;IACD,UAAU;QACR,OAAO,IAAI,CAAC,MAAM,CAAC;IACrB,CAAC;CACF;AAOD,MAAM,UAAU,gBAAgB,CAAqC,EAAM;IACzE,IAAI,CAAC;QACH,MAAM,GAAG,GAAG,EAAE,EAAE,CAAC;QACjB,IAAI,GAAG,YAAY,OAAO,EAAE,CAAC;YAC3B,OAAO,GAAG,CAAC,IAAI,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,MAAM,CAAC,EAAE,CAAC,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,CAA+B,CAAC;QACzG,CAAC;QACD,OAAO,MAAM,CAAC,EAAE,CAAC,GAAG,CAA+B,CAAC;IACtD,CAAC;IAAC,OAAO,CAAC,EAAE,CAAC;QACX,OAAO,MAAM,CAAC,GAAG,CAAC,CAAU,CAA+B,CAAC;IAC9D,CAAC;AACH,CAAC"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=result.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"result.test.d.ts","sourceRoot":"","sources":["../../src/result.test.ts"],"names":[],"mappings":""}
@@ -0,0 +1,79 @@
1
+ import { exception2Result, Result } from "@adviser/cement";
2
+ it("ResultOk", () => {
3
+ const result = Result.Ok(1);
4
+ expect(result.isOk()).toBe(true);
5
+ expect(result.is_ok()).toBe(true);
6
+ expect(result.Ok()).toBe(1);
7
+ expect(result.unwrap()).toBe(1);
8
+ expect(result.isErr()).toBe(false);
9
+ expect(result.is_err()).toBe(false);
10
+ expect(() => result.Err()).toThrow();
11
+ expect(() => result.unwrap_err()).toThrow();
12
+ });
13
+ it("ResultErr", () => {
14
+ const result = Result.Err("xxx");
15
+ expect(result.isOk()).toBe(false);
16
+ expect(result.is_ok()).toBe(false);
17
+ expect(result.Err().message).toEqual("xxx");
18
+ expect(result.unwrap_err().message).toBe("xxx");
19
+ expect(result.isErr()).toBe(true);
20
+ expect(result.is_err()).toBe(true);
21
+ expect(() => result.Ok()).toThrow();
22
+ expect(() => result.unwrap()).toThrow();
23
+ });
24
+ class xResult {
25
+ }
26
+ class fakeResult {
27
+ is_ok() {
28
+ return true;
29
+ }
30
+ is_err() {
31
+ return false;
32
+ }
33
+ unwrap() {
34
+ return 1;
35
+ }
36
+ unwrap_err() {
37
+ throw new Error("Result is Ok");
38
+ }
39
+ }
40
+ it("is Result", () => {
41
+ expect(Result.Is(Result.Ok(1))).toBe(true);
42
+ expect(Result.Is(Result.Err("xxx"))).toEqual(true);
43
+ expect(Result.Is(new fakeResult())).toBe(true);
44
+ expect(Result.Is(new xResult())).toBe(false);
45
+ });
46
+ it("WithoutResult", () => {
47
+ const result = Result.Ok({ a: 1 });
48
+ const a1 = {};
49
+ a1.a = 1;
50
+ expect(a1.a).toEqual(1);
51
+ expect(result.Ok().a).toEqual(1);
52
+ });
53
+ it("sync exception2Result ok", () => {
54
+ expect(exception2Result(() => 1)).toEqual(Result.Ok(1));
55
+ });
56
+ it("sync exception2Result throw", () => {
57
+ expect(exception2Result(() => {
58
+ throw new Error("x");
59
+ })).toEqual(Result.Err("x"));
60
+ });
61
+ it("async exception2Result ok", async () => {
62
+ expect(await exception2Result(async () => 1)).toEqual(Result.Ok(1));
63
+ });
64
+ it("async exception2Result throw", async () => {
65
+ expect(await exception2Result(async () => {
66
+ throw new Error("x");
67
+ })).toEqual(Result.Err("x"));
68
+ });
69
+ it("result typ", () => {
70
+ function ok() {
71
+ return Result.Ok(1);
72
+ }
73
+ function err() {
74
+ return Result.Err("x");
75
+ }
76
+ expect(ok().Ok()).toBe(1);
77
+ expect(err().Err().message).toBe("x");
78
+ });
79
+ //# sourceMappingURL=result.test.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"result.test.js","sourceRoot":"","sources":["../../src/result.test.ts"],"names":[],"m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AAgB,CAAC,KAAK,IAAI,EAAE;QAChC,MAAM,IAAI,KAAK,CAAC,GAAG,CAAC,CAAC;IACvB,CAAC,CAAC,CACH,CAAC,OAAO,CAAC,MAAM,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC,CAAC;AAC7B,CAAC,CAAC,CAAC;AAEH,EAAE,CAAC,YAAY,EAAE,GAAG,EAAE;IACpB,SAAS,EAAE;QACT,OAAO,MAAM,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC;IACtB,CAAC;IACD,SAAS,GAAG;QACV,OAAO,MAAM,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;IACzB,CAAC;IACD,MAAM,CAAC,EAAE,EAAE,CAAC,EAAE,EAAE,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;IAC1B,MAAM,CAAC,GAAG,EAAE,CAAC,GAAG,EAAE,CAAC,OAAO,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;AACxC,CAAC,CAAC,CAAC"}
@@ -0,0 +1,8 @@
1
+ export interface Runtime {
2
+ isNodeIsh: boolean;
3
+ isBrowser: boolean;
4
+ isDeno: boolean;
5
+ isReactNative: boolean;
6
+ }
7
+ export declare function runtimeFn(): Runtime;
8
+ //# sourceMappingURL=runtime.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"runtime.d.ts","sourceRoot":"","sources":["../../src/runtime.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,OAAO;IACtB,SAAS,EAAE,OAAO,CAAC;IACnB,SAAS,EAAE,OAAO,CAAC;IACnB,MAAM,EAAE,OAAO,CAAC;IAChB,aAAa,EAAE,OAAO,CAAC;CACxB;AAcD,wBAAgB,SAAS,IAAI,OAAO,CAgBnC"}
package/ts/runtime.js ADDED
@@ -0,0 +1,26 @@
1
+ function isSet(value, ref = globalThis) {
2
+ const [head, ...tail] = value.split(".");
3
+ if (["object", "function"].includes(typeof ref) && ref && ["object", "function"].includes(typeof ref[head]) && ref[head]) {
4
+ if (tail.length <= 1) {
5
+ return true;
6
+ }
7
+ return isSet(tail.join("."), ref[head]);
8
+ }
9
+ return false;
10
+ }
11
+ export function runtimeFn() {
12
+ const gt = globalThis;
13
+ const isReactNative = isSet("navigator.product") && typeof gt["navigator"] === "object" && gt["navigator"]["product"] === "ReactNative";
14
+ let isNodeIsh = false;
15
+ if (!isSet("Deno")) {
16
+ isNodeIsh = isSet("process.versions.node") && !isReactNative;
17
+ }
18
+ const isDeno = isSet("Deno");
19
+ return {
20
+ isNodeIsh,
21
+ isBrowser: !(isNodeIsh || isDeno) && !isReactNative,
22
+ isDeno,
23
+ isReactNative,
24
+ };
25
+ }
26
+ //# sourceMappingURL=runtime.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"runtime.js","sourceRoot":"","sources":["../../src/runtime.ts"],"names":[],"mappings":"AAQA,SAAS,KAAK,CAAC,KAAa,EAAE,MAAW,UAAU;IACjD,MAAM,CAAC,IAAI,EAAE,GAAG,IAAI,CAAC,GAAG,KAAK,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;IACzC,IAAI,CAAC,QAAQ,EAAE,UAAU,CAAC,CAAC,QAAQ,CAAC,OAAO,GAAG,CAAC,IAAI,GAAG,IAAI,CAAC,QAAQ,EAAE,UAAU,CAAC,CAAC,QAAQ,CAAC,OAAO,GAAG,CAAC,IAAI,CAAC,CAAC,IAAI,GAAG,CAAC,IAAI,CAAC,EAAE,CAAC;QACzH,IAAI,IAAI,CAAC,MAAM,IAAI,CAAC,EAAE,CAAC;YACrB,OAAO,IAAI,CAAC;QACd,CAAC;QACD,OAAO,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,EAAE,GAAG,CAAC,IAAI,CAAC,CAAC,CAAC;IAC1C,CAAC;IACD,OAAO,KAAK,CAAC;AACf,CAAC;AAED,MAAM,UAAU,SAAS;IAEvB,MAAM,EAAE,GAAQ,UAAU,CAAC;IAC3B,MAAM,aAAa,GACjB,KAAK,CAAC,mBAAmB,CAAC,IAAI,OAAO,EAAE,CAAC,WAAW,CAAC,KAAK,QAAQ,IAAI,EAAE,CAAC,WAAW,CAAC,CAAC,SAAS,CAAC,KAAK,aAAa,CAAC;IACpH,IAAI,SAAS,GAAG,KAAK,CAAC;IACtB,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,EAAE,CAAC;QACnB,SAAS,GAAG,KAAK,CAAC,uBAAuB,CAAC,IAAI,CAAC,aAAa,CAAC;IAC/D,CAAC;IACD,MAAM,MAAM,GAAG,KAAK,CAAC,MAAM,CAAC,CAAC;IAC7B,OAAO;QACL,SAAS;QACT,SAAS,EAAE,CAAC,CAAC,SAAS,IAAI,MAAM,CAAC,IAAI,CAAC,aAAa;QACnD,MAAM;QACN,aAAa;KACd,CAAC;AACJ,CAAC"}
@@ -0,0 +1,36 @@
1
+ import { FileService } from "./file-service.js";
2
+ import { Env } from "./sys-env.js";
3
+ import { Time } from "./time.js";
4
+ export declare enum TimeMode {
5
+ REAL = "real",
6
+ CONST = "const",
7
+ STEP = "step"
8
+ }
9
+ export declare enum RandomMode {
10
+ CONST = "const",
11
+ STEP = "step",
12
+ RANDOM = "random"
13
+ }
14
+ export declare enum IDMode {
15
+ UUID = "uuid",
16
+ CONST = "const",
17
+ STEP = "step"
18
+ }
19
+ export declare function String2TimeMode(s?: string): TimeMode;
20
+ export type VoidFunc = () => void | Promise<void>;
21
+ export interface SystemService {
22
+ Env(): Env;
23
+ Args(): string[];
24
+ OnExit(hdl: VoidFunc): VoidFunc;
25
+ Exit(code: number): void;
26
+ }
27
+ export interface SysAbstraction {
28
+ Time(): Time;
29
+ Stdout(): WritableStream<Uint8Array>;
30
+ Stderr(): WritableStream<Uint8Array>;
31
+ NextId(): string;
32
+ Random0ToValue(value: number): number;
33
+ System(): SystemService;
34
+ FileSystem(): FileService;
35
+ }
36
+ //# sourceMappingURL=sys-abstraction.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"sys-abstraction.d.ts","sourceRoot":"","sources":["../../src/sys-abstraction.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAChD,OAAO,EAAE,GAAG,EAAE,MAAM,cAAc,CAAC;AACnC,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AAEjC,oBAAY,QAAQ;IAClB,IAAI,SAAS;IACb,KAAK,UAAU;IACf,IAAI,SAAS;CACd;AAED,oBAAY,UAAU;IACpB,KAAK,UAAU;IACf,IAAI,SAAS;IACb,MAAM,WAAW;CAClB;AAED,oBAAY,MAAM;IAChB,IAAI,SAAS;IACb,KAAK,UAAU;IACf,IAAI,SAAS;CACd;AAED,wBAAgB,eAAe,CAAC,CAAC,CAAC,EAAE,MAAM,GAAG,QAAQ,CAWpD;AAED,MAAM,MAAM,QAAQ,GAAG,MAAM,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;AAElD,MAAM,WAAW,aAAa;IAC5B,GAAG,IAAI,GAAG,CAAC;IACX,IAAI,IAAI,MAAM,EAAE,CAAC;IACjB,MAAM,CAAC,GAAG,EAAE,QAAQ,GAAG,QAAQ,CAAC;IAChC,IAAI,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI,CAAC;CAC1B;AAED,MAAM,WAAW,cAAc;IAC7B,IAAI,IAAI,IAAI,CAAC;IACb,MAAM,IAAI,cAAc,CAAC,UAAU,CAAC,CAAC;IACrC,MAAM,IAAI,cAAc,CAAC,UAAU,CAAC,CAAC;IACrC,MAAM,IAAI,MAAM,CAAC;IACjB,cAAc,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAAC;IACtC,MAAM,IAAI,aAAa,CAAC;IACxB,UAAU,IAAI,WAAW,CAAC;CAC3B"}
@@ -0,0 +1,31 @@
1
+ export var TimeMode;
2
+ (function (TimeMode) {
3
+ TimeMode["REAL"] = "real";
4
+ TimeMode["CONST"] = "const";
5
+ TimeMode["STEP"] = "step";
6
+ })(TimeMode || (TimeMode = {}));
7
+ export var RandomMode;
8
+ (function (RandomMode) {
9
+ RandomMode["CONST"] = "const";
10
+ RandomMode["STEP"] = "step";
11
+ RandomMode["RANDOM"] = "random";
12
+ })(RandomMode || (RandomMode = {}));
13
+ export var IDMode;
14
+ (function (IDMode) {
15
+ IDMode["UUID"] = "uuid";
16
+ IDMode["CONST"] = "const";
17
+ IDMode["STEP"] = "step";
18
+ })(IDMode || (IDMode = {}));
19
+ export function String2TimeMode(s) {
20
+ switch (s === null || s === void 0 ? void 0 : s.toLowerCase()) {
21
+ case "real":
22
+ return TimeMode.REAL;
23
+ case "const":
24
+ return TimeMode.CONST;
25
+ case "step":
26
+ return TimeMode.STEP;
27
+ default:
28
+ return TimeMode.REAL;
29
+ }
30
+ }
31
+ //# sourceMappingURL=sys-abstraction.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"sys-abstraction.js","sourceRoot":"","sources":["../../src/sys-abstraction.ts"],"names":[],"mappings":"AAIA,MAAM,CAAN,IAAY,QAIX;AAJD,WAAY,QAAQ;IAClB,yBAAa,CAAA;IACb,2BAAe,CAAA;IACf,yBAAa,CAAA;AACf,CAAC,EAJW,QAAQ,KAAR,QAAQ,QAInB;AAED,MAAM,CAAN,IAAY,UAIX;AAJD,WAAY,UAAU;IACpB,6BAAe,CAAA;IACf,2BAAa,CAAA;IACb,+BAAiB,CAAA;AACnB,CAAC,EAJW,UAAU,KAAV,UAAU,QAIrB;AAED,MAAM,CAAN,IAAY,MAIX;AAJD,WAAY,MAAM;IAChB,uBAAa,CAAA;IACb,yBAAe,CAAA;IACf,uBAAa,CAAA;AACf,CAAC,EAJW,MAAM,KAAN,MAAM,QAIjB;AAED,MAAM,UAAU,eAAe,CAAC,CAAU;IACxC,QAAQ,CAAC,aAAD,CAAC,uBAAD,CAAC,CAAE,WAAW,EAAE,EAAE,CAAC;QACzB,KAAK,MAAM;YACT,OAAO,QAAQ,CAAC,IAAI,CAAC;QACvB,KAAK,OAAO;YACV,OAAO,QAAQ,CAAC,KAAK,CAAC;QACxB,KAAK,MAAM;YACT,OAAO,QAAQ,CAAC,IAAI,CAAC;QACvB;YACE,OAAO,QAAQ,CAAC,IAAI,CAAC;IACzB,CAAC;AACH,CAAC"}