From ea0cc7f15b78534d37b91f33f151d0fc4a3fa923 Mon Sep 17 00:00:00 2001 From: Michel Weststrate Date: Mon, 22 Mar 2021 05:30:14 -0700 Subject: [PATCH] Update to Immer 9 for better type inference Summary: Per title Reviewed By: passy Differential Revision: D27202660 fbshipit-source-id: 074c44275a93c3fb35d2f491b51d82aa9792dd70 --- desktop/app/package.json | 2 +- desktop/flipper-plugin/package.json | 2 +- .../flipper-plugin/src/state/DataSource.tsx | 2 +- .../src/ui/datatable/DataTableManager.tsx | 34 +++++++++---------- desktop/yarn.lock | 8 ++--- 5 files changed, 23 insertions(+), 25 deletions(-) diff --git a/desktop/app/package.json b/desktop/app/package.json index b9fbfc23a..6961d9c87 100644 --- a/desktop/app/package.json +++ b/desktop/app/package.json @@ -36,7 +36,7 @@ "flipper-plugin": "0.0.0", "flipper-plugin-lib": "0.0.0", "fs-extra": "^9.0.1", - "immer": "^8.0.1", + "immer": "^9.0.0", "immutable": "^4.0.0-rc.12", "invariant": "^2.2.2", "lodash": "^4.17.19", diff --git a/desktop/flipper-plugin/package.json b/desktop/flipper-plugin/package.json index 930194924..aa4fd3e84 100644 --- a/desktop/flipper-plugin/package.json +++ b/desktop/flipper-plugin/package.json @@ -11,7 +11,7 @@ "dependencies": { "@emotion/css": "^11.0.0", "@emotion/react": "^11.1.1", - "immer": "^8.0.1", + "immer": "^9.0.0", "lodash": "^4.17.20", "react-element-to-jsx-string": "^14.3.2", "react-virtual": "^2.4.0" diff --git a/desktop/flipper-plugin/src/state/DataSource.tsx b/desktop/flipper-plugin/src/state/DataSource.tsx index db58e0b6f..80db9d2ac 100644 --- a/desktop/flipper-plugin/src/state/DataSource.tsx +++ b/desktop/flipper-plugin/src/state/DataSource.tsx @@ -87,7 +87,7 @@ type OutputChange = }; export class DataSource< - T, + T = any, KEY extends keyof T = any, KEY_TYPE extends string | number | never = ExtractKeyType > implements Persistable { diff --git a/desktop/flipper-plugin/src/ui/datatable/DataTableManager.tsx b/desktop/flipper-plugin/src/ui/datatable/DataTableManager.tsx index 1a3b9ef69..dc06622d6 100644 --- a/desktop/flipper-plugin/src/ui/datatable/DataTableManager.tsx +++ b/desktop/flipper-plugin/src/ui/datatable/DataTableManager.tsx @@ -12,7 +12,7 @@ import {Percentage} from '../../utils/widthUtils'; import {MutableRefObject, Reducer} from 'react'; import {DataSource} from '../../state/DataSource'; import {DataSourceVirtualizer} from './DataSourceRenderer'; -import produce, {immerable, original} from 'immer'; +import produce, {castDraft, immerable, original} from 'immer'; export type OnColumnResize = (id: string, size: number | Percentage) => void; export type Sorting = { @@ -107,17 +107,17 @@ export type DataTableReducer = Reducer< >; export type DataTableDispatch = React.Dispatch>; -export const dataTableManagerReducer = produce(function ( - draft: DataManagerState, - action: DataManagerActions, -) { +export const dataTableManagerReducer = produce< + DataManagerState, + [DataManagerActions] +>(function (draft, action) { const config = original(draft.config)!; switch (action.type) { case 'reset': { draft.columns = computeInitialColumns(config.defaultColumns); draft.sorting = undefined; draft.searchValue = ''; - draft.selection = emptySelection; + draft.selection = castDraft(emptySelection); break; } case 'resizeColumn': { @@ -151,25 +151,20 @@ export const dataTableManagerReducer = produce(function ( } case 'selectItem': { const {nextIndex, addToSelection} = action; - draft.selection = computeSetSelection( - draft.selection, - nextIndex, - addToSelection, + draft.selection = castDraft( + computeSetSelection(draft.selection, nextIndex, addToSelection), ); break; } case 'addRangeToSelection': { const {start, end, allowUnselect} = action; - draft.selection = computeAddRangeToSelection( - draft.selection, - start, - end, - allowUnselect, + draft.selection = castDraft( + computeAddRangeToSelection(draft.selection, start, end, allowUnselect), ); break; } case 'clearSelection': { - draft.selection = emptySelection; + draft.selection = castDraft(emptySelection); break; } case 'addColumnFilter': { @@ -195,7 +190,10 @@ export const dataTableManagerReducer = produce(function ( break; } case 'setColumnFilterFromSelection': { - const items = getSelectedItems(config.dataSource, draft.selection); + const items = getSelectedItems( + config.dataSource as DataSource, + draft.selection, + ); items.forEach((item, index) => { addColumnFilter( draft.columns, @@ -214,7 +212,7 @@ export const dataTableManagerReducer = produce(function ( throw new Error('Unknown action ' + (action as any).type); } } -}) as any; +}); /** * Public only imperative convienience API for DataTable diff --git a/desktop/yarn.lock b/desktop/yarn.lock index a3ee2055b..09e701022 100644 --- a/desktop/yarn.lock +++ b/desktop/yarn.lock @@ -6765,10 +6765,10 @@ immediate@~3.0.5: resolved "https://registry.yarnpkg.com/immediate/-/immediate-3.0.6.tgz#9db1dbd0faf8de6fbe0f5dd5e56bb606280de69b" integrity sha1-nbHb0Pr43m++D13V5Wu2BigN5ps= -immer@^8.0.1: - version "8.0.1" - resolved "https://registry.yarnpkg.com/immer/-/immer-8.0.1.tgz#9c73db683e2b3975c424fb0572af5889877ae656" - integrity sha512-aqXhGP7//Gui2+UrEtvxZxSquQVXTpZ7KDxfCcKAF3Vysvw0CViVaW9RZ1j1xlIYqaaaipBoqdqeibkc18PNvA== +immer@^9.0.0: + version "9.0.1" + resolved "https://registry.yarnpkg.com/immer/-/immer-9.0.1.tgz#1116368e051f9a0fd188c5136b6efb74ed69c57f" + integrity sha512-7CCw1DSgr8kKYXTYOI1qMM/f5qxT5vIVMeGLDCDX8CSxsggr1Sjdoha4OhsP0AZ1UvWbyZlILHvLjaynuu02Mg== immutable@^4.0.0-rc.12: version "4.0.0-rc.12"