1
- import { ChangeDetectorRef , Injectable } from '@angular/core' ;
1
+ import {
2
+ ChangeDetectorRef ,
3
+ inject ,
4
+ Injectable ,
5
+ Injector ,
6
+ runInInjectionContext ,
7
+ } from '@angular/core' ;
2
8
import { RxState } from '@rx-angular/state' ;
3
9
import { merge , Observable , Subject } from 'rxjs' ;
4
10
import { map , scan } from 'rxjs/operators' ;
5
11
import { ngInputFlatten } from '../../utils/ngInputFlatten' ;
6
12
import { ProvidedValues } from './model' ;
7
- import { toBoolean , toImgUrl , toInt , toRandom , withCompleteAndError } from './utils' ;
13
+ import {
14
+ toBoolean ,
15
+ toImgUrl ,
16
+ toInt ,
17
+ toRandom ,
18
+ withCompleteAndError ,
19
+ } from './utils' ;
8
20
9
21
@Injectable ( )
10
22
export class PrimitivesProviderService {
23
+ private injector = inject ( Injector ) ;
11
24
protected outerChanges = new Subject < Observable < any > > ( ) ;
12
25
13
26
protected nextSubject = new Subject < any > ( ) ;
@@ -16,7 +29,7 @@ export class PrimitivesProviderService {
16
29
protected error$ = this . errorSubject . pipe (
17
30
map ( ( _ ) => {
18
31
throw new Error ( 'ERROR' ) ;
19
- } )
32
+ } ) ,
20
33
) ;
21
34
protected completeSubject = new Subject < any > ( ) ;
22
35
protected resetSubject = new Subject < any > ( ) ;
@@ -47,31 +60,34 @@ export class PrimitivesProviderService {
47
60
48
61
private resetObservables = ( ) => {
49
62
this . state . ngOnDestroy ( ) ;
50
- this . state = new RxState < ProvidedValues > ( ) ;
63
+ runInInjectionContext (
64
+ this . injector ,
65
+ ( ) => ( this . state = new RxState < ProvidedValues > ( ) ) ,
66
+ ) ;
51
67
52
68
this . state . connect (
53
69
'random' ,
54
70
merge ( this . nextSubject , this . outerChanges . pipe ( ngInputFlatten ( ) ) ) . pipe (
55
- map ( toRandom )
56
- )
71
+ map ( toRandom ) ,
72
+ ) ,
57
73
) ;
58
74
59
75
this . float$ = this . state . select ( 'random' ) ;
60
76
this . int$ = this . state . select (
61
77
map ( ( s ) => toInt ( s . random , this . min , this . max ) ) ,
62
- withCompleteAndError ( this . error$ , this . completeSubject )
78
+ withCompleteAndError ( this . error$ , this . completeSubject ) ,
63
79
) ;
64
80
this . incremental$ = this . state . select (
65
81
scan ( ( inc ) => ++ inc , 0 ) ,
66
- withCompleteAndError ( this . error$ , this . completeSubject )
82
+ withCompleteAndError ( this . error$ , this . completeSubject ) ,
67
83
) ;
68
84
this . boolean$ = this . state . select (
69
85
map ( ( s ) => toBoolean ( s . random , this . truthy ) ) ,
70
- withCompleteAndError ( this . error$ , this . completeSubject )
86
+ withCompleteAndError ( this . error$ , this . completeSubject ) ,
71
87
) ;
72
88
this . imgUrl$ = this . state . select (
73
89
map ( ( s ) => toImgUrl ( s . random ) ) ,
74
- withCompleteAndError ( this . error$ , this . completeSubject )
90
+ withCompleteAndError ( this . error$ , this . completeSubject ) ,
75
91
) ;
76
92
77
93
this . state . hold ( this . float$ , this . updateStatic ) ;
@@ -86,7 +102,7 @@ export class PrimitivesProviderService {
86
102
87
103
constructor (
88
104
protected state : RxState < ProvidedValues > ,
89
- protected cdRef : ChangeDetectorRef
105
+ protected cdRef : ChangeDetectorRef ,
90
106
) {
91
107
this . resetAll ( ) ;
92
108
}
0 commit comments