From 52549de4b81e3134c536acb75e2af984886e1e2e Mon Sep 17 00:00:00 2001 From: Talha Naqvi Date: Wed, 23 Feb 2022 15:55:57 -0800 Subject: [PATCH 1/2] cleaned up exceptions and added one for estimated item size --- src/RecyclerFlatList.tsx | 30 ++++++++++++++++-------------- src/errors/CustomError.ts | 10 ++++++++++ src/errors/ExceptionList.ts | 15 +++++++++++++++ 3 files changed, 41 insertions(+), 14 deletions(-) create mode 100644 src/errors/CustomError.ts create mode 100644 src/errors/ExceptionList.ts diff --git a/src/RecyclerFlatList.tsx b/src/RecyclerFlatList.tsx index 889827626..d2c162121 100644 --- a/src/RecyclerFlatList.tsx +++ b/src/RecyclerFlatList.tsx @@ -19,6 +19,8 @@ import AutoLayoutView, { BlankAreaEventHandler } from "./AutoLayoutView"; import ItemContainer from "./CellContainer"; import WrapperComponent from "./WrapperComponent"; import GridLayoutProviderWithProps from "./GridLayoutProviderWithProps"; +import CustomError from "./errors/CustomError"; +import ExceptionList from "./errors/ExceptionList"; export interface RecyclerFlatListProps extends FlatListProps { // TODO: This is to make eslint silent. Out prettier and lint rules are conflicting. @@ -108,25 +110,25 @@ class RecyclerFlatList extends React.PureComponent< constructor(props) { super(props); - this.setup(); - } - - private setup() { - const refreshingPrecondition = !( - this.props.onRefresh && typeof this.props.refreshing !== "boolean" - ); - const message = - 'Invariant Violation: `refreshing` prop must be set as a boolean in order to use `onRefresh`, but got `"undefined"`'; - invariant(refreshingPrecondition, message); - if (this.props.estimatedListSize) { - if (this.props.horizontal) { - this.listFixedDimensionSize = this.props.estimatedListSize.height; + this.validateProps(); + if (props.estimatedListSize) { + if (props.horizontal) { + this.listFixedDimensionSize = props.estimatedListSize.height; } else { - this.listFixedDimensionSize = this.props.estimatedListSize.width; + this.listFixedDimensionSize = props.estimatedListSize.width; } } } + private validateProps() { + if (this.props.onRefresh && typeof this.props.refreshing !== "boolean") { + throw new CustomError(ExceptionList.refreshBooleanMissing); + } + if (!(this.props.estimatedItemSize > 0)) { + throw new CustomError(ExceptionList.estimatedItemSizeMissing); + } + } + // Some of the state variables need to update when props change static getDerivedStateFromProps( nextProps: RecyclerFlatListProps, diff --git a/src/errors/CustomError.ts b/src/errors/CustomError.ts new file mode 100644 index 000000000..beb48a6be --- /dev/null +++ b/src/errors/CustomError.ts @@ -0,0 +1,10 @@ +export default class CustomError extends Error { + constructor(exception: Exception) { + super(exception.type + ": " + exception.message); + this.name = exception.type; + } +} +export interface Exception { + type: string; + message: string; +} diff --git a/src/errors/ExceptionList.ts b/src/errors/ExceptionList.ts new file mode 100644 index 000000000..1afd26258 --- /dev/null +++ b/src/errors/ExceptionList.ts @@ -0,0 +1,15 @@ +import { Exception } from "./CustomError"; + +const ExceptionList: { [key: string]: Exception } = { + estimatedItemSizeMissing: { + message: + "`estimatedItemSize` is a required prop in RecyclerFlatList. Please provide a value that is greater than 0.", + type: "InvalidPropException", + }, + refreshBooleanMissing: { + message: + "`refreshing` prop must be set as a boolean in order to use `onRefresh`, but got `undefined`", + type: "InvariantViolation", + }, +}; +export default ExceptionList; From da33d8b89baf04905d4671077bb7b3fa73a7a2fa Mon Sep 17 00:00:00 2001 From: Talha Naqvi Date: Wed, 23 Feb 2022 16:00:50 -0800 Subject: [PATCH 2/2] lint fix --- src/RecyclerFlatList.tsx | 1 - src/errors/CustomError.ts | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/src/RecyclerFlatList.tsx b/src/RecyclerFlatList.tsx index d2c162121..0d1f1b917 100644 --- a/src/RecyclerFlatList.tsx +++ b/src/RecyclerFlatList.tsx @@ -13,7 +13,6 @@ import { RecyclerListView, RecyclerListViewProps, } from "recyclerlistview"; -import invariant from "invariant"; import AutoLayoutView, { BlankAreaEventHandler } from "./AutoLayoutView"; import ItemContainer from "./CellContainer"; diff --git a/src/errors/CustomError.ts b/src/errors/CustomError.ts index beb48a6be..1d7d2fef4 100644 --- a/src/errors/CustomError.ts +++ b/src/errors/CustomError.ts @@ -1,6 +1,6 @@ export default class CustomError extends Error { constructor(exception: Exception) { - super(exception.type + ": " + exception.message); + super(`${exception.type}: ${exception.message}`); this.name = exception.type; } }