Skip to content

Commit

Permalink
Semi-improved performance of widget streams
Browse files Browse the repository at this point in the history
  • Loading branch information
Gold872 committed Feb 25, 2024
1 parent b47cc9f commit cb1b575
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 17 deletions.
6 changes: 4 additions & 2 deletions lib/services/nt4_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,14 @@ class NT4Subscription {
}

Stream<Object?> periodicStream({bool yieldAll = true}) async* {
final Duration delayTime =
Duration(microseconds: (options.periodicRateSeconds * 1e6).round());

yield currentValue;
Object? lastYielded = currentValue;

while (true) {
await Future.delayed(
Duration(microseconds: (options.periodicRateSeconds * 1e6).round()));
await Future.delayed(delayTime);

if (lastYielded != currentValue || yieldAll) {
yield currentValue;
Expand Down
12 changes: 7 additions & 5 deletions lib/widgets/nt_widgets/multi-topic/field_widget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,12 @@ class FieldWidgetModel extends NTWidgetModel {

@override
Stream<Object> get multiTopicPeriodicStream async* {
final Duration delayTime = Duration(
microseconds: ((subscription?.options.periodicRateSeconds ??
Settings.defaultPeriod) *
1e6)
.round());

yield Object();

int previousHash = Object.hashAll(getCurrentData());
Expand All @@ -329,11 +335,7 @@ class FieldWidgetModel extends NTWidgetModel {
yield Object();
}

await Future.delayed(Duration(
milliseconds: ((subscription?.options.periodicRateSeconds ??
Settings.defaultPeriod) *
1000)
.round()));
await Future.delayed(delayTime);
}
}

Expand Down
12 changes: 7 additions & 5 deletions lib/widgets/nt_widgets/nt_widget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,12 @@ class NTWidgetModel extends ChangeNotifier {
}

Stream<Object> get multiTopicPeriodicStream async* {
final Duration delayTime = Duration(
microseconds: ((subscription?.options.periodicRateSeconds ??
Settings.defaultPeriod) *
1e6)
.round());

int previousHash = Object.hashAll(getCurrentData());

while (true) {
Expand All @@ -203,11 +209,7 @@ class NTWidgetModel extends ChangeNotifier {
previousHash = currentHash;
}

await Future.delayed(Duration(
milliseconds: ((subscription?.options.periodicRateSeconds ??
Settings.defaultPeriod) *
1000)
.round()));
await Future.delayed(delayTime);
}
}

Expand Down
5 changes: 2 additions & 3 deletions lib/widgets/nt_widgets/single_topic/text_display.dart
Original file line number Diff line number Diff line change
Expand Up @@ -139,10 +139,9 @@ class TextDisplay extends NTWidget {
stream: model.subscription?.periodicStream(),
initialData: ntConnection.getLastAnnouncedValue(model.topic),
builder: (context, snapshot) {
Object data = snapshot.data ?? Object();
Object? data = snapshot.data;

if (data.toString() != model.previousValue.toString() &&
!data.isExactType<Object>()) {
if (data?.toString() != model.previousValue?.toString() && data != null) {
// Needed to prevent errors
Future(() async {
model.controller.text = data.toString();
Expand Down
4 changes: 2 additions & 2 deletions lib/widgets/tab_grid.dart
Original file line number Diff line number Diff line change
Expand Up @@ -697,9 +697,9 @@ class TabGrid extends StatelessWidget {
Widget getWidgetFromModel(WidgetContainerModel model) {
if (model is NTWidgetContainerModel) {
return ChangeNotifierProvider<NTWidgetContainerModel>.value(
key: model.key,
value: model,
child: DraggableNTWidgetContainer(
key: model.key,
tabGrid: this,
onUpdate: _ntContainerOnUpdate,
onDragBegin: _ntContainerOnDragBegin,
Expand All @@ -711,9 +711,9 @@ class TabGrid extends StatelessWidget {
);
} else if (model is ListLayoutModel) {
return ChangeNotifierProvider<ListLayoutModel>.value(
key: model.key,
value: model,
child: DraggableListLayout(
key: model.key,
tabGrid: this,
onUpdate: _layoutContainerOnUpdate,
onDragBegin: _layoutContainerOnDragBegin,
Expand Down

0 comments on commit cb1b575

Please sign in to comment.