@@ -27,8 +27,13 @@ class WindowToggleOperator<T, R, O> implements Operator<T, R> {
27
27
}
28
28
}
29
29
30
+ interface WindowContext < T > {
31
+ window : Subject < T > ;
32
+ subscription : Subscription < T > ;
33
+ }
34
+
30
35
class WindowToggleSubscriber < T , O > extends Subscriber < T > {
31
- private windows : Subject < T > [ ] = [ ] ;
36
+ private contexts : Array < WindowContext < T > > = [ ] ;
32
37
private closingNotification : Subscription < any > ;
33
38
34
39
constructor ( destination : Subscriber < T > ,
@@ -39,55 +44,56 @@ class WindowToggleSubscriber<T, O> extends Subscriber<T> {
39
44
}
40
45
41
46
_next ( value : T ) {
42
- const windows = this . windows ;
43
- const len = windows . length ;
47
+ const contexts = this . contexts ;
48
+ const len = contexts . length ;
44
49
for ( let i = 0 ; i < len ; i ++ ) {
45
- windows [ i ] . next ( value ) ;
50
+ contexts [ i ] . window . next ( value ) ;
46
51
}
47
52
}
48
53
49
54
_error ( err : any ) {
50
- const windows = this . windows ;
51
- while ( windows . length > 0 ) {
52
- windows . shift ( ) . error ( err ) ;
55
+ const contexts = this . contexts ;
56
+ while ( contexts . length > 0 ) {
57
+ contexts . shift ( ) . window . error ( err ) ;
53
58
}
54
59
this . destination . error ( err ) ;
55
60
}
56
61
57
62
_complete ( ) {
58
- const windows = this . windows ;
59
- while ( windows . length > 0 ) {
60
- windows . shift ( ) . complete ( ) ;
63
+ const contexts = this . contexts ;
64
+ while ( contexts . length > 0 ) {
65
+ const context = contexts . shift ( ) ;
66
+ context . window . complete ( ) ;
67
+ context . subscription . unsubscribe ( ) ;
61
68
}
62
69
this . destination . complete ( ) ;
63
70
}
64
71
65
72
openWindow ( value : O ) {
66
- const window = new Subject < T > ( ) ;
67
- this . windows . push ( window ) ;
68
- this . destination . next ( window ) ;
69
- let windowContext = {
70
- window,
71
- subscription : new Subscription ( )
72
- } ;
73
-
74
73
const closingSelector = this . closingSelector ;
75
74
let closingNotifier = tryCatch ( closingSelector ) ( value ) ;
76
75
if ( closingNotifier === errorObject ) {
77
76
this . error ( closingNotifier . e ) ;
78
77
} else {
79
- const subscriber = new WindowClosingNotifierSubscriber < T , O > ( this , windowContext ) ;
78
+ let context = {
79
+ window : new Subject < T > ( ) ,
80
+ subscription : new Subscription ( )
81
+ } ;
82
+ this . contexts . push ( context ) ;
83
+ this . destination . next ( context . window ) ;
84
+ const subscriber = new WindowClosingNotifierSubscriber < T , O > ( this , context ) ;
80
85
const subscription = closingNotifier . _subscribe ( subscriber ) ;
81
- this . add ( windowContext . subscription . add ( subscription ) ) ;
86
+ this . add ( context . subscription . add ( subscription ) ) ;
82
87
}
83
88
}
84
89
85
- closeWindow ( windowContext : { subscription : Subscription < T > , window : Subject < T > } ) {
86
- const { window, subscription } = windowContext ;
87
- const windows = this . windows ;
88
- windows . splice ( windows . indexOf ( window ) , 1 ) ;
90
+ closeWindow ( context : WindowContext < T > ) {
91
+ const { window, subscription } = context ;
92
+ const contexts = this . contexts ;
93
+ contexts . splice ( contexts . indexOf ( context ) , 1 ) ;
89
94
window . complete ( ) ;
90
95
this . remove ( subscription ) ;
96
+ subscription . unsubscribe ( ) ;
91
97
}
92
98
}
93
99
@@ -106,7 +112,7 @@ class WindowClosingNotifierSubscriber<T, O> extends Subscriber<T> {
106
112
}
107
113
108
114
_complete ( ) {
109
- // noop
115
+ this . parent . closeWindow ( this . windowContext ) ;
110
116
}
111
117
}
112
118
0 commit comments