How to convert value to Stream? - dart

I am learning Dart. Here is my code:
class MyClass {
MyClass() {
stream = Stream<int>.periodic(Duration(seconds: 1), (t) => t + 1).take(3);
myStreamController.addStream(stream);
Future.delayed(Duration(seconds: 2), () {
myStreamController.addStream(42); // but 42 is digit, not stream!
});
}
}
Working code:
class MyClass {
MyClass() {
stream = Stream<int>.periodic(Duration(seconds: 1), (t) => t + 1).take(3);
myStreamController.addStream(stream).then(
(done) {
Future.delayed(Duration(seconds: 2), () {
myStreamController.addStream( Stream.value(42) );
});
}
);
}
}
How I can add value to Stream?

Look at the Stream.value constructor:
Creates a stream which emits a single data event before completing.
This stream emits a single data event of value and then completes with
a done event.
https://api.dart.dev/stable/2.5.0/dart-async/Stream/Stream.value.html

Related

Understanding Future.delayed()

When I try to run the below code, it completes in a little more than 4 seconds. I couldn't understand why it finishes in that time. I thought it would complete in 14 seconds(4sec in declaring order variable,10sec in for loop). Don't Future.delayed() stop all the progress in program?
Future<void> printOrderMessage() async {
print("Awaiting user order ...");
var order = await fetchUserOrder(); //I couldn't understand here.
print('Your order is: $order');
}
Future<String> fetchUserOrder() {
return Future.delayed(const Duration(seconds: 4), () => 'Large Latte');
}
void main() async {
countSeconds(4); //Başlama yeri
await printOrderMessage();
}
void countSeconds(int s) {
for (var i = 1; i <= s; i++) {
Future.delayed(Duration(seconds: i), () => print(i)); //Also here
}
}
Output:
Awaiting user order ...
1
2
3
4
Your order is: Large Latte

Dart Stream: How to merge emitted items, when they're short after eachother?

Let's assume I have a Stream<int> emitting integers in different time deltas i.e. between 5ms and 1000ms.
When the delta is <= 50ms I want to merge them. for example:
3, (delta:100) 5, (delta:27) 6, (delta:976) 3
I want to consume: 3, 11(merged using addition), 3.
Is this possible?
You can use the debounceBuffer stream transformer from the stream_transform package.
stream
.transform(debounceBuffer(const Duration(milliseconds: 50)))
.map((list) => list.fold(0, (t, e) => t + e))
You can write that easily enough yourself:
Stream<int> debounce(
Stream<int> source, Duration limit, int combine(int a, int b)) async* {
int prev;
var stopwatch;
await for (var event in source) {
if (stopwatch == null) {
// First event.
prev = event;
stopwatch = Stopwatch()..start();
} else {
if (stopwatch.elapsed < limit) {
prev = combine(prev, event);
} else {
yield prev;
prev = event;
}
stopwatch.reset();
}
}
// If any event, yield prev.
if (stopwatch != null) yield prev;
}

Add delay constructing a Future

Playing with Dart, is it possible to create a delay constructing a Future?:
Future<String>.value("Hello").then((newsDigest) {
print(newsDigest);
}) // .delayed(Duration(seconds: 5))
Yes, this is possible:
factory Future.delayed(Duration duration, [FutureOr<T> computation()]) {
_Future<T> result = new _Future<T>();
new Timer(duration, () {
try {
result._complete(computation?.call());
} catch (e, s) {
_completeWithErrorCallback(result, e, s);
}
});
return result;
}
As you have already discovered Future.delayed constructor creates a future that runs after a delay:
From the docs:
Future<T>.delayed(
Duration duration,
[ FutureOr<T> computation()
])
The computation will be executed after the given duration has passed, and the future is completed with the result of the computation.
If computation returns a future, the future returned by this constructor will complete with the value or error of that future.
For the sake of simplicity, taking a future that complete immediately with a value, this snippet creates a delayed future that complete after 3 seconds:
import 'dart:async';
main() {
var future = Future<String>.value("Hello");
var delayedFuture = Future.delayed(Duration(seconds: 3), () => future);
delayedFuture.then((value) {
print("Done: $value");
});
}

How to cancel a Stream when using Stream.periodic?

I'm having trouble canceling a stream that is created using the Stream.periodic constructor. Below is my attempt at canceling the stream. However, I'm having a hard time extracting out the 'count' variable from the internal scope. Therefore, I can't cancel the subscription.
import 'dart:async';
void main() {
int count = 0;
final Stream newsStream = new Stream.periodic(Duration(seconds: 2), (_) {
return _;
});
StreamSubscription mySubscribedStream = newsStream.map((e) {
count = e;
print(count);
return 'stuff $e';
}).listen((e) {
print(e);
});
// count = 0 here because count is scoped inside mySubscribedStream
// How do I extract out 'count', so I can cancel the stream?
if (count > 5) {
mySubscribedStream.cancel();
mySubscribedStream = null;
}
}
I'd rather use take(5) instead of checking > 5 and then cancel
final Stream newsStream = new Stream.periodic(Duration(seconds: 2), (_) => count++);
newsStream.map((e) {
count = e;
print(count);
return 'stuff $e';
}).take(5).forEach((e) {
print(e);
});

Dart HttpClient.getUrl invoked by Timer without client or http server

EDIT: Problem wasn't related to Timer or HttpServer, it was dart.io sleep function pausing everything. It is clearly described in documentation, my bad.
//
I have weird problem with HttpClient working in server code. I call
client.getUrl(Uri.parse(url)).then((HttpClientRequest response) => response.close()).then(HttpBodyHandler.processResponse).then((HttpClientResponseBody body) {
print(body.response.statusCode);
from Timer object and it never reach print step.
It is almost copy and paste code from previous version, which wasn't called from Timer but from HttpRequest. Working code is in my question [here][1].
It fails on the long line, I suspect that it is a last Future it never reach (HttpClientResponseBody).
Timer object is created like this (just test code):
main() {
t = new Timer.periodic(new Duration(minutes: period), (Timer t) => hit());
}
void hit() {
if (new DateTime.now().hour == 17) {
print("syncing rock");
loadUrlBody(furl + filter).then((content) {
print("content loaded");
//edit:
okay, here is the source, it might be some trivial problem..which I can't figure out for two days :-D
import 'dart:async';
import 'dart:io';
import 'package:http_server/http_server.dart';
import 'package:slack/slack_io.dart' as slack;
Timer t;
bool check;
final period = 1;
final furl = "https://****.tpondemand.com";
final filter = "somefilter";
main() {
t = new Timer.periodic(new Duration(minutes: period), (Timer t) => hit());
}
void hit() {
if (new DateTime.now().hour == 17) {
print("syncing rock");
loadUrlBody(furl + filter).then((content) {
print("content loaded");
Map parsedMap = content.body;
handleMap(parsedMap);
});
sleep(new Duration(minutes: 60));
} else {
print("no time to rock " + new DateTime.now().toString());
sleep(new Duration(minutes: period * 10));
}
}
Future loadUrlBody(String url) {
final c = new Completer();
HttpClient client = new HttpClient();
client.addCredentials(Uri.parse("https://****.tpondemand.com/api"), "tprealm", new HttpClientBasicCredentials("user", "password"));
client.getUrl(Uri.parse(url)).then((HttpClientRequest response) => response.close()).then(HttpBodyHandler.processResponse).then((HttpClientResponseBody body) {
print(body.response.statusCode);
c.complete(body);
});
return c.future;
}
void send2Slack(String m) {
slack.Message message = new slack.Message()..text = m;
slack.token = 'token';
slack.team = 'team';
slack.send(message);
}
void handleMap(Map valueMap) {
final Duration lostInTime = new Duration(days: 30);
var sb = new StringBuffer();
sb.write('K o m p o s t \n');
for (var item in valueMap["Items"]) {
if (item['CreateDate'] == null) item['CreateDate'] = '/Date(1403167885000+0100)/';
if (item['ModifyDate'] == null) item['ModifyDate'] = '/Date(1403167885000+0100)/';
if (item['LastCommentDate'] == null) item['LastCommentDate'] = '/Date(1403167885000+0100)/';
DateTime moonLanding = new DateTime.fromMillisecondsSinceEpoch(int.parse(item['CreateDate'].substring(6, 19)));
DateTime modifyLanding = new DateTime.fromMillisecondsSinceEpoch(int.parse(item['ModifyDate'].substring(6, 19)));
DateTime commentLanding = new DateTime.fromMillisecondsSinceEpoch(int.parse(item['LastCommentDate'].substring(6, 19)));
DateTime lastChangeLanding = (modifyLanding.isBefore(commentLanding)) ? commentLanding : modifyLanding;
Duration difference = new DateTime.now().difference(lastChangeLanding);
if (moonLanding.add(lostInTime).isBefore(new DateTime.now()) && difference.inDays > 4) {
sb
..write('<https://****.tpondemand.com/entity/')
..write(item['Id'])
..write('|')
..write(item['Name'])
..write('> last change: ')
..write(difference.inDays)
..write(' days ago \n');
}
;
}
send2Slack(sb.toString());
print("sent to Slack");
sb.clear();
}
I created similar code but I can't reproduce your problem.
So basically this does work when called from a Timer.
import 'dart:io';
import 'dart:async';
import 'package:http_server/http_server.dart';
Timer t;
final period = 1;
void main(args) {
t = new Timer.periodic(new Duration(minutes: period), (Timer t) => hit());
}
void hit() {
loadUrlBody('http://www.google.com')
.then((HttpClientResponseBody b) => print('hit: ${b.response.statusCode}'));
}
Future loadUrlBody(String url) {
print('executing');
HttpClient client = new HttpClient();
// commented out because I have no server where I can use it
// HttpClient client = new HttpClient()
// ..addCredentials(Uri.parse("https://****.tpondemand.com/api"), "tprealm", new HttpClientBasicCredentials("user", "password"));
return client.getUrl(Uri.parse(url)) // <== return is important here
.then((HttpClientRequest response) => response.close())
.then(HttpBodyHandler.processResponse)
.then((HttpClientResponseBody body) {
print('body: (${new DateTime.now()}) ${body.response.statusCode}');
return body; // <== this value is the value the next 'then' receives.
// for example x in: loadUrlBody('http://someurl').then(x) => doSomething(x));
});
}
You don't need to use a Completer. Completer are for more complicated used cases where for example one method returns a Completer and for example an eventHandler completes it.
You just have to ensure that you return a Future everywhere. then always returns a Future. The value of the returned Future is the value returned inside then.

Resources