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
This commit is contained in:
John Knox
2019-04-26 04:03:57 -07:00
committed by Facebook Github Bot
parent a4164c89a0
commit 0a3805c0f4

View File

@@ -226,6 +226,10 @@ function renderSidebarSection(section: SidebarSection, index: number): Node {
}
}
type IncomingMessage =
| {method: 'updateRows', data: Array<RowData>}
| {method: 'clearTable'};
export default function createTableNativePlugin(id: string, title: string) {
return class extends FlipperPlugin<State, *, PersistedState> {
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<RowData>,
message: IncomingMessage,
): $Shape<PersistedState> => {
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<PersistedState> => {
// $FlowFixMe Unsafely treat the recieved message as what we're expecting.
return this.typedPersistedStateReducer(persistedState, {method, data});
};
state = {
selectedIds: [],
error: null,