|
| 1 | +// 🐦 Twitter https://twitter.com/vandadnp |
| 2 | +// 🔵 LinkedIn https://linkedin.com/in/vandadnp |
| 3 | +// 🎥 YouTube https://youtube.com/c/vandadnp |
| 4 | +// 🤝 Want to support my work? https://youtube.com/c/vandadnp/join/ |
| 5 | + |
| 6 | +// import 'package:flutter/material.dart'; |
| 7 | + |
| 8 | +import'dart:async'; |
| 9 | + |
| 10 | +import'package:flutter/material.dart'; |
| 11 | +import'dart:developer'as devtoolsshow log; |
| 12 | + |
| 13 | +voidmain() { |
| 14 | +runApp( |
| 15 | +constApp(), |
| 16 | + ); |
| 17 | +} |
| 18 | + |
| 19 | +extensionLogonObject { |
| 20 | +voidlog()=> devtools.log(toString()); |
| 21 | +} |
| 22 | + |
| 23 | +classTimeoutBetweenEvents<E>extendsStreamTransformerBase<E,E> { |
| 24 | +finalDuration duration; |
| 25 | + |
| 26 | +constTimeoutBetweenEvents({requiredthis.duration}); |
| 27 | + |
| 28 | +@override |
| 29 | +Stream<E>bind(Stream<E> stream) { |
| 30 | +StreamController<E>? controller; |
| 31 | +StreamSubscription? subscription; |
| 32 | +Timer? timer; |
| 33 | + |
| 34 | + controller=StreamController( |
| 35 | + onListen: () { |
| 36 | + subscription= stream.listen( |
| 37 | + (data) { |
| 38 | + timer?.cancel(); |
| 39 | + timer=Timer.periodic(duration, (_) { |
| 40 | + controller?.addError( |
| 41 | +TimeoutBetweenEventsException('Timeout'), |
| 42 | + ); |
| 43 | + }); |
| 44 | + controller?.add(data); |
| 45 | + }, |
| 46 | + onError: controller?.addError, |
| 47 | + onDone: controller?.close, |
| 48 | + ); |
| 49 | + }, |
| 50 | + onCancel: () { |
| 51 | + subscription?.cancel(); |
| 52 | + timer?.cancel(); |
| 53 | + }, |
| 54 | + ); |
| 55 | + |
| 56 | +return controller.stream; |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +classTimeoutBetweenEventsExceptionimplementsException { |
| 61 | +finalString message; |
| 62 | +TimeoutBetweenEventsException(this.message); |
| 63 | +} |
| 64 | + |
| 65 | +extensionWithTimeoutBetweenEvents<T>onStream<T> { |
| 66 | +Stream<T>withTimeoutBetweenEvents(Duration duration)=> |
| 67 | +transform(TimeoutBetweenEvents(duration: duration)); |
| 68 | +} |
| 69 | + |
| 70 | +Stream<String>getNames()async* { |
| 71 | +yield'John'; |
| 72 | +awaitFuture.delayed(constDuration(seconds:1)); |
| 73 | +yield'Jane'; |
| 74 | +awaitFuture.delayed(constDuration(seconds:10)); |
| 75 | +yield'Doe'; |
| 76 | +} |
| 77 | + |
| 78 | +Future<void>testIt()async { |
| 79 | +awaitfor (final nameingetNames().withTimeoutBetweenEvents( |
| 80 | +constDuration( |
| 81 | + seconds:3, |
| 82 | + ), |
| 83 | + )) { |
| 84 | + name.log(); |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +classAppextendsStatelessWidget { |
| 89 | +constApp({ |
| 90 | +Key? key, |
| 91 | + }):super(key: key); |
| 92 | + |
| 93 | +@override |
| 94 | +Widgetbuild(BuildContext context) { |
| 95 | +returnMaterialApp( |
| 96 | + theme:ThemeData.dark(), |
| 97 | + debugShowCheckedModeBanner:false, |
| 98 | + home:constHomePage(), |
| 99 | + ); |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +classHomePageextendsStatelessWidget { |
| 104 | +constHomePage({Key? key}):super(key: key); |
| 105 | + |
| 106 | +@override |
| 107 | +Widgetbuild(BuildContext context) { |
| 108 | +testIt(); |
| 109 | +returnScaffold( |
| 110 | + appBar:AppBar( |
| 111 | + title:constText('Home Page'), |
| 112 | + ), |
| 113 | + ); |
| 114 | + } |
| 115 | +} |