Skip to content

Commit

Permalink
Merge pull request #39 from marcobraghim/master
Browse files Browse the repository at this point in the history
Bugfix to the closed Stream receiving a new listener.
  • Loading branch information
jonataslaw committed Sep 17, 2020
2 parents c4fcc02 + 173b481 commit 6f6bfbd
Showing 1 changed file with 12 additions and 13 deletions.
25 changes: 12 additions & 13 deletions lib/src/video_compressor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,12 @@ class VideoCompress {
}

/// Subscribe the compress progress
static ObservableBuilder<double> compressProgress$ =
ObservableBuilder<double>();
static ObservableBuilder<double> compressProgress$ = ObservableBuilder<double>();

static Future<T> _invoke<T>(String name,
[Map<String, dynamic> params]) async {
static Future<T> _invoke<T>(String name, [Map<String, dynamic> params]) async {
T result;
try {
result = params != null
? await _channel.invokeMethod(name, params)
: await _channel.invokeMethod(name);
result = params != null ? await _channel.invokeMethod(name, params) : await _channel.invokeMethod(name);
} on PlatformException catch (e) {
debugPrint('''Error from VideoCompress:
Method: $name
Expand Down Expand Up @@ -174,19 +170,22 @@ class VideoCompress {
}

class ObservableBuilder<T> {
final StreamController<T> _observable = StreamController();
StreamController<T> _observable = StreamController();
bool notSubscribed = true;

void next(T value) {
_observable.add(value);
}

Subscription subscribe(void onData(T event),
{Function onError, void onDone(), bool cancelOnError}) {
Subscription subscribe(void onData(T event), {Function onError, void onDone(), bool cancelOnError}) {
notSubscribed = false;
_observable.stream.listen(onData,
onError: onError, onDone: onDone, cancelOnError: cancelOnError);
return Subscription(_observable.close);
_observable.stream.listen(onData, onError: onError, onDone: onDone, cancelOnError: cancelOnError);
return Subscription(() {
_observable.close();

// Create a new instance to avoid errors
_observable = StreamController();
});
}
}

Expand Down

0 comments on commit 6f6bfbd

Please sign in to comment.