@@ -2,7 +2,6 @@ type DialogId = string;
2
2
3
3
export type GetOrCreateParams = {
4
4
id : DialogId ;
5
- isOpen ?: boolean ;
6
5
} ;
7
6
8
7
export type Dialog = {
@@ -15,7 +14,7 @@ export type Dialog = {
15
14
toggleSingle : ( ) => void ;
16
15
} ;
17
16
18
- type DialogEvent = { type : 'close' | 'open' | 'openCountChange' } ;
17
+ type DialogEvent = { type : 'close' | 'open' } ;
19
18
20
19
const dialogsManagerEvents = [ 'openCountChange' ] as const ;
21
20
type DialogsManagerEvent = { type : typeof dialogsManagerEvents [ number ] } ;
@@ -46,15 +45,15 @@ export class DialogsManager {
46
45
this . id = id ?? new Date ( ) . getTime ( ) . toString ( ) ;
47
46
}
48
47
49
- getOrCreate ( { id, isOpen = false } : GetOrCreateParams ) {
48
+ getOrCreate ( { id } : GetOrCreateParams ) {
50
49
let dialog = this . dialogs [ id ] ;
51
50
if ( ! dialog ) {
52
51
dialog = {
53
52
close : ( ) => {
54
53
this . close ( id ) ;
55
54
} ,
56
55
id,
57
- isOpen,
56
+ isOpen : false ,
58
57
open : ( ) => {
59
58
this . open ( { id } ) ;
60
59
} ,
@@ -88,10 +87,11 @@ export class DialogsManager {
88
87
if ( ! id ) return noop ;
89
88
90
89
if ( ! this . dialogEventListeners [ id ] ) {
91
- this . dialogEventListeners [ id ] = { close : [ ] , open : [ ] } ;
90
+ this . dialogEventListeners [ id ] = { } ;
92
91
}
93
- this . dialogEventListeners [ id ] [ eventType ] = [
94
- ...( this . dialogEventListeners [ id ] [ eventType ] ?? [ ] ) ,
92
+
93
+ this . dialogEventListeners [ id ] [ eventType as DialogEvent [ 'type' ] ] = [
94
+ ...( this . dialogEventListeners [ id ] [ eventType as DialogEvent [ 'type' ] ] ?? [ ] ) ,
95
95
listener as DialogEventHandler ,
96
96
] ;
97
97
return ( ) => {
@@ -104,18 +104,33 @@ export class DialogsManager {
104
104
{ id, listener } : { listener : DialogEventHandler | DialogsManagerEventHandler ; id ?: DialogId } ,
105
105
) {
106
106
if ( dialogsManagerEvents . includes ( eventType as DialogsManagerEvent [ 'type' ] ) ) {
107
- const eventListeners = this . dialogsManagerEventListeners [
107
+ if ( ! this . dialogsManagerEventListeners [ eventType as DialogsManagerEvent [ 'type' ] ] ?. length )
108
+ return ;
109
+
110
+ this . dialogsManagerEventListeners [
108
111
eventType as DialogsManagerEvent [ 'type' ]
109
- ] ;
110
- eventListeners ?. filter ( ( l ) => l !== listener ) ;
112
+ ] = this . dialogsManagerEventListeners [ eventType as DialogsManagerEvent [ 'type' ] ] ?. filter (
113
+ ( l ) => l !== listener ,
114
+ ) ;
111
115
return ;
112
116
}
113
117
114
118
if ( ! id ) return ;
115
119
116
- const eventListeners = this . dialogEventListeners [ id ] ?. [ eventType ] ;
120
+ const eventListeners = this . dialogEventListeners [ id ] ?. [ eventType as DialogEvent [ 'type' ] ] ;
117
121
if ( ! eventListeners ) return ;
118
- this . dialogEventListeners [ id ] [ eventType ] = eventListeners . filter ( ( l ) => l !== listener ) ;
122
+
123
+ this . dialogEventListeners [ id ] [ eventType as DialogEvent [ 'type' ] ] = eventListeners . filter (
124
+ ( l ) => l !== listener ,
125
+ ) ;
126
+
127
+ if ( ! this . dialogEventListeners [ id ] [ eventType as DialogEvent [ 'type' ] ] ?. length ) {
128
+ delete this . dialogEventListeners [ id ] [ eventType as DialogEvent [ 'type' ] ] ;
129
+ }
130
+
131
+ if ( ! Object . keys ( this . dialogEventListeners [ id ] ) . length ) {
132
+ delete this . dialogEventListeners [ id ] ;
133
+ }
119
134
}
120
135
121
136
open ( params : GetOrCreateParams , single ?: boolean ) {
@@ -127,15 +142,15 @@ export class DialogsManager {
127
142
this . dialogs [ params . id ] . isOpen = true ;
128
143
this . openDialogCount ++ ;
129
144
this . dialogsManagerEventListeners . openCountChange . forEach ( ( listener ) => listener ( this ) ) ;
130
- this . dialogEventListeners [ params . id ] . open ?. forEach ( ( listener ) => listener ( dialog ) ) ;
145
+ this . dialogEventListeners [ params . id ] ? .open ?. forEach ( ( listener ) => listener ( dialog ) ) ;
131
146
}
132
147
133
148
close ( id : DialogId ) {
134
149
const dialog = this . dialogs [ id ] ;
135
150
if ( ! dialog ?. isOpen ) return ;
136
151
dialog . isOpen = false ;
137
152
this . openDialogCount -- ;
138
- this . dialogEventListeners [ id ] . close ?. forEach ( ( listener ) => listener ( dialog ) ) ;
153
+ this . dialogEventListeners [ id ] ? .close ?. forEach ( ( listener ) => listener ( dialog ) ) ;
139
154
this . dialogsManagerEventListeners . openCountChange . forEach ( ( listener ) => listener ( this ) ) ;
140
155
}
141
156
@@ -144,24 +159,24 @@ export class DialogsManager {
144
159
}
145
160
146
161
toggleOpen ( params : GetOrCreateParams ) {
147
- if ( this . dialogs [ params . id ] . isOpen ) {
162
+ if ( this . dialogs [ params . id ] ? .isOpen ) {
148
163
this . close ( params . id ) ;
149
164
} else {
150
165
this . open ( params ) ;
151
166
}
152
167
}
153
168
154
169
toggleOpenSingle ( params : GetOrCreateParams ) {
155
- if ( this . dialogs [ params . id ] . isOpen ) {
170
+ if ( this . dialogs [ params . id ] ? .isOpen ) {
156
171
this . close ( params . id ) ;
157
172
} else {
158
173
this . open ( params , true ) ;
159
174
}
160
175
}
161
176
162
177
remove ( id : DialogId ) {
163
- const dialogs = { ... this . dialogs } ;
164
- if ( ! dialogs [ id ] ) return ;
178
+ const dialog = this . dialogs [ id ] ;
179
+ if ( ! dialog ) return ;
165
180
166
181
const countListeners =
167
182
! ! this . dialogEventListeners [ id ] &&
@@ -172,7 +187,10 @@ export class DialogsManager {
172
187
173
188
if ( ! countListeners ) {
174
189
delete this . dialogEventListeners [ id ] ;
175
- delete dialogs [ id ] ;
190
+ if ( dialog . isOpen ) {
191
+ this . openDialogCount -- ;
192
+ }
193
+ delete this . dialogs [ id ] ;
176
194
}
177
195
}
178
196
}
0 commit comments