From 83fd1742dab30352ef4e66c705f83b1b2edb0d70 Mon Sep 17 00:00:00 2001 From: Thomas Nardone Date: Fri, 7 Feb 2025 12:39:11 -0800 Subject: [PATCH] Lazily create args in ResponseUtil (#49270) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/49270 Changelog: [Internal] Reviewed By: Abbondanzo Differential Revision: D69316855 fbshipit-source-id: 1c7f58cb9364540c5bf7309b6c2f81162efcc178 --- .../react/modules/network/ResponseUtil.kt | 63 ++++++++----------- 1 file changed, 27 insertions(+), 36 deletions(-) diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/network/ResponseUtil.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/network/ResponseUtil.kt index bc00838d414..cdc970816f3 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/network/ResponseUtil.kt +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/network/ResponseUtil.kt @@ -21,14 +21,13 @@ internal object ResponseUtil { progress: Long, total: Long ) { - val args = + reactContext?.emitDeviceEvent( + "didSendNetworkData", Arguments.createArray().apply { pushInt(requestId) pushInt(progress.toInt()) pushInt(total.toInt()) - } - - reactContext?.emitDeviceEvent("didSendNetworkData", args) + }) } @JvmStatic @@ -39,15 +38,14 @@ internal object ResponseUtil { progress: Long, total: Long ) { - val args = + reactContext?.emitDeviceEvent( + "didReceiveNetworkIncrementalData", Arguments.createArray().apply { pushInt(requestId) pushString(data) pushInt(progress.toInt()) pushInt(total.toInt()) - } - - reactContext?.emitDeviceEvent("didReceiveNetworkIncrementalData", args) + }) } @JvmStatic @@ -57,36 +55,33 @@ internal object ResponseUtil { progress: Long, total: Long ) { - val args = + reactContext?.emitDeviceEvent( + "didReceiveNetworkDataProgress", Arguments.createArray().apply { pushInt(requestId) pushInt(progress.toInt()) pushInt(total.toInt()) - } - - reactContext?.emitDeviceEvent("didReceiveNetworkDataProgress", args) + }) } @JvmStatic fun onDataReceived(reactContext: ReactApplicationContext?, requestId: Int, data: String?) { - val args = + reactContext?.emitDeviceEvent( + "didReceiveNetworkData", Arguments.createArray().apply { pushInt(requestId) pushString(data) - } - - reactContext?.emitDeviceEvent("didReceiveNetworkData", args) + }) } @JvmStatic fun onDataReceived(reactContext: ReactApplicationContext?, requestId: Int, data: WritableMap?) { - val args = + reactContext?.emitDeviceEvent( + "didReceiveNetworkData", Arguments.createArray().apply { pushInt(requestId) pushMap(data) - } - - reactContext?.emitDeviceEvent("didReceiveNetworkData", args) + }) } @JvmStatic @@ -96,28 +91,25 @@ internal object ResponseUtil { error: String?, e: Throwable? ) { - val args = + reactContext?.emitDeviceEvent( + "didCompleteNetworkResponse", Arguments.createArray().apply { pushInt(requestId) pushString(error) - } - - if ((e != null) && (e.javaClass == SocketTimeoutException::class.java)) { - args.pushBoolean(true) // last argument is a time out boolean - } - - reactContext?.emitDeviceEvent("didCompleteNetworkResponse", args) + if (e?.javaClass == SocketTimeoutException::class.java) { + pushBoolean(true) // last argument is a time out boolean + } + }) } @JvmStatic fun onRequestSuccess(reactContext: ReactApplicationContext?, requestId: Int) { - val args = + reactContext?.emitDeviceEvent( + "didCompleteNetworkResponse", Arguments.createArray().apply { pushInt(requestId) pushNull() - } - - reactContext?.emitDeviceEvent("didCompleteNetworkResponse", args) + }) } @JvmStatic @@ -128,14 +120,13 @@ internal object ResponseUtil { headers: WritableMap?, url: String? ) { - val args = + reactContext?.emitDeviceEvent( + "didReceiveNetworkResponse", Arguments.createArray().apply { pushInt(requestId) pushInt(statusCode) pushMap(headers) pushString(url) - } - - reactContext?.emitDeviceEvent("didReceiveNetworkResponse", args) + }) } }