@juulsgaard/signal-tools 18.0.2 → 18.0.3

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
@@ -642,6 +642,8 @@ declare const requestState: typeof valueRequestState & AltConstructors;
642
642
  * @category RequestState
643
643
  */
644
644
  declare class AsyncRequestState<T> extends IValueRequestState<T> {
645
+ private _completed;
646
+ get completed(): boolean;
645
647
  private readonly _request$;
646
648
  readonly request$: Observable<T>;
647
649
  private readonly _result$;
@@ -655,6 +657,9 @@ declare class AsyncRequestState<T> extends IValueRequestState<T> {
655
657
  readonly failed: _angular_core.Signal<boolean>;
656
658
  private readonly sub;
657
659
  constructor(request: Promise<T> | Observable<T>);
660
+ private onError;
661
+ private onValue;
662
+ private onCompleted;
658
663
  cancel(error?: string | Error | (() => Error)): boolean;
659
664
  }
660
665
 
@@ -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
  }
@@ -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.3",
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/",