Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: web based applications will now flush events before closing to …
…ensure events are sent (#129) **Requirements** - [x] I have added test coverage for new or changed functionality - [x] I have followed the repository's [pull request submission guidelines](../blob/main/CONTRIBUTING.md#submitting-pull-requests) - [x] I have validated my changes against all supported platform versions **Describe the solution you've provided** Created LDAppLifecycleListener and configured to use conditional imports. In the web case, uses an LDAppLifecycleListener that uses html document visibility. In the non-web case, uses the existing Flutter AppLifecycleListener. Note that the web based LDAppLifecycleListener only emits resume and hidden, no other events.
- Loading branch information
1 parent
f8244ab
commit c1e2828
Showing
6 changed files
with
95 additions
and
11 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
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
27 changes: 27 additions & 0 deletions
27
packages/flutter_client_sdk/lib/src/lifecycle/io_lifecycle_listener.dart
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 |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import 'dart:async'; | ||
|
||
import 'package:flutter/widgets.dart'; | ||
|
||
/// Lifecycle listener that uses the Flutter [AppLifecycleListener]. | ||
/// Unfortunately, the [AppLifecycleListener] does not support web very well at | ||
/// the moment, so the [LDAppLifecycleListener] was created. | ||
class LDAppLifecycleListener { | ||
late final StreamController<AppLifecycleState> _streamController; | ||
AppLifecycleListener? _underlyingListener; | ||
|
||
LDAppLifecycleListener() { | ||
_streamController = StreamController.broadcast(onListen: () { | ||
_underlyingListener = AppLifecycleListener( | ||
onStateChange: (state) => _streamController.add(state)); | ||
}, onCancel: () { | ||
_underlyingListener?.dispose(); | ||
_underlyingListener = null; | ||
}); | ||
} | ||
|
||
Stream<AppLifecycleState> get stream => _streamController.stream; | ||
|
||
void close() { | ||
_streamController.close(); | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
packages/flutter_client_sdk/lib/src/lifecycle/js_lifecycle_listener.dart
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 |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import 'dart:async'; | ||
import 'dart:html' as html; | ||
|
||
import 'package:flutter/widgets.dart'; | ||
|
||
/// Lifecycle listener that uses the underlying visibility of the html web | ||
/// document to emit events. | ||
class LDAppLifecycleListener { | ||
late final StreamController<AppLifecycleState> _streamController; | ||
|
||
LDAppLifecycleListener() { | ||
_streamController = StreamController.broadcast(); | ||
|
||
void listenerFunc(event) => | ||
_streamController.add(html.document.hidden == true | ||
? AppLifecycleState.hidden | ||
: AppLifecycleState.resumed); | ||
|
||
_streamController.onListen = () { | ||
html.document.addEventListener('visibilitychange', listenerFunc); | ||
}; | ||
|
||
_streamController.onCancel = () { | ||
html.document.removeEventListener('visibilitychange', listenerFunc); | ||
}; | ||
} | ||
|
||
Stream<AppLifecycleState> get stream => _streamController.stream; | ||
|
||
void close() { | ||
_streamController.close(); | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
packages/flutter_client_sdk/lib/src/lifecycle/stub_lifecycle_listener.dart
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 |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import 'package:flutter/widgets.dart'; | ||
|
||
class LDAppLifecycleListener { | ||
Stream<AppLifecycleState> get stream => | ||
throw Exception('Stub implementation'); | ||
|
||
void close() { | ||
throw Exception('Stub implementation'); | ||
} | ||
} |
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