11#import " MetaMapRNSdk.h"
2+ #import < React/RCTEventEmitter.h>
23#import < React/RCTUtils.h>
34
4- static NSString *const kEventVerificationSuccess = @" verificationSuccess" ;
5+ static NSString *const kEventVerificationSuccess = @" verificationSuccess" ;
56static NSString *const kEventVerificationCanceled = @" verificationCanceled" ;
6- static NSString *const kEventVerificationStarted = @" verificationStarted" ;
7+ static NSString *const kEventVerificationStarted = @" verificationStarted" ;
78
8- @interface MetaMapRNSdk () <MetaMapButtonResultDelegate>
9- @property (nonatomic , assign ) BOOL hasListeners;
10- @end
11-
12- @implementation MetaMapRNSdk
9+ @implementation MetaMapRNSdk {
10+ BOOL _hasListeners;
11+ NSInteger _listenerCount;
12+ }
1313
1414RCT_EXPORT_MODULE ()
1515
16- #pragma mark - React Native API
16+ #pragma mark - RN 0.71+ listener contract
17+ - (void )addListener : (NSString *)eventName
18+ {
19+ [super addListener: eventName];
20+ _listenerCount += 1 ;
21+ _hasListeners = YES ;
22+ }
1723
18- RCT_EXPORT_METHOD (showFlow
19- :(NSString *)
20- clientId
21- flowId
22- :(NSString *)
23- flowId
24- metadata
25- :(NSDictionary *)metadata)
24+ - (void )removeListeners : (double )count
2625{
27- [self startFlowWithClientId: clientId
28- flowId: flowId
29- configurationId: nil
30- encryptionConfigurationId: nil
31- metadata: metadata];
26+ [super removeListeners: count];
27+ _listenerCount -= (NSInteger )count;
28+ if (_listenerCount <= 0 ) {
29+ _listenerCount = 0 ;
30+ _hasListeners = NO ;
31+ }
3232}
3333
34- RCT_EXPORT_METHOD (showFlowWithConfigurationId
35- :(NSString *)
36- clientId
37- flowId
38- :(NSString *)
39- flowId
40- configurationId
41- :(NSString *)
42- configurationId
43- encryptionConfigurationId
44- :(NSString *)
45- encryptionConfigurationId
46- metadata
47- :(NSDictionary *)metadata)
34+ #pragma mark - Required overrides
35+ + (BOOL )requiresMainQueueSetup
4836{
49- [self startFlowWithClientId: clientId
50- flowId: flowId
51- configurationId: configurationId
52- encryptionConfigurationId: encryptionConfigurationId
53- metadata: metadata];
37+ // We show a native UI immediately; initialise on main.
38+ return YES ;
5439}
5540
56- #pragma mark - Private helpers
41+ - (NSArray <NSString *> *)supportedEvents
42+ {
43+ return @[
44+ kEventVerificationSuccess ,
45+ kEventVerificationCanceled ,
46+ kEventVerificationStarted
47+ ];
48+ }
49+
50+ - (void )invalidate
51+ {
52+ // Called when the bridge is deallocated (e.g. hot reload)
53+ _hasListeners = NO ;
54+ _listenerCount = 0 ;
55+ }
56+
57+ #pragma mark - React‑exposed API
58+ RCT_EXPORT_METHOD (showFlow:(NSString *)clientId
59+ flowId:(NSString *)flowId
60+ metadata:(NSDictionary *)metadata)
61+ {
62+ [self startFlowWithClientId: clientId
63+ flowId: flowId
64+ configurationId: nil
65+ encryptionConfigurationId: nil
66+ metadata: metadata];
67+ }
5768
69+ RCT_EXPORT_METHOD (showFlowWithConfigurationId:(NSString *)clientId
70+ flowId:(NSString *)flowId
71+ configurationId:(NSString *)configurationId
72+ encryptionConfigurationId:(NSString *)encryptionConfigurationId
73+ metadata:(NSDictionary *)metadata)
74+ {
75+ [self startFlowWithClientId: clientId
76+ flowId: flowId
77+ configurationId: configurationId
78+ encryptionConfigurationId: encryptionConfigurationId
79+ metadata: metadata];
80+ }
81+
82+ #pragma mark - Internal helper
5883- (void )startFlowWithClientId : (NSString *)clientId
5984 flowId : (NSString *)flowId
6085 configurationId : (NSString *)configurationId
6186 encryptionConfigurationId : (NSString *)encryptionConfigurationId
62- metadata : (NSDictionary *)metadata {
63- dispatch_async (dispatch_get_main_queue (), ^{
64- NSMutableDictionary *mutableMetadata = metadata
65- ? [metadata mutableCopy ]
66- : [NSMutableDictionary dictionary ];
67- mutableMetadata[@" sdkType" ] = @" react-native-ios" ;
68-
69- [MetaMap.shared showMetaMapFlowWithClientId: clientId
70- flowId: flowId
71- configurationId: configurationId
72- encryptionConfigurationId: encryptionConfigurationId
73- metadata: mutableMetadata];
74-
75- [MetaMapButtonResult shared ].delegate = self;
76- self.hasListeners = YES ;
77- });
78- }
79-
80- - (NSArray
81-
82- <NSString *> *)supportedEvents
87+ metadata : (NSDictionary *)metadata
8388{
84- return @[kEventVerificationSuccess ,
85- kEventVerificationCanceled ,
86- kEventVerificationStarted ];
89+ dispatch_async (dispatch_get_main_queue (), ^{
90+ NSMutableDictionary *mutableMetadata =
91+ metadata ? [metadata mutableCopy ] : [NSMutableDictionary dictionary ];
92+ mutableMetadata[@" sdkType" ] = @" react-native-ios" ;
93+
94+ [MetaMap.shared showMetaMapFlowWithClientId: clientId
95+ flowId: flowId
96+ configurationId: configurationId
97+ encryptionConfigurationId: encryptionConfigurationId
98+ metadata: mutableMetadata];
99+
100+ [MetaMapButtonResult shared ].delegate = self;
101+ });
87102}
88103
89104#pragma mark - MetaMapButtonResultDelegate
90-
91105- (void )verificationCreatedWithIdentityId : (NSString *)identityId
92- verificationID : (NSString *)verificationID {
93- if (self.hasListeners ) {
94- [self sendEventWithName: kEventVerificationStarted
95- body: @{@" identityId" : identityId ?: [NSNull null ],
96- @" verificationId" : verificationID ?: [NSNull null ]}];
97- }
106+ verificationID : (NSString *)verificationID
107+ {
108+ if (_hasListeners) {
109+ [self sendEventWithName: kEventVerificationStarted
110+ body: @{@" identityId" : identityId ?: [NSNull null ],
111+ @" verificationId" : verificationID ?: [NSNull null ]}];
112+ }
98113}
99114
100115- (void )verificationSuccessWithIdentityId : (NSString *)identityId
101- verificationID : (NSString *)verificationID {
102- if (self.hasListeners ) {
103- [self sendEventWithName: kEventVerificationSuccess
104- body: @{@" identityId" : identityId ?: [NSNull null ],
105- @" verificationId" : verificationID ?: [NSNull null ]}];
106- }
116+ verificationID : (NSString *)verificationID
117+ {
118+ if (_hasListeners) {
119+ [self sendEventWithName: kEventVerificationSuccess
120+ body: @{@" identityId" : identityId ?: [NSNull null ],
121+ @" verificationId" : verificationID ?: [NSNull null ]}];
122+ }
107123}
108124
109125- (void )verificationCancelledWithIdentityId : (NSString *)identityId
110- verificationID : (NSString *)verificationID {
111- if (self.hasListeners ) {
112- [self sendEventWithName: kEventVerificationCanceled
113- body: @{@" identityId" : identityId ?: [NSNull null ],
114- @" verificationId" : verificationID ?: [NSNull null ]}];
115- }
126+ verificationID : (NSString *)verificationID
127+ {
128+ if (_hasListeners) {
129+ [self sendEventWithName: kEventVerificationCanceled
130+ body: @{@" identityId" : identityId ?: [NSNull null ],
131+ @" verificationId" : verificationID ?: [NSNull null ]}];
132+ }
116133}
117-
118- @end
134+ @end
0 commit comments