Skip to content

Commit

Permalink
Combine sendTabsMessage and sendRuntimeMessage
Browse files Browse the repository at this point in the history
  • Loading branch information
elliette committed Oct 27, 2023
1 parent fdd210c commit 39d08de
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 44 deletions.
4 changes: 1 addition & 3 deletions dwds/debug_extension_mv3/web/copier.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
library copier;

import 'dart:html';
import 'dart:js_util';

import 'package:js/js.dart';

Expand Down Expand Up @@ -35,8 +34,7 @@ void _handleRuntimeMessages(
messageHandler: _copyAppId,
);

final response = {'response': 'received'};
sendResponse(jsify(response));
sendResponse(defaultResponse);
}

void _copyAppId(String appId) {
Expand Down
92 changes: 51 additions & 41 deletions dwds/debug_extension_mv3/web/messaging.dart
Original file line number Diff line number Diff line change
Expand Up @@ -113,37 +113,19 @@ void interceptMessage<T>({
}
}

/// Send a message using the chrome.runtime.sendMessage API.
Future<bool> sendRuntimeMessage({
required MessageType type,
required String body,
required Script sender,
required Script recipient,
}) {
final message = Message(
to: recipient,
from: sender,
type: type,
body: body,
);
final completer = Completer<bool>();
chrome.runtime.sendMessage(
// id
null,
message.toJSON(),
// options
null,
allowInterop(([dynamic response]) {
final error = chrome.runtime.lastError;
if (error != null) {
debugError(
'Error sending $type to $recipient from $sender: ${error.message}',
);
}
completer.complete(error != null);
}),
);
return completer.future;
}
}) =>
_sendMessage(
type: type,
body: body,
sender: sender,
recipient: recipient,
);

/// Send a message using the chrome.tabs.sendMessage API.
Future<bool> sendTabsMessage({
Expand All @@ -152,27 +134,55 @@ Future<bool> sendTabsMessage({
required String body,
required Script sender,
required Script recipient,
}) =>
_sendMessage(
tabId: tabId,
type: type,
body: body,
sender: sender,
recipient: recipient,
);

Future<bool> _sendMessage({
required MessageType type,
required String body,
required Script sender,
required Script recipient,
int? tabId,
}) {
final message = Message(
to: recipient,
from: sender,
type: type,
body: body,
);
).toJSON();
final completer = Completer<bool>();
chrome.tabs.sendMessage(
tabId,
message.toJSON(),
null,
allowInterop(([dynamic response]) {
final error = chrome.runtime.lastError;
if (error != null) {
debugError(
'Error sending $type to $recipient from $sender: ${error.message}',
);
}
completer.complete(error != null);
}),
);
final responseHandler = ([dynamic response]) {
final error = chrome.runtime.lastError;
if (error != null) {
debugError(
'Error sending $type to $recipient from $sender: ${error.message}',
);
}
completer.complete(error != null);
};
if (tabId != null) {
chrome.tabs.sendMessage(
tabId,
message,
// options
null,
allowInterop(responseHandler),
);
} else {
chrome.runtime.sendMessage(
// id
null,
message,
// options
null,
allowInterop(responseHandler),
);
}
return completer.future;
}

0 comments on commit 39d08de

Please sign in to comment.