ctx-core 5.6.1 → 5.8.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.
@@ -2,4 +2,4 @@ export declare function promise_timeout<O>(
2
2
  promise:(()=>Promise<O>)|Promise<O>,
3
3
  ms:number,
4
4
  error?:Error
5
- ):Promise<O>
5
+ ):Promise<O>&{ cancel:(val:O)=>void }
@@ -4,20 +4,26 @@
4
4
  * @param {Error}[error]
5
5
  * @returns {Promise<unknown>}
6
6
  */
7
- export async function promise_timeout(
7
+ export function promise_timeout(
8
8
  promise,
9
9
  ms,
10
- error = new Error(`Timeout ${ms}ms`),
10
+ error
11
11
  ) {
12
+ error ??= Error(`Timeout ${ms}ms`)
12
13
  let id
13
14
  let timeout = new Promise((_resolve, reject)=>{
14
15
  id = setTimeout(()=>reject(error), ms)
15
16
  })
16
- return Promise.race([
17
+ let cancel_promise__resolve
18
+ let cancel_promise = new Promise(resolve=>cancel_promise__resolve = resolve)
19
+ let ret_promise = Promise.race([
17
20
  typeof promise === 'function' ? promise() : promise,
18
- timeout
21
+ timeout,
22
+ cancel_promise,
19
23
  ]).then(result=>{
20
24
  clearTimeout(id)
21
25
  return result
22
26
  })
27
+ ret_promise.cancel = cancel_promise__resolve
28
+ return ret_promise
23
29
  }
@@ -29,4 +29,11 @@ test('promise_timeout|timeout', async ()=>{
29
29
  equal(count, 1)
30
30
  equal(err?.message, 'Timeout 1ms')
31
31
  })
32
+ test('promise_timeout|cancel|arg', async ()=>{
33
+ const promise = promise_timeout(
34
+ new Promise<number>(()=>{}),
35
+ 1000)
36
+ promise.cancel(5)
37
+ equal(await promise, 5)
38
+ })
32
39
  test.run()
@@ -4,8 +4,10 @@ export declare function rmemo__wait<
4
4
  >(
5
5
  memo:_rmemo_T,
6
6
  condition_fn:(val:rmemo_val_T<_rmemo_T>)=>unknown,
7
- timeout?:number
7
+ timeout?:number,
8
+ error?:Error
8
9
  ):Promise<rmemo_val_T<_rmemo_T>>&{
9
10
  // prevent early usGC
10
11
  m:memo_T<unknown>
12
+ cancel:(val:rmemo_val_T<_rmemo_T>)=>void
11
13
  }
@@ -1,15 +1,20 @@
1
1
  /// <reference types="../rmemo/index.d.ts" />
2
2
  /// <reference types="./index.d.ts" />
3
- import { isNumber_ } from '../isNumber/index.js'
4
3
  import { promise_timeout } from '../promise_timeout/index.js'
5
4
  import { memo_ } from '../rmemo/index.js'
6
5
  /**
7
6
  * @param {rmemo_T}rmemo
8
7
  * @param {(val:unknown)=>unknown}condition_fn
9
8
  * @param {number}[timeout]
9
+ * @param {Error}[error]
10
10
  * @returns {Promise<*>|Promise<unknown>}
11
11
  */
12
- export function rmemo__wait(rmemo, condition_fn, timeout) {
12
+ export function rmemo__wait(
13
+ rmemo,
14
+ condition_fn,
15
+ timeout,
16
+ error
17
+ ) {
13
18
  let memo
14
19
  const _subscribe_wait = new Promise(resolve=>{
15
20
  memo = memo_(()=>{
@@ -20,9 +25,10 @@ export function rmemo__wait(rmemo, condition_fn, timeout) {
20
25
  memo()
21
26
  })
22
27
  let promise =
23
- isNumber_(timeout)
24
- ? promise_timeout(_subscribe_wait, timeout)
25
- : _subscribe_wait
28
+ promise_timeout(
29
+ _subscribe_wait,
30
+ timeout ?? Infinity,
31
+ error)
26
32
  // prevent GC
27
33
  promise.m = memo
28
34
  return promise
@@ -15,4 +15,13 @@ test('rmemo__wait', async ()=>{
15
15
  await promise
16
16
  equal(subject$(), 1)
17
17
  })
18
+ test('rmemo__wait|cancel', async ()=>{
19
+ const subject$ = sig_(-1)
20
+ const promise = rmemo__wait(
21
+ subject$,
22
+ subject=>subject >= 0,
23
+ 10_000)
24
+ promise.cancel(500)
25
+ equal(await promise, 500)
26
+ })
18
27
  test.run()
@@ -2,4 +2,4 @@ export declare function waitfor<V>(
2
2
  fn:()=>Promise<V>|V,
3
3
  timeout:number,
4
4
  period?:number
5
- ):Promise<V>&{ cancel: ()=>Promise<V> }
5
+ ):Promise<V>&{ cancel:()=>Promise<V> }
package/package.json CHANGED
@@ -1,241 +1,240 @@
1
1
  {
2
- "name": "ctx-core",
3
- "version": "5.6.1",
4
- "description": "ctx-core core library",
5
- "keywords": [
6
- "ctx-core",
7
- "array",
8
- "combinators",
9
- "function",
10
- "object",
11
- "set"
12
- ],
13
- "homepage": "https://github.com/ctx-core/ctx-core#readme",
14
- "bugs": {
15
- "url": "https://github.com/ctx-core/ctx-core/issues"
16
- },
17
- "repository": {
18
- "type": "git",
19
- "url": "https://github.com/ctx-core/ctx-core.git"
20
- },
21
- "license": "Apache-2.0",
22
- "author": "Brian Takita",
23
- "type": "module",
24
- "files": [
25
- "*.d.ts",
26
- "*.js",
27
- "*.json",
28
- "all",
29
- "array",
30
- "atob",
31
- "base16",
32
- "be",
33
- "btoa",
34
- "buffer",
35
- "chain",
36
- "class",
37
- "cli-args",
38
- "color",
39
- "combinators",
40
- "crypto",
41
- "currency",
42
- "data",
43
- "date",
44
- "debounce",
45
- "deep_equal",
46
- "env",
47
- "error",
48
- "fetch",
49
- "fibonacci",
50
- "fs",
51
- "function",
52
- "functional",
53
- "html",
54
- "http",
55
- "math",
56
- "matrix",
57
- "number",
58
- "object",
59
- "queue",
60
- "random",
61
- "regex",
62
- "rmemo",
63
- "set",
64
- "sleep",
65
- "string",
66
- "tempfile",
67
- "test",
68
- "time",
69
- "types",
70
- "uri",
71
- "uuid",
72
- "package.json"
73
- ],
74
- "types": "./src/index.d.ts",
75
- "exports": {
76
- ".": "./index.js",
77
- "./all": "./all/index.js",
78
- "./array": "./array/index.js",
79
- "./atob": "./atob/index.js",
80
- "./base16": "./base16/index.js",
81
- "./be": "./be/index.js",
82
- "./btoa": "./btoa/index.js",
83
- "./buffer": "./buffer/index.js",
84
- "./chain": "./chain/index.js",
85
- "./class": "./class/index.js",
86
- "./cli-args": "./cli-args/index.js",
87
- "./color": "./color/index.js",
88
- "./combinators": "./combinators/index.js",
89
- "./crypto": "./crypto/index.js",
90
- "./currency": "./currency/index.js",
91
- "./data": "./data/index.js",
92
- "./date": "./date/index.js",
93
- "./debounce": "./debounce/index.js",
94
- "./deep_equal": "./deep_equal/index.js",
95
- "./env": "./env/index.js",
96
- "./error": "./error/index.js",
97
- "./fetch": "./fetch/index.js",
98
- "./fibonacci": "./fibonacci/index.js",
99
- "./fs": "./fs/index.js",
100
- "./function": "./function/index.js",
101
- "./functional": "./functional/index.js",
102
- "./html": "./html/index.js",
103
- "./http": "./http/index.js",
104
- "./math": "./math/index.js",
105
- "./matrix": "./matrix/index.js",
106
- "./number": "./number/index.js",
107
- "./object": "./object/index.js",
108
- "./queue": "./queue/index.js",
109
- "./random": "./random/index.js",
110
- "./regex": "./regex/index.js",
111
- "./rmemo": "./rmemo/index.js",
112
- "./set": "./set/index.js",
113
- "./sleep": "./sleep/index.js",
114
- "./string": "./string/index.js",
115
- "./tempfile": "./tempfile/index.js",
116
- "./test": "./test/index.js",
117
- "./time": "./time/index.js",
118
- "./types": "./types/index.js",
119
- "./uri": "./uri/index.js",
120
- "./uuid": "./uuid/index.js",
121
- "./package.json": "./package.json"
122
- },
123
- "scripts": {
124
- "build": ":",
125
- "clean": ":",
126
- "exec": "$@",
127
- "prepublishOnly": "pnpm clean && pnpm build && pnpm test",
128
- "test": "pnpm run /^test:/",
129
- "test:size": "size-limit",
130
- "test:type": "check-dts",
131
- "test:unit": "NODE_OPTIONS=--loader=esmock tsx node_modules/uvu/bin.js . '\\.test\\.(ts|js)$'",
132
- "disable:test:coverage": "c8 pnpm test:unit"
133
- },
134
- "devDependencies": {
135
- "@arethetypeswrong/cli": "^0.13.5",
136
- "@ctx-core/preprocess": "^0.1.0",
137
- "@size-limit/preset-small-lib": "^11.0.1",
138
- "@types/node": "^20.10.6",
139
- "@types/sinon": "^17.0.2",
140
- "c8": "^8.0.1",
141
- "check-dts": "^0.7.2",
142
- "esbuild": "^0.19.11",
143
- "esmock": "^2.6.0",
144
- "sinon": "^17.0.1",
145
- "size-limit": "^11.0.1",
146
- "tsx": "^4.7.0",
147
- "typescript": "next",
148
- "uvu": "^0.5.6"
149
- },
150
- "publishConfig": {
151
- "access": "public",
152
- "cache": "~/.npm"
153
- },
154
- "sideEffects": false,
155
- "size-limit": [
156
- {
157
- "name": "ctx_",
158
- "import": {
159
- "./be": "{ ctx_ }"
160
- },
161
- "limit": "33 B"
162
- },
163
- {
164
- "name": "ns_ctx_",
165
- "import": {
166
- "./be": "{ ns_ctx_ }"
167
- },
168
- "limit": "85 B"
169
- },
170
- {
171
- "name": "be_",
172
- "import": {
173
- "./be": "{ be_ }"
174
- },
175
- "limit": "99 B"
176
- },
177
- {
178
- "name": "be_ ctx_",
179
- "import": {
180
- "./be": "{ be_, ctx_ }"
181
- },
182
- "limit": "131 B"
183
- },
184
- {
185
- "name": "be_ ns_ctx_",
186
- "import": {
187
- "./be": "{ be_, ctx_, ns_ctx_ }"
188
- },
189
- "limit": "190 B"
190
- },
191
- {
192
- "name": "be_ ctx_ ns_ctx_",
193
- "import": {
194
- "./be": "{ be_, ctx_, ns_ctx_ }"
195
- },
196
- "limit": "190 B"
197
- },
198
- {
199
- "name": "memo_",
200
- "import": {
201
- "./rmemo": "{ memo_ }"
202
- },
203
- "limit": "336 B"
204
- },
205
- {
206
- "name": "memo_ sig_",
207
- "import": {
208
- "./rmemo": "{ sig_, memo_ }"
209
- },
210
- "limit": "352 B"
211
- },
212
- {
213
- "name": "memo_ sig_ be_ ctx_",
214
- "import": {
215
- "./rmemo": "{ sig_, memo_, be_, ctx_ }"
216
- },
217
- "limit": "458 B"
218
- },
219
- {
220
- "name": "memo_ sig_ be_ ctx_ be_memo_pair_ be_sig_triple_",
221
- "import": {
222
- "./rmemo": "{ sig_, memo_, be_, ctx_, be_memo_pair_, be_sig_triple_ }"
223
- },
224
- "limit": "552 B"
225
- },
226
- {
227
- "name": "uuid",
228
- "import": {
229
- "./uuid": "{ uuid_ }"
230
- },
231
- "limit": "39 B"
232
- },
233
- {
234
- "name": "short uuid",
235
- "import": {
236
- "./uuid": "{ short_uuid_ }"
237
- },
238
- "limit": "116 B"
239
- }
240
- ]
2
+ "name": "ctx-core",
3
+ "version": "5.8.0",
4
+ "description": "ctx-core core library",
5
+ "keywords": [
6
+ "ctx-core",
7
+ "array",
8
+ "combinators",
9
+ "function",
10
+ "object",
11
+ "set"
12
+ ],
13
+ "homepage": "https://github.com/ctx-core/ctx-core#readme",
14
+ "bugs": {
15
+ "url": "https://github.com/ctx-core/ctx-core/issues"
16
+ },
17
+ "repository": {
18
+ "type": "git",
19
+ "url": "https://github.com/ctx-core/ctx-core.git"
20
+ },
21
+ "license": "Apache-2.0",
22
+ "author": "Brian Takita",
23
+ "type": "module",
24
+ "files": [
25
+ "*.d.ts",
26
+ "*.js",
27
+ "*.json",
28
+ "all",
29
+ "array",
30
+ "atob",
31
+ "base16",
32
+ "be",
33
+ "btoa",
34
+ "buffer",
35
+ "chain",
36
+ "class",
37
+ "cli-args",
38
+ "color",
39
+ "combinators",
40
+ "crypto",
41
+ "currency",
42
+ "data",
43
+ "date",
44
+ "debounce",
45
+ "deep_equal",
46
+ "env",
47
+ "error",
48
+ "fetch",
49
+ "fibonacci",
50
+ "fs",
51
+ "function",
52
+ "functional",
53
+ "html",
54
+ "http",
55
+ "math",
56
+ "matrix",
57
+ "number",
58
+ "object",
59
+ "queue",
60
+ "random",
61
+ "regex",
62
+ "rmemo",
63
+ "set",
64
+ "sleep",
65
+ "string",
66
+ "tempfile",
67
+ "test",
68
+ "time",
69
+ "types",
70
+ "uri",
71
+ "uuid",
72
+ "package.json"
73
+ ],
74
+ "types": "./src/index.d.ts",
75
+ "exports": {
76
+ ".": "./index.js",
77
+ "./all": "./all/index.js",
78
+ "./array": "./array/index.js",
79
+ "./atob": "./atob/index.js",
80
+ "./base16": "./base16/index.js",
81
+ "./be": "./be/index.js",
82
+ "./btoa": "./btoa/index.js",
83
+ "./buffer": "./buffer/index.js",
84
+ "./chain": "./chain/index.js",
85
+ "./class": "./class/index.js",
86
+ "./cli-args": "./cli-args/index.js",
87
+ "./color": "./color/index.js",
88
+ "./combinators": "./combinators/index.js",
89
+ "./crypto": "./crypto/index.js",
90
+ "./currency": "./currency/index.js",
91
+ "./data": "./data/index.js",
92
+ "./date": "./date/index.js",
93
+ "./debounce": "./debounce/index.js",
94
+ "./deep_equal": "./deep_equal/index.js",
95
+ "./env": "./env/index.js",
96
+ "./error": "./error/index.js",
97
+ "./fetch": "./fetch/index.js",
98
+ "./fibonacci": "./fibonacci/index.js",
99
+ "./fs": "./fs/index.js",
100
+ "./function": "./function/index.js",
101
+ "./functional": "./functional/index.js",
102
+ "./html": "./html/index.js",
103
+ "./http": "./http/index.js",
104
+ "./math": "./math/index.js",
105
+ "./matrix": "./matrix/index.js",
106
+ "./number": "./number/index.js",
107
+ "./object": "./object/index.js",
108
+ "./queue": "./queue/index.js",
109
+ "./random": "./random/index.js",
110
+ "./regex": "./regex/index.js",
111
+ "./rmemo": "./rmemo/index.js",
112
+ "./set": "./set/index.js",
113
+ "./sleep": "./sleep/index.js",
114
+ "./string": "./string/index.js",
115
+ "./tempfile": "./tempfile/index.js",
116
+ "./test": "./test/index.js",
117
+ "./time": "./time/index.js",
118
+ "./types": "./types/index.js",
119
+ "./uri": "./uri/index.js",
120
+ "./uuid": "./uuid/index.js",
121
+ "./package.json": "./package.json"
122
+ },
123
+ "devDependencies": {
124
+ "@arethetypeswrong/cli": "^0.13.5",
125
+ "@ctx-core/preprocess": "^0.1.0",
126
+ "@size-limit/preset-small-lib": "^11.0.1",
127
+ "@types/node": "^20.10.6",
128
+ "@types/sinon": "^17.0.2",
129
+ "c8": "^8.0.1",
130
+ "check-dts": "^0.7.2",
131
+ "esbuild": "^0.19.11",
132
+ "esmock": "^2.6.0",
133
+ "sinon": "^17.0.1",
134
+ "size-limit": "^11.0.1",
135
+ "tsx": "^4.7.0",
136
+ "typescript": "next",
137
+ "uvu": "^0.5.6"
138
+ },
139
+ "publishConfig": {
140
+ "access": "public",
141
+ "cache": "~/.npm"
142
+ },
143
+ "sideEffects": false,
144
+ "size-limit": [
145
+ {
146
+ "name": "ctx_",
147
+ "import": {
148
+ "./be": "{ ctx_ }"
149
+ },
150
+ "limit": "33 B"
151
+ },
152
+ {
153
+ "name": "ns_ctx_",
154
+ "import": {
155
+ "./be": "{ ns_ctx_ }"
156
+ },
157
+ "limit": "85 B"
158
+ },
159
+ {
160
+ "name": "be_",
161
+ "import": {
162
+ "./be": "{ be_ }"
163
+ },
164
+ "limit": "99 B"
165
+ },
166
+ {
167
+ "name": "be_ ctx_",
168
+ "import": {
169
+ "./be": "{ be_, ctx_ }"
170
+ },
171
+ "limit": "131 B"
172
+ },
173
+ {
174
+ "name": "be_ ns_ctx_",
175
+ "import": {
176
+ "./be": "{ be_, ctx_, ns_ctx_ }"
177
+ },
178
+ "limit": "190 B"
179
+ },
180
+ {
181
+ "name": "be_ ctx_ ns_ctx_",
182
+ "import": {
183
+ "./be": "{ be_, ctx_, ns_ctx_ }"
184
+ },
185
+ "limit": "190 B"
186
+ },
187
+ {
188
+ "name": "memo_",
189
+ "import": {
190
+ "./rmemo": "{ memo_ }"
191
+ },
192
+ "limit": "336 B"
193
+ },
194
+ {
195
+ "name": "memo_ sig_",
196
+ "import": {
197
+ "./rmemo": "{ sig_, memo_ }"
198
+ },
199
+ "limit": "352 B"
200
+ },
201
+ {
202
+ "name": "memo_ sig_ be_ ctx_",
203
+ "import": {
204
+ "./rmemo": "{ sig_, memo_, be_, ctx_ }"
205
+ },
206
+ "limit": "458 B"
207
+ },
208
+ {
209
+ "name": "memo_ sig_ be_ ctx_ be_memo_pair_ be_sig_triple_",
210
+ "import": {
211
+ "./rmemo": "{ sig_, memo_, be_, ctx_, be_memo_pair_, be_sig_triple_ }"
212
+ },
213
+ "limit": "552 B"
214
+ },
215
+ {
216
+ "name": "uuid",
217
+ "import": {
218
+ "./uuid": "{ uuid_ }"
219
+ },
220
+ "limit": "39 B"
221
+ },
222
+ {
223
+ "name": "short uuid",
224
+ "import": {
225
+ "./uuid": "{ short_uuid_ }"
226
+ },
227
+ "limit": "116 B"
228
+ }
229
+ ],
230
+ "scripts": {
231
+ "build": ":",
232
+ "clean": ":",
233
+ "exec": "$@",
234
+ "test": "pnpm run /^test:/",
235
+ "test:size": "size-limit",
236
+ "test:type": "check-dts",
237
+ "test:unit": "NODE_OPTIONS=--loader=esmock tsx node_modules/uvu/bin.js . '\\.test\\.(ts|js)$'",
238
+ "disable:test:coverage": "c8 pnpm test:unit"
239
+ }
241
240
  }