|
| 1 | +// Copyright 2020 Workiva Inc. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +library over_react.lazy; |
| 16 | + |
| 17 | +import 'package:over_react/over_react.dart'; |
| 18 | +import 'package:react/react.dart' as react; |
| 19 | + |
| 20 | +import '../component_declaration/function_component.dart'; |
| 21 | + |
| 22 | +/// A HOC that creates a "lazy" component that lets you defer loading a component’s code until it is rendered for the first time. |
| 23 | +/// |
| 24 | +/// Returns a `UiFactory` you can use just render in your tree. While the code for the lazy component is still loading, |
| 25 | +/// attempting to render it will suspend. Use <Suspense> to display a loading indicator while it’s loading. |
| 26 | +/// [load] is a function that should return a `Future<UiFactory<TProps>>` that resolves to the component to be rendered. |
| 27 | +/// [_config] should be a `UiFactoryConfig<TProps>` or `null` and is only `dynamic` to avoid an unnecessary cast in the boilerplate. |
| 28 | +/// |
| 29 | +/// React will not call [load] until the first time the component is rendered. |
| 30 | +/// After React first calls [load], it will wait for it to resolve, and then render the resolved value. |
| 31 | +/// Both the returned Future and the Future's resolved value will be cached, so React will not call [load] more than once. |
| 32 | +/// If the Future rejects, React will throw the rejection reason for the nearest Error Boundary to handle. |
| 33 | +/// |
| 34 | +/// Example: |
| 35 | +/// ```dart |
| 36 | +/// import 'package:over_react/over_react.dart'; |
| 37 | +/// |
| 38 | +/// part 'main.over_react.g.dart'; |
| 39 | +/// |
| 40 | +/// mixin ALazyComponentPropsMixin on UiProps {} |
| 41 | +/// |
| 42 | +/// UiFactory<ALazyComponentPropsMixin> ALazyComponent = lazy( |
| 43 | +/// () async { |
| 44 | +/// final componentModule = await loadComponent(); |
| 45 | +/// return uiForwardRef( |
| 46 | +/// (props, ref) { |
| 47 | +/// return (componentModule.AnotherComponent() |
| 48 | +/// ..ref = ref |
| 49 | +/// ..addProps(props) |
| 50 | +/// )(props.children); |
| 51 | +/// }, |
| 52 | +/// _$ALazyComponentConfig, |
| 53 | +/// ); |
| 54 | +/// }, |
| 55 | +/// _$ALazyComponentConfig |
| 56 | +/// ); |
| 57 | +/// ``` |
| 58 | +/// |
| 59 | +/// > __NOTE:__ A lazy component MUST be wrapped with a `Suspense` component to provide a fallback ui while it loads. |
| 60 | +/// |
| 61 | +/// ```dart |
| 62 | +/// (Suspense() |
| 63 | +/// ..fallback = Dom.p()('Loading...') |
| 64 | +/// )( |
| 65 | +/// ALazyComponent()(), |
| 66 | +/// ); |
| 67 | +/// ``` |
| 68 | +/// See: <https://react.dev/reference/react/lazy>. |
| 69 | +UiFactory<TProps> lazy<TProps extends UiProps>( |
| 70 | + Future<UiFactory<TProps>> Function() load, /* UiFactoryConfig<TProps> */ dynamic _config) { |
| 71 | + ArgumentError.checkNotNull(_config, '_config'); |
| 72 | + if (_config is! UiFactoryConfig<TProps>) { |
| 73 | + throw ArgumentError('_config is required when using a custom props class and should be a UiFactoryConfig<TProps>. Make sure you are ' |
| 74 | + r'using either the generated factory config (i.e. _$FooConfig) or manually declaring your config correctly.'); |
| 75 | + } |
| 76 | + // ignore: invalid_use_of_protected_member |
| 77 | + var propsFactory = _config.propsFactory; |
| 78 | + |
| 79 | + final lazyFactoryProxy = react.lazy(() async { |
| 80 | + final factory = await load(); |
| 81 | + return factory().componentFactory!; |
| 82 | + }); |
| 83 | + |
| 84 | + if (propsFactory == null) { |
| 85 | + if (TProps != UiProps && TProps != GenericUiProps) { |
| 86 | + throw ArgumentError( |
| 87 | + 'config.propsFactory must be provided when using custom props classes'); |
| 88 | + } |
| 89 | + propsFactory = PropsFactory.fromUiFactory( |
| 90 | + ([backingMap]) => GenericUiProps(lazyFactoryProxy, backingMap)) |
| 91 | + as PropsFactory<TProps>; |
| 92 | + } |
| 93 | + // Work around propsFactory not getting promoted to non-nullable in _uiFactory: https://github.com/dart-lang/language/issues/1536 |
| 94 | + final nonNullablePropsFactory = propsFactory; |
| 95 | + |
| 96 | + TProps _uiFactory([Map? props]) { |
| 97 | + TProps builder; |
| 98 | + if (props == null) { |
| 99 | + // propsFactory should get promoted to non-nullable here, but it does not some reason propsF |
| 100 | + builder = nonNullablePropsFactory.jsMap(JsBackedMap()); |
| 101 | + } else if (props is JsBackedMap) { |
| 102 | + builder = nonNullablePropsFactory.jsMap(props); |
| 103 | + } else { |
| 104 | + builder = nonNullablePropsFactory.map(props); |
| 105 | + } |
| 106 | + |
| 107 | + return builder..componentFactory = lazyFactoryProxy; |
| 108 | + } |
| 109 | + |
| 110 | + return _uiFactory; |
| 111 | +} |
0 commit comments