|
| 1 | +import 'package:meta/meta.dart'; |
| 2 | +import 'package:over_react/over_react.dart'; |
| 3 | + |
| 4 | +// ignore: uri_has_not_been_generated |
| 5 | +part 'error_boundary.over_react.g.dart'; |
| 6 | + |
| 7 | +// TODO: Need to type the second argument once react-dart implements bindings for the ReactJS "componentStack". |
| 8 | +typedef _ComponentDidCatchCallback(Error error, /*ComponentStack*/dynamic componentStack); |
| 9 | + |
| 10 | +// TODO: Need to type the second argument once react-dart implements bindings for the ReactJS "componentStack". |
| 11 | +typedef ReactElement _FallbackUiRenderer(Error error, /*ComponentStack*/dynamic componentStack); |
| 12 | + |
| 13 | +/// A higher-order component that will catch ReactJS errors anywhere within the child component tree and |
| 14 | +/// display a fallback UI instead of the component tree that crashed. |
| 15 | +/// |
| 16 | +/// Optionally, use the [ErrorBoundaryProps.onComponentDidCatch] |
| 17 | +/// to send error / stack trace information to a logging endpoint for your application. |
| 18 | +/// |
| 19 | +/// > __NOTE: This component does not yet do any of this__. |
| 20 | +/// > |
| 21 | +/// > It will begin providing the boundary / fallback UI behavior once support |
| 22 | +/// for ReactJS 16 is released in over_react version 3.0.0 |
| 23 | +@Factory() |
| 24 | +// ignore: undefined_identifier |
| 25 | +UiFactory<ErrorBoundaryProps> ErrorBoundary = $ErrorBoundary; |
| 26 | + |
| 27 | +@Props() |
| 28 | +class _$ErrorBoundaryProps extends UiProps { |
| 29 | + /// An optional callback that will be called with an [Error] and a `ComponentStack` |
| 30 | + /// containing information about which component in the tree threw the error when |
| 31 | + /// the `componentDidCatch` lifecycle method is called. |
| 32 | + /// |
| 33 | + /// This callback can be used to log component errors like so: |
| 34 | + /// |
| 35 | + /// (ErrorBoundary() |
| 36 | + /// ..onComponentDidCatch = (error, componentStack) { |
| 37 | + /// // It is up to you to implement the service / thing that calls the service. |
| 38 | + /// logComponentStackToAService(error, componentStack); |
| 39 | + /// } |
| 40 | + /// )( |
| 41 | + /// // The rest of your component tree |
| 42 | + /// ) |
| 43 | + /// |
| 44 | + /// > See: <https://reactjs.org/docs/react-component.html#componentdidcatch> |
| 45 | + _ComponentDidCatchCallback onComponentDidCatch; |
| 46 | + |
| 47 | + /// A renderer that will be used to render "fallback" UI instead of the child |
| 48 | + /// component tree that crashed. |
| 49 | + /// |
| 50 | + /// > Default: [ErrorBoundaryComponent._renderDefaultFallbackUI] |
| 51 | + _FallbackUiRenderer fallbackUIRenderer; |
| 52 | +} |
| 53 | + |
| 54 | +@State() |
| 55 | +class _$ErrorBoundaryState extends UiState { |
| 56 | + /// Whether the tree that the [ErrorBoundary] is wrapping around threw an error. |
| 57 | + /// |
| 58 | + /// When `true`, fallback UI will be rendered using [ErrorBoundaryProps.fallbackUIRenderer]. |
| 59 | + bool hasError; |
| 60 | +} |
| 61 | + |
| 62 | +@Component(isWrapper: true) |
| 63 | +class ErrorBoundaryComponent<T extends ErrorBoundaryProps, S extends ErrorBoundaryState> |
| 64 | + extends UiStatefulComponent<T, S> { |
| 65 | + Error _error; |
| 66 | + /*ComponentStack*/dynamic _componentStack; |
| 67 | + |
| 68 | + @override |
| 69 | + Map getDefaultProps() => (newProps() |
| 70 | + ..fallbackUIRenderer = _renderDefaultFallbackUI |
| 71 | + ); |
| 72 | + |
| 73 | + @override |
| 74 | + Map getInitialState() => (newState() |
| 75 | + ..hasError = false |
| 76 | + ); |
| 77 | + |
| 78 | + @mustCallSuper |
| 79 | + /*@override*/ |
| 80 | + S getDerivedStateFromError(_) { |
| 81 | + return newState()..hasError = true; |
| 82 | + } |
| 83 | + |
| 84 | + @mustCallSuper |
| 85 | + /*@override*/ |
| 86 | + void componentDidCatch(Error error, /*ComponentStack*/dynamic componentStack) { |
| 87 | + _error = error; |
| 88 | + _componentStack = componentStack; |
| 89 | + |
| 90 | + if (props.onComponentDidCatch != null) { |
| 91 | + props.onComponentDidCatch(error, componentStack); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + @override |
| 96 | + render() => state.hasError |
| 97 | + ? props.fallbackUIRenderer(_error, _componentStack) |
| 98 | + : props.children.single; |
| 99 | + |
| 100 | + ReactElement _renderDefaultFallbackUI(_, __) => |
| 101 | + throw new UnimplementedError('Fallback UI will not be supported until support for ReactJS 16 is released in version 3.0.0'); |
| 102 | + |
| 103 | + @mustCallSuper |
| 104 | + @override |
| 105 | + void validateProps([Map appliedProps]) { |
| 106 | + super.validateProps(appliedProps); |
| 107 | + final children = domProps(appliedProps).children; |
| 108 | + |
| 109 | + if (children.length != 1) { |
| 110 | + throw new PropError.value(children, 'children', 'ErrorBoundary accepts only a single child.'); |
| 111 | + } else if (!isValidElement(children.single)) { |
| 112 | + throw new PropError.value(children, 'children', 'ErrorBoundary accepts only a single ReactComponent child.'); |
| 113 | + } |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +// ignore: mixin_of_non_class, undefined_class |
| 118 | +class ErrorBoundaryProps extends _$ErrorBoundaryProps with _$ErrorBoundaryPropsAccessorsMixin { |
| 119 | + // ignore: undefined_identifier, undefined_class, const_initialized_with_non_constant_value |
| 120 | + static const PropsMeta meta = $metaForErrorBoundaryProps; |
| 121 | +} |
| 122 | + |
| 123 | +// ignore: mixin_of_non_class, undefined_class |
| 124 | +class ErrorBoundaryState extends _$ErrorBoundaryState with _$ErrorBoundaryStateAccessorsMixin { |
| 125 | + // ignore: undefined_identifier, undefined_class, const_initialized_with_non_constant_value |
| 126 | + static const StateMeta meta = $metaForErrorBoundaryState; |
| 127 | +} |
0 commit comments