1515 *
1616 */
1717
18+ #import " RNFBFunctionsModule.h"
19+ #import " RNFBApp/RNFBSharedUtils.h"
1820#import < Firebase/Firebase.h>
1921#import < React/RCTUtils.h>
20-
21- #import " RNFBApp/RNFBSharedUtils.h"
22- #import " RNFBFunctionsModule.h"
2322#import < RNFBApp/RNFBRCTEventEmitter.h>
2423
2524@implementation RNFBFunctionsModule
@@ -117,8 +116,8 @@ @implementation RNFBFunctionsModule
117116 NSString *projectId = firebaseApp.options .projectID ?: @" " ;
118117 NSString *urlString;
119118 if (host != nil && port != nil ) {
120- urlString = [NSString stringWithFormat: @" http:// %@ : %@ / %@ / %@ / %@ " ,
121- host, port, projectId, customUrlOrRegion, name];
119+ urlString = [NSString
120+ stringWithFormat: @" http:// %@ : %@ / %@ / %@ / %@ " , host, port, projectId, customUrlOrRegion, name];
122121 } else {
123122 urlString = [NSString stringWithFormat: @" https://%@ -%@ .cloudfunctions.net/%@ " ,
124123 customUrlOrRegion, projectId, name];
@@ -150,60 +149,61 @@ @implementation RNFBFunctionsModule
150149 config.URLCache = nil ;
151150 NSURLSession *session = [NSURLSession sessionWithConfiguration: config];
152151
153- NSURLSessionDataTask *task =
154- [session dataTaskWithRequest: request
155- completionHandler: ^(NSData *data, NSURLResponse *response, NSError *error) {
156- if (error) {
157- NSMutableDictionary *body = [@{@" error" : error.localizedDescription ?: @" error" } mutableCopy];
158- [RNFBSharedUtils sendJSEventForApp: firebaseApp
159- name: RNFB_FUNCTIONS_STREAMING_EVENT
160- body: @{
161- @" listenerId" : listenerId,
162- @" eventName" : RNFB_FUNCTIONS_STREAMING_EVENT,
163- @" body" : body
164- }];
165- } else if ([response isKindOfClass: [NSHTTPURLResponse class ]] &&
166- [(NSHTTPURLResponse *)response statusCode ] >= 400 ) {
167- NSHTTPURLResponse *http = (NSHTTPURLResponse *)response;
168- NSString *msg = [NSString stringWithFormat: @" http_error_%ld _%@ " ,
169- (long )http.statusCode,
170- [NSHTTPURLResponse localizedStringForStatusCode: http.statusCode]];
171- NSMutableDictionary *body = [@{@" error" : msg} mutableCopy];
172- [RNFBSharedUtils sendJSEventForApp: firebaseApp
173- name: RNFB_FUNCTIONS_STREAMING_EVENT
174- body: @{
175- @" listenerId" : listenerId,
176- @" eventName" : RNFB_FUNCTIONS_STREAMING_EVENT,
177- @" body" : body
178- }];
179- } else if (data.length > 0 ) {
180- NSString *payload = [[NSString alloc ] initWithData: data encoding: NSUTF8StringEncoding];
181- // Split into lines (handles SSE or NDJSON)
182- [payload enumerateLinesUsingBlock: ^(NSString *line, BOOL *stop) {
183- if (line.length == 0 ) return ;
184- NSString *trimmed = [line hasPrefix: @" data: " ] ? [line substringFromIndex: 6 ] : line;
185- NSMutableDictionary *body = [@{@" text" : trimmed} mutableCopy];
186- [RNFBSharedUtils sendJSEventForApp: firebaseApp
187- name: RNFB_FUNCTIONS_STREAMING_EVENT
188- body: @{
189- @" listenerId" : listenerId,
190- @" eventName" : RNFB_FUNCTIONS_STREAMING_EVENT,
191- @" body" : body
192- }];
193- }];
194- }
195- // Always emit done at end
196- [RNFBSharedUtils sendJSEventForApp: firebaseApp
197- name: RNFB_FUNCTIONS_STREAMING_EVENT
198- body: @{
199- @" listenerId" : listenerId,
200- @" eventName" : RNFB_FUNCTIONS_STREAMING_EVENT,
201- @" body" : @{@" done" : @YES }
202- }];
203- @synchronized (httpsCallableStreamListeners) {
204- [httpsCallableStreamListeners removeObjectForKey: listenerId];
205- }
206- }];
152+ NSURLSessionDataTask *task = [session
153+ dataTaskWithRequest: request
154+ completionHandler: ^(NSData *data, NSURLResponse *response, NSError *error) {
155+ if (error) {
156+ NSMutableDictionary *body =
157+ [@{@" error" : error.localizedDescription ?: @" error" } mutableCopy];
158+ [RNFBSharedUtils sendJSEventForApp: firebaseApp
159+ name: RNFB_FUNCTIONS_STREAMING_EVENT
160+ body: @{
161+ @" listenerId" : listenerId,
162+ @" eventName" : RNFB_FUNCTIONS_STREAMING_EVENT,
163+ @" body" : body
164+ }];
165+ } else if ([response isKindOfClass: [NSHTTPURLResponse class ]] &&
166+ [(NSHTTPURLResponse *)response statusCode ] >= 400 ) {
167+ NSHTTPURLResponse *http = (NSHTTPURLResponse *)response;
168+ NSString *msg = [NSString
169+ stringWithFormat: @" http_error_%ld _%@ " , (long )http.statusCode,
170+ [NSHTTPURLResponse localizedStringForStatusCode: http.statusCode]];
171+ NSMutableDictionary *body = [@{@" error" : msg} mutableCopy];
172+ [RNFBSharedUtils sendJSEventForApp: firebaseApp
173+ name: RNFB_FUNCTIONS_STREAMING_EVENT
174+ body: @{
175+ @" listenerId" : listenerId,
176+ @" eventName" : RNFB_FUNCTIONS_STREAMING_EVENT,
177+ @" body" : body
178+ }];
179+ } else if (data.length > 0 ) {
180+ NSString *payload = [[NSString alloc ] initWithData: data encoding: NSUTF8StringEncoding];
181+ // Split into lines (handles SSE or NDJSON)
182+ [payload enumerateLinesUsingBlock: ^(NSString *line, BOOL *stop) {
183+ if (line.length == 0 ) return ;
184+ NSString *trimmed = [line hasPrefix: @" data: " ] ? [line substringFromIndex: 6 ] : line;
185+ NSMutableDictionary *body = [@{@" text" : trimmed} mutableCopy];
186+ [RNFBSharedUtils sendJSEventForApp: firebaseApp
187+ name: RNFB_FUNCTIONS_STREAMING_EVENT
188+ body: @{
189+ @" listenerId" : listenerId,
190+ @" eventName" : RNFB_FUNCTIONS_STREAMING_EVENT,
191+ @" body" : body
192+ }];
193+ }];
194+ }
195+ // Always emit done at end
196+ [RNFBSharedUtils sendJSEventForApp: firebaseApp
197+ name: RNFB_FUNCTIONS_STREAMING_EVENT
198+ body: @{
199+ @" listenerId" : listenerId,
200+ @" eventName" : RNFB_FUNCTIONS_STREAMING_EVENT,
201+ @" body" : @{@" done" : @YES }
202+ }];
203+ @synchronized (httpsCallableStreamListeners) {
204+ [httpsCallableStreamListeners removeObjectForKey: listenerId];
205+ }
206+ }];
207207
208208 @synchronized (httpsCallableStreamListeners) {
209209 httpsCallableStreamListeners[listenerId] = task;
@@ -229,7 +229,7 @@ @implementation RNFBFunctionsModule
229229 if (!httpsCallableStreamListeners) {
230230 httpsCallableStreamListeners = [NSMutableDictionary dictionary ];
231231 }
232-
232+
233233 // Use the provided URL directly
234234 NSURLComponents *components = [NSURLComponents componentsWithString: url];
235235 if (components == nil ) {
@@ -243,14 +243,14 @@ @implementation RNFBFunctionsModule
243243 }];
244244 return ;
245245 }
246-
246+
247247 // Override to emulator if provided
248248 if (host != nil && port != nil ) {
249249 components.scheme = @" http" ;
250250 components.host = host;
251251 components.port = port;
252252 }
253-
253+
254254 NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL: [components URL ]];
255255 [request setHTTPMethod: @" GET" ];
256256 [request setValue: @" text/event-stream, application/x-ndjson, */*" forHTTPHeaderField: @" Accept" ];
@@ -260,60 +260,61 @@ @implementation RNFBFunctionsModule
260260 config.URLCache = nil ;
261261 NSURLSession *session = [NSURLSession sessionWithConfiguration: config];
262262
263- NSURLSessionDataTask *task =
264- [session dataTaskWithRequest: request
265- completionHandler: ^(NSData *data, NSURLResponse *response, NSError *error) {
266- if (error) {
267- NSMutableDictionary *body = [@{@" error" : error.localizedDescription ?: @" error" } mutableCopy];
268- [RNFBSharedUtils sendJSEventForApp: firebaseApp
269- name: RNFB_FUNCTIONS_STREAMING_EVENT
270- body: @{
271- @" listenerId" : listenerId,
272- @" eventName" : RNFB_FUNCTIONS_STREAMING_EVENT,
273- @" body" : body
274- }];
275- } else if ([response isKindOfClass: [NSHTTPURLResponse class ]] &&
276- [(NSHTTPURLResponse *)response statusCode ] >= 400 ) {
277- NSHTTPURLResponse *http = (NSHTTPURLResponse *)response;
278- NSString *msg = [NSString stringWithFormat: @" http_error_%ld _%@ " ,
279- (long )http.statusCode,
280- [NSHTTPURLResponse localizedStringForStatusCode: http.statusCode]];
281- NSMutableDictionary *body = [@{@" error" : msg} mutableCopy];
282- [RNFBSharedUtils sendJSEventForApp: firebaseApp
283- name: RNFB_FUNCTIONS_STREAMING_EVENT
284- body: @{
285- @" listenerId" : listenerId,
286- @" eventName" : RNFB_FUNCTIONS_STREAMING_EVENT,
287- @" body" : body
288- }];
289- } else if (data.length > 0 ) {
290- NSString *payload = [[NSString alloc ] initWithData: data encoding: NSUTF8StringEncoding];
291- // Split into lines (handles SSE or NDJSON)
292- [payload enumerateLinesUsingBlock: ^(NSString *line, BOOL *stop) {
293- if (line.length == 0 ) return ;
294- NSString *trimmed = [line hasPrefix: @" data: " ] ? [line substringFromIndex: 6 ] : line;
295- NSMutableDictionary *body = [@{@" text" : trimmed} mutableCopy];
296- [RNFBSharedUtils sendJSEventForApp: firebaseApp
297- name: RNFB_FUNCTIONS_STREAMING_EVENT
298- body: @{
299- @" listenerId" : listenerId,
300- @" eventName" : RNFB_FUNCTIONS_STREAMING_EVENT,
301- @" body" : body
302- }];
303- }];
304- }
305- // Always emit done at end
306- [RNFBSharedUtils sendJSEventForApp: firebaseApp
307- name: RNFB_FUNCTIONS_STREAMING_EVENT
308- body: @{
309- @" listenerId" : listenerId,
310- @" eventName" : RNFB_FUNCTIONS_STREAMING_EVENT,
311- @" body" : @{@" done" : @YES }
312- }];
313- @synchronized (httpsCallableStreamListeners) {
314- [httpsCallableStreamListeners removeObjectForKey: listenerId];
315- }
316- }];
263+ NSURLSessionDataTask *task = [session
264+ dataTaskWithRequest: request
265+ completionHandler: ^(NSData *data, NSURLResponse *response, NSError *error) {
266+ if (error) {
267+ NSMutableDictionary *body =
268+ [@{@" error" : error.localizedDescription ?: @" error" } mutableCopy];
269+ [RNFBSharedUtils sendJSEventForApp: firebaseApp
270+ name: RNFB_FUNCTIONS_STREAMING_EVENT
271+ body: @{
272+ @" listenerId" : listenerId,
273+ @" eventName" : RNFB_FUNCTIONS_STREAMING_EVENT,
274+ @" body" : body
275+ }];
276+ } else if ([response isKindOfClass: [NSHTTPURLResponse class ]] &&
277+ [(NSHTTPURLResponse *)response statusCode ] >= 400 ) {
278+ NSHTTPURLResponse *http = (NSHTTPURLResponse *)response;
279+ NSString *msg = [NSString
280+ stringWithFormat: @" http_error_%ld _%@ " , (long )http.statusCode,
281+ [NSHTTPURLResponse localizedStringForStatusCode: http.statusCode]];
282+ NSMutableDictionary *body = [@{@" error" : msg} mutableCopy];
283+ [RNFBSharedUtils sendJSEventForApp: firebaseApp
284+ name: RNFB_FUNCTIONS_STREAMING_EVENT
285+ body: @{
286+ @" listenerId" : listenerId,
287+ @" eventName" : RNFB_FUNCTIONS_STREAMING_EVENT,
288+ @" body" : body
289+ }];
290+ } else if (data.length > 0 ) {
291+ NSString *payload = [[NSString alloc ] initWithData: data encoding: NSUTF8StringEncoding];
292+ // Split into lines (handles SSE or NDJSON)
293+ [payload enumerateLinesUsingBlock: ^(NSString *line, BOOL *stop) {
294+ if (line.length == 0 ) return ;
295+ NSString *trimmed = [line hasPrefix: @" data: " ] ? [line substringFromIndex: 6 ] : line;
296+ NSMutableDictionary *body = [@{@" text" : trimmed} mutableCopy];
297+ [RNFBSharedUtils sendJSEventForApp: firebaseApp
298+ name: RNFB_FUNCTIONS_STREAMING_EVENT
299+ body: @{
300+ @" listenerId" : listenerId,
301+ @" eventName" : RNFB_FUNCTIONS_STREAMING_EVENT,
302+ @" body" : body
303+ }];
304+ }];
305+ }
306+ // Always emit done at end
307+ [RNFBSharedUtils sendJSEventForApp: firebaseApp
308+ name: RNFB_FUNCTIONS_STREAMING_EVENT
309+ body: @{
310+ @" listenerId" : listenerId,
311+ @" eventName" : RNFB_FUNCTIONS_STREAMING_EVENT,
312+ @" body" : @{@" done" : @YES }
313+ }];
314+ @synchronized (httpsCallableStreamListeners) {
315+ [httpsCallableStreamListeners removeObjectForKey: listenerId];
316+ }
317+ }];
317318
318319 @synchronized (httpsCallableStreamListeners) {
319320 httpsCallableStreamListeners[listenerId] = task;
0 commit comments