1
1
/**
2
- * Copyright 2020, 2022, Optimizely
2
+ * Copyright 2020, 2022, 2024, Optimizely
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
15
15
*/
16
16
import { LogHandler , ErrorHandler } from '../modules/logging' ;
17
17
import { objectValues } from '../utils/fns' ;
18
- import { NotificationListener , ListenerPayload } from '../shared_types' ;
19
18
20
19
import {
21
20
LOG_LEVEL ,
22
21
LOG_MESSAGES ,
23
- NOTIFICATION_TYPES ,
24
22
} from '../utils/enums' ;
25
23
24
+ import { NOTIFICATION_TYPES } from './type' ;
25
+ import { NotificationType , NotificationPayload } from './type' ;
26
+ import { Consumer } from '../utils/type' ;
27
+
26
28
const MODULE_NAME = 'NOTIFICATION_CENTER' ;
27
29
28
30
interface NotificationCenterOptions {
@@ -40,13 +42,30 @@ type NotificationListeners = {
40
42
[ key : string ] : ListenerEntry [ ] ;
41
43
}
42
44
45
+ export interface NotificationCenter {
46
+ addNotificationListener < N extends NotificationType > (
47
+ notificationType : N ,
48
+ callback : Consumer < NotificationPayload [ N ] >
49
+ ) : number
50
+ removeNotificationListener ( listenerId : number ) : boolean ;
51
+ clearAllNotificationListeners ( ) : void ;
52
+ clearNotificationListeners ( notificationType : NotificationType ) : void ;
53
+ }
54
+
55
+ export interface NotificationSender {
56
+ sendNotifications < N extends NotificationType > (
57
+ notificationType : N ,
58
+ notificationData : NotificationPayload [ N ]
59
+ ) : void ;
60
+ }
61
+
43
62
/**
44
63
* NotificationCenter allows registration and triggering of callback functions using
45
64
* notification event types defined in NOTIFICATION_TYPES of utils/enums/index.js:
46
65
* - ACTIVATE: An impression event will be sent to Optimizely.
47
66
* - TRACK a conversion event will be sent to Optimizely
48
67
*/
49
- export class NotificationCenter {
68
+ export class DefaultNotificationCenter implements NotificationCenter , NotificationSender {
50
69
private logger : LogHandler ;
51
70
private errorHandler : ErrorHandler ;
52
71
private notificationListeners : NotificationListeners ;
@@ -80,9 +99,9 @@ export class NotificationCenter {
80
99
* can happen if the first argument is not a valid notification type, or if the same callback
81
100
* function was already added as a listener by a prior call to this function.
82
101
*/
83
- addNotificationListener < T extends ListenerPayload > (
84
- notificationType : string ,
85
- callback : NotificationListener < T >
102
+ addNotificationListener < N extends NotificationType > (
103
+ notificationType : N ,
104
+ callback : Consumer < NotificationPayload [ N ] >
86
105
) : number {
87
106
try {
88
107
const notificationTypeValues : string [ ] = objectValues ( NOTIFICATION_TYPES ) ;
@@ -187,7 +206,7 @@ export class NotificationCenter {
187
206
* Remove all previously added notification listeners for the argument type
188
207
* @param {NOTIFICATION_TYPES } notificationType One of NOTIFICATION_TYPES
189
208
*/
190
- clearNotificationListeners ( notificationType : NOTIFICATION_TYPES ) : void {
209
+ clearNotificationListeners ( notificationType : NotificationType ) : void {
191
210
try {
192
211
this . notificationListeners [ notificationType ] = [ ] ;
193
212
} catch ( e : any ) {
@@ -202,9 +221,9 @@ export class NotificationCenter {
202
221
* @param {string } notificationType One of NOTIFICATION_TYPES
203
222
* @param {Object } notificationData Will be passed to callbacks called
204
223
*/
205
- sendNotifications < T extends ListenerPayload > (
206
- notificationType : string ,
207
- notificationData ?: T
224
+ sendNotifications < N extends NotificationType > (
225
+ notificationType : N ,
226
+ notificationData : NotificationPayload [ N ]
208
227
) : void {
209
228
try {
210
229
( this . notificationListeners [ notificationType ] || [ ] ) . forEach (
@@ -235,12 +254,6 @@ export class NotificationCenter {
235
254
* @param {NotificationCenterOptions } options
236
255
* @returns {NotificationCenter } An instance of NotificationCenter
237
256
*/
238
- export function createNotificationCenter ( options : NotificationCenterOptions ) : NotificationCenter {
239
- return new NotificationCenter ( options ) ;
240
- }
241
-
242
- export interface NotificationSender {
243
- // TODO[OASIS-6649]: Don't use any type
244
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
245
- sendNotifications ( notificationType : NOTIFICATION_TYPES , notificationData ?: any ) : void
257
+ export function createNotificationCenter ( options : NotificationCenterOptions ) : DefaultNotificationCenter {
258
+ return new DefaultNotificationCenter ( options ) ;
246
259
}
0 commit comments