Skip to content

Commit da5d15b

Browse files
mdvaccafacebook-github-bot
authored andcommitted
Add support to diff ImageSource props (#51643)
Summary: Pull Request resolved: #51643 Add support to diff ImageSource props /react-native/ReactCommon/react/renderer/imagemanager/primitives.h changelog: [internal] internal Reviewed By: javache Differential Revision: D69497848 fbshipit-source-id: 56afe1d8601f7f4dc5dab815b6e534d7c63f651b
1 parent ad0ea17 commit da5d15b

6 files changed

Lines changed: 78 additions & 52 deletions

File tree

packages/react-native-codegen/e2e/deep_imports/__tests__/components/__snapshots__/GeneratePropsCpp-test.js.snap

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -531,6 +531,9 @@ folly::dynamic ImagePropNativeComponentViewProps::getDiffProps(
531531
}
532532
folly::dynamic result = HostPlatformViewProps::getDiffProps(prevProps);
533533
534+
if (thumbImage != oldProps->thumbImage) {
535+
result[\\"thumbImage\\"] = thumbImage.toDynamic();
536+
}
534537
return result;
535538
}
536539
#endif
@@ -736,6 +739,9 @@ folly::dynamic MultiNativePropNativeComponentViewProps::getDiffProps(
736739
}
737740
folly::dynamic result = HostPlatformViewProps::getDiffProps(prevProps);
738741
742+
if (thumbImage != oldProps->thumbImage) {
743+
result[\\"thumbImage\\"] = thumbImage.toDynamic();
744+
}
739745
740746
if (color != oldProps->color) {
741747
result[\\"color\\"] = *color;

packages/react-native-codegen/e2e/namespaced/__tests__/components/__snapshots__/GeneratePropsCpp-test.js.snap

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -531,6 +531,9 @@ folly::dynamic ImagePropNativeComponentViewProps::getDiffProps(
531531
}
532532
folly::dynamic result = HostPlatformViewProps::getDiffProps(prevProps);
533533
534+
if (thumbImage != oldProps->thumbImage) {
535+
result[\\"thumbImage\\"] = thumbImage.toDynamic();
536+
}
534537
return result;
535538
}
536539
#endif
@@ -736,6 +739,9 @@ folly::dynamic MultiNativePropNativeComponentViewProps::getDiffProps(
736739
}
737740
folly::dynamic result = HostPlatformViewProps::getDiffProps(prevProps);
738741
742+
if (thumbImage != oldProps->thumbImage) {
743+
result[\\"thumbImage\\"] = thumbImage.toDynamic();
744+
}
739745
740746
if (color != oldProps->color) {
741747
result[\\"color\\"] = *color;

packages/react-native-codegen/src/generators/components/GeneratePropsCpp.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,10 @@ function generatePropsDiffString(
103103
result["${prop.name}"] = *${prop.name};
104104
}`;
105105
case 'ImageSourcePrimitive':
106-
return '';
106+
return `
107+
if (${prop.name} != oldProps->${prop.name}) {
108+
result["${prop.name}"] = ${prop.name}.toDynamic();
109+
}`;
107110
case 'ImageRequestPrimitive':
108111
// Shouldn't be used in props
109112
throw new Error(

packages/react-native-codegen/src/generators/components/__tests__/__snapshots__/GeneratePropsCpp-test.js.snap

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -851,6 +851,9 @@ folly::dynamic ImagePropNativeComponentProps::getDiffProps(
851851
}
852852
folly::dynamic result = HostPlatformViewProps::getDiffProps(prevProps);
853853
854+
if (thumbImage != oldProps->thumbImage) {
855+
result[\\"thumbImage\\"] = thumbImage.toDynamic();
856+
}
854857
return result;
855858
}
856859
#endif
@@ -1148,6 +1151,9 @@ folly::dynamic ImageColorPropNativeComponentProps::getDiffProps(
11481151
}
11491152
folly::dynamic result = HostPlatformViewProps::getDiffProps(prevProps);
11501153
1154+
if (thumbImage != oldProps->thumbImage) {
1155+
result[\\"thumbImage\\"] = thumbImage.toDynamic();
1156+
}
11511157
11521158
if (color != oldProps->color) {
11531159
result[\\"color\\"] = *color;

packages/react-native/ReactCommon/react/renderer/components/image/ImageProps.cpp

Lines changed: 3 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -176,53 +176,6 @@ void ImageProps::setProp(
176176

177177
#ifdef RN_SERIALIZABLE_STATE
178178

179-
static folly::dynamic convertImageSource(const ImageSource& imageSource) {
180-
folly::dynamic imageSourceResult = folly::dynamic::object();
181-
switch (imageSource.type) {
182-
case ImageSource::Type::Invalid:
183-
imageSourceResult["type"] = "invalid";
184-
break;
185-
case ImageSource::Type::Remote:
186-
imageSourceResult["type"] = "remote";
187-
break;
188-
case ImageSource::Type::Local:
189-
imageSourceResult["type"] = "local";
190-
break;
191-
}
192-
193-
imageSourceResult["uri"] = imageSource.uri;
194-
imageSourceResult["bundle"] = imageSource.bundle;
195-
imageSourceResult["scale"] = imageSource.scale;
196-
197-
imageSourceResult["width"] = imageSource.size.width;
198-
imageSourceResult["height"] = imageSource.size.height;
199-
200-
imageSourceResult["body"] = imageSource.body;
201-
imageSourceResult["method"] = imageSource.method;
202-
203-
switch (imageSource.cache) {
204-
case ImageSource::CacheStategy::Default:
205-
imageSourceResult["cache"] = "default";
206-
break;
207-
case ImageSource::CacheStategy::Reload:
208-
imageSourceResult["cache"] = "reload";
209-
break;
210-
case ImageSource::CacheStategy::ForceCache:
211-
imageSourceResult["cache"] = "force-cache";
212-
break;
213-
case ImageSource::CacheStategy::OnlyIfCached:
214-
imageSourceResult["cache"] = "only-if-cached";
215-
break;
216-
}
217-
218-
folly::dynamic headersObject = folly::dynamic::object();
219-
for (const auto& header : imageSource.headers) {
220-
headersObject[header.first] = header.second;
221-
}
222-
imageSourceResult["headers"] = headersObject;
223-
return imageSourceResult;
224-
}
225-
226179
static folly::dynamic convertEdgeInsets(const EdgeInsets& edgeInsets) {
227180
folly::dynamic edgeInsetsResult = folly::dynamic::object();
228181
edgeInsetsResult["left"] = edgeInsets.left;
@@ -244,18 +197,17 @@ folly::dynamic ImageProps::getDiffProps(const Props* prevProps) const {
244197
if (sources != oldProps->sources) {
245198
auto sourcesArray = folly::dynamic::array();
246199
for (const auto& source : sources) {
247-
sourcesArray.push_back(convertImageSource(source));
200+
sourcesArray.push_back(source.toDynamic());
248201
}
249202
result["source"] = sourcesArray;
250203
}
251204

252205
if (defaultSource != oldProps->defaultSource) {
253-
result["defaultSource"] = convertImageSource(defaultSource);
206+
result["defaultSource"] = defaultSource.toDynamic();
254207
}
255208

256209
if (loadingIndicatorSource != oldProps->loadingIndicatorSource) {
257-
result["loadingIndicatorSource"] =
258-
convertImageSource(loadingIndicatorSource);
210+
result["loadingIndicatorSource"] = loadingIndicatorSource.toDynamic();
259211
}
260212

261213
if (resizeMode != oldProps->resizeMode) {

packages/react-native/ReactCommon/react/renderer/imagemanager/primitives.h

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <string>
1111
#include <vector>
1212

13+
#include <react/renderer/core/propsConversions.h>
1314
#include <react/renderer/graphics/Float.h>
1415
#include <react/renderer/graphics/Size.h>
1516

@@ -37,6 +38,58 @@ class ImageSource {
3738
bool operator!=(const ImageSource& rhs) const {
3839
return !(*this == rhs);
3940
}
41+
42+
#ifdef RN_SERIALIZABLE_STATE
43+
folly::dynamic toDynamic() const {
44+
folly::dynamic imageSourceResult = folly::dynamic::object();
45+
switch (type) {
46+
case ImageSource::Type::Invalid:
47+
imageSourceResult["type"] = "invalid";
48+
break;
49+
case ImageSource::Type::Remote:
50+
imageSourceResult["type"] = "remote";
51+
break;
52+
case ImageSource::Type::Local:
53+
imageSourceResult["type"] = "local";
54+
break;
55+
}
56+
57+
imageSourceResult["uri"] = uri;
58+
imageSourceResult["bundle"] = bundle;
59+
imageSourceResult["scale"] = scale;
60+
61+
folly::dynamic sizeResult = folly::dynamic::object();
62+
sizeResult["width"] = size.width;
63+
sizeResult["height"] = size.height;
64+
imageSourceResult["size"] = sizeResult;
65+
66+
imageSourceResult["body"] = body;
67+
imageSourceResult["method"] = method;
68+
69+
switch (cache) {
70+
case ImageSource::CacheStategy::Default:
71+
imageSourceResult["cache"] = "default";
72+
break;
73+
case ImageSource::CacheStategy::Reload:
74+
imageSourceResult["cache"] = "reload";
75+
break;
76+
case ImageSource::CacheStategy::ForceCache:
77+
imageSourceResult["cache"] = "force-cache";
78+
break;
79+
case ImageSource::CacheStategy::OnlyIfCached:
80+
imageSourceResult["cache"] = "only-if-cached";
81+
break;
82+
}
83+
84+
folly::dynamic headersObject = folly::dynamic::object();
85+
for (const auto& header : headers) {
86+
headersObject[header.first] = header.second;
87+
}
88+
imageSourceResult["headers"] = headersObject;
89+
return imageSourceResult;
90+
}
91+
92+
#endif
4093
};
4194

4295
using ImageSources = std::vector<ImageSource>;

0 commit comments

Comments
 (0)