-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathSCGatewayEventEmitter.js
More file actions
189 lines (164 loc) · 5.12 KB
/
Copy pathSCGatewayEventEmitter.js
File metadata and controls
189 lines (164 loc) · 5.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import { NativeEventEmitter, NativeModules } from 'react-native';
/**
* @typedef {Object} GatewayEvent
* @property {string} type - Event type
* @property {any} data - Event payload data
* @property {number} timestamp - Event timestamp
*
* @typedef {Object} GatewayEventSubscription
* @property {() => void} remove - Method to unsubscribe from gateway events
*
*/
const nativeModule = NativeModules.SCGatewayBridgeEmitter;
export const SCGatewayEventTypes = {
ANALYTICS_EVENT: 'scgateway_analytics_event',
SUPER_PROPERTIES_UPDATED: 'scgateway_super_properties_updated',
USER_RESET: 'scgateway_user_reset',
USER_IDENTIFY: 'scgateway_user_identify',
SMALLPLUG_ANALYTICS_EVENT: 'smallplug_analytics_event',
};
const SCGatewayNotificationEvent = 'scg_notification';
class SCGatewayEvents {
constructor() {
this.eventEmitter = null;
this.subscriptions = [];
this.initialize();
}
get isInitialized() {
return this.eventEmitter !== null;
}
initialize() {
if (nativeModule) {
this.eventEmitter = new NativeEventEmitter(nativeModule);
} else {
console.warn('[SCGatewayEvents] Native module not available');
}
}
// ===== GATEWAY EVENT METHODS =====
/**
* Subscribe to Gateway Events
* @param {(event: GatewayEvent) => void} callback - Callback function to handle gateway events
* @returns {GatewayEventSubscription} subscription - Subscription object with remove() method
*/
subscribeToGatewayEvents(callback) {
if (!this.isInitialized) {
console.warn('[SCGatewayEvents] Event emitter not initialized');
return null;
}
if (typeof callback !== 'function') {
console.warn(
'[SCGatewayEvents] Invalid callback provided for subscription'
);
return null;
}
const subscription = this.eventEmitter.addListener(
SCGatewayNotificationEvent,
(jsonString) => {
if (!jsonString) {
console.warn('[SCGatewayEvents] Received null/undefined event data');
return;
}
let eventData;
try {
eventData = JSON.parse(jsonString);
} catch (error) {
console.warn(
'[SCGatewayEvents] Failed to parse event JSON:',
error,
'Raw data:',
jsonString
);
return;
}
if (!eventData.type) {
console.warn(
'[SCGatewayEvents] Dropping event - missing event type:',
eventData
);
return;
}
const normalizedEvent = {
type: eventData.type,
data: eventData.data,
timestamp: eventData.timestamp || Date.now(),
};
callback(normalizedEvent);
}
);
this.subscriptions.push(subscription);
return subscription;
}
/**
* Subscribe to SmallPlug / DM analytics events streamed during a launchSmallplug session.
* Events have shape: { eventName: string, data: object, timestamp: number }
* @param {(event: {eventName: string, data: any, timestamp: number}) => void} callback
* @returns {GatewayEventSubscription}
*/
subscribeToSmallplugEvents(callback) {
if (!this.isInitialized) {
console.warn('[SCGatewayEvents] Event emitter not initialized');
return null;
}
if (typeof callback !== 'function') {
console.warn(
'[SCGatewayEvents] Invalid callback provided for SmallPlug subscription'
);
return null;
}
const subscription = this.eventEmitter.addListener(
SCGatewayNotificationEvent,
(jsonString) => {
if (!jsonString) return;
let parsed;
try {
parsed = JSON.parse(jsonString);
} catch (e) {
return;
}
if (parsed.type !== 'smallplug_analytics_event') return;
const { eventName, data } = parsed.data ?? {};
callback({
eventName,
data,
timestamp: parsed.timestamp ?? Date.now(),
});
}
);
this.subscriptions.push(subscription);
return subscription;
}
/**
* Unsubscribe from SmallPlug analytics events
* @param {GatewayEventSubscription} subscription - Subscription returned from subscribeToSmallplugEvents
*/
unsubscribeFromSmallplugEvents(subscription) {
if (subscription && typeof subscription.remove === 'function') {
subscription.remove();
this.subscriptions = this.subscriptions.filter(
(sub) => sub !== subscription
);
}
}
/**
* Unsubscribe from Gateway Events
* @param {GatewayEventSubscription} subscription - Subscription returned from subscribeToGatewayEvents
*/
unsubscribeFromGatewayEvents(subscription) {
if (subscription && typeof subscription.remove === 'function') {
subscription.remove();
this.subscriptions = this.subscriptions.filter(
(sub) => sub !== subscription
);
}
}
cleanup() {
this.subscriptions.forEach((subscription) => {
if (subscription && typeof subscription.remove === 'function') {
subscription.remove();
}
});
this.subscriptions = [];
}
}
const scGatewayEventManager = new SCGatewayEvents();
export default scGatewayEventManager;