Skip to content

Commit

Permalink
Merge pull request #14 from waterbustech/fix/isolate-not-working-in-web
Browse files Browse the repository at this point in the history
Fix: Isolate not working in web
  • Loading branch information
lambiengcode authored Sep 21, 2024
2 parents d082df7 + a3d305c commit 08cb7f0
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 26 deletions.
14 changes: 4 additions & 10 deletions lib/core/api/chat/datasources/chat_remote_datasource.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import 'dart:isolate';
import 'package:flutter/foundation.dart';

import 'package:dio/dio.dart';
import 'package:injectable/injectable.dart';
Expand Down Expand Up @@ -41,29 +41,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 +75,7 @@ class ChatRemoteDataSourceImpl extends ChatRemoteDataSource {
);
}

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

@override
Expand Down
25 changes: 9 additions & 16 deletions lib/core/api/messages/datasources/message_remote_datasource.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import 'dart:isolate';
import 'package:flutter/foundation.dart';

import 'package:dio/dio.dart';
import 'package:injectable/injectable.dart';
Expand Down Expand Up @@ -51,26 +51,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 +74,7 @@ class MessageRemoteDataSourceImpl extends MessageRemoteDataSource {
messagesDecrypt.add(messageModel.copyWith(data: data));
}

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

@override
Expand Down

0 comments on commit 08cb7f0

Please sign in to comment.