@juulsgaard/signal-tools 18.0.2 → 18.0.4

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.
package/dist/index.d.ts CHANGED
@@ -497,6 +497,7 @@ declare abstract class IRequestState implements Subscribable<unknown> {
497
497
  * @param onError - Action to perform on error
498
498
  */
499
499
  abstract then(onValue: (value: unknown) => void, onError?: (error: Error) => void): this;
500
+ asPromise(): Promise<unknown>;
500
501
  /**
501
502
  * Perform an action if the request fails
502
503
  * @param onError
@@ -525,6 +526,7 @@ declare abstract class IValueRequestState<T> extends IRequestState implements Su
525
526
  then(onValue: (value: T) => void, onError?: (error: Error) => void): this;
526
527
  catch(onError: (error: Error) => void): this;
527
528
  finally(onComplete: (error?: Error) => void): this;
529
+ asPromise(): Promise<T>;
528
530
  /**
529
531
  * Cancel a running request
530
532
  * @param error - Optionally provide a custom error to throw when cancelling the request
@@ -642,6 +644,8 @@ declare const requestState: typeof valueRequestState & AltConstructors;
642
644
  * @category RequestState
643
645
  */
644
646
  declare class AsyncRequestState<T> extends IValueRequestState<T> {
647
+ private _completed;
648
+ get completed(): boolean;
645
649
  private readonly _request$;
646
650
  readonly request$: Observable<T>;
647
651
  private readonly _result$;
@@ -655,6 +659,9 @@ declare class AsyncRequestState<T> extends IValueRequestState<T> {
655
659
  readonly failed: _angular_core.Signal<boolean>;
656
660
  private readonly sub;
657
661
  constructor(request: Promise<T> | Observable<T>);
662
+ private onError;
663
+ private onValue;
664
+ private onCompleted;
658
665
  cancel(error?: string | Error | (() => Error)): boolean;
659
666
  }
660
667
 
@@ -7,6 +7,7 @@ import { IValueRequestState } from './request-state.mjs';
7
7
  class AsyncRequestState extends IValueRequestState {
8
8
  constructor(request) {
9
9
  super();
10
+ this._completed = false;
10
11
  this._request$ = new ReplaySubject();
11
12
  this.request$ = this._request$.asObservable();
12
13
  this._result$ = new ReplaySubject();
@@ -19,28 +20,45 @@ class AsyncRequestState extends IValueRequestState {
19
20
  this.error = this._error.asReadonly();
20
21
  this.failed = computed(() => this._error() !== void 0);
21
22
  const request$ = isObservable(request) ? request.pipe(take(1)) : from(request);
22
- this.sub = request$.subscribe(this._request$);
23
- this.request$.subscribe({
24
- next: (value) => {
25
- this._result$.next(value);
26
- this._result.set(value);
27
- },
28
- error: (error) => {
29
- this._error.set(parseError(error));
30
- this._result$.complete();
31
- this._loading.set(false);
32
- },
33
- complete: () => {
34
- this._result$.complete();
35
- this._loading.set(false);
36
- }
23
+ this.sub = request$.subscribe({
24
+ next: (x) => this.onValue(x),
25
+ error: (x) => this.onError(x),
26
+ complete: () => this.onCompleted()
37
27
  });
38
28
  }
29
+ get completed() {
30
+ return this._completed;
31
+ }
32
+ onError(error) {
33
+ if (this.completed) return;
34
+ this._completed = true;
35
+ this._request$.error(error);
36
+ this._result$.complete();
37
+ this._loading.set(false);
38
+ this._error.set(parseError(error));
39
+ }
40
+ onValue(value) {
41
+ if (this.completed) return;
42
+ this._completed = true;
43
+ this._request$.next(value);
44
+ this._request$.complete();
45
+ this._result$.next(value);
46
+ this._result$.complete();
47
+ this._loading.set(false);
48
+ this._result.set(value);
49
+ }
50
+ onCompleted() {
51
+ if (this.completed) return;
52
+ this._completed = true;
53
+ this._request$.complete();
54
+ this._result$.complete();
55
+ this._loading.set(false);
56
+ }
39
57
  cancel(error) {
40
- if (this.sub.closed) return false;
58
+ if (this.completed) return false;
41
59
  this.sub.unsubscribe();
42
60
  const fullError = isString(error) ? new CancelledError(error) : isFunction(error) ? error() : error;
43
- this._request$.error(fullError);
61
+ this.onError(fullError);
44
62
  return true;
45
63
  }
46
64
  }
@@ -2,6 +2,9 @@ import { first } from 'rxjs';
2
2
  import { parseError } from '@juulsgaard/ts-tools';
3
3
 
4
4
  class IRequestState {
5
+ asPromise() {
6
+ return new Promise((resolve, reject) => this.then(resolve, reject));
7
+ }
5
8
  /**
6
9
  * Subscribe to the underlying request Observable
7
10
  * @param observer
@@ -34,6 +37,9 @@ class IValueRequestState extends IRequestState {
34
37
  });
35
38
  return this;
36
39
  }
40
+ asPromise() {
41
+ return new Promise((resolve, reject) => this.then(resolve, reject));
42
+ }
37
43
  }
38
44
 
39
45
  export { IRequestState, IValueRequestState };
@@ -57,6 +57,8 @@ class WritableRequestState extends IValueRequestState {
57
57
  this._completed = true;
58
58
  this._request$.next(value);
59
59
  this._request$.complete();
60
+ this._result$.next(value);
61
+ this._result$.complete();
60
62
  this._loading.set(false);
61
63
  this._result.set(value);
62
64
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@juulsgaard/signal-tools",
3
- "version": "18.0.2",
3
+ "version": "18.0.4",
4
4
  "description": "Tools for working with Angular signals",
5
5
  "repository": "git@github.com:Juulsgaard/signal-tools.git",
6
6
  "homepage": "https://juulsgaard.github.io/signal-tools/",