Jonatas

added debounce to update

  1 +import 'dart:async';
1 import 'dart:collection'; 2 import 'dart:collection';
2 import 'package:flutter/foundation.dart'; 3 import 'package:flutter/foundation.dart';
3 import 'package:flutter/widgets.dart'; 4 import 'package:flutter/widgets.dart';
@@ -10,6 +11,12 @@ typedef Disposer = void Function(); @@ -10,6 +11,12 @@ typedef Disposer = void Function();
10 typedef GetStateUpdate = void Function(); 11 typedef GetStateUpdate = void Function();
11 12
12 class ListNotifier implements Listenable { 13 class ListNotifier implements Listenable {
  14 + int _version = 0;
  15 + int _microtask = 0;
  16 +
  17 + int get notifierVersion => _version;
  18 + int get notifierMicrotask => _microtask;
  19 +
13 List<GetStateUpdate> _updaters = <GetStateUpdate>[]; 20 List<GetStateUpdate> _updaters = <GetStateUpdate>[];
14 21
15 HashMap<String, List<GetStateUpdate>> _updatersGroupIds = 22 HashMap<String, List<GetStateUpdate>> _updatersGroupIds =
@@ -18,21 +25,50 @@ class ListNotifier implements Listenable { @@ -18,21 +25,50 @@ class ListNotifier implements Listenable {
18 @protected 25 @protected
19 void refresh() { 26 void refresh() {
20 assert(_debugAssertNotDisposed()); 27 assert(_debugAssertNotDisposed());
  28 +
  29 + /// This debounce the call to update.
  30 + /// It prevent errors and duplicates builds
  31 + if (_microtask == _version) {
  32 + _microtask++;
  33 + scheduleMicrotask(() {
  34 + _version++;
  35 + _microtask = _version;
  36 + _notifyUpdate();
  37 + });
  38 + }
  39 + }
  40 +
  41 + void _notifyUpdate() {
21 for (var element in _updaters) { 42 for (var element in _updaters) {
22 element(); 43 element();
23 } 44 }
24 } 45 }
25 46
26 - @protected  
27 - void refreshGroup(String id) {  
28 - assert(_debugAssertNotDisposed()); 47 + void _notifyIdUpdate(String id) {
29 if (_updatersGroupIds.containsKey(id)) { 48 if (_updatersGroupIds.containsKey(id)) {
30 - for (var item in _updatersGroupIds[id]) { 49 + final listGroup = _updatersGroupIds[id];
  50 + for (var item in listGroup) {
31 item(); 51 item();
32 } 52 }
33 } 53 }
34 } 54 }
35 55
  56 + @protected
  57 + void refreshGroup(String id) {
  58 + assert(_debugAssertNotDisposed());
  59 +
  60 + /// This debounce the call to update.
  61 + /// It prevent errors and duplicates builds
  62 + if (_microtask == _version) {
  63 + _microtask++;
  64 + scheduleMicrotask(() {
  65 + _version++;
  66 + _microtask = _version;
  67 + _notifyIdUpdate(id);
  68 + });
  69 + }
  70 + }
  71 +
36 bool _debugAssertNotDisposed() { 72 bool _debugAssertNotDisposed() {
37 assert(() { 73 assert(() {
38 if (_updaters == null) { 74 if (_updaters == null) {
1 import 'dart:async'; 1 import 'dart:async';
2 2
  3 +class GetMicrotask {
  4 + int _version = 0;
  5 + int _microtask = 0;
  6 +
  7 + int get version => _version;
  8 + int get microtask => _microtask;
  9 +
  10 + void exec(Function callback) {
  11 + if (_microtask == _version) {
  12 + _microtask++;
  13 + scheduleMicrotask(() {
  14 + _version++;
  15 + _microtask = _version;
  16 + callback();
  17 + });
  18 + }
  19 + }
  20 +}
  21 +
3 class GetQueue { 22 class GetQueue {
4 final List<_Item> _queue = []; 23 final List<_Item> _queue = [];
5 bool _active = false; 24 bool _active = false;