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:
committed by
Facebook Github Bot
parent
a4164c89a0
commit
0a3805c0f4
@@ -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) {
|
export default function createTableNativePlugin(id: string, title: string) {
|
||||||
return class extends FlipperPlugin<State, *, PersistedState> {
|
return class extends FlipperPlugin<State, *, PersistedState> {
|
||||||
static keyboardActions = ['clear', 'createPaste'];
|
static keyboardActions = ['clear', 'createPaste'];
|
||||||
@@ -238,19 +242,15 @@ export default function createTableNativePlugin(id: string, title: string) {
|
|||||||
tableMetadata: null,
|
tableMetadata: null,
|
||||||
};
|
};
|
||||||
|
|
||||||
static persistedStateReducer = (
|
static typedPersistedStateReducer = (
|
||||||
persistedState: PersistedState,
|
persistedState: PersistedState,
|
||||||
method: string,
|
message: IncomingMessage,
|
||||||
payload: RowData | Array<RowData>,
|
|
||||||
): $Shape<PersistedState> => {
|
): $Shape<PersistedState> => {
|
||||||
if (method === 'updateRows') {
|
if (message.method === 'updateRows') {
|
||||||
const newRows = [];
|
const newRows = [];
|
||||||
const newData = {};
|
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) {
|
if (rowData.id == null) {
|
||||||
throw new Error(
|
throw new Error(
|
||||||
`updateRows: row is missing id: ${JSON.stringify(rowData)}`,
|
`updateRows: row is missing id: ${JSON.stringify(rowData)}`,
|
||||||
@@ -281,7 +281,7 @@ export default function createTableNativePlugin(id: string, title: string) {
|
|||||||
datas: {...persistedState.datas, ...newData},
|
datas: {...persistedState.datas, ...newData},
|
||||||
rows: [...persistedState.rows, ...newRows],
|
rows: [...persistedState.rows, ...newRows],
|
||||||
};
|
};
|
||||||
} else if (method === 'clearTable') {
|
} else if (message.method === 'clearTable') {
|
||||||
return {
|
return {
|
||||||
...persistedState,
|
...persistedState,
|
||||||
rows: [],
|
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 = {
|
state = {
|
||||||
selectedIds: [],
|
selectedIds: [],
|
||||||
error: null,
|
error: null,
|
||||||
|
|||||||
Reference in New Issue
Block a user