20
20
#import " OPTLYExperiment.h"
21
21
#import " OPTLYVariation.h"
22
22
#import < objc/runtime.h>
23
- #import " OPTLYNotificationDelegate.h"
24
23
25
24
@interface OPTLYNotificationCenter ()
26
25
@@ -56,16 +55,12 @@ -(NSUInteger)notificationsCount {
56
55
return notificationsCount;
57
56
}
58
57
59
- - (NSInteger )addNotification : (OPTLYNotificationType)type activateListener : (id <OPTLYNotificationDelegate>)activateListener {
60
- if (![self isNotificationTypeValid: type expectedNotificationType: OPTLYNotificationTypeActivate])
61
- return 0 ;
62
- return [self addNotification: type listener: activateListener];
58
+ - (NSInteger )addActivateNotificationListener : (ActivateListener)activateListener {
59
+ return [self addNotification: OPTLYNotificationTypeActivate listener: (GenericListener) activateListener];
63
60
}
64
61
65
- - (NSInteger )addNotification : (OPTLYNotificationType)type trackListener : (id <OPTLYNotificationDelegate>)trackListener {
66
- if (![self isNotificationTypeValid: type expectedNotificationType: OPTLYNotificationTypeTrack])
67
- return 0 ;
68
- return [self addNotification: type listener: trackListener];
62
+ - (NSInteger )addTrackNotificationListener : (TrackListener)trackListener {
63
+ return [self addNotification: OPTLYNotificationTypeTrack listener: (GenericListener)trackListener];
69
64
}
70
65
71
66
- (BOOL )removeNotification : (NSUInteger )notificationId {
@@ -91,12 +86,18 @@ - (void)clearAllNotifications {
91
86
92
87
- (void )sendNotifications : (OPTLYNotificationType)type args : (NSArray *)args {
93
88
OPTLYNotificationHolder *notification = _notifications[@(type)];
94
- for (id <OPTLYNotificationDelegate> object in notification.allValues ) {
89
+ for (GenericListener listener in notification.allValues ) {
95
90
@try {
96
- if (type == OPTLYNotificationTypeActivate)
97
- [self invokeSelector: object selector: @selector (onActivate:userId:attributes:variation:event: ) arguments: args];
98
- else
99
- [self invokeSelector: object selector: @selector (onTrack:userId:attributes:eventTags:event: ) arguments: args];
91
+ switch (type) {
92
+ case OPTLYNotificationTypeActivate:
93
+ [self notifyActivateListener: ((ActivateListener) listener) args: args];
94
+ break ;
95
+ case OPTLYNotificationTypeTrack:
96
+ [self notifyTrackListener: ((TrackListener) listener) args: args];
97
+ break ;
98
+ default :
99
+ listener (args);
100
+ }
100
101
} @catch (NSException *exception) {
101
102
NSString *logMessage = [NSString stringWithFormat: @" Problem calling notify callback. Error: %@ " , exception.reason];
102
103
[_config.logger logMessage: logMessage withLevel: OptimizelyLogLevelError];
@@ -106,15 +107,15 @@ - (void)sendNotifications:(OPTLYNotificationType)type args:(NSArray *)args {
106
107
107
108
#pragma mark - Private Methods
108
109
109
- - (NSInteger )addNotification : (OPTLYNotificationType)type listener : (id <OPTLYNotificationDelegate> )listener {
110
+ - (NSInteger )addNotification : (OPTLYNotificationType)type listener : (GenericListener )listener {
110
111
NSNumber *notificationTypeNumber = [NSNumber numberWithUnsignedInteger: type];
111
112
NSNumber *notificationIdNumber = [NSNumber numberWithUnsignedInteger: _notificationId];
112
113
OPTLYNotificationHolder *notificationHoldersList = _notifications[notificationTypeNumber];
113
114
114
115
if (![_notifications.allKeys containsObject: notificationTypeNumber] || notificationHoldersList.count == 0 ) {
115
116
notificationHoldersList[notificationIdNumber] = listener;
116
117
} else {
117
- for (id <OPTLYNotificationDelegate> notificationListener in notificationHoldersList.allValues ) {
118
+ for (GenericListener notificationListener in notificationHoldersList.allValues ) {
118
119
if (notificationListener == listener) {
119
120
[_config.logger logMessage: @" The notification callback already exists." withLevel: OptimizelyLogLevelError];
120
121
return -1 ;
@@ -126,39 +127,66 @@ - (NSInteger)addNotification:(OPTLYNotificationType)type listener:(id<OPTLYNotif
126
127
return _notificationId++;
127
128
}
128
129
129
- - (BOOL )isNotificationTypeValid : (OPTLYNotificationType)notificationType expectedNotificationType : (OPTLYNotificationType)expectedNotificationType {
130
- if (notificationType != expectedNotificationType) {
131
- NSString *logMessage = [NSString stringWithFormat: @" Invalid notification type provided for %lu listener." , (unsigned long )expectedNotificationType];
130
+ - (void )notifyActivateListener : (ActivateListener)listener args : (NSArray *)args {
131
+
132
+ if (args.count < 5 ) {
133
+ NSString *logMessage = [NSString stringWithFormat: @" Not enough arguments to call %@ for notification callback." , listener];
132
134
[_config.logger logMessage: logMessage withLevel: OptimizelyLogLevelError];
133
- return NO ;
135
+ return ; // Not enough arguments in the array
134
136
}
135
- return YES ;
137
+
138
+ OPTLYExperiment *experiment = (OPTLYExperiment *)args[0 ];
139
+ assert (experiment);
140
+ assert ([experiment isKindOfClass: [OPTLYExperiment class ]]);
141
+
142
+ NSString *userId = (NSString *)args[1 ];
143
+ assert (userId);
144
+ assert ([userId isKindOfClass: [NSString class ]]);
145
+
146
+ NSDictionary *attributes = (NSDictionary *)args[2 ];
147
+ assert (attributes);
148
+ assert ([attributes isKindOfClass: [NSDictionary class ]]);
149
+
150
+ OPTLYVariation *variation = (OPTLYVariation *)args[3 ];
151
+ assert (variation);
152
+ assert ([variation isKindOfClass: [OPTLYVariation class ]]);
153
+
154
+ NSDictionary *logEvent = (NSDictionary *)args[4 ];
155
+ assert (logEvent);
156
+ assert ([logEvent isKindOfClass: [NSDictionary class ]]);
157
+
158
+ listener (experiment, userId, attributes, variation, logEvent);
136
159
}
137
160
138
- - (void )invokeSelector : (id )object selector : (SEL )selector arguments : (NSArray *)arguments {
139
- Method method = class_getInstanceMethod ([object class ], selector);
140
- int argumentCount = method_getNumberOfArguments (method);
161
+ - (void )notifyTrackListener : (TrackListener)listener args : (NSArray *)args {
141
162
142
- // The first two arguments are the hidden arguments self and _cmd
143
- int hiddenArguments = 2 ;
144
-
145
- if (argumentCount > [arguments count ] + hiddenArguments) {
146
- NSString *logMessage = [NSString stringWithFormat: @" Not enough arguments to call %@ for notification Delegate." , object];
163
+ if (args.count < 5 ) {
164
+ NSString *logMessage = [NSString stringWithFormat: @" Not enough arguments to call %@ for notification callback." , listener];
147
165
[_config.logger logMessage: logMessage withLevel: OptimizelyLogLevelError];
148
166
return ; // Not enough arguments in the array
149
167
}
150
168
151
- NSMethodSignature *signature = [object methodSignatureForSelector: selector];
152
- NSInvocation *invocation = [NSInvocation invocationWithMethodSignature: signature];
153
- [invocation setTarget: object];
154
- [invocation setSelector: selector];
169
+ NSString *eventKey = (NSString *)args[0 ];
170
+ assert (eventKey);
171
+ assert ([eventKey isKindOfClass: [NSString class ]]);
155
172
156
- for (int i=0 ; i<[arguments count ]; i++) {
157
- id arg = [arguments objectAtIndex: i];
158
- [invocation setArgument: &arg atIndex: i+hiddenArguments];
159
- }
173
+ NSString *userId = (NSString *)args[1 ];
174
+ assert (userId);
175
+ assert ([userId isKindOfClass: [NSString class ]]);
176
+
177
+ NSDictionary *attributes = (NSDictionary *)args[2 ];
178
+ assert (attributes);
179
+ assert ([attributes isKindOfClass: [NSDictionary class ]]);
180
+
181
+ NSDictionary *eventTags = (NSDictionary *)args[3 ];
182
+ assert (eventTags);
183
+ assert ([eventTags isKindOfClass: [NSDictionary class ]]);
184
+
185
+ NSDictionary *logEvent = (NSDictionary *)args[4 ];
186
+ assert (logEvent);
187
+ assert ([logEvent isKindOfClass: [NSDictionary class ]]);
160
188
161
- [invocation invoke ]; // Invoke the selector
189
+ listener (eventKey, userId, attributes, eventTags, logEvent);
162
190
}
163
191
164
192
@end
0 commit comments