How to make an asynchronous Dart call synchronous?

前端 未结 8 1544
心在旅途
心在旅途 2020-12-14 16:39

I\'m on the way to evaluate Dart for a German company by porting various Java programs to Dart and compare and analyze the results. In the browser Dart wins hands down. For

相关标签:
8条回答
  • 2020-12-14 17:34

    Here's a solution based on staggering the start of the async function with start times at least 1 second apart, when calls come in almost simultaneously.

    Steps:

    1. Use the lastKnownTime to calculate the delta, where the initial value is 0

    2. Once the delta is not some huge number, you know it's a duplicate call.

    
        class StartConversationState extends State<StartConversationStatefulWidget> {
    
          @override
          Widget build(BuildContext context) {
    
            _delayPush(); // this is the call that gets triggered multiple times
          }
    
          int lastKnownTime = 0;
          int delayMillis = 3000;
    
          _delayPush() async {
            delayMillis += 1500;
    
            await new Future.delayed(Duration(milliseconds: delayMillis));
    
            int millisSinceEpoch = new DateTime.now().millisecondsSinceEpoch;
            int delta = millisSinceEpoch - lastKnownTime;
    
            // if delta is less than 10 seconds, means it was a subsequent interval
            if (delta < 10000) {
    
              print('_delayPush() , SKIPPING DUPLICATE CALL');
              return;
            }
    
            // here is the logic you don't want to duplicate
            // eg, insert DB record and navigate to next screen
        }
    
    
    0 讨论(0)
  • 2020-12-14 17:35
    import 'package:synchronized_lite/synchronized_lite.dart';
    
    import 'dart:async';
    
    // Using Lock as a mixin to further mimic Java-style synchronized blocks
    class SomeActivity with Lock {
    
      bool _started = false;
    
      Future<bool> start() async {
        // It's correct to return a Future returned by synchronized()
        return synchronized(() async {
          if(_started)
            return false;
          // perform the start operation
          await Future.delayed(Duration(seconds: 1));
          print("Started");
          _started = true;
          return true;
        });
      }
    
      Future<void> stop() async {
        // It's also correct to await a synchronized() call before returning
        // It's incorrect to neither await a synchronized() call nor return its Future.
        await synchronized(() async {
          if(!_started)
            return;
          // perform the stop operation`enter code here`
          await Future.delayed(Duration(seconds: 1));
          print("Stopped");
          _started = false;
        });
      }
    }
    
    // Prints:
    //   Started
    //   Stopped
    main() async {
      var a = SomeActivity();
      print("Hello");
      a.start();
      a.start();
      a.stop();
      await a.stop();
    }
    
    0 讨论(0)
提交回复
热议问题