@xh/hoist 73.0.0-SNAPSHOT.1745618326968 → 73.0.0-SNAPSHOT.1745690606180

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.
@@ -18,7 +18,7 @@ declare global {
18
18
  * selector will be handled by this method.
19
19
  * @param fn - catch handler
20
20
  */
21
- catchWhen<TResult = undefined>(selector: ((e: any) => boolean) | Some<string>, fn?: (reason: any) => Awaitable<TResult>): Promise<T | TResult>;
21
+ catchWhen<TResult = undefined>(selector: ((e: unknown) => boolean) | Some<string>, fn?: (reason: unknown) => Awaitable<TResult>): Promise<T | TResult>;
22
22
  /**
23
23
  * Version of `catch()` that passes the error onto Hoist's default exception handler for
24
24
  * convention-driven logging and alerting. Typically called last in a Promise chain.
@@ -27,7 +27,7 @@ declare global {
27
27
  /**
28
28
  * Version of `catchDefault()` that will only catch certain exceptions.
29
29
  */
30
- catchDefaultWhen(selector: ((e: any) => boolean) | Some<string>, options: ExceptionHandlerOptions): Promise<T | undefined>;
30
+ catchDefaultWhen(selector: ((e: unknown) => boolean) | Some<string>, options: ExceptionHandlerOptions): Promise<T | undefined>;
31
31
  /**
32
32
  * Wait on a potentially async function before passing on the original value.
33
33
  * Useful when we want to block and do something on the promise chain, but do not want to
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xh/hoist",
3
- "version": "73.0.0-SNAPSHOT.1745618326968",
3
+ "version": "73.0.0-SNAPSHOT.1745690606180",
4
4
  "description": "Hoist add-on for building and deploying React Applications.",
5
5
  "repository": "github:xh/hoist-react",
6
6
  "homepage": "https://xh.io",
@@ -12,7 +12,8 @@ import {
12
12
  TrackOptions,
13
13
  XH,
14
14
  Some,
15
- Awaitable
15
+ Awaitable,
16
+ TimeoutExceptionConfig
16
17
  } from '@xh/hoist/core';
17
18
  import {action} from '@xh/hoist/mobx';
18
19
  import {olderThan, SECONDS} from '@xh/hoist/utils/datetime';
@@ -39,8 +40,8 @@ declare global {
39
40
  * @param fn - catch handler
40
41
  */
41
42
  catchWhen<TResult = undefined>(
42
- selector: ((e: any) => boolean) | Some<string>,
43
- fn?: (reason: any) => Awaitable<TResult>
43
+ selector: ((e: unknown) => boolean) | Some<string>,
44
+ fn?: (reason: unknown) => Awaitable<TResult>
44
45
  ): Promise<T | TResult>;
45
46
 
46
47
  /**
@@ -53,7 +54,7 @@ declare global {
53
54
  * Version of `catchDefault()` that will only catch certain exceptions.
54
55
  */
55
56
  catchDefaultWhen(
56
- selector: ((e: any) => boolean) | Some<string>,
57
+ selector: ((e: unknown) => boolean) | Some<string>,
57
58
  options: ExceptionHandlerOptions
58
59
  ): Promise<T | undefined>;
59
60
 
@@ -169,43 +170,63 @@ export function never<T>(): Promise<T> {
169
170
  //--------------------------------
170
171
  const enhancePromise = promisePrototype => {
171
172
  Object.assign(promisePrototype, {
172
- thenAction(fn) {
173
+ thenAction<T, TResult>(fn: (value: T) => Awaitable<TResult>): Promise<TResult> {
173
174
  return this.then(action(fn));
174
175
  },
175
176
 
176
- catchWhen(selector, fn) {
177
- return this.catch(e => {
177
+ catchWhen<T, TResult = undefined>(
178
+ selector: ((e: unknown) => boolean) | Some<string>,
179
+ fn?: (reason: unknown) => Awaitable<TResult>
180
+ ): Promise<T | TResult> {
181
+ return this.catch((e: unknown) => {
178
182
  this.throwIfFailsSelector(e, selector);
179
183
  return fn ? fn(e) : undefined;
180
184
  });
181
185
  },
182
186
 
183
- catchDefault(options) {
184
- return this.catch(e => XH.handleException(e, options));
187
+ catchDefault<T>(options?: ExceptionHandlerOptions): Promise<T | undefined> {
188
+ return this.catch((e: unknown) => XH.handleException(e, options));
185
189
  },
186
190
 
187
- catchDefaultWhen(selector, options) {
188
- return this.catch(e => {
191
+ catchDefaultWhen<T>(
192
+ selector: ((e: unknown) => boolean) | Some<string>,
193
+ options: ExceptionHandlerOptions
194
+ ): Promise<T | undefined> {
195
+ return this.catch((e: unknown) => {
189
196
  this.throwIfFailsSelector(e, selector);
190
197
  return XH.handleException(e, options);
191
198
  });
192
199
  },
193
200
 
194
- track(options) {
195
- if (!options || (isFunction(options.omit) ? options.omit() : options.omit)) return this;
196
- if (isString(options)) options = {message: options};
197
-
198
- const startTime = Date.now();
199
- return this.finally(() => {
200
- XH.track({
201
- timestamp: startTime,
202
- elapsed: Date.now() - startTime,
203
- ...options
204
- });
205
- });
201
+ track<T>(options: TrackOptions | string): Promise<T> {
202
+ if (!options) return this;
203
+
204
+ const startTime = Date.now(),
205
+ doTrack = (isError: boolean) => {
206
+ const opts: TrackOptions = isString(options)
207
+ ? {message: options}
208
+ : {...options};
209
+
210
+ opts.timestamp = startTime;
211
+ opts.elapsed = Date.now() - startTime;
212
+ if (isError) opts.severity = 'ERROR';
213
+
214
+ XH.track(opts);
215
+ };
216
+
217
+ return this.then(
218
+ (v: T) => {
219
+ doTrack(true);
220
+ return v;
221
+ },
222
+ (t: unknown) => {
223
+ doTrack(false);
224
+ throw t;
225
+ }
226
+ );
206
227
  },
207
228
 
208
- tap(onFulfillment) {
229
+ tap<T>(onFulfillment: (value: T) => any): Promise<T> {
209
230
  let ret = null;
210
231
  const resolveFn = data => {
211
232
  ret = data;
@@ -215,13 +236,14 @@ const enhancePromise = promisePrototype => {
215
236
  return this.then(resolveFn).then(() => ret);
216
237
  },
217
238
 
218
- wait(interval) {
239
+ wait<T>(interval: number): Promise<T> {
219
240
  return this.finally(() => wait(interval));
220
241
  },
221
242
 
222
- timeout(config) {
223
- if (config == null) return this;
224
- if (isNumber(config)) config = {interval: config};
243
+ timeout<T>(spec: PromiseTimeoutSpec): Promise<T> {
244
+ if (spec == null) return this;
245
+
246
+ const config: TimeoutExceptionConfig = isNumber(spec) ? {interval: spec} : spec;
225
247
  const interval = config.interval;
226
248
 
227
249
  let completed = false;
@@ -236,10 +258,10 @@ const enhancePromise = promisePrototype => {
236
258
  return Promise.race([deadline, promise]);
237
259
  },
238
260
 
239
- linkTo(cfg) {
261
+ linkTo<T>(cfg: PromiseLinkSpec): Promise<T> {
240
262
  if (!cfg) return this;
241
263
 
242
- if (cfg.isTaskObserver) {
264
+ if (cfg instanceof TaskObserver) {
243
265
  cfg = {observer: cfg};
244
266
  }
245
267
 
@@ -252,7 +274,7 @@ const enhancePromise = promisePrototype => {
252
274
  //--------------------------------
253
275
  // Implementation
254
276
  //--------------------------------
255
- throwIfFailsSelector(e, selector) {
277
+ throwIfFailsSelector(e: any, selector: any) {
256
278
  const fn = isFunction(selector) ? selector : e => castArray(selector).includes(e.name);
257
279
  if (!fn(e)) throw e;
258
280
  }