lib0 0.2.93 → 0.2.95

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (95) hide show
  1. package/coverage/tmp/coverage-17427-1722257176008-0.json +1 -0
  2. package/dist/{broadcastchannel-80813eea.cjs → broadcastchannel-8b08f847.cjs} +2 -2
  3. package/dist/{broadcastchannel-80813eea.cjs.map → broadcastchannel-8b08f847.cjs.map} +1 -1
  4. package/dist/broadcastchannel.cjs +5 -5
  5. package/dist/{buffer-f11b8d4d.cjs → buffer-757ffcaa.cjs} +2 -2
  6. package/dist/{buffer-f11b8d4d.cjs.map → buffer-757ffcaa.cjs.map} +1 -1
  7. package/dist/buffer.cjs +4 -4
  8. package/dist/cache.cjs +2 -2
  9. package/dist/component.cjs +3 -3
  10. package/dist/{diff-fbaa426b.cjs → diff-38d8f45b.cjs} +2 -2
  11. package/dist/{diff-fbaa426b.cjs.map → diff-38d8f45b.cjs.map} +1 -1
  12. package/dist/diff.cjs +3 -3
  13. package/dist/{environment-f4d7c3e5.cjs → environment-99bbcd58.cjs} +16 -5
  14. package/dist/environment-99bbcd58.cjs.map +1 -0
  15. package/dist/environment.cjs +3 -3
  16. package/dist/environment.d.ts +7 -0
  17. package/dist/environment.d.ts.map +1 -1
  18. package/dist/{function-314fdc56.cjs → function-18d343cf.cjs} +2 -2
  19. package/dist/{function-314fdc56.cjs.map → function-18d343cf.cjs.map} +1 -1
  20. package/dist/function.cjs +2 -2
  21. package/dist/index.cjs +9 -9
  22. package/dist/{indexeddb-8d495848.cjs → indexeddb-0cbb4d6f.cjs} +2 -2
  23. package/dist/{indexeddb-8d495848.cjs.map → indexeddb-0cbb4d6f.cjs.map} +1 -1
  24. package/dist/indexeddb.cjs +2 -2
  25. package/dist/indexeddbV2.cjs +287 -0
  26. package/dist/indexeddbV2.cjs.map +1 -0
  27. package/dist/indexeddbV2.d.ts +35 -0
  28. package/dist/indexeddbV2.d.ts.map +1 -0
  29. package/dist/indexeddbV2.test.d.ts +3 -0
  30. package/dist/indexeddbV2.test.d.ts.map +1 -0
  31. package/dist/jwt.cjs +4 -4
  32. package/dist/list.cjs +2 -2
  33. package/dist/logging.cjs +3 -3
  34. package/dist/logging.common.cjs +22 -6
  35. package/dist/logging.common.cjs.map +1 -1
  36. package/dist/logging.common.d.ts +1 -1
  37. package/dist/logging.common.d.ts.map +1 -1
  38. package/dist/logging.node.cjs +3 -3
  39. package/dist/{object-fecf6a7b.cjs → object-b0539a4e.cjs} +11 -2
  40. package/dist/object-b0539a4e.cjs.map +1 -0
  41. package/dist/object.cjs +2 -1
  42. package/dist/object.cjs.map +1 -1
  43. package/dist/object.d.ts +3 -0
  44. package/dist/object.d.ts.map +1 -1
  45. package/dist/performance.node.cjs +2 -2
  46. package/dist/pledge.cjs +307 -0
  47. package/dist/pledge.cjs.map +1 -0
  48. package/dist/pledge.d.ts +68 -0
  49. package/dist/pledge.d.ts.map +1 -0
  50. package/dist/pledge.test.d.ts +6 -0
  51. package/dist/pledge.test.d.ts.map +1 -0
  52. package/dist/{prng-e4f28259.cjs → prng-90743426.cjs} +2 -2
  53. package/dist/{prng-e4f28259.cjs.map → prng-90743426.cjs.map} +1 -1
  54. package/dist/prng.cjs +5 -5
  55. package/dist/{promise-afe9dfc3.cjs → promise-7d13a97c.cjs} +22 -2
  56. package/dist/promise-7d13a97c.cjs.map +1 -0
  57. package/dist/promise.cjs +2 -1
  58. package/dist/promise.cjs.map +1 -1
  59. package/dist/promise.d.ts +1 -0
  60. package/dist/promise.d.ts.map +1 -1
  61. package/dist/promise.test.d.ts.map +1 -1
  62. package/dist/rabin-gf2-polynomial.cjs +4 -4
  63. package/dist/rabin-uncached.cjs +4 -4
  64. package/dist/rabin.cjs +4 -4
  65. package/dist/testing.cjs +7 -7
  66. package/dist/url.cjs +1 -1
  67. package/environment.d.ts +7 -0
  68. package/environment.d.ts.map +1 -1
  69. package/environment.js +14 -3
  70. package/indexeddbV2.d.ts +35 -0
  71. package/indexeddbV2.d.ts.map +1 -0
  72. package/indexeddbV2.js +265 -0
  73. package/indexeddbV2.test.d.ts +3 -0
  74. package/indexeddbV2.test.d.ts.map +1 -0
  75. package/logging.common.d.ts +1 -1
  76. package/logging.common.d.ts.map +1 -1
  77. package/logging.common.js +14 -3
  78. package/object.d.ts +3 -0
  79. package/object.d.ts.map +1 -1
  80. package/object.js +8 -1
  81. package/package.json +1 -1
  82. package/pledge.d.ts +68 -0
  83. package/pledge.d.ts.map +1 -0
  84. package/pledge.js +292 -0
  85. package/pledge.test.d.ts +6 -0
  86. package/pledge.test.d.ts.map +1 -0
  87. package/promise.d.ts +1 -0
  88. package/promise.d.ts.map +1 -1
  89. package/promise.js +19 -1
  90. package/promise.test.d.ts.map +1 -1
  91. package/test.js +4 -0
  92. package/coverage/tmp/coverage-45503-1710701602083-0.json +0 -1
  93. package/dist/environment-f4d7c3e5.cjs.map +0 -1
  94. package/dist/object-fecf6a7b.cjs.map +0 -1
  95. package/dist/promise-afe9dfc3.cjs.map +0 -1
package/pledge.js ADDED
@@ -0,0 +1,292 @@
1
+ /**
2
+ * @experimental Use of this module is not encouraged!
3
+ * This is just an experiment.
4
+ * @todo remove `c8 ignore` line once this is moved to "non-experimental"
5
+ */
6
+
7
+ import * as queue from './queue.js'
8
+ import * as object from './object.js'
9
+
10
+ /* c8 ignore start */
11
+
12
+ /**
13
+ * @type {queue.Queue<queue.QueueValue<()=>void>>}
14
+ */
15
+ const ctxFs = queue.create()
16
+
17
+ /**
18
+ * @param {() => void} f
19
+ */
20
+ const runInGlobalContext = f => {
21
+ const isEmpty = queue.isEmpty(ctxFs)
22
+ queue.enqueue(ctxFs, new queue.QueueValue(f))
23
+ if (isEmpty) {
24
+ while (!queue.isEmpty(ctxFs)) {
25
+ /** @type {queue.QueueValue<()=>{}>} */ (ctxFs.start).v()
26
+ queue.dequeue(ctxFs)
27
+ }
28
+ }
29
+ }
30
+
31
+ /**
32
+ * @template V
33
+ * @typedef {V | PledgeInstance<V>} Pledge
34
+ */
35
+
36
+ /**
37
+ * @template {any} Val
38
+ * @template {any} [CancelReason=Error]
39
+ */
40
+ export class PledgeInstance {
41
+ constructor () {
42
+ /**
43
+ * @type {Val | CancelReason | null}
44
+ */
45
+ this._v = null
46
+ this.isResolved = false
47
+ /**
48
+ * @type {Array<function(Val):void> | null}
49
+ */
50
+ this._whenResolved = []
51
+ /**
52
+ * @type {Array<function(CancelReason):void> | null}
53
+ */
54
+ this._whenCanceled = []
55
+ }
56
+
57
+ get isDone () {
58
+ return this._whenResolved === null
59
+ }
60
+
61
+ get isCanceled () {
62
+ return !this.isResolved && this._whenResolved === null
63
+ }
64
+
65
+ /**
66
+ * @param {Val} v
67
+ */
68
+ resolve (v) {
69
+ const whenResolved = this._whenResolved
70
+ if (whenResolved === null) return
71
+ this._v = v
72
+ this.isResolved = true
73
+ this._whenResolved = null
74
+ this._whenCanceled = null
75
+ for (let i = 0; i < whenResolved.length; i++) {
76
+ whenResolved[i](v)
77
+ }
78
+ }
79
+
80
+ /**
81
+ * @param {CancelReason} reason
82
+ */
83
+ cancel (reason) {
84
+ const whenCanceled = this._whenCanceled
85
+ if (whenCanceled === null) return
86
+ this._v = reason
87
+ this._whenResolved = null
88
+ this._whenCanceled = null
89
+ for (let i = 0; i < whenCanceled.length; i++) {
90
+ whenCanceled[i](reason)
91
+ }
92
+ }
93
+
94
+ /**
95
+ * @template R
96
+ * @param {function(Val):Pledge<R>} f
97
+ * @return {PledgeInstance<R>}
98
+ */
99
+ map (f) {
100
+ /**
101
+ * @type {PledgeInstance<R>}
102
+ */
103
+ const p = new PledgeInstance()
104
+ this.whenResolved(v => {
105
+ const result = f(v)
106
+ if (result instanceof PledgeInstance) {
107
+ if (result._whenResolved === null) {
108
+ result.resolve(/** @type {R} */ (result._v))
109
+ } else {
110
+ result._whenResolved.push(p.resolve.bind(p))
111
+ }
112
+ } else {
113
+ p.resolve(result)
114
+ }
115
+ })
116
+ return p
117
+ }
118
+
119
+ /**
120
+ * @param {function(Val):void} f
121
+ */
122
+ whenResolved (f) {
123
+ if (this.isResolved) {
124
+ f(/** @type {Val} */ (this._v))
125
+ } else {
126
+ this._whenResolved?.push(f)
127
+ }
128
+ }
129
+
130
+ /**
131
+ * @param {(reason: CancelReason) => void} f
132
+ */
133
+ whenCanceled (f) {
134
+ if (this.isCanceled) {
135
+ f(/** @type {CancelReason} */ (this._v))
136
+ } else {
137
+ this._whenCanceled?.push(f)
138
+ }
139
+ }
140
+
141
+ /**
142
+ * @return {Promise<Val>}
143
+ */
144
+ promise () {
145
+ return new Promise((resolve, reject) => {
146
+ this.whenResolved(resolve)
147
+ this.whenCanceled(reject)
148
+ })
149
+ }
150
+ }
151
+
152
+ /**
153
+ * @template T
154
+ * @return {PledgeInstance<T>}
155
+ */
156
+ export const create = () => new PledgeInstance()
157
+
158
+ /**
159
+ * @typedef {Array<Pledge<unknown>> | Object<string,Pledge<unknown>>} PledgeMap
160
+ */
161
+
162
+ /**
163
+ * @template {Pledge<unknown> | PledgeMap} P
164
+ * @typedef {P extends PledgeMap ? { [K in keyof P]: P[K] extends Pledge<infer V> ? V : P[K]} : (P extends Pledge<infer V> ? V : never)} Resolved<P>
165
+ */
166
+
167
+ /**
168
+ * @todo Create a "resolveHelper" that will simplify creating indxeddbv2 functions. Double arguments
169
+ * are not necessary.
170
+ *
171
+ * @template V
172
+ * @template {Array<Pledge<unknown>>} DEPS
173
+ * @param {(p: PledgeInstance<V>, ...deps: Resolved<DEPS>) => void} init
174
+ * @param {DEPS} deps
175
+ * @return {PledgeInstance<V>}
176
+ */
177
+ export const createWithDependencies = (init, ...deps) => {
178
+ /**
179
+ * @type {PledgeInstance<V>}
180
+ */
181
+ const p = new PledgeInstance()
182
+ // @ts-ignore @todo remove this
183
+ all(deps).whenResolved(ds => init(p, ...ds))
184
+ return p
185
+ }
186
+
187
+ /**
188
+ * @template R
189
+ * @param {Pledge<R>} p
190
+ * @param {function(R):void} f
191
+ */
192
+ export const whenResolved = (p, f) => {
193
+ if (p instanceof PledgeInstance) {
194
+ return p.whenResolved(f)
195
+ }
196
+ return f(p)
197
+ }
198
+
199
+ /**
200
+ * @template {Pledge<unknown>} P
201
+ * @param {P} p
202
+ * @param {P extends PledgeInstance<unknown, infer CancelReason> ? function(CancelReason):void : function(any):void} f
203
+ */
204
+ export const whenCanceled = (p, f) => {
205
+ if (p instanceof PledgeInstance) {
206
+ p.whenCanceled(f)
207
+ }
208
+ }
209
+
210
+ /**
211
+ * @template P
212
+ * @template Q
213
+ * @param {Pledge<P>} p
214
+ * @param {(r: P) => Q} f
215
+ * @return {Pledge<Q>}
216
+ */
217
+ export const map = (p, f) => {
218
+ if (p instanceof PledgeInstance) {
219
+ return p.map(f)
220
+ }
221
+ return f(p)
222
+ }
223
+
224
+ /**
225
+ * @template {PledgeMap} PS
226
+ * @param {PS} ps
227
+ * @return {PledgeInstance<Resolved<PS>>}
228
+ */
229
+ export const all = ps => {
230
+ /**
231
+ * @type {any}
232
+ */
233
+ const pall = create()
234
+ /**
235
+ * @type {any}
236
+ */
237
+ const result = ps instanceof Array ? new Array(ps.length) : {}
238
+ let waitingPs = ps instanceof Array ? ps.length : object.size(ps)
239
+ for (const key in ps) {
240
+ const p = ps[key]
241
+ whenResolved(p, r => {
242
+ result[key] = r
243
+ if (--waitingPs === 0) {
244
+ // @ts-ignore
245
+ pall.resolve(result)
246
+ }
247
+ })
248
+ }
249
+ return pall
250
+ }
251
+
252
+ /**
253
+ * @template Result
254
+ * @template {any} YieldResults
255
+ * @param {() => Generator<Pledge<YieldResults> | PledgeInstance<YieldResults,any>, Result, any>} f
256
+ * @return {PledgeInstance<Result>}
257
+ */
258
+ export const coroutine = f => {
259
+ const p = create()
260
+ const gen = f()
261
+ /**
262
+ * @param {any} [yv]
263
+ */
264
+ const handleGen = (yv) => {
265
+ const res = gen.next(yv)
266
+ if (res.done) {
267
+ p.resolve(res.value)
268
+ return
269
+ }
270
+ // @ts-ignore
271
+ whenCanceled(res.value, (reason) => {
272
+ gen.throw(reason)
273
+ })
274
+ runInGlobalContext(() =>
275
+ whenResolved(res.value, handleGen)
276
+ )
277
+ }
278
+ handleGen()
279
+ return p
280
+ }
281
+
282
+ /**
283
+ * @param {number} timeout
284
+ * @return {PledgeInstance<undefined>}
285
+ */
286
+ export const wait = timeout => {
287
+ const p = create()
288
+ setTimeout(p.resolve.bind(p), timeout)
289
+ return p
290
+ }
291
+
292
+ /* c8 ignore end */
@@ -0,0 +1,6 @@
1
+ export function testPledgeCoroutine(_tc: t.TestCase): Promise<void>;
2
+ export function testPledgeVsPromisePerformanceTimeout(_tc: t.TestCase): Promise<void>;
3
+ export function testPledgeVsPromisePerformanceResolved(_tc: t.TestCase): Promise<void>;
4
+ export type MaybePromise = Promise<number> | number;
5
+ import * as t from './testing.js';
6
+ //# sourceMappingURL=pledge.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"pledge.test.d.ts","sourceRoot":"","sources":["pledge.test.js"],"names":[],"mappings":"AAOO,yCAFI,EAAE,QAAQ,iBAepB;AAKM,2DAFI,EAAE,QAAQ,iBAiBpB;AASM,4DAFI,EAAE,QAAQ,iBAqCpB;2BAzCY,QAAQ,MAAM,CAAC,GAAG,MAAM;mBA3ClB,cAAc"}
package/promise.d.ts CHANGED
@@ -15,6 +15,7 @@ export function reject(reason?: Error | undefined): Promise<never>;
15
15
  export function resolve<T>(res: void | T): Promise<void | T>;
16
16
  export function resolveWith<T>(res: T): Promise<T>;
17
17
  export function until(timeout: number, check: () => boolean, intervalResolution?: number | undefined): Promise<void>;
18
+ export function untilAsync(check: () => Promise<boolean> | boolean, timeout?: number, intervalResolution?: number): Promise<void>;
18
19
  export function wait(timeout: number): Promise<undefined>;
19
20
  export function isPromise(p: any): boolean;
20
21
  export type PromiseResolve<T> = (result?: T | PromiseLike<T> | undefined) => any;
package/promise.d.ts.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"promise.d.ts","sourceRoot":"","sources":["promise.js"],"names":[],"mappings":"AAmBO,oEAHwC,KAAK,KAAE,IAAI,KAAE,GAAG,cAGM;AAM9D,4CAHwB,IAAI,eAAU,KAAK,KAAE,IAAI,KAAE,IAAI,GAClD,QAAQ,IAAI,CAAC,CAEqB;AAE9C;;;;;;GAMG;AACH;;;EAA4C;AAMrC,oDAFK,QAAQ,KAAK,CAAC,CAE4B;AAO/C,6DAA2C;AAO3C,mDAA+C;AAU/C,+BALI,MAAM,eACK,OAAO,4CAEjB,QAAQ,IAAI,CAAC,CAkBvB;AAMK,8BAHI,MAAM,GACL,QAAQ,SAAS,CAAC,CAE0D;AAWjF,6BAHI,GAAG,GACF,OAAO,CAEwE"}
1
+ {"version":3,"file":"promise.d.ts","sourceRoot":"","sources":["promise.js"],"names":[],"mappings":"AAmBO,oEAHwC,KAAK,KAAE,IAAI,KAAE,GAAG,cAGM;AAM9D,4CAHwB,IAAI,eAAU,KAAK,KAAE,IAAI,KAAE,IAAI,GAClD,QAAQ,IAAI,CAAC,CAEqB;AAE9C;;;;;;GAMG;AACH;;;EAA4C;AAMrC,oDAFK,QAAQ,KAAK,CAAC,CAE4B;AAO/C,6DAA2C;AAO3C,mDAA+C;AAW/C,+BALI,MAAM,eACK,OAAO,4CAEjB,QAAQ,IAAI,CAAC,CAkBvB;AAQK,kCALI,MAAI,QAAQ,OAAO,CAAC,GAAC,OAAO,YAC5B,MAAM,uBACN,MAAM,GACL,QAAQ,IAAI,CAAC,CAWxB;AAMM,8BAHI,MAAM,GACL,QAAQ,SAAS,CAAC,CAE2D;AAWlF,6BAHI,GAAG,GACF,OAAO,CAEwE"}
package/promise.js CHANGED
@@ -56,6 +56,7 @@ export const resolveWith = res => Promise.resolve(res)
56
56
 
57
57
  /**
58
58
  * @todo Next version, reorder parameters: check, [timeout, [intervalResolution]]
59
+ * @deprecated use untilAsync instead
59
60
  *
60
61
  * @param {number} timeout
61
62
  * @param {function():boolean} check
@@ -80,11 +81,28 @@ export const until = (timeout, check, intervalResolution = 10) => create((resolv
80
81
  const intervalHandle = setInterval(untilInterval, intervalResolution)
81
82
  })
82
83
 
84
+ /**
85
+ * @param {()=>Promise<boolean>|boolean} check
86
+ * @param {number} timeout
87
+ * @param {number} intervalResolution
88
+ * @return {Promise<void>}
89
+ */
90
+ export const untilAsync = async (check, timeout = 0, intervalResolution = 10) => {
91
+ const startTime = time.getUnixTime()
92
+ const noTimeout = timeout <= 0
93
+ // eslint-disable-next-line no-unmodified-loop-condition
94
+ while (noTimeout || time.getUnixTime() - startTime <= timeout) {
95
+ if (await check()) return
96
+ await wait(intervalResolution)
97
+ }
98
+ throw new Error('Timeout')
99
+ }
100
+
83
101
  /**
84
102
  * @param {number} timeout
85
103
  * @return {Promise<undefined>}
86
104
  */
87
- export const wait = timeout => create((resolve, reject) => setTimeout(resolve, timeout))
105
+ export const wait = timeout => create((resolve, _reject) => setTimeout(resolve, timeout))
88
106
 
89
107
  /**
90
108
  * Checks if an object is a promise using ducktyping.
@@ -1 +1 @@
1
- {"version":3,"file":"promise.test.d.ts","sourceRoot":"","sources":["promise.test.js"],"names":[],"mappings":"AA6BO,uCAFI,EAAE,QAAQ,iBAgBpB;AAKM,mCAFI,EAAE,QAAQ,QAapB;AAKM,iCAFI,EAAE,QAAQ,iBAapB;mBA1EkB,cAAc"}
1
+ {"version":3,"file":"promise.test.d.ts","sourceRoot":"","sources":["promise.test.js"],"names":[],"mappings":"AA6BO,uCAFI,EAAE,QAAQ,iBAmBpB;AAKM,mCAFI,EAAE,QAAQ,QAapB;AAKM,iCAFI,EAAE,QAAQ,iBAapB;mBA7EkB,cAAc"}
package/test.js CHANGED
@@ -10,6 +10,7 @@ import * as encoding from './encoding.test.js'
10
10
  import * as diff from './diff.test.js'
11
11
  import * as testing from './testing.test.js'
12
12
  import * as indexeddb from './indexeddb.test.js'
13
+ import * as indexeddbV2 from './indexeddbV2.test.js'
13
14
  import * as prng from './prng.test.js'
14
15
  import * as log from 'lib0/logging'
15
16
  import * as statistics from './statistics.test.js'
@@ -23,6 +24,7 @@ import * as time from './time.test.js'
23
24
  import * as pair from './pair.test.js'
24
25
  import * as object from './object.test.js'
25
26
  import * as observable from './observable.test.js'
27
+ import * as pledge from './pledge.test.js'
26
28
  import * as math from './math.test.js'
27
29
  import * as number from './number.test.js'
28
30
  import * as buffer from './buffer.test.js'
@@ -55,6 +57,7 @@ runTests({
55
57
  diff,
56
58
  testing,
57
59
  indexeddb,
60
+ indexeddbV2,
58
61
  prng,
59
62
  statistics,
60
63
  binary,
@@ -67,6 +70,7 @@ runTests({
67
70
  pair,
68
71
  object,
69
72
  observable,
73
+ pledge,
70
74
  math,
71
75
  number,
72
76
  buffer,