ctx-core 5.3.1 → 5.4.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.
@@ -8,10 +8,11 @@ import { waitfor } from '../waitfor/index.js'
8
8
  export async function file_exists_(path) {
9
9
  return (
10
10
  (process_release_name ?? false)
11
- && import('node:fs/promises').then(({ access, constants })=>
12
- access(path, constants.F_OK)
11
+ && import('node:fs/promises').then(({ access, constants })=>{
12
+ return access(path, constants.F_OK)
13
13
  .then(()=>true)
14
- .catch(()=>false)))
14
+ .catch(()=>false)
15
+ }))
15
16
  }
16
17
  export {
17
18
  file_exists_ as path__exists_
@@ -0,0 +1,32 @@
1
+ import { test } from 'uvu'
2
+ import { equal } from 'uvu/assert'
3
+ import { run } from '../run/index.js'
4
+ import { sleep } from '../sleep/index.js'
5
+ import { promise_timeout } from './index.js'
6
+ test('promise_timeout|success', async ()=>{
7
+ let count = 0
8
+ const promise = promise_timeout(run(async ()=>{
9
+ count++
10
+ await sleep(10)
11
+ return true
12
+ }), 100)
13
+ equal(count, 1)
14
+ equal(await promise, true)
15
+ equal(count, 1)
16
+ })
17
+ test('promise_timeout|timeout', async ()=>{
18
+ let count = 0
19
+ let err:Error|undefined = undefined
20
+ try {
21
+ await promise_timeout(run(async ()=>{
22
+ count++
23
+ await sleep(10)
24
+ return false
25
+ }), 1)
26
+ } catch (_err) {
27
+ err = _err as Error
28
+ }
29
+ equal(count, 1)
30
+ equal(err?.message, 'Timeout 1ms')
31
+ })
32
+ test.run()
@@ -0,0 +1,9 @@
1
+ import { test } from 'uvu'
2
+ import { equal } from 'uvu/assert'
3
+ import { sleep } from './index.js'
4
+ test('sleep', async ()=>{
5
+ const now0 = performance.now()
6
+ await sleep(10)
7
+ equal(performance.now() >= now0 + 10, true)
8
+ })
9
+ test.run()
@@ -1,5 +1,5 @@
1
- export declare function waitfor(
2
- fn:()=>Promise<boolean>|boolean,
1
+ export declare function waitfor<V>(
2
+ fn:()=>Promise<V>|V,
3
3
  timeout:number,
4
4
  period?:number
5
- ):Promise<void>
5
+ ):Promise<V>&{ cancel: ()=>Promise<V> }
@@ -1,22 +1,31 @@
1
1
  import { promise_timeout } from '../promise_timeout/index.js'
2
2
  import { sleep } from '../sleep/index.js'
3
3
  /**
4
- * @param {()=>Promise<boolean>}fn
4
+ * @param {()=>Promise<unknown>}fn
5
5
  * @param {unknown}timeout
6
6
  * @param {unknown}[period]
7
7
  * @returns {Promise<void>}
8
8
  */
9
- export async function waitfor(fn, timeout, period = 0) {
9
+ export function waitfor(fn, timeout, period = 0) {
10
10
  let cancel
11
- try {
12
- await promise_timeout(async ()=>{
11
+ let promise = new Promise((resolve, reject)=>
12
+ promise_timeout(async ()=>{
13
+ let rv
13
14
  for (; !cancel;) {
14
- if (await fn()) return
15
+ rv = await fn()
16
+ if (rv) return rv
15
17
  await sleep(period)
16
18
  }
19
+ return rv
17
20
  }, timeout)
18
- } catch (err) {
21
+ .then(resolve)
22
+ .catch(err=>{
23
+ cancel = 1
24
+ reject(err)
25
+ }))
26
+ promise.cancel = ()=>{
19
27
  cancel = 1
20
- throw err
28
+ return promise
21
29
  }
30
+ return promise
22
31
  }
@@ -0,0 +1,49 @@
1
+ import { test } from 'uvu'
2
+ import { equal } from 'uvu/assert'
3
+ import { waitfor } from './index.js'
4
+ test('waitfor|success', async ()=>{
5
+ let count = 0
6
+ let ret = false
7
+ const returned_value_a:boolean[] = []
8
+ const promise = waitfor(()=>{
9
+ count++
10
+ return ret
11
+ }, 10)
12
+ .then(returned_value=>{
13
+ returned_value_a.push(returned_value)
14
+ return returned_value
15
+ })
16
+ equal(count, 1)
17
+ equal(returned_value_a, [])
18
+ ret = true
19
+ equal(await promise, true)
20
+ equal(count, 2)
21
+ equal(returned_value_a, [true])
22
+ })
23
+ test('waitfor|timeout', async ()=>{
24
+ let count = 0
25
+ let err:Error|undefined = undefined
26
+ try {
27
+ await waitfor(()=>{
28
+ count++
29
+ return false
30
+ }, 1)
31
+ } catch (_err) {
32
+ err = _err as Error
33
+ }
34
+ equal(count, 1)
35
+ equal(err?.message, 'Timeout 1ms')
36
+ })
37
+ test('waitfor|cancel', async ()=>{
38
+ let count = 0
39
+ const promise = waitfor(()=>{
40
+ count++
41
+ return false
42
+ }, 10)
43
+ equal(count, 1)
44
+ equal(await promise.cancel(), false)
45
+ equal(count, 1)
46
+ equal(await promise, false)
47
+ equal(count, 1)
48
+ })
49
+ test.run()
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ctx-core",
3
- "version": "5.3.1",
3
+ "version": "5.4.0",
4
4
  "description": "ctx-core core library",
5
5
  "keywords": [
6
6
  "ctx-core",
@@ -121,7 +121,7 @@
121
121
  "./package.json": "./package.json"
122
122
  },
123
123
  "devDependencies": {
124
- "@arethetypeswrong/cli": "^0.13.4",
124
+ "@arethetypeswrong/cli": "^0.13.5",
125
125
  "@ctx-core/preprocess": "^0.1.0",
126
126
  "@size-limit/preset-small-lib": "^11.0.1",
127
127
  "@types/node": "^20.10.5",