ctx-core 5.11.0 → 5.14.0

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 (58) hide show
  1. package/all/be/index.d.ts +3 -3
  2. package/all/be/index.test.ts +21 -0
  3. package/all/be_/index.js +7 -0
  4. package/all/be_/index.test.ts +9 -3
  5. package/all/bind_apply/index.d.ts +26 -6
  6. package/all/bind_apply/index.js +5 -5
  7. package/all/bind_apply/index.test.ts +19 -0
  8. package/all/bind_call/index.d.ts +22 -4
  9. package/all/bind_call/index.js +1 -1
  10. package/all/bind_call/index.test.ts +18 -0
  11. package/all/call/index.d.ts +1 -1
  12. package/all/ctx/index.d.ts +1 -1
  13. package/all/index.d.ts +2 -3
  14. package/all/index.js +2 -3
  15. package/all/nullish__guard__async/index.js +1 -1
  16. package/all/nullish__none/index.d.ts +1 -1
  17. package/all/rmemo__wait/index.js +2 -2
  18. package/all/run/index.d.ts +0 -1
  19. package/all/run/index.js +0 -1
  20. package/all/tap/index.d.ts +9 -11
  21. package/all/tap/index.test.ts +10 -0
  22. package/all/timeout/index.d.ts +3 -0
  23. package/all/timeout/index.js +8 -0
  24. package/all/timeout/index.test.ts +8 -0
  25. package/all/timeout_promise/index.d.ts +7 -6
  26. package/all/timeout_promise/index.js +28 -8
  27. package/all/{promise_timeout → timeout_promise}/index.test.ts +9 -7
  28. package/all/tup/index.d.ts +0 -28
  29. package/all/tuple/index.d.ts +28 -0
  30. package/all/tuple/index.js +1 -0
  31. package/all/tuple__o/index.d.ts +2 -2
  32. package/all/tuple__union/index.d.ts +1 -1
  33. package/all/tuple_a_o/index.d.ts +1 -1
  34. package/all/waitfor/index.d.ts +1 -0
  35. package/all/waitfor/index.js +3 -2
  36. package/all/waitfor_fibonacci_backoff/index.js +4 -1
  37. package/all/zip__tuple_a/index.d.ts +2 -2
  38. package/falsy/index.d.ts +4 -0
  39. package/falsy/index.js +4 -0
  40. package/function/index.d.ts +5 -4
  41. package/function/index.js +5 -4
  42. package/nullish/index.d.ts +6 -0
  43. package/nullish/index.js +6 -0
  44. package/package.json +249 -238
  45. package/promise/index.d.ts +11 -0
  46. package/promise/index.js +11 -0
  47. package/rmemo/index.d.ts +8 -0
  48. package/rmemo/index.js +8 -0
  49. package/run/index.d.ts +12 -0
  50. package/run/index.js +12 -0
  51. package/tuple/index.d.ts +6 -0
  52. package/tuple/index.js +6 -0
  53. package/all/Timeout/index.d.ts +0 -1
  54. package/all/Timeout/index.js +0 -1
  55. package/all/promise_timeout/index.d.ts +0 -6
  56. package/all/promise_timeout/index.js +0 -30
  57. package/all/waitfor_val/index.d.ts +0 -6
  58. package/all/waitfor_val/index.js +0 -20
package/all/be/index.d.ts CHANGED
@@ -1,11 +1,11 @@
1
1
  import type { be__val__new_T, be_config_T, Ctx, Ctx_wide_T } from '../be_/index.js'
2
2
  export declare function be<
3
- Out extends NonNullable<unknown>,
3
+ val_T extends NonNullable<unknown>,
4
4
  ns_T extends string = '',
5
5
  ctx_T extends Ctx = Ctx_wide_T<ns_T>
6
6
  >(
7
7
  ctx:ctx_T,
8
- val_:be__val__new_T<Out, ns_T, ctx_T>,
8
+ val_:be__val__new_T<val_T, ns_T, ctx_T>,
9
9
  config:be_config_T<ns_T>
10
- ):Out
10
+ ):val_T
11
11
  export { be as b, }
@@ -0,0 +1,21 @@
1
+ import { test } from 'uvu'
2
+ import { equal } from 'uvu/assert'
3
+ import { be_ } from '../be_/index.js'
4
+ import { ctx__new } from '../ctx/index.js'
5
+ import { be } from './index.js'
6
+ test('be|s|BeMap', ()=>{
7
+ const ctx = ctx__new()
8
+ let incrementer_num = 0
9
+ const incrementer = ()=>++incrementer_num
10
+ const root_ = be_(()=>
11
+ incrementer(),
12
+ { id: 'root_' })
13
+ const child = be(ctx, ctx=>
14
+ root_(ctx) + incrementer(),
15
+ { id: 'child_' })
16
+ equal(root_(ctx), 1)
17
+ equal(ctx.s[''].get('root_')![0], 1)
18
+ equal(child, 3)
19
+ equal(ctx.s[''].get('child_')![0], 3)
20
+ })
21
+ test.run()
package/all/be_/index.js CHANGED
@@ -132,6 +132,12 @@ export function be_map__find(be_or_id, ctx, ns = be_or_id.ns ?? '') {
132
132
  export function be__val_(be_or_id, ctx, ns) {
133
133
  return be_map__find(be_or_id, ctx, ns)?.get?.(be_or_id.id ?? be_or_id)?.[0]
134
134
  }
135
+ /**
136
+ * @param {be__val__new_T}val__new
137
+ * @param {be_config_T}config
138
+ * @returns {Be}
139
+ * @private
140
+ */
135
141
  export function ondelete_be_(val__new, config) {
136
142
  let be = be_(val__new, config)
137
143
  let ondelete_cb_a = []
@@ -140,6 +146,7 @@ export function ondelete_be_(val__new, config) {
140
146
  for (let ondelete_cb of ondelete_cb_a) {
141
147
  ondelete_cb(...state)
142
148
  }
149
+ ondelete_cb_a = []
143
150
  }
144
151
  return be
145
152
  }
@@ -22,9 +22,9 @@ import {
22
22
  globalThis__be_,
23
23
  ondelete_be_
24
24
  } from '../be_/index.js'
25
- import { ctx_, ctx__new, ns_ctx__new } from '../ctx/index.js'
25
+ import { ctx__new, ns_ctx__new } from '../ctx/index.js'
26
26
  import { tempfile_path_ } from '../tempfile_path/index.js'
27
- import { Equal, Expect } from '../test/index.js'
27
+ import type { Equal, Expect } from '../test/index.js'
28
28
  test.after(()=>{
29
29
  delete (globalThis as { root_be?:Be<unknown> }).root_be
30
30
  })
@@ -165,7 +165,7 @@ test('be_|Ctx generic type', ()=>{
165
165
  { id: 'val_', ns: 'test_ns' })
166
166
  val_(valid_ctx)
167
167
  // @ts-expect-error TS2322
168
- throws(()=>val_(ctx_()))
168
+ throws(()=>val_(ctx__new()))
169
169
  })
170
170
  test('be_|Ctx|ns', ()=>{
171
171
  const ctx0 = ctx__new()
@@ -425,6 +425,12 @@ test('ondelete_be_', ()=>{
425
425
  ctx__delete(ctx, be)
426
426
  equal(ondelete0_arg_aa, [[1, ctx, be]])
427
427
  equal(ondelete1_arg_aa, [[1, ctx, be]])
428
+ equal(be(ctx), 1)
429
+ equal(ondelete0_arg_aa, [[1, ctx, be]])
430
+ equal(ondelete1_arg_aa, [[1, ctx, be]])
431
+ ctx__delete(ctx, be)
432
+ equal(ondelete0_arg_aa, [[1, ctx, be], [1, ctx, be]])
433
+ equal(ondelete1_arg_aa, [[1, ctx, be], [1, ctx, be]])
428
434
  })
429
435
  test('ctx__clear', ()=>{
430
436
  const ondelete0_arg_aa:[val:number, ctx:Ctx, be:Be<number, ''|'test_ns'>][] = []
@@ -4,14 +4,34 @@ import type { call_fn_T } from '../call/index.js'
4
4
  */
5
5
  export declare function bind_apply_<
6
6
  Self,
7
- A_outer = unknown,
8
- A_inner extends unknown[] = unknown[],
9
- Out = unknown
7
+ A extends unknown[] = unknown[],
8
+ in_A extends unknown[] = unknown[],
9
+ O = unknown
10
10
  >(
11
- fn:call_fn_T<A_inner, Out>, self:Self, in_arg_a?:A_outer
12
- ):ReturnType<call_fn_T<A_inner, Out>>
11
+ fn:call_fn_T<A, O>,
12
+ self:Self,
13
+ in_arg_a?:bind_apply__in_arg_a_T<A, in_A>
14
+ ):(...arg_a:bind_apply__out_arg_a_T<A, in_A>)=>O
13
15
  export {
14
16
  bind_apply_ as _bind_apply,
15
17
  bind_apply_ as _apply__bind,
16
18
  }
17
-
19
+ export type bind_apply__in_arg_a_T<
20
+ A extends unknown[],
21
+ in_A extends unknown[]
22
+ > =
23
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
24
+ A extends [...in_A, ...infer out_a]
25
+ ? in_A
26
+ : never
27
+ export type bind_apply__out_arg_a_T<
28
+ A extends unknown[],
29
+ in_A extends unknown[]
30
+ > =
31
+ A extends [...in_A, ...infer out_A]
32
+ ? [...in_A, ...out_A] extends A
33
+ ? out_A
34
+ : in_A extends unknown[]
35
+ ? A
36
+ : never
37
+ : never
@@ -2,14 +2,14 @@
2
2
  /**
3
3
  * Returns function bound to self that applies arg_a with ...arg_a_
4
4
  * @param {call_fn_T}fn
5
- * @param {unknown}self
5
+ * @param {unknown}_this
6
6
  * @param {unknown}[in_arg_a]
7
7
  * @returns {(fn:call_fn_T, unknown, in_arg_a?:unknown[])=>ReturnType<call_fn_T>}
8
8
  */
9
- export function bind_apply_(fn, _self, _in_arg_a) {
10
- return (find, self, in_arg_a)=>fn.apply(self ?? _self, [
11
- ...in_arg_a,
12
- ...(fn_arg_a ?? _in_arg_a)
9
+ export function bind_apply_(fn, _this, in_arg_a) {
10
+ return (..._in_arg_a)=>fn.apply(_this, [
11
+ ...in_arg_a ?? [],
12
+ ..._in_arg_a
13
13
  ])
14
14
  }
15
15
  export {
@@ -0,0 +1,19 @@
1
+ import { test } from 'uvu'
2
+ import { equal } from 'uvu/assert'
3
+ import { tup } from '../tup/index.js'
4
+ import { bind_apply_ } from './index.js'
5
+ test('bind_apply_', ()=>{
6
+ function fn(this:unknown, a:number, b:number, c:number, d:number) {
7
+ return [this, a, b, c, d]
8
+ }
9
+ const _this = { foo: 'bar' }
10
+ equal(bind_apply_(fn, _this)(0, 1, 2, 3), [_this, 0, 1, 2, 3])
11
+ equal(bind_apply_(fn, _this, tup(0, 1))(2, 3), [_this, 0, 1, 2, 3])
12
+ // @ts-expect-error TS2345
13
+ equal(bind_apply_(fn, _this)(0, 1, 2, 3, 4), [_this, 0, 1, 2, 3])
14
+ // @ts-expect-error TS2345
15
+ equal(bind_apply_(fn, _this, tup(0, 1))(2, 3, 4), [_this, 0, 1, 2, 3])
16
+ // @ts-expect-error TS2345
17
+ equal(bind_apply_(fn, _this, tup(0, 1, 2, 3, 4))(), [_this, 0, 1, 2, 3])
18
+ })
19
+ test.run()
@@ -7,14 +7,32 @@ export {
7
7
  * Returns function bound to self that calls ...in_arg_a concat with ...fn_arg_a passed to function
8
8
  */
9
9
  export declare function bind_call_<
10
+ Self,
10
11
  A extends unknown[] = unknown[],
11
- O = unknown,
12
- S = unknown
12
+ in_A extends unknown[] = unknown[],
13
+ Out = unknown
13
14
  >(
14
- fn:call_fn_T<A, O>, self:S, ...in_arg_a:A[]
15
- ):ReturnType<call_fn_T<A, O>>
15
+ fn:call_fn_T<A, Out>,
16
+ self:Self,
17
+ ...in_arg_a:bind_call__in_arg_a_T<A, in_A>
18
+ ):(...arg_a:[...bind_call__out_arg_a_T<A, in_A>])=>Out
16
19
  export declare type bind_call_T<
17
20
  A extends readonly unknown[] = readonly unknown[],
18
21
  O = unknown
19
22
  > = (...fn_arg_a:A)=>O
20
23
  export declare type bind_call_type = bind_call_T
24
+ export type bind_call__in_arg_a_T<
25
+ A extends unknown[],
26
+ in_A extends unknown[]
27
+ > =
28
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
29
+ A extends [...in_A, ...infer Right]
30
+ ? in_A
31
+ : never
32
+ export type bind_call__out_arg_a_T<
33
+ A extends unknown[],
34
+ in_A extends unknown[]
35
+ > =
36
+ A extends [...in_A, ...infer out_A]
37
+ ? out_A
38
+ : never
@@ -7,7 +7,7 @@
7
7
  * @returns {(fn:call_fn_T, self:unknown, ...in_arg_a:unknown[])=>ReturnType<call_fn_T>}
8
8
  */
9
9
  export function bind_call_(fn, self, ...in_arg_a) {
10
- return (...fn_arg_a)=>fn.call(self, ...[
10
+ return (...fn_arg_a)=>fn.apply(self, [
11
11
  ...in_arg_a,
12
12
  ...fn_arg_a
13
13
  ])
@@ -0,0 +1,18 @@
1
+ import { test } from 'uvu'
2
+ import { equal } from 'uvu/assert'
3
+ import { bind_call_ } from './index.js'
4
+ test('bind_call_', ()=>{
5
+ function fn(this:unknown, a:number, b:number, c:number, d:number) {
6
+ return [this, a, b, c, d]
7
+ }
8
+ const _this = { foo: 'bar' }
9
+ equal(bind_call_(fn, _this)(0, 1, 2, 3), [_this, 0, 1, 2, 3])
10
+ equal(bind_call_(fn, _this, 0, 1)(2, 3), [_this, 0, 1, 2, 3])
11
+ // @ts-expect-error TS2345
12
+ equal(bind_call_(fn, _this)(0, 1, 2, 3, 4), [_this, 0, 1, 2, 3])
13
+ // @ts-expect-error TS2345
14
+ equal(bind_call_(fn, _this, 0, 1)(2, 3, 4), [_this, 0, 1, 2, 3])
15
+ // @ts-expect-error TS2345
16
+ equal(bind_call_(fn, _this, 0, 1, 2, 3, 4)(), [_this, 0, 1, 2, 3])
17
+ })
18
+ test.run()
@@ -18,5 +18,5 @@ export { call_ as _call, }
18
18
  export declare type call_fn_T<
19
19
  A extends readonly unknown[] = readonly unknown[],
20
20
  O = unknown
21
- > = (...args:A)=>O
21
+ > = (...arg_a:A)=>O
22
22
  export declare type call_fn_type = call_fn_T
@@ -1,5 +1,5 @@
1
1
  import type { Ctx } from '../be_/index.js'
2
- import { TupleToUnion } from '../tup/index.js'
2
+ import type { TupleToUnion } from '../tuple/index.js'
3
3
  export declare function ctx__new():Ctx<''>
4
4
  export { ctx__new as ctx_ }
5
5
  export declare function ns_ctx__new<ns0_T extends string|Ctx, ns_a_T extends (string|Ctx)[]>(
package/all/index.d.ts CHANGED
@@ -14,7 +14,6 @@ export * from './NumericUnit/index.js'
14
14
  export * from './PHI/index.js'
15
15
  export * from './Request/index.js'
16
16
  export * from './Response/index.js'
17
- export * from './Timeout/index.js'
18
17
  export * from './VERSION/index.js'
19
18
  export * from './a/index.js'
20
19
  export * from './a_assign/index.js'
@@ -341,7 +340,6 @@ export * from './prev_idx/index.js'
341
340
  export * from './primitive/index.js'
342
341
  export * from './promise/index.js'
343
342
  export * from './promise_o/index.js'
344
- export * from './promise_timeout/index.js'
345
343
  export * from './prototype/index.js'
346
344
  export * from './prototype_mixin/index.js'
347
345
  export * from './push/index.js'
@@ -406,6 +404,7 @@ export * from './throttle/index.js'
406
404
  export * from './tick/index.js'
407
405
  export * from './timedout/index.js'
408
406
  export * from './timedout/index.js'
407
+ export * from './timeout/index.js'
409
408
  export * from './timeout_promise/index.js'
410
409
  export * from './times/index.js'
411
410
  export * from './timestamp_ms/index.js'
@@ -413,6 +412,7 @@ export * from './timestamp_ms/index.js'
413
412
  export * from './toLocalDateString/index.js'
414
413
  export * from './toString/index.js'
415
414
  export * from './tup/index.js'
415
+ export * from './tuple/index.js'
416
416
  export * from './tuple__o/index.js'
417
417
  export * from './tuple__union/index.js'
418
418
  export * from './tuple_a_o/index.js'
@@ -442,7 +442,6 @@ export * from './valueof/index.js'
442
442
  export * from './values/index.js'
443
443
  export * from './waitfor/index.js'
444
444
  export * from './waitfor_fibonacci_backoff/index.js'
445
- export * from './waitfor_val/index.js'
446
445
  export * from './weak_r/index.js'
447
446
  export * from './week/index.js'
448
447
  export * from './wrap_a/index.js'
package/all/index.js CHANGED
@@ -14,7 +14,6 @@ export * from './NumericUnit/index.js'
14
14
  export * from './PHI/index.js'
15
15
  export * from './Request/index.js'
16
16
  export * from './Response/index.js'
17
- export * from './Timeout/index.js'
18
17
  export * from './VERSION/index.js'
19
18
  export * from './a/index.js'
20
19
  export * from './a_assign/index.js'
@@ -341,7 +340,6 @@ export * from './prev_idx/index.js'
341
340
  export * from './primitive/index.js'
342
341
  export * from './promise/index.js'
343
342
  export * from './promise_o/index.js'
344
- export * from './promise_timeout/index.js'
345
343
  export * from './prototype/index.js'
346
344
  export * from './prototype_mixin/index.js'
347
345
  export * from './push/index.js'
@@ -406,6 +404,7 @@ export * from './throttle/index.js'
406
404
  export * from './tick/index.js'
407
405
  export * from './timedout/index.js'
408
406
  export * from './timedout/index.js'
407
+ export * from './timeout/index.js'
409
408
  export * from './timeout_promise/index.js'
410
409
  export * from './times/index.js'
411
410
  export * from './timestamp_ms/index.js'
@@ -413,6 +412,7 @@ export * from './timestamp_ms/index.js'
413
412
  export * from './toLocalDateString/index.js'
414
413
  export * from './toString/index.js'
415
414
  export * from './tup/index.js'
415
+ export * from './tuple/index.js'
416
416
  export * from './tuple__o/index.js'
417
417
  export * from './tuple__union/index.js'
418
418
  export * from './tuple_a_o/index.js'
@@ -442,7 +442,6 @@ export * from './valueof/index.js'
442
442
  export * from './values/index.js'
443
443
  export * from './waitfor/index.js'
444
444
  export * from './waitfor_fibonacci_backoff/index.js'
445
- export * from './waitfor_val/index.js'
446
445
  export * from './weak_r/index.js'
447
446
  export * from './week/index.js'
448
447
  export * from './wrap_a/index.js'
@@ -10,7 +10,7 @@ export async function nullish__guard__async(
10
10
  const rv = await fn()
11
11
  if (rv != null) return rv
12
12
  }
13
- throw new Error(`${label} cannot be nullish`)
13
+ throw Error(`${label} cannot be nullish`)
14
14
  }
15
15
  export {
16
16
  nullish__guard__async as nullish_async_guard,
@@ -1,5 +1,5 @@
1
1
  import type { nullish } from '../nullish/index.js'
2
- import type { TupleExclude } from '../tup/index.js'
2
+ import type { TupleExclude } from '../tuple/index.js'
3
3
  export function nullish__none_<T extends unknown[], U>(
4
4
  $:T,
5
5
  _:(...$:TupleExclude<T, nullish>)=>U,
@@ -1,6 +1,6 @@
1
1
  /// <reference types="../rmemo/index.d.ts" />
2
2
  /// <reference types="./index.d.ts" />
3
- import { promise_timeout } from '../promise_timeout/index.js'
3
+ import { timeout_promise } from '../timeout_promise/index.js'
4
4
  import { memo_ } from '../rmemo/index.js'
5
5
  /**
6
6
  * @param {rmemo_T}rmemo
@@ -26,7 +26,7 @@ export function rmemo__wait(
26
26
  })
27
27
  /** @type {cancel_Promise&{ m:memo_T<unknown> }} */
28
28
  let promise =
29
- promise_timeout(
29
+ timeout_promise(
30
30
  _subscribe_wait,
31
31
  timeout ?? Infinity,
32
32
  error)
@@ -6,4 +6,3 @@ export declare function run<O, A extends unknown[] = unknown[]>(
6
6
  arg_a:A,
7
7
  fn:(...arg_a:A)=>O
8
8
  ):O
9
- export { run as _ }
package/all/run/index.js CHANGED
@@ -10,4 +10,3 @@ export function run(...arg_a) {
10
10
  : arg_a[1](...arg_a[0])
11
11
  )
12
12
  }
13
- export { run as _ }
@@ -1,20 +1,18 @@
1
1
  /**
2
- * Invokes interceptor with the obj, and then returns the object.
2
+ * Invokes interceptor with the val, and then returns the object.
3
3
  * The primary purpose of this method is to "tap into" a method chain,
4
4
  * to perform operations on intermediate results within the chain.
5
5
  * @see {@link https://underscorejs.org/#tap}
6
6
  */
7
- export declare function tap<Obj>(
8
- obj:Obj, interceptor:tap__interceptor_T<Obj>
9
- ):Obj
10
- export declare type tap__interceptor_T<T = unknown> = (obj:T)=>void
11
- export declare type tap_interceptor_T<T = unknown> = tap__interceptor_T<T>
7
+ export declare function tap<V>(
8
+ val:V, interceptor:tap__interceptor_T<V>
9
+ ):V
10
+ export declare type tap__interceptor_T<V = unknown> = (Val:V)=>void
11
+ export declare type tap_interceptor_T<V = unknown> = tap__interceptor_T<V>
12
12
  /**
13
13
  * Returns function that calls tap with obj.
14
14
  */
15
- export declare function tap_<
16
- Val = unknown
17
- >(
18
- fn:tap__interceptor_T<Val>
19
- ):(val:Val)=>Val
15
+ export declare function tap_<V = unknown>(
16
+ fn:tap__interceptor_T<V>
17
+ ):(val:V)=>V
20
18
  export { tap_ as _tap, }
@@ -0,0 +1,10 @@
1
+ import { test } from 'uvu'
2
+ import { equal } from 'uvu/assert'
3
+ import { tap } from './index.js'
4
+ test('tap', ()=>{
5
+ const o = {} as { foo?:string }
6
+ equal(tap(o, _o=>{
7
+ _o.foo = 'bar'
8
+ }), { foo: 'bar' })
9
+ })
10
+ test.run()
@@ -0,0 +1,3 @@
1
+ export declare class Timeout extends Error {
2
+ constructor(ms_OR_message:number|string)
3
+ }
@@ -0,0 +1,8 @@
1
+ export class Timeout extends Error {
2
+ constructor(ms_OR_message) {
3
+ super(
4
+ typeof ms_OR_message === 'number'
5
+ ? 'Timeout ' + ms_OR_message + 'ms'
6
+ : ms_OR_message)
7
+ }
8
+ }
@@ -0,0 +1,8 @@
1
+ import { test } from 'uvu'
2
+ import { equal } from 'uvu/assert'
3
+ import { Timeout } from './index.js'
4
+ test('Timeout', ()=>{
5
+ equal(new Timeout(10).message, 'Timeout 10ms')
6
+ equal(new Timeout('custom message').message, 'custom message')
7
+ })
8
+ test.run()
@@ -1,6 +1,7 @@
1
- /**
2
- * @see {@link https://italonascimento.github.io/applying-a-timeout-to-your-promises/}
3
- * @see {@link http://disq.us/p/1k8w63m}
4
- */
5
- export declare function timeout_promise<O>(ms:number, promise:Promise<O>):Promise<O>
6
- export { timeout_promise as _timeout_promise }
1
+ import type { cancel_Promise } from '../promise/index.js'
2
+ export declare function timeout_promise<O>(
3
+ promise:(()=>Promise<O>)|Promise<O>,
4
+ ms:number,
5
+ error?:Error
6
+ ):cancel_Promise<O, (val?:O)=>Promise<O>>
7
+ export { timeout_promise as promise_timeout }
@@ -1,12 +1,32 @@
1
- import { promise_timeout } from '../promise_timeout/index.js'
1
+ import { Timeout } from '../timeout/index.js'
2
2
  /**
3
+ * @param {(()=>Promise<unknown>)|Promise<unknown>}promise
3
4
  * @param {number}ms
4
- * @param {Promise<unknown>}promise
5
- * @returns {Promise<unknown>}
6
- * @see {@link https://italonascimento.github.io/applying-a-timeout-to-your-promises/}
7
- * @see {@link http://disq.us/p/1k8w63m}
5
+ * @param {Error}[error]
6
+ * @returns {cancel_Promise}
8
7
  */
9
- export async function timeout_promise(ms, promise) {
10
- return await promise_timeout(promise, ms)
8
+ export function timeout_promise(
9
+ promise,
10
+ ms,
11
+ error
12
+ ) {
13
+ error ??= new Timeout(ms)
14
+ let id
15
+ let timeout = new Promise((_resolve, reject)=>{
16
+ id = setTimeout(()=>reject(error), ms)
17
+ })
18
+ let cancel_promise__resolve
19
+ let cancel_promise = new Promise(resolve=>cancel_promise__resolve = resolve)
20
+ /** @type {cancel_Promise} */
21
+ let ret_promise = Promise.race([
22
+ typeof promise === 'function' ? promise() : promise,
23
+ timeout,
24
+ cancel_promise,
25
+ ]).then(result=>{
26
+ clearTimeout(id)
27
+ return result
28
+ })
29
+ ret_promise.cancel = cancel_promise__resolve
30
+ return ret_promise
11
31
  }
12
- export { timeout_promise as _timeout_promise }
32
+ export { timeout_promise as promise_timeout }
@@ -2,10 +2,11 @@ import { test } from 'uvu'
2
2
  import { equal } from 'uvu/assert'
3
3
  import { run } from '../run/index.js'
4
4
  import { sleep } from '../sleep/index.js'
5
- import { promise_timeout } from './index.js'
6
- test('promise_timeout|success', async ()=>{
5
+ import { Timeout } from '../timeout/index.js'
6
+ import { timeout_promise } from './index.js'
7
+ test('timeout_promise|success', async ()=>{
7
8
  let count = 0
8
- const promise = promise_timeout(run(async ()=>{
9
+ const promise = timeout_promise(run(async ()=>{
9
10
  count++
10
11
  await sleep(10)
11
12
  return true
@@ -14,11 +15,11 @@ test('promise_timeout|success', async ()=>{
14
15
  equal(await promise, true)
15
16
  equal(count, 1)
16
17
  })
17
- test('promise_timeout|timeout', async ()=>{
18
+ test('timeout_promise|timeout', async ()=>{
18
19
  let count = 0
19
20
  let err:Error|undefined = undefined
20
21
  try {
21
- await promise_timeout(run(async ()=>{
22
+ await timeout_promise(run(async ()=>{
22
23
  count++
23
24
  await sleep(10)
24
25
  return false
@@ -27,10 +28,11 @@ test('promise_timeout|timeout', async ()=>{
27
28
  err = _err as Error
28
29
  }
29
30
  equal(count, 1)
31
+ equal(err instanceof Timeout, true)
30
32
  equal(err?.message, 'Timeout 1ms')
31
33
  })
32
- test('promise_timeout|cancel|arg', async ()=>{
33
- const promise = promise_timeout(
34
+ test('timeout_promise|cancel|arg', async ()=>{
35
+ const promise = timeout_promise(
34
36
  new Promise<number>(()=>{}),
35
37
  1000)
36
38
  promise.cancel(5)
@@ -11,31 +11,3 @@ export {
11
11
  tup as _tup,
12
12
  tup as tup_fn,
13
13
  }
14
- export type TupleRest<T extends unknown[]> =
15
- T['length'] extends 0
16
- ? undefined
17
- : (((...b:T)=>void) extends (a:unknown, ...b:infer I)=>void ? I : [])
18
- export type TupleFirst<T extends unknown[]> =
19
- T['length'] extends 0 ? undefined : T[0]
20
- export type TupleExclude<T extends unknown[], E> =
21
- T extends []
22
- ? [Exclude<T, E>]
23
- : T extends [infer Head, ...infer Tail]
24
- ? [Exclude<Head, E>, ...TupleExclude<Tail, E>]
25
- : Exclude<T[number], E>[]
26
- export type TupleMemberExtends<T extends Readonly<unknown[]>, E> =
27
- T extends Readonly<[infer Head, ...infer Tail]>
28
- ? Head extends E
29
- ? true
30
- : Tail extends unknown[]
31
- ? TupleMemberExtends<Tail, E>
32
- : { ERROR:'Tail is not an array' }
33
- : { ERROR:'Head does not extend E' }
34
- export type TupleValues<T extends Readonly<unknown[]>> =
35
- T extends Readonly<[infer Head, ...infer Tail]>
36
- ? [Head, ...Tail]
37
- : { ERROR:'Unable to infer Head & Tail from T' }
38
- export type TupleConcat<T> = T extends [infer A extends Readonly<unknown[]>, ...infer Rest extends Readonly<unknown[]>]
39
- ? A extends unknown[] ? [...A, ...TupleConcat<Rest>] : A
40
- : T
41
- export type TupleToUnion<T extends unknown[]> = T[number]
@@ -0,0 +1,28 @@
1
+ export type TupleRest<T extends unknown[]> =
2
+ T['length'] extends 0
3
+ ? undefined
4
+ : (((...b:T)=>void) extends (a:unknown, ...b:infer I)=>void ? I : [])
5
+ export type TupleFirst<T extends unknown[]> =
6
+ T['length'] extends 0 ? undefined : T[0]
7
+ export type TupleExclude<T extends unknown[], E> =
8
+ T extends []
9
+ ? [Exclude<T, E>]
10
+ : T extends [infer Head, ...infer Tail]
11
+ ? [Exclude<Head, E>, ...TupleExclude<Tail, E>]
12
+ : Exclude<T[number], E>[]
13
+ export type TupleMemberExtends<T extends Readonly<unknown[]>, E> =
14
+ T extends Readonly<[infer Head, ...infer Tail]>
15
+ ? Head extends E
16
+ ? true
17
+ : Tail extends unknown[]
18
+ ? TupleMemberExtends<Tail, E>
19
+ : { ERROR:'Tail is not an array' }
20
+ : { ERROR:'Head does not extend E' }
21
+ export type TupleValues<T extends Readonly<unknown[]>> =
22
+ T extends Readonly<[infer Head, ...infer Tail]>
23
+ ? [Head, ...Tail]
24
+ : { ERROR:'Unable to infer Head & Tail from T' }
25
+ export type TupleConcat<T> = T extends [infer A extends Readonly<unknown[]>, ...infer Rest extends Readonly<unknown[]>]
26
+ ? A extends unknown[] ? [...A, ...TupleConcat<Rest>] : A
27
+ : T
28
+ export type TupleToUnion<T extends unknown[]> = T[number]
@@ -0,0 +1 @@
1
+ export {}
@@ -1,7 +1,7 @@
1
1
  export type tuple__o_T<
2
- T extends readonly any[],
2
+ T extends readonly unknown[],
3
3
  Key extends keyof T[number],
4
4
  Val extends keyof T[number]
5
5
  > = {
6
- [P in Exclude<keyof T, keyof readonly any[]> as T[P][Key]&string]:T[P][Val]
6
+ [P in Exclude<keyof T, keyof readonly unknown[]> as T[P][Key]&string]:T[P][Val]
7
7
  }
@@ -1 +1 @@
1
- export type tuple__union_T<T extends any[]> = T[number]
1
+ export type tuple__union_T<T extends unknown[]> = T[number]
@@ -4,7 +4,7 @@ import type { zip__tuple_a_T } from '../zip__tuple_a/index.js'
4
4
  */
5
5
  export type tuple_a_o_T<
6
6
  K extends readonly PropertyKey[],
7
- V extends readonly any[]
7
+ V extends readonly unknown[]
8
8
  > = {
9
9
  [T in zip__tuple_a_T<K, V>[number] as T[0]]:T[1]
10
10
  }
@@ -4,6 +4,7 @@ export declare function waitfor<V>(
4
4
  timeout:number,
5
5
  period?:number|((promise:waitfor_Promise<V>)=>Promise<number>)
6
6
  ):waitfor_Promise<V>
7
+ export { waitfor as waitfor_val, waitfor as waitfor_val_ }
7
8
  export declare function cancel__period_<V>(
8
9
  ms:number,
9
10
  should_cancel_:()=>boolean