From 96fdaa541e30e1f52a4649e747f2372bac28b4cf Mon Sep 17 00:00:00 2001 From: Ramanpreet Nara Date: Wed, 3 Jun 2020 20:45:45 -0700 Subject: [PATCH] Cache constants for MP Search NativeModules Summary: TurboModules doesn't cache invocations of getConstants(). This diff looks at which NativeModules' getConstants() methods gets repeated called in Marketplace, and caches those invocations in JS. Reviewed By: fkgozali Differential Revision: D21874123 fbshipit-source-id: a44b98b3ac8621f67c9c0f3b7c4003a561d1e15d --- Libraries/Blob/NativeBlobModule.js | 36 ++++++++++++++++++- .../NativeModules/specs/NativeSourceCode.js | 17 ++++++++- 2 files changed, 51 insertions(+), 2 deletions(-) diff --git a/Libraries/Blob/NativeBlobModule.js b/Libraries/Blob/NativeBlobModule.js index ab2572bd0ea..d58aeeab6eb 100644 --- a/Libraries/Blob/NativeBlobModule.js +++ b/Libraries/Blob/NativeBlobModule.js @@ -23,4 +23,38 @@ export interface Spec extends TurboModule { +release: (blobId: string) => void; } -export default (TurboModuleRegistry.get('BlobModule'): ?Spec); +const NativeModule = TurboModuleRegistry.get('BlobModule'); + +let constants = null; +let NativeBlobModule = null; + +if (NativeModule != null) { + NativeBlobModule = { + getConstants(): {|BLOB_URI_SCHEME: ?string, BLOB_URI_HOST: ?string|} { + if (constants == null) { + constants = NativeModule.getConstants(); + } + return constants; + }, + addNetworkingHandler(): void { + NativeModule.addNetworkingHandler(); + }, + addWebSocketHandler(id: number): void { + NativeModule.addWebSocketHandler(id); + }, + removeWebSocketHandler(id: number): void { + NativeModule.removeWebSocketHandler(id); + }, + sendOverSocket(blob: Object, socketID: number): void { + NativeModule.sendOverSocket(blob, socketID); + }, + createFromParts(parts: Array, withId: string): void { + NativeModule.createFromParts(parts, withId); + }, + release(blobId: string): void { + NativeModule.release(blobId); + }, + }; +} + +export default (NativeBlobModule: ?Spec); diff --git a/Libraries/NativeModules/specs/NativeSourceCode.js b/Libraries/NativeModules/specs/NativeSourceCode.js index 7bcd16bdf8e..903123c94da 100644 --- a/Libraries/NativeModules/specs/NativeSourceCode.js +++ b/Libraries/NativeModules/specs/NativeSourceCode.js @@ -19,4 +19,19 @@ export interface Spec extends TurboModule { |}; } -export default (TurboModuleRegistry.getEnforcing('SourceCode'): Spec); +const NativeModule = TurboModuleRegistry.getEnforcing('SourceCode'); +let constants = null; + +const NativeSourceCode = { + getConstants(): {| + scriptURL: string, + |} { + if (constants == null) { + constants = NativeModule.getConstants(); + } + + return constants; + }, +}; + +export default NativeSourceCode;