From 3b31e54b6edac7b85fdae03a5e3399a59c3958b3 Mon Sep 17 00:00:00 2001 From: Pritesh Nandgaonkar Date: Tue, 6 Aug 2019 06:54:41 -0700 Subject: [PATCH] Get all image data for iOS Summary: We just implemented the sending of the required metadata of the images plugin for the android, but not for iOS. Due to which we get the following error. {F173807861} I tried to implement `getAllImageData` first for iOS in which we send all the information in one message for the images plugin in iOS. But there were lot of images and due to which it exceeded the max payload of the rsocket. That is why I had to implement this bit for iOS in a little different way. We fetch all the image Ids first and then loop over it to get image data. Due to this the android and iOS implementations are different. I plan to change the android part in the next diffs and make it similar to iOS, as it is more scalable. Reviewed By: jknoxville Differential Revision: D16606748 fbshipit-source-id: e98c2bd5db7ec247b45a7cde304d4f51053ea6fe --- src/plugins/fresco/index.js | 142 ++++++++++++++++++++++++++---------- 1 file changed, 104 insertions(+), 38 deletions(-) diff --git a/src/plugins/fresco/index.js b/src/plugins/fresco/index.js index abad6a32c..1e449c093 100644 --- a/src/plugins/fresco/index.js +++ b/src/plugins/fresco/index.js @@ -81,7 +81,11 @@ type ImagesMetaData = {| imageDataList: Array, |}; -export default class extends FlipperPlugin { +export default class FlipperImagesPlugin extends FlipperPlugin< + PluginState, + *, + PersistedState, +> { static defaultPersistedState: PersistedState = { images: [], events: [], @@ -98,50 +102,112 @@ export default class extends FlipperPlugin { store: ?MiddlewareAPI, ): Promise => { const defaultPromise = Promise.resolve(persistedState); + if (!persistedState) { + persistedState = FlipperImagesPlugin.defaultPersistedState; + } if (!store) { return defaultPromise; } - return callClient('getAllImageData').then((data: ImagesMetaData) => { - if (!data) { - return; - } - const {levels, events, imageDataList} = data; - let pluginData: PersistedState = { - ...persistedState, - images: persistedState - ? [...persistedState.images, ...levels.levels] - : levels.levels, - closeableReferenceLeaks: - (persistedState && persistedState.closeableReferenceLeaks) || [], - }; + const selectedDevice = store.getState().connections.selectedDevice; + if (selectedDevice && selectedDevice.os === 'iOS') { + return Promise.all([ + callClient('listImages'), + callClient('getAllImageEventsInfo'), + ]).then(async ([responseImages, responseEvents]) => { + const levels: ImagesList = responseImages.levels; + const events: Array = responseEvents.events; + let pluginData: PersistedState = { + ...persistedState, + images: persistedState + ? [...persistedState.images, ...levels] + : levels, + closeableReferenceLeaks: + (persistedState && persistedState.closeableReferenceLeaks) || [], + }; - events.forEach((event: ImageEventWithId, index) => { - if (!event) { + events.forEach((event: ImageEventWithId, index) => { + if (!event) { + return; + } + const {attribution} = event; + if ( + attribution && + attribution instanceof Array && + attribution.length > 0 + ) { + const surface = attribution[0].trim(); + if (surface.length > 0) { + pluginData.surfaceList.add(surface); + } + } + pluginData = { + ...pluginData, + events: [{eventId: index, ...event}, ...pluginData.events], + }; + }); + const idSet: Set = levels.reduce((acc, level: CacheInfo) => { + level.imageIds.forEach(id => { + acc.add(id); + }); + return acc; + }, new Set()); + const imageDataList: Array = []; + for (const id: string of idSet) { + const imageData: ImageData = await callClient('getImage', { + imageId: id, + }); + imageDataList.push(imageData); + } + imageDataList.forEach((data: ImageData) => { + const imagesMap = {...pluginData.imagesMap}; + imagesMap[data.imageId] = data; + pluginData.imagesMap = imagesMap; + }); + return pluginData; + }); + } else { + return callClient('getAllImageData').then((data: ImagesMetaData) => { + if (!data) { return; } - const {attribution} = event; - if ( - attribution && - attribution instanceof Array && - attribution.length > 0 - ) { - const surface = attribution[0].trim(); - if (surface.length > 0) { - pluginData.surfaceList.add(surface); - } - } - pluginData = { - ...pluginData, - events: [{eventId: index, ...event}, ...pluginData.events], + const {levels, events, imageDataList} = data; + let pluginData: PersistedState = { + ...persistedState, + images: persistedState + ? [...persistedState.images, ...levels.levels] + : levels.levels, + closeableReferenceLeaks: + (persistedState && persistedState.closeableReferenceLeaks) || [], }; - }); - imageDataList.forEach((imageData: ImageData) => { - const {imageId} = imageData; - pluginData.imagesMap[imageId] = imageData; + events.forEach((event: ImageEventWithId, index) => { + if (!event) { + return; + } + const {attribution} = event; + if ( + attribution && + attribution instanceof Array && + attribution.length > 0 + ) { + const surface = attribution[0] ? attribution[0].trim() : undefined; + if (surface && surface.length > 0) { + pluginData.surfaceList.add(surface); + } + } + pluginData = { + ...pluginData, + events: [{eventId: index, ...event}, ...pluginData.events], + }; + }); + + imageDataList.forEach((imageData: ImageData) => { + const {imageId} = imageData; + pluginData.imagesMap[imageId] = imageData; + }); + return pluginData; }); - return pluginData; - }); + } }; static persistedStateReducer = ( @@ -164,8 +230,8 @@ export default class extends FlipperPlugin { const {surfaceList} = persistedState; const {attribution} = event; if (attribution instanceof Array && attribution.length > 0) { - const surface = attribution[0].trim(); - if (surface.length > 0) { + const surface = attribution[0] ? attribution[0].trim() : undefined; + if (surface && surface.length > 0) { surfaceList.add(surface); } }