1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52//
// PortalsPubSub.swift
// ReactNativePortals
//
// Created by Steven Sherry on 10/5/22.
// Copyright ยฉ 2022 Ionic. All rights reserved.
//
import IonicPortals
import React
import Combine
@objc(IONPortalPubSub)
class PortalsPubSub: RCTEventEmitter {
private let eventPrefix = "PortalsSubscription:"
private var events: Set<String> = []
override func supportedEvents() -> [String] {
Array(events)
}
private let publishers = ConcurrentDictionary<String, AnyCancellable>(label: "io.ionic.rn.portalspubsub")
override func addListener(_ eventName: String) {
var topic = eventName
if topic.hasPrefix(eventPrefix) {
topic = String(eventName.suffix(from: eventPrefix.endIndex))
}
events.insert(eventName)
super.addListener(eventName)
if let _ = publishers[topic] { return }
publishers[topic] = IonicPortals.PortalsPubSub.subscribe(to: topic) { [weak self] result in
self?.sendEvent(
withName: eventName,
body: [
"topic": result.topic,
"data": result.data
]
)
}
}
@objc func publish(_ topic: String, data: Any, resolver: RCTPromiseResolveBlock, rejector: RCTPromiseRejectBlock) {
IONPortalsPubSub.publish(message: data, topic: topic)
resolver(())
}
override class func requiresMainQueueSetup() -> Bool { true }
}