-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
de21018
commit 358cf04
Showing
2 changed files
with
152 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,115 @@ | ||
import 'dart:async'; | ||
|
||
import 'package:rxdart/rxdart.dart'; | ||
import 'package:tekartik_common_utils/common_utils_import.dart'; | ||
|
||
final _debug = false; | ||
|
||
/// Stream helpers | ||
extension TekartikRxStreamExt<T> on Stream<T> { | ||
/// Convert any stream to a behavior subject | ||
@Deprecated('Use toBroadcastValueStream') | ||
BehaviorSubject<T> toBehaviorSubject() { | ||
late StreamSubscription<T> subscription; | ||
StreamSubscription<T>? subscription; | ||
late BehaviorSubject<T> subject; | ||
if (_debug) { | ||
print('toBehaviorSubject $hashCode'); | ||
} | ||
subject = BehaviorSubject<T>( | ||
onListen: () { | ||
if (_debug) { | ||
print('onListen $hashCode'); | ||
} | ||
subscription = listen((event) { | ||
subject.add(event); | ||
}); | ||
}, | ||
onCancel: () { | ||
subscription.cancel(); | ||
if (_debug) { | ||
print('onCancel $hashCode'); | ||
} | ||
subscription?.cancel(); | ||
}, | ||
sync: true); | ||
return subject; | ||
} | ||
|
||
/// It won't listen to the stream until the first listener is added and stop | ||
/// when it is closed. | ||
BroadcastValueStream<T> toBroadcastValueStream() => | ||
_BroadcastValueStream(this); | ||
} | ||
|
||
/// Stream subscription is closed when then stream is closed. | ||
abstract class BroadcastValueStream<T> extends ValueStream<T> { | ||
Future<void> close(); | ||
} | ||
|
||
class _BroadcastValueStream<T> extends Stream<T> | ||
implements BroadcastValueStream<T> { | ||
late final BehaviorSubject<T> _subject; | ||
StreamSubscription<T>? subscription; | ||
|
||
_BroadcastValueStream(Stream<T> stream) { | ||
_subject = BehaviorSubject<T>( | ||
onListen: () { | ||
if (_debug) { | ||
print('onListen $hashCode'); | ||
} | ||
subscription ??= stream.listen((event) { | ||
_subject.add(event); | ||
}); | ||
}, | ||
onCancel: () { | ||
if (_debug) { | ||
print('onCancel $hashCode'); | ||
} | ||
}, | ||
sync: true); | ||
} | ||
|
||
@override | ||
StreamSubscription<T> listen( | ||
void Function(T event)? onData, { | ||
Function? onError, | ||
void Function()? onDone, | ||
bool? cancelOnError, | ||
}) => | ||
_subject.listen( | ||
onData, | ||
onError: onError, | ||
onDone: onDone, | ||
cancelOnError: cancelOnError, | ||
); | ||
|
||
@override | ||
Future<void> close() async { | ||
if (_debug) { | ||
print('close $hashCode'); | ||
} | ||
subscription?.cancel().unawait(); | ||
await _subject.close(); | ||
} | ||
|
||
@override | ||
bool get isBroadcast => _subject.isBroadcast; | ||
|
||
@override | ||
Object get error => _subject.error; | ||
|
||
@override | ||
Object? get errorOrNull => _subject.errorOrNull; | ||
|
||
@override | ||
bool get hasError => _subject.hasError; | ||
|
||
@override | ||
bool get hasValue => _subject.hasValue; | ||
|
||
@override | ||
StackTrace? get stackTrace => _subject.stackTrace; | ||
|
||
@override | ||
T get value => _subject.value; | ||
|
||
@override | ||
T? get valueOrNull => _subject.valueOrNull; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters