Skip to content

Check for new events more often in batched stream. #2123

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions dwds/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
- Refactor code for presenting record instances. - [#2074](https://github.com/dart-lang/webdev/pull/2074)
- Display record types concisely. - [#2070](https://github.com/dart-lang/webdev/pull/2070)
- Display type objects concisely. - [#2103](https://github.com/dart-lang/webdev/pull/2103)
- Check for new events more often in batched stream. - [#2123](https://github.com/dart-lang/webdev/pull/2123)

## 19.0.0

Expand Down
6 changes: 4 additions & 2 deletions dwds/lib/shared/batched_stream.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
// BSD-style license that can be found in the LICENSE file.

import 'dart:async';
import 'dart:math';
import 'package:async/async.dart';
import 'package:dwds/src/utilities/shared.dart';

/// Stream controller allowing to batch events.
class BatchedStreamController<T> {
static const _defaultBatchDelayMilliseconds = 1000;
static const _checkDelayMilliseconds = 100;

final int _checkDelayMilliseconds;
final int _batchDelayMilliseconds;

final StreamController<T> _inputController;
Expand All @@ -26,6 +27,7 @@ class BatchedStreamController<T> {
BatchedStreamController({
int delay = _defaultBatchDelayMilliseconds,
}) : _batchDelayMilliseconds = delay,
_checkDelayMilliseconds = max(delay ~/ 10, 1),
_inputController = StreamController<T>(),
_outputController = StreamController<List<T>>() {
_inputQueue = StreamQueue<T>(_inputController.stream);
Expand All @@ -46,7 +48,7 @@ class BatchedStreamController<T> {

/// Send events to the output in a batch every [_batchDelayMilliseconds].
Future<void> _batchAndSendEvents() async {
const duration = Duration(milliseconds: _checkDelayMilliseconds);
final duration = Duration(milliseconds: _checkDelayMilliseconds);
final buffer = <T>[];

// Batch events every `_batchDelayMilliseconds`.
Expand Down