@juulsgaard/signal-tools 18.0.0 → 18.0.2

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.
Files changed (33) hide show
  1. package/dist/async.mjs +96 -0
  2. package/dist/data-structures/signal-map.mjs +134 -0
  3. package/dist/data-structures/signal-queue.js +115 -0
  4. package/dist/data-structures/signal-queue.mjs +115 -0
  5. package/dist/data-structures/signal-set.mjs +143 -0
  6. package/dist/data-structures/signal-stack.js +90 -0
  7. package/dist/data-structures/signal-stack.mjs +90 -0
  8. package/dist/index.d.ts +564 -62
  9. package/dist/index.js +12 -1
  10. package/dist/index.mjs +16 -0
  11. package/dist/interop.mjs +11 -0
  12. package/dist/loading-signal.mjs +68 -0
  13. package/dist/operators.js +13 -0
  14. package/dist/operators.mjs +13 -0
  15. package/dist/request-state/async-request-state.js +48 -0
  16. package/dist/request-state/async-request-state.mjs +48 -0
  17. package/dist/request-state/constructors.js +25 -0
  18. package/dist/request-state/constructors.mjs +25 -0
  19. package/dist/request-state/empty-request-state.js +20 -0
  20. package/dist/request-state/empty-request-state.mjs +20 -0
  21. package/dist/request-state/error-request-state.js +20 -0
  22. package/dist/request-state/error-request-state.mjs +20 -0
  23. package/dist/request-state/loading-request-state.js +25 -0
  24. package/dist/request-state/loading-request-state.mjs +25 -0
  25. package/dist/request-state/request-state.js +39 -0
  26. package/dist/request-state/request-state.mjs +39 -0
  27. package/dist/request-state/request-tracker.js +64 -0
  28. package/dist/request-state/request-tracker.mjs +64 -0
  29. package/dist/request-state/static-request-state.js +32 -0
  30. package/dist/request-state/static-request-state.mjs +32 -0
  31. package/dist/request-state/writable-request-state.js +75 -0
  32. package/dist/request-state/writable-request-state.mjs +75 -0
  33. package/package.json +12 -11
@@ -0,0 +1,32 @@
1
+ import { of } from 'rxjs';
2
+ import { signal } from '@angular/core';
3
+ import { IValueRequestState } from './request-state.mjs';
4
+
5
+ class StaticRequestState extends IValueRequestState {
6
+ constructor(value) {
7
+ super();
8
+ this.value = value;
9
+ this.loading = signal(false);
10
+ this.error = signal(void 0);
11
+ this.failed = signal(false);
12
+ this.request$ = of(value);
13
+ this.result$ = this.request$;
14
+ this.result = signal(value);
15
+ }
16
+ cancel() {
17
+ return false;
18
+ }
19
+ then(next, _error) {
20
+ next(this.value);
21
+ return this;
22
+ }
23
+ catch(_onError) {
24
+ return this;
25
+ }
26
+ finally(onComplete) {
27
+ onComplete();
28
+ return this;
29
+ }
30
+ }
31
+
32
+ export { StaticRequestState };
@@ -0,0 +1,75 @@
1
+ import { ReplaySubject } from 'rxjs';
2
+ import { signal, computed } from '@angular/core';
3
+ import { parseError, isString, isFunction } from '@juulsgaard/ts-tools';
4
+ import { CancelledError } from '@juulsgaard/rxjs-tools';
5
+ import { IValueRequestState } from './request-state.js';
6
+
7
+ class WritableRequestState extends IValueRequestState {
8
+ constructor(onCancel) {
9
+ super();
10
+ this.onCancel = onCancel;
11
+ this._completed = false;
12
+ this._request$ = new ReplaySubject();
13
+ this.request$ = this._request$.asObservable();
14
+ this._result$ = new ReplaySubject();
15
+ this.result$ = this._result$.asObservable();
16
+ this._result = signal(void 0);
17
+ this.result = this._result.asReadonly();
18
+ this._loading = signal(true);
19
+ this.loading = this._loading.asReadonly();
20
+ this._error = signal(void 0);
21
+ this.error = this._error.asReadonly();
22
+ this.failed = computed(() => this._error() !== void 0);
23
+ this._cancelled = signal(false);
24
+ /** Emits true when the RequestState has been cancelled by a consumer, otherwise emits false */
25
+ this.cancelled = this._cancelled.asReadonly();
26
+ this._cancelled$ = new ReplaySubject();
27
+ /** Emits true when the RequestState has been cancelled by a consumer */
28
+ this.cancelled$ = this._cancelled$.asObservable();
29
+ }
30
+ get completed() {
31
+ return this._completed;
32
+ }
33
+ /**
34
+ * Create a Readonly version of the RequestState
35
+ */
36
+ asReadonly() {
37
+ return this;
38
+ }
39
+ /**
40
+ * Set the RequestState to an Error state
41
+ * @param error - The error for the RequestState
42
+ */
43
+ setError(error) {
44
+ if (this.completed) return;
45
+ this._completed = true;
46
+ this._request$.error(error);
47
+ this._result$.complete();
48
+ this._loading.set(false);
49
+ this._error.set(parseError(error));
50
+ }
51
+ /**
52
+ * Set the RequestState to a completed state with a given value
53
+ * @param value - The value to emit
54
+ */
55
+ setValue(value) {
56
+ if (this.completed) return;
57
+ this._completed = true;
58
+ this._request$.next(value);
59
+ this._request$.complete();
60
+ this._loading.set(false);
61
+ this._result.set(value);
62
+ }
63
+ cancel(error) {
64
+ if (this.completed) return false;
65
+ const fullError = isString(error) ? new CancelledError(error) : isFunction(error) ? error() : error;
66
+ this.setError(fullError);
67
+ this._cancelled.set(true);
68
+ this._cancelled$.next(true);
69
+ this._cancelled$.complete();
70
+ this.onCancel?.();
71
+ return true;
72
+ }
73
+ }
74
+
75
+ export { WritableRequestState };
@@ -0,0 +1,75 @@
1
+ import { ReplaySubject } from 'rxjs';
2
+ import { signal, computed } from '@angular/core';
3
+ import { parseError, isString, isFunction } from '@juulsgaard/ts-tools';
4
+ import { CancelledError } from '@juulsgaard/rxjs-tools';
5
+ import { IValueRequestState } from './request-state.mjs';
6
+
7
+ class WritableRequestState extends IValueRequestState {
8
+ constructor(onCancel) {
9
+ super();
10
+ this.onCancel = onCancel;
11
+ this._completed = false;
12
+ this._request$ = new ReplaySubject();
13
+ this.request$ = this._request$.asObservable();
14
+ this._result$ = new ReplaySubject();
15
+ this.result$ = this._result$.asObservable();
16
+ this._result = signal(void 0);
17
+ this.result = this._result.asReadonly();
18
+ this._loading = signal(true);
19
+ this.loading = this._loading.asReadonly();
20
+ this._error = signal(void 0);
21
+ this.error = this._error.asReadonly();
22
+ this.failed = computed(() => this._error() !== void 0);
23
+ this._cancelled = signal(false);
24
+ /** Emits true when the RequestState has been cancelled by a consumer, otherwise emits false */
25
+ this.cancelled = this._cancelled.asReadonly();
26
+ this._cancelled$ = new ReplaySubject();
27
+ /** Emits true when the RequestState has been cancelled by a consumer */
28
+ this.cancelled$ = this._cancelled$.asObservable();
29
+ }
30
+ get completed() {
31
+ return this._completed;
32
+ }
33
+ /**
34
+ * Create a Readonly version of the RequestState
35
+ */
36
+ asReadonly() {
37
+ return this;
38
+ }
39
+ /**
40
+ * Set the RequestState to an Error state
41
+ * @param error - The error for the RequestState
42
+ */
43
+ setError(error) {
44
+ if (this.completed) return;
45
+ this._completed = true;
46
+ this._request$.error(error);
47
+ this._result$.complete();
48
+ this._loading.set(false);
49
+ this._error.set(parseError(error));
50
+ }
51
+ /**
52
+ * Set the RequestState to a completed state with a given value
53
+ * @param value - The value to emit
54
+ */
55
+ setValue(value) {
56
+ if (this.completed) return;
57
+ this._completed = true;
58
+ this._request$.next(value);
59
+ this._request$.complete();
60
+ this._loading.set(false);
61
+ this._result.set(value);
62
+ }
63
+ cancel(error) {
64
+ if (this.completed) return false;
65
+ const fullError = isString(error) ? new CancelledError(error) : isFunction(error) ? error() : error;
66
+ this.setError(fullError);
67
+ this._cancelled.set(true);
68
+ this._cancelled$.next(true);
69
+ this._cancelled$.complete();
70
+ this.onCancel?.();
71
+ return true;
72
+ }
73
+ }
74
+
75
+ export { WritableRequestState };
package/package.json CHANGED
@@ -1,10 +1,10 @@
1
1
  {
2
2
  "name": "@juulsgaard/signal-tools",
3
- "version": "18.0.0",
3
+ "version": "18.0.2",
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/",
7
- "main": "dist/index.js",
7
+ "main": "dist/index.mjs",
8
8
  "types": "dist/index.d.ts",
9
9
  "type": "module",
10
10
  "sideEffects": false,
@@ -14,24 +14,25 @@
14
14
  "dist/**"
15
15
  ],
16
16
  "peerDependencies": {
17
- "@angular/core": "^18.2.5",
18
- "@juulsgaard/ts-tools": "^1.6.4",
19
- "@juulsgaard/rxjs-tools": "^1.7.4",
17
+ "@angular/core": "^18.2.8",
18
+ "@juulsgaard/ts-tools": "^1.6.5",
19
+ "@juulsgaard/rxjs-tools": "^1.7.6",
20
20
  "rxjs": "^7.8.1",
21
21
  "tslib": "^2.7.0",
22
22
  "typescript": "~5.5.4"
23
23
  },
24
24
  "devDependencies": {
25
- "@angular/core": "18.2.5",
26
- "@juulsgaard/rxjs-tools": "1.7.4",
27
- "@types/jest": "^29.5.12",
25
+ "@angular/core": "18.2.8",
26
+ "@juulsgaard/ts-tools": "1.6.5",
27
+ "@juulsgaard/rxjs-tools": "1.7.6",
28
+ "@types/jest": "^29.5.13",
28
29
  "jest": "^29.7.0",
29
- "rollup": "^4.19.1",
30
+ "rollup": "^4.24.0",
30
31
  "rollup-plugin-dts": "^6.1.1",
31
32
  "rollup-plugin-esbuild": "^6.1.1",
32
33
  "rxjs": "7.8.1",
33
- "ts-jest": "^29.2.3",
34
- "typedoc": "^0.26.7",
34
+ "ts-jest": "^29.2.5",
35
+ "typedoc": "^0.26.8",
35
36
  "typescript": "~5.5.4",
36
37
  "tslib": "2.7.0"
37
38
  },