Skip to content

Commit

Permalink
fix: isolate not working in web
Browse files Browse the repository at this point in the history
  • Loading branch information
chopper985 committed Sep 21, 2024
1 parent d082df7 commit 06f53a6
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 28 deletions.
15 changes: 4 additions & 11 deletions lib/core/api/chat/datasources/chat_remote_datasource.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import 'dart:isolate';

import 'package:dio/dio.dart';
import 'package:flutter/foundation.dart';
import 'package:injectable/injectable.dart';

import 'package:waterbus_sdk/constants/api_enpoints.dart';
Expand Down Expand Up @@ -41,29 +40,23 @@ class ChatRemoteDataSourceImpl extends ChatRemoteDataSource {
);

if ([StatusCode.ok, StatusCode.created].contains(response.statusCode)) {
final ReceivePort receivePort = ReceivePort();

final Map<String, dynamic> message = {
"conversations": (response.data as List)
.map((meeting) => Meeting.fromMap(meeting))
.toList(),
"sendPort": receivePort.sendPort,
"key": WaterbusSdk.privateMessageKey,
};

await Isolate.spawn(_handleDecryptLastMessage, message);

return await receivePort.first;
return await compute(_handleDecryptLastMessage, message);
}

return [];
}

static Future<void> _handleDecryptLastMessage(
static Future<List<Meeting>> _handleDecryptLastMessage(
Map<String, dynamic> map,
) async {
final List<Meeting> conversations = map['conversations'];
final SendPort sendPort = map['sendPort'];
final String key = map['key'];
final List<Meeting> conversationsDecrypt = [];
for (final Meeting conversation in conversations) {
Expand All @@ -81,7 +74,7 @@ class ChatRemoteDataSourceImpl extends ChatRemoteDataSource {
);
}

Isolate.exit(sendPort, conversationsDecrypt);
return conversationsDecrypt;
}

@override
Expand Down
26 changes: 9 additions & 17 deletions lib/core/api/messages/datasources/message_remote_datasource.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import 'dart:isolate';

import 'package:dio/dio.dart';
import 'package:flutter/foundation.dart';
import 'package:injectable/injectable.dart';

import 'package:waterbus_sdk/constants/api_enpoints.dart';
Expand Down Expand Up @@ -51,26 +50,19 @@ class MessageRemoteDataSourceImpl extends MessageRemoteDataSource {
.map((message) => MessageModel.fromMap(message))
.toList();

final ReceivePort receivePort = ReceivePort();

await Isolate.spawn(
_handleDecryptMessages,
{
"messages": messages,
"sendPort": receivePort.sendPort,
"key": WaterbusSdk.privateMessageKey,
},
);

return await receivePort.first;
return await compute(_handleDecryptMessages, {
"messages": messages,
"key": WaterbusSdk.privateMessageKey,
});
}

return [];
}

static Future<void> _handleDecryptMessages(Map<String, dynamic> map) async {
static Future<List<MessageModel>> _handleDecryptMessages(
Map<String, dynamic> map,
) async {
final List<MessageModel> messages = map['messages'];
final SendPort sendPort = map['sendPort'];
final String key = map['key'];

final List<MessageModel> messagesDecrypt = [];
Expand All @@ -81,7 +73,7 @@ class MessageRemoteDataSourceImpl extends MessageRemoteDataSource {
messagesDecrypt.add(messageModel.copyWith(data: data));
}

Isolate.exit(sendPort, messagesDecrypt);
return messagesDecrypt;
}

@override
Expand Down

0 comments on commit 06f53a6

Please sign in to comment.