@@ -14,7 +14,7 @@ import type {
14
14
IClientFeatureToggleDeltaReadModel ,
15
15
} from './client-feature-toggle-delta-read-model-type' ;
16
16
import { CLIENT_DELTA_MEMORY } from '../../../metric-events' ;
17
- import type EventEmitter from 'events' ;
17
+ import EventEmitter from 'events' ;
18
18
import type { Logger } from '../../../logger' ;
19
19
import type { ClientFeaturesDeltaSchema } from '../../../openapi' ;
20
20
import {
@@ -28,6 +28,8 @@ import {
28
28
29
29
type EnvironmentRevisions = Record < string , DeltaCache > ;
30
30
31
+ export const UPDATE_DELTA = 'UPDATE_DELTA' ;
32
+
31
33
export const filterEventsByQuery = (
32
34
events : DeltaEvent [ ] ,
33
35
requiredRevisionId : number ,
@@ -86,7 +88,9 @@ export const filterHydrationEventByQuery = (
86
88
} ;
87
89
} ;
88
90
89
- export class ClientFeatureToggleDelta {
91
+ export class ClientFeatureToggleDelta extends EventEmitter {
92
+ private static instance : ClientFeatureToggleDelta ;
93
+
90
94
private clientFeatureToggleDeltaReadModel : IClientFeatureToggleDeltaReadModel ;
91
95
92
96
private delta : EnvironmentRevisions = { } ;
@@ -113,6 +117,7 @@ export class ClientFeatureToggleDelta {
113
117
flagResolver : IFlagResolver ,
114
118
config : IUnleashConfig ,
115
119
) {
120
+ super ( ) ;
116
121
this . eventStore = eventStore ;
117
122
this . configurationRevisionService = configurationRevisionService ;
118
123
this . clientFeatureToggleDeltaReadModel =
@@ -124,16 +129,31 @@ export class ClientFeatureToggleDelta {
124
129
this . onUpdateRevisionEvent = this . onUpdateRevisionEvent . bind ( this ) ;
125
130
this . delta = { } ;
126
131
127
- this . initRevisionId ( ) ;
128
132
this . configurationRevisionService . on (
129
133
UPDATE_REVISION ,
130
134
this . onUpdateRevisionEvent ,
131
135
) ;
132
136
}
133
137
134
- private async initRevisionId ( ) {
135
- this . currentRevisionId =
136
- await this . configurationRevisionService . getMaxRevisionId ( ) ;
138
+ static getInstance (
139
+ clientFeatureToggleDeltaReadModel : IClientFeatureToggleDeltaReadModel ,
140
+ segmentReadModel : ISegmentReadModel ,
141
+ eventStore : IEventStore ,
142
+ configurationRevisionService : ConfigurationRevisionService ,
143
+ flagResolver : IFlagResolver ,
144
+ config : IUnleashConfig ,
145
+ ) {
146
+ if ( ! ClientFeatureToggleDelta . instance ) {
147
+ ClientFeatureToggleDelta . instance = new ClientFeatureToggleDelta (
148
+ clientFeatureToggleDeltaReadModel ,
149
+ segmentReadModel ,
150
+ eventStore ,
151
+ configurationRevisionService ,
152
+ flagResolver ,
153
+ config ,
154
+ ) ;
155
+ }
156
+ return ClientFeatureToggleDelta . instance ;
137
157
}
138
158
139
159
async getDelta (
@@ -198,6 +218,7 @@ export class ClientFeatureToggleDelta {
198
218
if ( this . flagResolver . isEnabled ( 'deltaApi' ) ) {
199
219
await this . updateFeaturesDelta ( ) ;
200
220
this . storeFootprint ( ) ;
221
+ this . emit ( UPDATE_DELTA ) ;
201
222
}
202
223
}
203
224
0 commit comments