Skip to content

Commit

Permalink
feat: Allow multiple listeners.
Browse files Browse the repository at this point in the history
  • Loading branch information
llfbandit committed Aug 4, 2024
1 parent 5d3d0d2 commit bb717cc
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 3 deletions.
3 changes: 3 additions & 0 deletions app_links/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## 6.2.0
* feat: Allow multiple listeners.

## 6.1.4
* fix: Channel method names.

Expand Down
48 changes: 46 additions & 2 deletions app_links/lib/src/app_links.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ class AppLinks extends AppLinksPlatform {

AppLinks._();

StreamController<String>? _stringStreamController;
StreamController<Uri>? _uriStreamController;

@override
Future<Uri?> getInitialLink() {
return AppLinksPlatform.instance.getInitialLink();
Expand All @@ -33,11 +36,52 @@ class AppLinks extends AppLinksPlatform {

@override
Stream<String> get stringLinkStream {
return AppLinksPlatform.instance.stringLinkStream;
if (_stringStreamController == null) {
_stringStreamController = StreamController.broadcast();

_initController<String>(
_stringStreamController!,
AppLinksPlatform.instance.stringLinkStream,
onCancel: () => _stringStreamController = null,
);
}

return _stringStreamController!.stream;
}

@override
Stream<Uri> get uriLinkStream {
return AppLinksPlatform.instance.uriLinkStream;
if (_uriStreamController == null) {
_uriStreamController = StreamController.broadcast();

_initController<Uri>(
_uriStreamController!,
AppLinksPlatform.instance.uriLinkStream,
onCancel: () => _uriStreamController = null,
);
}

return _uriStreamController!.stream;
}

void _initController<T>(
StreamController<T> controller,
Stream<T> stream, {
required void Function() onCancel,
}) {
final subscription = stream.listen(
controller.add,
onError: controller.addError,
);

// Broadcast controller doesn't support pause/resume
//
// Forward cancel event when there's no more listeners
// and dispose controller
controller.onCancel = () async {
await subscription.cancel();
await controller.close();
onCancel();
};
}
}
2 changes: 1 addition & 1 deletion app_links/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: app_links
description: Android App Links, Deep Links, iOs Universal Links and Custom URL schemes handler for Flutter (desktop included).
version: 6.1.4
version: 6.2.0
homepage: https://github.com/llfbandit/app_links

environment:
Expand Down

0 comments on commit bb717cc

Please sign in to comment.