Skip to content

Commit

Permalink
fix(hot-reload): only reset hooks if error occurs
Browse files Browse the repository at this point in the history
  • Loading branch information
luo3house committed Nov 5, 2023
1 parent 85aaf1b commit 01f16e4
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 11 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
28 changes: 22 additions & 6 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import 'package:flutter_fc/flutter_fc.dart';
void main() {
runApp(const MaterialApp(
home: SafeArea(
child: ReComputeAfterReassembleTestScreen(),
child: SkipErrorAfterReassembleTestScreen(),
),
));
}
Expand Down Expand Up @@ -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%");
}
}

Expand Down
2 changes: 1 addition & 1 deletion example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ packages:
path: ".."
relative: true
source: path
version: "0.5.0"
version: "0.5.1"
flutter_lints:
dependency: "direct dev"
description:
Expand Down
16 changes: 13 additions & 3 deletions lib/src/fc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,8 @@ class _FCElement<T extends _FCWidget> extends ComponentElement
@override
List<Hook>? memoizedHooks;

var _reassembled = false;

_FCElement(super.widget);

@override
Expand Down Expand Up @@ -254,8 +256,8 @@ class _FCElement<T extends _FCWidget> extends ComponentElement

@override
void reassemble() {
_reassembled = true;
super.reassemble();
memoizedHooks = null;
}

@override
Expand Down Expand Up @@ -287,6 +289,8 @@ class _FCElement<T extends _FCWidget> extends ComponentElement
}

Widget _buildWithHooks() {
final reassembledJust = _reassembled;
_reassembled = false;
if (memoizedHooks == null) {
_kCurrentDispatcher = _FCMountDispatcher(this);
} else {
Expand All @@ -297,8 +301,14 @@ class _FCElement<T extends _FCWidget> 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;
}
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -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:
Expand Down

0 comments on commit 01f16e4

Please sign in to comment.