Skip to content

Commit

Permalink
Cache constants for MP Search NativeModules
Browse files Browse the repository at this point in the history
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
  • Loading branch information
RSNara authored and facebook-github-bot committed Jun 4, 2020
1 parent 4e9c428 commit 96fdaa5
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 2 deletions.
36 changes: 35 additions & 1 deletion Libraries/Blob/NativeBlobModule.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,38 @@ export interface Spec extends TurboModule {
+release: (blobId: string) => void;
}

export default (TurboModuleRegistry.get<Spec>('BlobModule'): ?Spec);
const NativeModule = TurboModuleRegistry.get<Spec>('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<Object>, withId: string): void {
NativeModule.createFromParts(parts, withId);
},
release(blobId: string): void {
NativeModule.release(blobId);
},
};
}

export default (NativeBlobModule: ?Spec);
17 changes: 16 additions & 1 deletion Libraries/NativeModules/specs/NativeSourceCode.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,19 @@ export interface Spec extends TurboModule {
|};
}

export default (TurboModuleRegistry.getEnforcing<Spec>('SourceCode'): Spec);
const NativeModule = TurboModuleRegistry.getEnforcing<Spec>('SourceCode');
let constants = null;

const NativeSourceCode = {
getConstants(): {|
scriptURL: string,
|} {
if (constants == null) {
constants = NativeModule.getConstants();
}

return constants;
},
};

export default NativeSourceCode;

0 comments on commit 96fdaa5

Please sign in to comment.