Skip to content

Commit 2cd0630

Browse files
committed
feat: Add SendBroadcast method and update BroadcastReceiver interface to include topic in message handling
1 parent 4a57820 commit 2cd0630

File tree

3 files changed

+29
-24
lines changed

3 files changed

+29
-24
lines changed

src/broadcast_center.cpp

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,15 @@ BroadcastCenter::~BroadcastCenter() {
1212
std::cout << "BroadcastCenter::~BroadcastCenter()" << std::endl;
1313
};
1414

15-
void BroadcastCenter::NotifyReceivers(
16-
const std::string& topic,
17-
std::function<void(BroadcastReceiver*)> callback) {
18-
for (const auto& receiver : receivers_) {
19-
callback(receiver);
20-
}
21-
}
22-
2315
BroadcastEventHandler::BroadcastEventHandler(
24-
std::function<void(const std::string& message)> onBroadcastReceivedCallback)
16+
std::function<void(const std::string& topic, const std::string& message)>
17+
onBroadcastReceivedCallback)
2518
: onBroadcastReceivedCallback_(std::move(onBroadcastReceivedCallback)) {}
2619

27-
void BroadcastEventHandler::OnBroadcastReceived(const std::string& message) {
20+
void BroadcastEventHandler::OnBroadcastReceived(const std::string& topic,
21+
const std::string& message) {
2822
if (onBroadcastReceivedCallback_) {
29-
onBroadcastReceivedCallback_(message);
23+
onBroadcastReceivedCallback_(topic, message);
3024
}
3125
}
3226

src/broadcast_center.h

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ namespace nativeapi {
1414
// want to listen for broadcasts.
1515
class BroadcastReceiver {
1616
public:
17-
virtual void OnBroadcastReceived(const std::string& message) = 0;
17+
virtual void OnBroadcastReceived(const std::string& topic,
18+
const std::string& message) = 0;
1819
};
1920

2021
// BroadcastCenter is a singleton that manages all broadcasts on the system.
@@ -23,17 +24,16 @@ class BroadcastCenter {
2324
BroadcastCenter();
2425
virtual ~BroadcastCenter();
2526

27+
// Send a broadcast message to all receivers of a given topic
28+
void SendBroadcast(const std::string& topic, const std::string& message);
29+
2630
// Register a receiver to the broadcast center
2731
void RegisterReceiver(const std::string& topic, BroadcastReceiver* receiver);
2832

2933
// Unregister a receiver from the broadcast center
3034
void UnregisterReceiver(const std::string& topic,
3135
BroadcastReceiver* receiver);
3236

33-
// Notify all receivers of a given topic
34-
void NotifyReceivers(const std::string& topic,
35-
std::function<void(BroadcastReceiver*)> callback);
36-
3737
private:
3838
std::vector<BroadcastReceiver*> receivers_;
3939
};
@@ -43,14 +43,17 @@ class BroadcastCenter {
4343
class BroadcastEventHandler : public BroadcastReceiver {
4444
public:
4545
// Constructor that takes callbacks for broadcast events
46-
BroadcastEventHandler(std::function<void(const std::string& message)>
47-
onBroadcastReceivedCallback);
46+
BroadcastEventHandler(
47+
std::function<void(const std::string& topic, const std::string& message)>
48+
onBroadcastReceivedCallback);
4849

4950
// Implementation of OnBroadcastReceive from BroadcastReceiver interface
50-
void OnBroadcastReceived(const std::string& message) override;
51+
void OnBroadcastReceived(const std::string& topic,
52+
const std::string& message) override;
5153

5254
private:
53-
std::function<void(const std::string&)> onBroadcastReceivedCallback_;
55+
std::function<void(const std::string& topic, const std::string& message)>
56+
onBroadcastReceivedCallback_;
5457
};
5558

5659
} // namespace nativeapi

src/broadcast_center_macos.mm

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,27 @@
1313
// Static map to store BroadcastCenter instances
1414
static std::map<BroadcastCenter*, std::string> g_broadcast_centers;
1515

16-
void BroadcastCenter::RegisterReceiver(const std::string& topic, BroadcastReceiver* receiver) {
17-
receivers_.push_back(receiver);
16+
void BroadcastCenter::SendBroadcast(const std::string& topic, const std::string& message) {
17+
NSDistributedNotificationCenter* center = [NSDistributedNotificationCenter defaultCenter];
18+
[center postNotificationName:[NSString stringWithUTF8String:topic.c_str()]
19+
object:nil
20+
userInfo:@{@"message" : [NSString stringWithUTF8String:message.c_str()]}
21+
deliverImmediately:YES];
22+
}
1823

24+
void BroadcastCenter::RegisterReceiver(const std::string& topic, BroadcastReceiver* receiver) {
1925
NSDistributedNotificationCenter* center = [NSDistributedNotificationCenter defaultCenter];
2026
[center addObserverForName:[NSString stringWithUTF8String:topic.c_str()]
2127
object:nil
2228
queue:[NSOperationQueue mainQueue]
2329
usingBlock:^(NSNotification* notification) {
2430
std::string message = [notification.userInfo[@"message"] UTF8String];
25-
receiver->OnBroadcastReceived(message);
31+
receiver->OnBroadcastReceived(topic, message);
2632
}];
2733
}
2834

29-
void BroadcastCenter::UnregisterReceiver(const std::string& topic, BroadcastReceiver* receiver) {}
35+
void BroadcastCenter::UnregisterReceiver(const std::string& topic, BroadcastReceiver* receiver) {
36+
// TODO: Implement
37+
}
3038

3139
} // namespace nativeapi

0 commit comments

Comments
 (0)