Line data Source code
1 : // Copyright 2023 The terCAD team. All rights reserved. 2 : // Use of this source code is governed by a CC BY-NC-ND 4.0 license that can be found in the LICENSE file. 3 : 4 : import 'dart:async'; 5 : import 'package:flutter/material.dart'; 6 : 7 : class DelayedCall { 8 : final int delay; 9 : Timer? _timer; 10 : VoidCallback? _action; 11 : bool preserveFirst; 12 : 13 2 : DelayedCall(this.delay, {this.preserveFirst = false}); 14 : 15 2 : void run(VoidCallback action) { 16 4 : _timer?.cancel(); 17 2 : if (!preserveFirst || _action == null) { 18 2 : _action = action; 19 : } 20 10 : _timer = Timer(Duration(milliseconds: delay), () { 21 2 : final v = _action; 22 2 : _action = null; 23 2 : v?.call(); 24 : }); 25 : } 26 : 27 0 : void cancel() { 28 0 : _action?.call(); 29 0 : _timer?.cancel(); 30 : } 31 : }