Skip to content

Commit

Permalink
fix: Equality of ProviderData
Browse files Browse the repository at this point in the history
  • Loading branch information
Gustl22 committed Jan 16, 2024
1 parent 0e8b298 commit 67bf7f7
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 110 deletions.
51 changes: 40 additions & 11 deletions wrestling_scoreboard_client/lib/provider/data_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,35 +8,64 @@ import 'package:wrestling_scoreboard_common/common.dart';

part 'data_provider.g.dart';

/// Class to wrap equal single data for providers.
class SingleProviderData<T extends DataObject> {
final int id;
final T? initialData;

SingleProviderData({required this.id, this.initialData});

@override
bool operator ==(Object other) => identical(this, other) || other is SingleProviderData<T> && id == other.id;

@override
int get hashCode => Object.hash(id, T);
}

@riverpod
Stream<T> singleDataStream<T extends DataObject>(
SingleDataStreamRef ref, {
required int id,
T? initialData,
}) {
SingleDataStreamRef ref,
SingleProviderData<T> pData,
) {
// Reload, whenever the stream connects or disconnects
// TODO: integrate in updating websocket URL from stream provider
ref.watch(webSocketStateStreamProvider);

// ref.onDispose(webSocketConnectionStream.close);
// TODO: e.g. may be triggered twice
return dataProvider
.streamSingle<T>(id, init: initialData == null);
return dataProvider.streamSingle<T>(pData.id, init: pData.initialData == null);
}

/// Class to wrap equal many data for providers.
class ManyProviderData<T extends DataObject, S extends DataObject?> {
final S? filterObject;
final List<T>? initialData;

ManyProviderData({this.filterObject, this.initialData});

@override
bool operator ==(Object other) {
return identical(this, other) ||
other is ManyProviderData<T, S> &&
(filterObject == null && other.filterObject == null || filterObject?.id == other.filterObject?.id);
}

@override
int get hashCode => Object.hash(filterObject?.id, T, S);
}

@riverpod
Stream<List<T>> manyDataStream<T extends DataObject, S extends DataObject?>(
ManyDataStreamRef ref, {
S? filterObject,
List<T>? initialData,
}) {
ManyDataStreamRef ref,
ManyProviderData<T,S> pData,
) {
// Reload, whenever the stream connects or disconnects
// TODO: integrate in updating websocket URL from stream provider
ref.watch(webSocketStateStreamProvider);

// ref.onDispose(webSocketConnectionStream.close);
// TODO: e.g. bout action event triggered twice
return dataProvider
.streamMany<T, S>(filterObject: filterObject, init: initialData == null)
.streamMany<T, S>(filterObject: pData.filterObject, init: pData.initialData == null)
.map((event) => event.data);
}
Loading

0 comments on commit 67bf7f7

Please sign in to comment.