mirror of
https://gitlab.com/veilid/veilidchat.git
synced 2025-07-24 15:05:22 -04:00
refactor and move stuff
This commit is contained in:
parent
8c22bf8cc0
commit
b54868cc55
28 changed files with 500 additions and 94 deletions
27
lib/tools/external_stream_state.dart
Normal file
27
lib/tools/external_stream_state.dart
Normal file
|
@ -0,0 +1,27 @@
|
|||
import 'dart:async';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
|
||||
// Caches a state value which can be changed from anywhere
|
||||
// Creates a provider interface that notices when the value changes
|
||||
class ExternalStreamState<T> {
|
||||
T currentState;
|
||||
StreamController<T> streamController;
|
||||
ExternalStreamState(T initialState)
|
||||
: currentState = initialState,
|
||||
streamController = StreamController<T>.broadcast();
|
||||
void add(T newState) {
|
||||
currentState = newState;
|
||||
streamController.add(newState);
|
||||
}
|
||||
|
||||
AutoDisposeStreamProvider<T> provider() {
|
||||
return AutoDisposeStreamProvider<T>((ref) async* {
|
||||
if (await streamController.stream.isEmpty) {
|
||||
yield currentState;
|
||||
}
|
||||
await for (final value in streamController.stream) {
|
||||
yield value;
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue