1
1
import { Injectable } from '@angular/core' ;
2
- import { Observable , ReplaySubject } from 'rxjs' ;
2
+ import { Observable , ReplaySubject , BehaviorSubject } from 'rxjs' ;
3
3
import { scan , shareReplay } from 'rxjs/operators' ;
4
4
5
5
import { ElementBoundary } from '../types' ;
@@ -37,21 +37,14 @@ export class GlobalBoundarySharingStrategyOptions {
37
37
*/
38
38
@Injectable ( { providedIn : 'root' } )
39
39
export class GlobalBoundarySharingStrategy implements BoundarySharingStrategy {
40
- private readonly addBoundary$ = this . _boundary$ ;
41
-
42
- private readonly boundaries$ = this . addBoundary$ . pipe (
43
- scan ( ( acc , boundary ) => [ ...acc , boundary ] , [ ] as ElementBoundary [ ] ) ,
44
- shareReplay ( { bufferSize : 1 , refCount : false } ) ,
45
- ) ;
46
-
47
- private get _boundary$ ( ) : ReplaySubject < ElementBoundary > {
40
+ private get boundaries$ ( ) : BehaviorSubject < ElementBoundary [ ] > {
48
41
return (
49
42
this . globalRef . global [ this . options . propName ] ||
50
- ( this . _boundary $ = new ReplaySubject ( 1 ) )
43
+ ( this . boundaries $ = new BehaviorSubject < ElementBoundary [ ] > ( [ ] ) )
51
44
) ;
52
45
}
53
46
54
- private set _boundary $( boundaries : ReplaySubject < ElementBoundary > ) {
47
+ private set boundaries $( boundaries : BehaviorSubject < ElementBoundary [ ] > ) {
55
48
this . globalRef . global [ this . options . propName ] = boundaries ;
56
49
}
57
50
@@ -61,10 +54,28 @@ export class GlobalBoundarySharingStrategy implements BoundarySharingStrategy {
61
54
) { }
62
55
63
56
getBoundaries ( ) : Observable < ElementBoundary [ ] > {
64
- return this . boundaries$ ;
57
+ return this . boundaries$ . asObservable ( ) ;
65
58
}
66
59
67
60
addBoundary ( boundary : ElementBoundary ) : void {
68
- this . addBoundary$ . next ( boundary ) ;
61
+ const boundaries = this . boundaries$ . getValue ( ) ;
62
+
63
+ boundaries . push ( boundary ) ;
64
+
65
+ this . boundaries$ . next ( boundaries ) ;
66
+ }
67
+
68
+ removeBoundary ( boundary : ElementBoundary ) : void {
69
+ const boundaries = this . boundaries$ . getValue ( ) ;
70
+
71
+ const idx = boundaries . indexOf ( boundary ) ;
72
+
73
+ if ( idx === - 1 ) {
74
+ return ;
75
+ }
76
+
77
+ boundaries . splice ( idx , 1 ) ;
78
+
79
+ this . boundaries$ . next ( boundaries ) ;
69
80
}
70
81
}
0 commit comments