From 0a3805c0f41550ae4e54903a6e68a774d56b9fc8 Mon Sep 17 00:00:00 2001 From: John Knox Date: Fri, 26 Apr 2019 04:03:57 -0700 Subject: [PATCH] Add types for native table plugin persisted state reducer Summary: I haven't done this at the platform level (plugin.js) because the persistedStateReducer method signature prevents it unfortunately. We might want to add a different method type at some point, like this one so you don't need to do this unsafe casting in the plugin itself, but for now, just doing it here. Reviewed By: danielbuechele Differential Revision: D15080331 fbshipit-source-id: c51749ac84cd38559d85089557f44ace8ae6e08b --- src/plugins/TableNativePlugin.js | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/src/plugins/TableNativePlugin.js b/src/plugins/TableNativePlugin.js index 71d5b3945..a4acade62 100644 --- a/src/plugins/TableNativePlugin.js +++ b/src/plugins/TableNativePlugin.js @@ -226,6 +226,10 @@ function renderSidebarSection(section: SidebarSection, index: number): Node { } } +type IncomingMessage = + | {method: 'updateRows', data: Array} + | {method: 'clearTable'}; + export default function createTableNativePlugin(id: string, title: string) { return class extends FlipperPlugin { static keyboardActions = ['clear', 'createPaste']; @@ -238,19 +242,15 @@ export default function createTableNativePlugin(id: string, title: string) { tableMetadata: null, }; - static persistedStateReducer = ( + static typedPersistedStateReducer = ( persistedState: PersistedState, - method: string, - payload: RowData | Array, + message: IncomingMessage, ): $Shape => { - if (method === 'updateRows') { + if (message.method === 'updateRows') { const newRows = []; const newData = {}; - if (!Array.isArray(payload)) { - throw new Error('updateRows called with non array type'); - } - for (const rowData of payload.reverse()) { + for (const rowData of message.data.reverse()) { if (rowData.id == null) { throw new Error( `updateRows: row is missing id: ${JSON.stringify(rowData)}`, @@ -281,7 +281,7 @@ export default function createTableNativePlugin(id: string, title: string) { datas: {...persistedState.datas, ...newData}, rows: [...persistedState.rows, ...newRows], }; - } else if (method === 'clearTable') { + } else if (message.method === 'clearTable') { return { ...persistedState, rows: [], @@ -292,6 +292,15 @@ export default function createTableNativePlugin(id: string, title: string) { } }; + static persistedStateReducer = ( + persistedState: PersistedState, + method: string, + data: any, + ): $Shape => { + // $FlowFixMe Unsafely treat the recieved message as what we're expecting. + return this.typedPersistedStateReducer(persistedState, {method, data}); + }; + state = { selectedIds: [], error: null,