From 026e3d4afefe2efc94580cc2a56264de87669789 Mon Sep 17 00:00:00 2001 From: Evan Bacon Date: Thu, 7 Feb 2019 03:41:53 -0800 Subject: [PATCH 1/5] Update ImageStore.js # Why - Related: #23313 - ImageStore is **iOS only** - base64 is very inefficient with the React Native bridge - Ideally the `FileSystem` solutions will integrate Turbo Modules to circumvent bridge issues by passing direct references to files. # How - Added a deprecation notice with info about third-party solutions for getting a base64-encoded string. - Added missing warnings for unimplemented platform methods. --- Libraries/Image/ImageStore.js | 33 +++++++++++++++++++++++++++++---- 1 file changed, 29 insertions(+), 4 deletions(-) diff --git a/Libraries/Image/ImageStore.js b/Libraries/Image/ImageStore.js index 5373020376a5f1..2e12a323b96e21 100644 --- a/Libraries/Image/ImageStore.js +++ b/Libraries/Image/ImageStore.js @@ -11,16 +11,30 @@ const RCTImageStoreManager = require('NativeModules').ImageStoreManager; +const Platform = require('Platform'); + +function warnDeprecated(): void { + console.warn(`react-native: ImageStore is deprecated. + To get a base64-encoded string from a local image use either of the following third-party libraries: + * expo-file-system: \`readAsStringAsync(filepath, 'base64')\` + * react-native-fs: \`readFile(filepath, 'base64')\``) +} + +function warnUnimplementedMethod(methodName: string): void { + console.warn(`react-native: ImageStore.${methodName}() is not implemented on ${Platform.OS}`) +} + class ImageStore { /** * Check if the ImageStore contains image data for the specified URI. * @platform ios */ static hasImageForTag(uri: string, callback: (hasImage: boolean) => void) { + warnDeprecated(); if (RCTImageStoreManager.hasImageForTag) { RCTImageStoreManager.hasImageForTag(uri, callback); } else { - console.warn('hasImageForTag() not implemented'); + warnUnimplementedMethod('hasImageForTag'); } } @@ -33,10 +47,11 @@ class ImageStore { * @platform ios */ static removeImageForTag(uri: string) { + warnDeprecated(); if (RCTImageStoreManager.removeImageForTag) { RCTImageStoreManager.removeImageForTag(uri); } else { - console.warn('removeImageForTag() not implemented'); + warnUnimplementedMethod('removeImageForTag'); } } @@ -56,7 +71,12 @@ class ImageStore { success: (uri: string) => void, failure: (error: any) => void, ) { - RCTImageStoreManager.addImageFromBase64(base64ImageData, success, failure); + warnDeprecated(); + if (RCTImageStoreManager.addImageFromBase64) { + RCTImageStoreManager.addImageFromBase64(base64ImageData, success, failure); + } else { + warnUnimplementedMethod('addImageFromBase64'); + } } /** @@ -75,7 +95,12 @@ class ImageStore { success: (base64ImageData: string) => void, failure: (error: any) => void, ) { - RCTImageStoreManager.getBase64ForTag(uri, success, failure); + warnDeprecated(); + if (RCTImageStoreManager.getBase64ForTag) { + RCTImageStoreManager.getBase64ForTag(uri, success, failure); + } else { + warnUnimplementedMethod('getBase64ForTag'); + } } } From 03b447cc62fc2ad6aefc4ea02cc3210f5552e7ea Mon Sep 17 00:00:00 2001 From: Evan Bacon Date: Fri, 8 Feb 2019 13:34:38 -0800 Subject: [PATCH 2/5] Moved warning --- Libraries/Image/ImageStore.js | 14 +++++++++++--- .../react-native/react-native-implementation.js | 4 ++++ 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/Libraries/Image/ImageStore.js b/Libraries/Image/ImageStore.js index 2e12a323b96e21..1b275a9289fede 100644 --- a/Libraries/Image/ImageStore.js +++ b/Libraries/Image/ImageStore.js @@ -17,11 +17,15 @@ function warnDeprecated(): void { console.warn(`react-native: ImageStore is deprecated. To get a base64-encoded string from a local image use either of the following third-party libraries: * expo-file-system: \`readAsStringAsync(filepath, 'base64')\` - * react-native-fs: \`readFile(filepath, 'base64')\``) + * react-native-fs: \`readFile(filepath, 'base64')\``); } function warnUnimplementedMethod(methodName: string): void { - console.warn(`react-native: ImageStore.${methodName}() is not implemented on ${Platform.OS}`) + console.warn( + `react-native: ImageStore.${methodName}() is not implemented on ${ + Platform.OS + }`, + ); } class ImageStore { @@ -73,7 +77,11 @@ class ImageStore { ) { warnDeprecated(); if (RCTImageStoreManager.addImageFromBase64) { - RCTImageStoreManager.addImageFromBase64(base64ImageData, success, failure); + RCTImageStoreManager.addImageFromBase64( + base64ImageData, + success, + failure, + ); } else { warnUnimplementedMethod('addImageFromBase64'); } diff --git a/Libraries/react-native/react-native-implementation.js b/Libraries/react-native/react-native-implementation.js index 3ff7b9ed3b495e..8f05d8916e68c5 100644 --- a/Libraries/react-native/react-native-implementation.js +++ b/Libraries/react-native/react-native-implementation.js @@ -49,6 +49,10 @@ const ReactNative = { return require('ImageEditor'); }, get ImageStore() { + console.warn(`react-native: ImageStore is deprecated. + To get a base64-encoded string from a local image use either of the following third-party libraries: + * expo-file-system: \`readAsStringAsync(filepath, 'base64')\` + * react-native-fs: \`readFile(filepath, 'base64')\``); return require('ImageStore'); }, get InputAccessoryView() { From 9af9dad1692b7b780f8ae793b11b50fc30a9f65c Mon Sep 17 00:00:00 2001 From: Evan Bacon Date: Fri, 8 Feb 2019 13:45:43 -0800 Subject: [PATCH 3/5] Update react-native-implementation.js --- Libraries/react-native/react-native-implementation.js | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/Libraries/react-native/react-native-implementation.js b/Libraries/react-native/react-native-implementation.js index 896d718d118950..8fcffafc82f84e 100644 --- a/Libraries/react-native/react-native-implementation.js +++ b/Libraries/react-native/react-native-implementation.js @@ -50,10 +50,13 @@ module.exports = { return require('ImageEditor'); }, get ImageStore() { - console.warn(`react-native: ImageStore is deprecated. - To get a base64-encoded string from a local image use either of the following third-party libraries: - * expo-file-system: \`readAsStringAsync(filepath, 'base64')\` - * react-native-fs: \`readFile(filepath, 'base64')\``); + warnOnce( + 'imagestore-deprecation', + 'ImageStore is deprecated and will be removed in a future release. ' + + 'To get a base64-encoded string from a local image use either of the following third-party libraries:' + + '* expo-file-system: `readAsStringAsync(filepath, \'base64\')`' + + '* react-native-fs: `readFile(filepath, \'base64\')`', + ); return require('ImageStore'); }, get InputAccessoryView() { From ce7e518e9e5063906320541cc0d770341d96268e Mon Sep 17 00:00:00 2001 From: Evan Bacon Date: Fri, 8 Feb 2019 13:45:47 -0800 Subject: [PATCH 4/5] Update ImageStore.js --- Libraries/Image/ImageStore.js | 14 +++----------- 1 file changed, 3 insertions(+), 11 deletions(-) diff --git a/Libraries/Image/ImageStore.js b/Libraries/Image/ImageStore.js index c8ec95f37783a8..8ba5c3162be83d 100644 --- a/Libraries/Image/ImageStore.js +++ b/Libraries/Image/ImageStore.js @@ -13,15 +13,11 @@ const RCTImageStoreManager = require('NativeModules').ImageStoreManager; const Platform = require('Platform'); -function warnDeprecated(): void { - console.warn(`react-native: ImageStore is deprecated. - To get a base64-encoded string from a local image use either of the following third-party libraries: - * expo-file-system: \`readAsStringAsync(filepath, 'base64')\` - * react-native-fs: \`readFile(filepath, 'base64')\``); -} +const warnOnce = require('warnOnce'); function warnUnimplementedMethod(methodName: string): void { - console.warn( + warnOnce( + `imagestore-${methodName}`, `react-native: ImageStore.${methodName}() is not implemented on ${ Platform.OS }`, @@ -34,7 +30,6 @@ class ImageStore { * @platform ios */ static hasImageForTag(uri: string, callback: (hasImage: boolean) => void) { - warnDeprecated(); if (RCTImageStoreManager.hasImageForTag) { RCTImageStoreManager.hasImageForTag(uri, callback); } else { @@ -51,7 +46,6 @@ class ImageStore { * @platform ios */ static removeImageForTag(uri: string) { - warnDeprecated(); if (RCTImageStoreManager.removeImageForTag) { RCTImageStoreManager.removeImageForTag(uri); } else { @@ -75,7 +69,6 @@ class ImageStore { success: (uri: string) => void, failure: (error: any) => void, ) { - warnDeprecated(); if (RCTImageStoreManager.addImageFromBase64) { RCTImageStoreManager.addImageFromBase64( base64ImageData, @@ -103,7 +96,6 @@ class ImageStore { success: (base64ImageData: string) => void, failure: (error: any) => void, ) { - warnDeprecated(); if (RCTImageStoreManager.getBase64ForTag) { RCTImageStoreManager.getBase64ForTag(uri, success, failure); } else { From a479b2260ee8d418ffaff197d3cdcddbd8ce56be Mon Sep 17 00:00:00 2001 From: Evan Bacon Date: Fri, 8 Feb 2019 13:59:30 -0800 Subject: [PATCH 5/5] Update react-native-implementation.js --- Libraries/react-native/react-native-implementation.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Libraries/react-native/react-native-implementation.js b/Libraries/react-native/react-native-implementation.js index 8fcffafc82f84e..849b7f066bfdc4 100644 --- a/Libraries/react-native/react-native-implementation.js +++ b/Libraries/react-native/react-native-implementation.js @@ -54,8 +54,8 @@ module.exports = { 'imagestore-deprecation', 'ImageStore is deprecated and will be removed in a future release. ' + 'To get a base64-encoded string from a local image use either of the following third-party libraries:' + - '* expo-file-system: `readAsStringAsync(filepath, \'base64\')`' + - '* react-native-fs: `readFile(filepath, \'base64\')`', + "* expo-file-system: `readAsStringAsync(filepath, 'base64')`" + + "* react-native-fs: `readFile(filepath, 'base64')`", ); return require('ImageStore'); },