feat(linux): Get the isolate implementation somewhat working
This commit is contained in:
parent
cefe90b93a
commit
01374a8eb9
@ -20,13 +20,13 @@ private fun wrapError(exception: Throwable): List<Any?> {
|
|||||||
return listOf(
|
return listOf(
|
||||||
exception.code,
|
exception.code,
|
||||||
exception.message,
|
exception.message,
|
||||||
exception.details,
|
exception.details
|
||||||
)
|
)
|
||||||
} else {
|
} else {
|
||||||
return listOf(
|
return listOf(
|
||||||
exception.javaClass.simpleName,
|
exception.javaClass.simpleName,
|
||||||
exception.toString(),
|
exception.toString(),
|
||||||
"Cause: " + exception.cause + ", Stacktrace: " + Log.getStackTraceString(exception),
|
"Cause: " + exception.cause + ", Stacktrace: " + Log.getStackTraceString(exception)
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -40,14 +40,13 @@ private fun wrapError(exception: Throwable): List<Any?> {
|
|||||||
class FlutterError (
|
class FlutterError (
|
||||||
val code: String,
|
val code: String,
|
||||||
override val message: String? = null,
|
override val message: String? = null,
|
||||||
val details: Any? = null,
|
val details: Any? = null
|
||||||
) : Throwable()
|
) : Throwable()
|
||||||
|
|
||||||
enum class NotificationIcon(val raw: Int) {
|
enum class NotificationIcon(val raw: Int) {
|
||||||
WARNING(0),
|
WARNING(0),
|
||||||
ERROR(1),
|
ERROR(1),
|
||||||
NONE(2),
|
NONE(2);
|
||||||
;
|
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
fun ofRaw(raw: Int): NotificationIcon? {
|
fun ofRaw(raw: Int): NotificationIcon? {
|
||||||
@ -59,8 +58,7 @@ enum class NotificationIcon(val raw: Int) {
|
|||||||
enum class NotificationEventType(val raw: Int) {
|
enum class NotificationEventType(val raw: Int) {
|
||||||
MARKASREAD(0),
|
MARKASREAD(0),
|
||||||
REPLY(1),
|
REPLY(1),
|
||||||
OPEN(2),
|
OPEN(2);
|
||||||
;
|
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
fun ofRaw(raw: Int): NotificationEventType? {
|
fun ofRaw(raw: Int): NotificationEventType? {
|
||||||
@ -72,8 +70,7 @@ enum class NotificationEventType(val raw: Int) {
|
|||||||
enum class NotificationChannelImportance(val raw: Int) {
|
enum class NotificationChannelImportance(val raw: Int) {
|
||||||
MIN(0),
|
MIN(0),
|
||||||
HIGH(1),
|
HIGH(1),
|
||||||
DEFAULT(2),
|
DEFAULT(2);
|
||||||
;
|
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
fun ofRaw(raw: Int): NotificationChannelImportance? {
|
fun ofRaw(raw: Int): NotificationChannelImportance? {
|
||||||
@ -88,7 +85,7 @@ data class NotificationMessageContent(
|
|||||||
val body: String? = null,
|
val body: String? = null,
|
||||||
/** The path and mime type of the media to show. */
|
/** The path and mime type of the media to show. */
|
||||||
val mime: String? = null,
|
val mime: String? = null,
|
||||||
val path: String? = null,
|
val path: String? = null
|
||||||
|
|
||||||
) {
|
) {
|
||||||
companion object {
|
companion object {
|
||||||
@ -122,7 +119,7 @@ data class NotificationMessage(
|
|||||||
/** Milliseconds since epoch. */
|
/** Milliseconds since epoch. */
|
||||||
val timestamp: Long,
|
val timestamp: Long,
|
||||||
/** The path to the avatar to use */
|
/** The path to the avatar to use */
|
||||||
val avatarPath: String? = null,
|
val avatarPath: String? = null
|
||||||
|
|
||||||
) {
|
) {
|
||||||
companion object {
|
companion object {
|
||||||
@ -166,7 +163,7 @@ data class MessagingNotification(
|
|||||||
/** The id for notification grouping. */
|
/** The id for notification grouping. */
|
||||||
val groupId: String? = null,
|
val groupId: String? = null,
|
||||||
/** Additional data to include. */
|
/** Additional data to include. */
|
||||||
val extra: Map<String?, String?>? = null,
|
val extra: Map<String?, String?>? = null
|
||||||
|
|
||||||
) {
|
) {
|
||||||
companion object {
|
companion object {
|
||||||
@ -210,7 +207,7 @@ data class RegularNotification(
|
|||||||
/** The id of the notification. */
|
/** The id of the notification. */
|
||||||
val id: Long,
|
val id: Long,
|
||||||
/** The icon to use. */
|
/** The icon to use. */
|
||||||
val icon: NotificationIcon,
|
val icon: NotificationIcon
|
||||||
|
|
||||||
) {
|
) {
|
||||||
companion object {
|
companion object {
|
||||||
@ -252,7 +249,7 @@ data class NotificationEvent(
|
|||||||
*/
|
*/
|
||||||
val payload: String? = null,
|
val payload: String? = null,
|
||||||
/** Extra data. Only set when type == NotificationType.reply. */
|
/** Extra data. Only set when type == NotificationType.reply. */
|
||||||
val extra: Map<String?, String?>? = null,
|
val extra: Map<String?, String?>? = null
|
||||||
|
|
||||||
) {
|
) {
|
||||||
companion object {
|
companion object {
|
||||||
@ -284,7 +281,7 @@ data class NotificationI18nData(
|
|||||||
/** The content of the "mark as read" button. */
|
/** The content of the "mark as read" button. */
|
||||||
val markAsRead: String,
|
val markAsRead: String,
|
||||||
/** The text to show when *you* reply. */
|
/** The text to show when *you* reply. */
|
||||||
val you: String,
|
val you: String
|
||||||
|
|
||||||
) {
|
) {
|
||||||
companion object {
|
companion object {
|
||||||
@ -308,7 +305,7 @@ data class NotificationI18nData(
|
|||||||
/** Generated class from Pigeon that represents data sent in messages. */
|
/** Generated class from Pigeon that represents data sent in messages. */
|
||||||
data class NotificationGroup (
|
data class NotificationGroup (
|
||||||
val id: String,
|
val id: String,
|
||||||
val description: String,
|
val description: String
|
||||||
|
|
||||||
) {
|
) {
|
||||||
companion object {
|
companion object {
|
||||||
@ -336,7 +333,7 @@ data class NotificationChannel(
|
|||||||
val showBadge: Boolean,
|
val showBadge: Boolean,
|
||||||
val groupId: String? = null,
|
val groupId: String? = null,
|
||||||
val vibration: Boolean,
|
val vibration: Boolean,
|
||||||
val enableLights: Boolean,
|
val enableLights: Boolean
|
||||||
|
|
||||||
) {
|
) {
|
||||||
companion object {
|
companion object {
|
||||||
@ -366,7 +363,6 @@ data class NotificationChannel(
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Suppress("UNCHECKED_CAST")
|
@Suppress("UNCHECKED_CAST")
|
||||||
private object MoxxyNotificationsApiCodec : StandardMessageCodec() {
|
private object MoxxyNotificationsApiCodec : StandardMessageCodec() {
|
||||||
override fun readValueOfType(type: Byte, buffer: ByteBuffer): Any? {
|
override fun readValueOfType(type: Byte, buffer: ByteBuffer): Any? {
|
||||||
@ -472,7 +468,6 @@ interface MoxxyNotificationsApi {
|
|||||||
val codec: MessageCodec<Any?> by lazy {
|
val codec: MessageCodec<Any?> by lazy {
|
||||||
MoxxyNotificationsApiCodec
|
MoxxyNotificationsApiCodec
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Sets up an instance of `MoxxyNotificationsApi` to handle messages through the `binaryMessenger`. */
|
/** Sets up an instance of `MoxxyNotificationsApi` to handle messages through the `binaryMessenger`. */
|
||||||
@Suppress("UNCHECKED_CAST")
|
@Suppress("UNCHECKED_CAST")
|
||||||
fun setUp(binaryMessenger: BinaryMessenger, api: MoxxyNotificationsApi?) {
|
fun setUp(binaryMessenger: BinaryMessenger, api: MoxxyNotificationsApi?) {
|
||||||
|
@ -20,10 +20,10 @@ class MoxxyBackgroundServiceApi {
|
|||||||
|
|
||||||
Future<String> getExtraData() async {
|
Future<String> getExtraData() async {
|
||||||
final BasicMessageChannel<Object?> channel = BasicMessageChannel<Object?>(
|
final BasicMessageChannel<Object?> channel = BasicMessageChannel<Object?>(
|
||||||
'dev.flutter.pigeon.moxxy_native.MoxxyBackgroundServiceApi.getExtraData', codec,
|
'dev.flutter.pigeon.moxxy_native.MoxxyBackgroundServiceApi.getExtraData',
|
||||||
|
codec,
|
||||||
binaryMessenger: _binaryMessenger);
|
binaryMessenger: _binaryMessenger);
|
||||||
final List<Object?>? replyList =
|
final List<Object?>? replyList = await channel.send(null) as List<Object?>?;
|
||||||
await channel.send(null) as List<Object?>?;
|
|
||||||
if (replyList == null) {
|
if (replyList == null) {
|
||||||
throw PlatformException(
|
throw PlatformException(
|
||||||
code: 'channel-error',
|
code: 'channel-error',
|
||||||
@ -47,7 +47,8 @@ class MoxxyBackgroundServiceApi {
|
|||||||
|
|
||||||
Future<void> setNotificationBody(String arg_body) async {
|
Future<void> setNotificationBody(String arg_body) async {
|
||||||
final BasicMessageChannel<Object?> channel = BasicMessageChannel<Object?>(
|
final BasicMessageChannel<Object?> channel = BasicMessageChannel<Object?>(
|
||||||
'dev.flutter.pigeon.moxxy_native.MoxxyBackgroundServiceApi.setNotificationBody', codec,
|
'dev.flutter.pigeon.moxxy_native.MoxxyBackgroundServiceApi.setNotificationBody',
|
||||||
|
codec,
|
||||||
binaryMessenger: _binaryMessenger);
|
binaryMessenger: _binaryMessenger);
|
||||||
final List<Object?>? replyList =
|
final List<Object?>? replyList =
|
||||||
await channel.send(<Object?>[arg_body]) as List<Object?>?;
|
await channel.send(<Object?>[arg_body]) as List<Object?>?;
|
||||||
@ -69,7 +70,8 @@ class MoxxyBackgroundServiceApi {
|
|||||||
|
|
||||||
Future<void> sendData(String arg_data) async {
|
Future<void> sendData(String arg_data) async {
|
||||||
final BasicMessageChannel<Object?> channel = BasicMessageChannel<Object?>(
|
final BasicMessageChannel<Object?> channel = BasicMessageChannel<Object?>(
|
||||||
'dev.flutter.pigeon.moxxy_native.MoxxyBackgroundServiceApi.sendData', codec,
|
'dev.flutter.pigeon.moxxy_native.MoxxyBackgroundServiceApi.sendData',
|
||||||
|
codec,
|
||||||
binaryMessenger: _binaryMessenger);
|
binaryMessenger: _binaryMessenger);
|
||||||
final List<Object?>? replyList =
|
final List<Object?>? replyList =
|
||||||
await channel.send(<Object?>[arg_data]) as List<Object?>?;
|
await channel.send(<Object?>[arg_data]) as List<Object?>?;
|
||||||
@ -93,8 +95,7 @@ class MoxxyBackgroundServiceApi {
|
|||||||
final BasicMessageChannel<Object?> channel = BasicMessageChannel<Object?>(
|
final BasicMessageChannel<Object?> channel = BasicMessageChannel<Object?>(
|
||||||
'dev.flutter.pigeon.moxxy_native.MoxxyBackgroundServiceApi.stop', codec,
|
'dev.flutter.pigeon.moxxy_native.MoxxyBackgroundServiceApi.stop', codec,
|
||||||
binaryMessenger: _binaryMessenger);
|
binaryMessenger: _binaryMessenger);
|
||||||
final List<Object?>? replyList =
|
final List<Object?>? replyList = await channel.send(null) as List<Object?>?;
|
||||||
await channel.send(null) as List<Object?>?;
|
|
||||||
if (replyList == null) {
|
if (replyList == null) {
|
||||||
throw PlatformException(
|
throw PlatformException(
|
||||||
code: 'channel-error',
|
code: 'channel-error',
|
||||||
|
53
lib/src/service/background/isolate.dart
Normal file
53
lib/src/service/background/isolate.dart
Normal file
@ -0,0 +1,53 @@
|
|||||||
|
import 'dart:convert';
|
||||||
|
import 'dart:isolate';
|
||||||
|
import 'dart:ui';
|
||||||
|
import 'package:logging/logging.dart';
|
||||||
|
import 'package:moxlib/moxlib.dart';
|
||||||
|
import 'package:moxxy_native/src/service/background/base.dart';
|
||||||
|
import 'package:moxxy_native/src/service/config.dart';
|
||||||
|
import 'package:moxxy_native/src/service/datasender/types.dart';
|
||||||
|
import 'package:uuid/uuid.dart';
|
||||||
|
|
||||||
|
class IsolateBackgroundService extends BackgroundService {
|
||||||
|
IsolateBackgroundService(this._sendPort);
|
||||||
|
final SendPort _sendPort;
|
||||||
|
final ReceivePort receivePort = ReceivePort();
|
||||||
|
|
||||||
|
/// A logger.
|
||||||
|
final Logger _log = Logger('IsolateBackgroundService');
|
||||||
|
|
||||||
|
@override
|
||||||
|
Future<void> send(BackgroundEvent event, {String? id}) async {
|
||||||
|
final data = DataWrapper(
|
||||||
|
id ?? const Uuid().v4(),
|
||||||
|
event,
|
||||||
|
);
|
||||||
|
|
||||||
|
_sendPort.send(jsonEncode(data.toJson()));
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
Future<void> init(
|
||||||
|
ServiceConfig config,
|
||||||
|
) async {
|
||||||
|
// Ensure that the Dart executor is ready to use plugins
|
||||||
|
// NOTE: We're not allowed to use this here. Maybe reusing the RootIsolateToken
|
||||||
|
// (See IsolateForegroundService) helps?
|
||||||
|
// WidgetsFlutterBinding.ensureInitialized();
|
||||||
|
DartPluginRegistrant.ensureInitialized();
|
||||||
|
|
||||||
|
// Register the channel for Foreground -> Service communication
|
||||||
|
receivePort.listen((data) async {
|
||||||
|
// TODO(Unknown): Maybe do something smarter like pigeon and use Lists instead of Maps
|
||||||
|
await config
|
||||||
|
.handleData(jsonDecode(data! as String) as Map<String, dynamic>);
|
||||||
|
});
|
||||||
|
|
||||||
|
// Start execution
|
||||||
|
_log.finest('Setup complete. Calling main entrypoint...');
|
||||||
|
await config.entrypoint(config.initialLocale);
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
void setNotificationBody(String body) {}
|
||||||
|
}
|
15
lib/src/service/datasender/isolate.dart
Normal file
15
lib/src/service/datasender/isolate.dart
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
import 'dart:convert';
|
||||||
|
import 'dart:isolate';
|
||||||
|
import 'package:moxlib/moxlib.dart';
|
||||||
|
import 'package:moxxy_native/src/service/datasender/types.dart';
|
||||||
|
|
||||||
|
class IsolateForegroundServiceDataSender
|
||||||
|
extends AwaitableDataSender<BackgroundCommand, BackgroundEvent> {
|
||||||
|
IsolateForegroundServiceDataSender(this._port);
|
||||||
|
final SendPort _port;
|
||||||
|
|
||||||
|
@override
|
||||||
|
Future<void> sendDataImpl(DataWrapper<JsonImplementation> data) async {
|
||||||
|
_port.send(jsonEncode(data.toJson()));
|
||||||
|
}
|
||||||
|
}
|
@ -1,8 +1,4 @@
|
|||||||
import 'dart:io';
|
|
||||||
import 'package:moxlib/moxlib.dart';
|
import 'package:moxlib/moxlib.dart';
|
||||||
import 'package:moxxy_native/pigeon/service.g.dart';
|
|
||||||
import 'package:moxxy_native/src/service/datasender/pigeon.dart';
|
|
||||||
import 'package:moxxy_native/src/service/exceptions.dart';
|
|
||||||
|
|
||||||
typedef ForegroundServiceDataSender
|
typedef ForegroundServiceDataSender
|
||||||
= AwaitableDataSender<BackgroundCommand, BackgroundEvent>;
|
= AwaitableDataSender<BackgroundCommand, BackgroundEvent>;
|
||||||
@ -10,11 +6,3 @@ typedef ForegroundServiceDataSender
|
|||||||
abstract class BackgroundCommand implements JsonImplementation {}
|
abstract class BackgroundCommand implements JsonImplementation {}
|
||||||
|
|
||||||
abstract class BackgroundEvent implements JsonImplementation {}
|
abstract class BackgroundEvent implements JsonImplementation {}
|
||||||
|
|
||||||
ForegroundServiceDataSender getForegroundDataSender(MoxxyServiceApi api) {
|
|
||||||
if (Platform.isAndroid) {
|
|
||||||
return PigeonForegroundServiceDataSender(api);
|
|
||||||
} else {
|
|
||||||
throw UnsupportedPlatformException();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
28
lib/src/service/entrypoints/base.dart
Normal file
28
lib/src/service/entrypoints/base.dart
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
import 'dart:io';
|
||||||
|
import 'package:moxxy_native/src/service/config.dart';
|
||||||
|
import 'package:moxxy_native/src/service/entrypoints/isolate.dart';
|
||||||
|
import 'package:moxxy_native/src/service/entrypoints/pigeon.dart';
|
||||||
|
import 'package:moxxy_native/src/service/exceptions.dart';
|
||||||
|
|
||||||
|
typedef PlatformEntrypointCallback = Future<void> Function(dynamic);
|
||||||
|
|
||||||
|
ServiceConfig getServiceConfig(
|
||||||
|
HandleEventCallback srvHandleData,
|
||||||
|
HandleEventCallback uiHandleData,
|
||||||
|
String initialLocale,
|
||||||
|
) {
|
||||||
|
PlatformEntrypointCallback entrypoint;
|
||||||
|
if (Platform.isAndroid) {
|
||||||
|
entrypoint = pigeonEntrypoint;
|
||||||
|
} else if (Platform.isLinux || Platform.isWindows || Platform.isMacOS) {
|
||||||
|
entrypoint = isolateEntrypoint;
|
||||||
|
} else {
|
||||||
|
throw UnsupportedPlatformException();
|
||||||
|
}
|
||||||
|
|
||||||
|
return ServiceConfig(
|
||||||
|
entrypoint,
|
||||||
|
srvHandleData,
|
||||||
|
initialLocale,
|
||||||
|
);
|
||||||
|
}
|
30
lib/src/service/entrypoints/isolate.dart
Normal file
30
lib/src/service/entrypoints/isolate.dart
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
import 'dart:isolate';
|
||||||
|
import 'package:flutter/services.dart';
|
||||||
|
import 'package:get_it/get_it.dart';
|
||||||
|
import 'package:moxxy_native/src/service/background/base.dart';
|
||||||
|
import 'package:moxxy_native/src/service/background/isolate.dart';
|
||||||
|
import 'package:moxxy_native/src/service/config.dart';
|
||||||
|
|
||||||
|
@pragma('vm:entry-point')
|
||||||
|
Future<void> isolateEntrypoint(dynamic parameters) async {
|
||||||
|
parameters as List<dynamic>;
|
||||||
|
|
||||||
|
final sendPort = parameters[0] as SendPort;
|
||||||
|
final config = ServiceConfig.fromString(parameters[1] as String);
|
||||||
|
|
||||||
|
// This allows us to use the root isolate's method channels.
|
||||||
|
// See https://medium.com/flutter/introducing-background-isolate-channels-7a299609cad8
|
||||||
|
BackgroundIsolateBinaryMessenger.ensureInitialized(
|
||||||
|
parameters[2] as RootIsolateToken,
|
||||||
|
);
|
||||||
|
|
||||||
|
// Set up the background service
|
||||||
|
final srv = IsolateBackgroundService(sendPort);
|
||||||
|
GetIt.I.registerSingleton<BackgroundService>(srv);
|
||||||
|
|
||||||
|
// Reply back with the new send port
|
||||||
|
sendPort.send(srv.receivePort.sendPort);
|
||||||
|
|
||||||
|
// Run the entrypoint
|
||||||
|
await srv.init(config);
|
||||||
|
}
|
@ -8,7 +8,7 @@ import 'package:moxxy_native/src/service/config.dart';
|
|||||||
/// An entrypoint that should be used when the service runs
|
/// An entrypoint that should be used when the service runs
|
||||||
/// in a new Flutter Engine.
|
/// in a new Flutter Engine.
|
||||||
@pragma('vm:entry-point')
|
@pragma('vm:entry-point')
|
||||||
Future<void> pigeonEntrypoint() async {
|
Future<void> pigeonEntrypoint(dynamic _) async {
|
||||||
// ignore: avoid_print
|
// ignore: avoid_print
|
||||||
print('androidEntrypoint: Called on new FlutterEngine');
|
print('androidEntrypoint: Called on new FlutterEngine');
|
||||||
|
|
||||||
|
@ -3,6 +3,7 @@ import 'package:moxlib/moxlib.dart';
|
|||||||
import 'package:moxxy_native/src/service/config.dart';
|
import 'package:moxxy_native/src/service/config.dart';
|
||||||
import 'package:moxxy_native/src/service/datasender/types.dart';
|
import 'package:moxxy_native/src/service/datasender/types.dart';
|
||||||
import 'package:moxxy_native/src/service/exceptions.dart';
|
import 'package:moxxy_native/src/service/exceptions.dart';
|
||||||
|
import 'package:moxxy_native/src/service/foreground/isolate.dart';
|
||||||
import 'package:moxxy_native/src/service/foreground/pigeon.dart';
|
import 'package:moxxy_native/src/service/foreground/pigeon.dart';
|
||||||
|
|
||||||
/// Wrapper API that is only available to the UI isolate.
|
/// Wrapper API that is only available to the UI isolate.
|
||||||
@ -40,6 +41,8 @@ ForegroundService getForegroundService() {
|
|||||||
if (_service == null) {
|
if (_service == null) {
|
||||||
if (Platform.isAndroid) {
|
if (Platform.isAndroid) {
|
||||||
_service = PigeonForegroundService();
|
_service = PigeonForegroundService();
|
||||||
|
} else if (Platform.isLinux || Platform.isWindows || Platform.isMacOS) {
|
||||||
|
_service = IsolateForegroundService();
|
||||||
} else {
|
} else {
|
||||||
throw UnsupportedPlatformException();
|
throw UnsupportedPlatformException();
|
||||||
}
|
}
|
||||||
|
98
lib/src/service/foreground/isolate.dart
Normal file
98
lib/src/service/foreground/isolate.dart
Normal file
@ -0,0 +1,98 @@
|
|||||||
|
import 'dart:async';
|
||||||
|
import 'dart:convert';
|
||||||
|
import 'dart:isolate';
|
||||||
|
import 'dart:ui';
|
||||||
|
import 'package:logging/logging.dart';
|
||||||
|
import 'package:moxxy_native/src/service/config.dart';
|
||||||
|
import 'package:moxxy_native/src/service/datasender/isolate.dart';
|
||||||
|
import 'package:moxxy_native/src/service/datasender/types.dart';
|
||||||
|
import 'package:moxxy_native/src/service/entrypoints/isolate.dart';
|
||||||
|
import 'package:moxxy_native/src/service/foreground/base.dart';
|
||||||
|
|
||||||
|
class IsolateForegroundService extends ForegroundService {
|
||||||
|
/// The port on which we receive data from the isolate.
|
||||||
|
final ReceivePort _receivePort = ReceivePort();
|
||||||
|
|
||||||
|
/// The port on which we send data to the isolate.
|
||||||
|
late final SendPort _sendPort;
|
||||||
|
|
||||||
|
/// A completer that indicates when _sendPort has been set.
|
||||||
|
/// For more notes, see the comment in [start].
|
||||||
|
Completer<void>? _sendPortCompleter = Completer<void>();
|
||||||
|
|
||||||
|
/// The data sender backing this class.
|
||||||
|
late final IsolateForegroundServiceDataSender _dataSender;
|
||||||
|
|
||||||
|
/// A logger.
|
||||||
|
final Logger _log = Logger('IsolateForegroundService');
|
||||||
|
|
||||||
|
@override
|
||||||
|
Future<void> attach(
|
||||||
|
HandleEventCallback handleData,
|
||||||
|
) async {
|
||||||
|
_receivePort.asBroadcastStream().listen((data) async {
|
||||||
|
if (data is SendPort) {
|
||||||
|
// Set the send port.
|
||||||
|
_sendPort = data;
|
||||||
|
|
||||||
|
// Resolve the waiting future.
|
||||||
|
assert(
|
||||||
|
_sendPortCompleter != null,
|
||||||
|
'_sendPort should only be received once!',
|
||||||
|
);
|
||||||
|
_sendPortCompleter?.complete();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
await handleData(
|
||||||
|
jsonDecode(data! as String) as Map<String, dynamic>,
|
||||||
|
);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
Future<void> start(
|
||||||
|
ServiceConfig config,
|
||||||
|
HandleEventCallback uiHandleData,
|
||||||
|
) async {
|
||||||
|
// Listen for events
|
||||||
|
await attach(uiHandleData);
|
||||||
|
|
||||||
|
await Isolate.spawn(
|
||||||
|
isolateEntrypoint,
|
||||||
|
[
|
||||||
|
_receivePort.sendPort,
|
||||||
|
config.toString(),
|
||||||
|
RootIsolateToken.instance!,
|
||||||
|
],
|
||||||
|
);
|
||||||
|
|
||||||
|
// Wait for [_sendPort] to get set.
|
||||||
|
// The issue is that [_receivePort] provides a stream that only one listener can listen to.
|
||||||
|
// This means that we cannot do `await _receivePort.first`. To work around this, we just cram
|
||||||
|
// an approximation of `_receivePort.first` into the actual listener.
|
||||||
|
await _sendPortCompleter!.future;
|
||||||
|
_sendPortCompleter = null;
|
||||||
|
|
||||||
|
// Create the data sender
|
||||||
|
_dataSender = IsolateForegroundServiceDataSender(_sendPort);
|
||||||
|
_log.finest('Background service started...');
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
Future<bool> isRunning() async => false;
|
||||||
|
|
||||||
|
@override
|
||||||
|
ForegroundServiceDataSender getDataSender() => _dataSender;
|
||||||
|
|
||||||
|
@override
|
||||||
|
Future<BackgroundEvent?> send(
|
||||||
|
BackgroundCommand command, {
|
||||||
|
bool awaitable = true,
|
||||||
|
}) {
|
||||||
|
return _dataSender.sendData(
|
||||||
|
command,
|
||||||
|
awaitable: awaitable,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user