From 01f16e4bb4d78c3e3737b7673b6e3825f4685593 Mon Sep 17 00:00:00 2001 From: luoyz Date: Sun, 5 Nov 2023 17:16:21 +0800 Subject: [PATCH] fix(hot-reload): only reset hooks if error occurs --- CHANGELOG.md | 4 ++++ example/lib/main.dart | 28 ++++++++++++++++++++++------ example/pubspec.lock | 2 +- lib/src/fc.dart | 16 +++++++++++++--- pubspec.yaml | 2 +- 5 files changed, 41 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1a81512..17f421c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +## 0.5.1 + +* [Hot Reload] only reset hooks if error occurs after reassemble + ## 0.5.0 * [Hot Reload] reset hooks after reassemble diff --git a/example/lib/main.dart b/example/lib/main.dart index ad89c8d..f1cd204 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -4,7 +4,7 @@ import 'package:flutter_fc/flutter_fc.dart'; void main() { runApp(const MaterialApp( home: SafeArea( - child: ReComputeAfterReassembleTestScreen(), + child: SkipErrorAfterReassembleTestScreen(), ), )); } @@ -50,14 +50,30 @@ class ErrorTestScreen extends FCWidget { } /// value memoized with no deps, should be re-derived after reassemble -class ReComputeAfterReassembleTestScreen extends FCWidget { - const ReComputeAfterReassembleTestScreen({super.key}); +class SkipErrorAfterReassembleTestScreen extends FCWidget { + const SkipErrorAfterReassembleTestScreen({super.key}); @override Widget build(BuildContext context) { - var value; - value = useMemo(() => 1 /* try 2 after build */, const []); - return Text("$value"); + final now = useMemo(() => DateTime.now(), const []); + return Scaffold( + body: Column( + children: [ + Text("$now"), + DangerousFC(), + ], + ), + ); + } +} + +class DangerousFC extends FCWidget { + static var flag = 0; + @override + Widget build(BuildContext context) { + flag++; + final rs = flag % 2 == 0 ? useMemo(() => 1, const []) : useRef(2).current; + return Text("$rs%"); } } diff --git a/example/pubspec.lock b/example/pubspec.lock index 7af9ca8..be89e23 100644 --- a/example/pubspec.lock +++ b/example/pubspec.lock @@ -68,7 +68,7 @@ packages: path: ".." relative: true source: path - version: "0.5.0" + version: "0.5.1" flutter_lints: dependency: "direct dev" description: diff --git a/lib/src/fc.dart b/lib/src/fc.dart index 22a0303..7ac0834 100644 --- a/lib/src/fc.dart +++ b/lib/src/fc.dart @@ -222,6 +222,8 @@ class _FCElement extends ComponentElement @override List? memoizedHooks; + var _reassembled = false; + _FCElement(super.widget); @override @@ -254,8 +256,8 @@ class _FCElement extends ComponentElement @override void reassemble() { + _reassembled = true; super.reassemble(); - memoizedHooks = null; } @override @@ -287,6 +289,8 @@ class _FCElement extends ComponentElement } Widget _buildWithHooks() { + final reassembledJust = _reassembled; + _reassembled = false; if (memoizedHooks == null) { _kCurrentDispatcher = _FCMountDispatcher(this); } else { @@ -297,8 +301,14 @@ class _FCElement extends ComponentElement memoizedHooks = _kCurrentDispatcher!.memoizedHooks; return built; } catch (e) { - memoizedHooks = null; - rethrow; + if (reassembledJust) { + memoizedHooks = null; + final built = widget.build(this); + memoizedHooks = _kCurrentDispatcher!.memoizedHooks; + return built; + } else { + rethrow; + } } finally { _kCurrentDispatcher = null; } diff --git a/pubspec.yaml b/pubspec.yaml index 917b600..6d3487d 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: flutter_fc description: Writing Function Component in Flutter, just as writing in React. -version: 0.5.0 +version: 0.5.1 homepage: https://github.com/luo3house/flutter_fc environment: