diff --git a/CHANGELOG.md b/CHANGELOG.md index 510cb652a..f44e13556 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,19 @@ +## [0.5.3] - July 13 2018 + +### Breaking change + +n/a + +#### Fixes / Enhancements + +- Added polling timer as a variable for easy deletion on dispose +- Fixed bug when Query timer is still active when the Query is disposed +- Added instant query fetch when the query variables are updated + +#### Docs + +n/a + ## [0.5.2] - July 11 2018 ### Breaking change diff --git a/README.md b/README.md index fe61bbb86..c7004390b 100644 --- a/README.md +++ b/README.md @@ -25,7 +25,7 @@ First depend on the library by adding this to your packages `pubspec.yaml`: ```yaml dependencies: - graphql_flutter: ^0.5.2 + graphql_flutter: ^0.5.3 ``` Now inside your Dart code you can import it. diff --git a/lib/src/widgets/query.dart b/lib/src/widgets/query.dart index b2a14fac2..f897a5e87 100644 --- a/lib/src/widgets/query.dart +++ b/lib/src/widgets/query.dart @@ -27,23 +27,40 @@ class Query extends StatefulWidget { QueryState createState() => new QueryState(); } -class QueryState extends State with WidgetsBindingObserver { +class QueryState extends State { bool loading = true; Map data = {}; String error = ''; Duration pollInterval; + Timer pollTimer; + bool initialFetch = false; + Map currentVariables = new Map(); @override void initState() { super.initState(); - if (widget.pollInterval != null) { + if (widget.pollInterval is int) { pollInterval = new Duration(seconds: widget.pollInterval); } getQueryResult(); } + @override + void dispose() { + _deleteTimer(); + + super.dispose(); + } + + void _deleteTimer() { + if (pollTimer is Timer) { + pollTimer.cancel(); + pollTimer = null; + } + } + void getQueryResult() async { try { final Map result = client.readQuery( @@ -71,8 +88,8 @@ class QueryState extends State with WidgetsBindingObserver { data = result; }); - if (widget.pollInterval != null) { - new Timer(pollInterval, getQueryResult); + if (pollInterval is Duration && !(pollTimer is Timer)) { + pollTimer = new Timer(pollInterval, () => getQueryResult()); } } catch (e) { if (data == {}) { @@ -82,8 +99,8 @@ class QueryState extends State with WidgetsBindingObserver { }); } - if (widget.pollInterval != null) { - new Timer(pollInterval, getQueryResult); + if (pollInterval is Duration && !(pollTimer is Timer)) { + pollTimer = new Timer(pollInterval, () => getQueryResult()); } // TODO: handle error @@ -91,7 +108,40 @@ class QueryState extends State with WidgetsBindingObserver { } } + bool _areDifferentMaps(Map a, Map b) { + if (a.length != b.length) { + return true; + } + + bool areDifferent = false; + + a.forEach((key, value) { + if (b[key] != a[key] || (!b.containsKey(key))) { + areDifferent = true; + } + }); + + return areDifferent; + } + Widget build(BuildContext context) { + if (!initialFetch) { + initialFetch = true; + + currentVariables = widget.variables; + + getQueryResult(); + } + + if (_areDifferentMaps(currentVariables, widget.variables)) { + currentVariables = widget.variables; + + loading = true; + _deleteTimer(); + + getQueryResult(); + } + return widget.builder( loading: loading, error: error, diff --git a/pubspec.yaml b/pubspec.yaml index c52448aea..e3c169289 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: graphql_flutter description: A GraphQL client for Flutter. -version: 0.5.2 +version: 0.5.3 authors: - Eus Dima - Zino Hofmann