@ionic/portals-react-native 0.0.6 → 0.2.0-1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +11 -159
- package/ReactNativePortals.podspec +2 -1
- package/android/build.gradle +1 -1
- package/android/src/main/java/io/ionic/portals/reactnative/PortalView.kt +132 -0
- package/android/src/main/java/io/ionic/portals/reactnative/ReactNativeLiveUpdatesModule.kt +10 -28
- package/android/src/main/java/io/ionic/portals/reactnative/ReactNativePortalManager.kt +149 -0
- package/android/src/main/java/io/ionic/portals/reactnative/ReactNativePortalsModule.kt +76 -215
- package/android/src/main/java/io/ionic/portals/reactnative/ReactNativePortalsPackage.kt +0 -1
- package/android/src/main/java/io/ionic/portals/reactnative/ReactNativePortalsPubSub.kt +45 -0
- package/ios/LiveUpdate+Dict.swift +30 -0
- package/ios/LiveUpdateManager+Async.swift +69 -0
- package/ios/LiveUpdateManagerError+Dict.swift +19 -0
- package/ios/Podfile +2 -1
- package/ios/Podfile.lock +13 -12
- package/ios/Portal+Dict.swift +35 -0
- package/ios/PortalManager.m +10 -4
- package/ios/PortalView.m +2 -3
- package/ios/PortalView.swift +67 -0
- package/ios/PortalsConfig.swift +92 -0
- package/ios/PortalsPubSub.m +3 -3
- package/ios/PortalsPubSub.swift +47 -0
- package/ios/PortalsReactNative.swift +104 -0
- package/ios/ReactNativePortals.xcodeproj/project.pbxproj +32 -8
- package/lib/commonjs/index.js +108 -13
- package/lib/commonjs/index.js.map +1 -1
- package/lib/module/index.js +104 -12
- package/lib/module/index.js.map +1 -1
- package/lib/typescript/index.d.ts +106 -3
- package/package.json +3 -1
- package/src/index.ts +127 -13
- package/ios/LiveUpdatesManager.m +0 -16
- package/ios/ReactNativePortals.swift +0 -264
|
@@ -1,264 +0,0 @@
|
|
|
1
|
-
import Foundation
|
|
2
|
-
import IonicPortals
|
|
3
|
-
import IonicLiveUpdates
|
|
4
|
-
import React
|
|
5
|
-
import UIKit
|
|
6
|
-
import Capacitor
|
|
7
|
-
|
|
8
|
-
@objc(IONPortalManager)
|
|
9
|
-
public class PortalManager: NSObject {
|
|
10
|
-
private static var portals: [String: Portal] = [:]
|
|
11
|
-
|
|
12
|
-
public static func register(_ key: String) {
|
|
13
|
-
PortalsRegistrationManager.shared.register(key: key)
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
public static func add(_ portal: Portal) {
|
|
17
|
-
portals[portal.name] = portal
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
static func getPortal(named name: String) -> Portal? { portals[name] }
|
|
21
|
-
|
|
22
|
-
@objc func register(_ key: String) {
|
|
23
|
-
Self.register(key)
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
@objc func addPortal(_ portalDict: [String: Any]) {
|
|
27
|
-
guard let portal = Portal(portalDict) else { return }
|
|
28
|
-
Self.add(portal)
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
@objc static func requiresMainQueueSetup() -> Bool { true }
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
@objc(IONPortalPubSub)
|
|
35
|
-
class PortalsPubSub: RCTEventEmitter {
|
|
36
|
-
private let eventName = "PortalsSubscription"
|
|
37
|
-
|
|
38
|
-
override func supportedEvents() -> [String]! {
|
|
39
|
-
[eventName]
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
@objc func subscribe(_ topic: String, resolver: RCTPromiseResolveBlock, rejector: RCTPromiseRejectBlock) {
|
|
43
|
-
let subRef = IonicPortals.PortalsPubSub.subscribe(topic) { [weak self] result in
|
|
44
|
-
guard let self = self else { return }
|
|
45
|
-
self.sendEvent(
|
|
46
|
-
withName: self.eventName,
|
|
47
|
-
body: [
|
|
48
|
-
"subscriptionRef": result.subscriptionRef,
|
|
49
|
-
"topic": result.topic,
|
|
50
|
-
"data": result.data
|
|
51
|
-
]
|
|
52
|
-
)
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
resolver(subRef)
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
@objc func unsubscribe(_ topic: String, subscriptionRef: NSNumber) {
|
|
59
|
-
IonicPortals.PortalsPubSub.unsubscribe(from: topic, subscriptionRef: subscriptionRef.intValue)
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
@objc func publish(_ topic: String, data: Any) {
|
|
63
|
-
IONPortalsPubSub.publish(message: data, topic: topic)
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
override class func requiresMainQueueSetup() -> Bool { true }
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
@objc(IONPortalViewManager)
|
|
70
|
-
class PortalViewManager: RCTViewManager {
|
|
71
|
-
override class func requiresMainQueueSetup() -> Bool { true }
|
|
72
|
-
override func view() -> UIView! { PortalView() }
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
class PortalView: UIView {
|
|
76
|
-
private var webView: PortalUIView?
|
|
77
|
-
private var _initialContext: [String: JSValue]?
|
|
78
|
-
|
|
79
|
-
@objc var name: String? {
|
|
80
|
-
get {
|
|
81
|
-
guard let _portal = _portal else { return nil }
|
|
82
|
-
return _portal.name
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
set {
|
|
86
|
-
guard let portalName = newValue else { return }
|
|
87
|
-
_portal = PortalManager.getPortal(named: portalName)
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
@objc var initialContext: [String: Any]? {
|
|
92
|
-
get { _initialContext }
|
|
93
|
-
set {
|
|
94
|
-
_initialContext = JSTypes.coerceDictionaryToJSObject(newValue)
|
|
95
|
-
// If the Portal is already present then we need to set the initialContext so it's the `didSet` property observer is fired
|
|
96
|
-
_portal?.initialContext = _initialContext ?? [:]
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
private var _portal: Portal? {
|
|
101
|
-
didSet {
|
|
102
|
-
guard var portal = _portal else { return }
|
|
103
|
-
|
|
104
|
-
if let initialContext = _initialContext {
|
|
105
|
-
portal.initialContext = initialContext
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
DispatchQueue.main.async { [weak self] in
|
|
109
|
-
guard let self = self else { return }
|
|
110
|
-
self.webView?.removeFromSuperview()
|
|
111
|
-
let webView = PortalUIView(portal: portal)
|
|
112
|
-
webView.translatesAutoresizingMaskIntoConstraints = false
|
|
113
|
-
self.addSubview(webView)
|
|
114
|
-
NSLayoutConstraint.activate([
|
|
115
|
-
webView.topAnchor.constraint(equalTo: self.topAnchor),
|
|
116
|
-
webView.bottomAnchor.constraint(equalTo: self.bottomAnchor),
|
|
117
|
-
webView.leadingAnchor.constraint(equalTo: self.leadingAnchor),
|
|
118
|
-
webView.trailingAnchor.constraint(equalTo: self.trailingAnchor)
|
|
119
|
-
])
|
|
120
|
-
self.webView = webView
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
extension Portal {
|
|
127
|
-
init?(_ dict: [String: Any]) {
|
|
128
|
-
guard let name = dict["name"] as? String else { return nil }
|
|
129
|
-
self.init(
|
|
130
|
-
name: name,
|
|
131
|
-
startDir: dict["startDir"] as? String,
|
|
132
|
-
initialContext: JSTypes.coerceDictionaryToJSObject(dict["initialContext"] as? [String: Any]) ?? [:],
|
|
133
|
-
liveUpdateConfig: (dict["liveUpdate"] as? [String: Any]).flatMap(LiveUpdate.init)
|
|
134
|
-
)
|
|
135
|
-
}
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
extension LiveUpdate {
|
|
139
|
-
init?(_ dict: [String: Any]) {
|
|
140
|
-
guard let appId = dict["appId"] as? String,
|
|
141
|
-
let channel = dict["channel"] as? String,
|
|
142
|
-
let syncOnAdd = dict["syncOnAdd"] as? Bool
|
|
143
|
-
else { return nil }
|
|
144
|
-
|
|
145
|
-
self.init(appId: appId, channel: channel, syncOnAdd: syncOnAdd)
|
|
146
|
-
}
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
extension LiveUpdate {
|
|
150
|
-
var dict: [String: Any] {
|
|
151
|
-
return [
|
|
152
|
-
"appId": appId,
|
|
153
|
-
"channel": channel,
|
|
154
|
-
"syncOnAdd": syncOnAdd
|
|
155
|
-
]
|
|
156
|
-
}
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
extension LiveUpdateManager.Error {
|
|
160
|
-
var dict: [String: Any] {
|
|
161
|
-
return [
|
|
162
|
-
"appId": appId,
|
|
163
|
-
"failStep": failStep.rawValue.uppercased(),
|
|
164
|
-
"message": localizedDescription
|
|
165
|
-
]
|
|
166
|
-
}
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
private struct SyncResults {
|
|
170
|
-
var liveUpdates: [LiveUpdate]
|
|
171
|
-
var errors: [LiveUpdateManager.Error]
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
extension SyncResults {
|
|
175
|
-
var dict: [String: Any] {
|
|
176
|
-
return [
|
|
177
|
-
"liveUpdates": liveUpdates.map(\.dict),
|
|
178
|
-
"errors": errors.map(\.dict)
|
|
179
|
-
]
|
|
180
|
-
}
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
@objc(IONLiveUpdatesManager)
|
|
184
|
-
public class LiveUpdatesManager: NSObject {
|
|
185
|
-
private var lum = LiveUpdateManager.shared
|
|
186
|
-
|
|
187
|
-
@objc func addLiveUpdate(_ dict: [String: Any]) {
|
|
188
|
-
guard let liveUpdate = LiveUpdate(dict) else { return }
|
|
189
|
-
try? lum.add(liveUpdate)
|
|
190
|
-
}
|
|
191
|
-
|
|
192
|
-
@objc func syncOne(_ appId: String, resolver: @escaping RCTPromiseResolveBlock, rejector: @escaping RCTPromiseRejectBlock) {
|
|
193
|
-
lum.sync(appId: appId, isParallel: true) { result in
|
|
194
|
-
switch result {
|
|
195
|
-
case .success(let update):
|
|
196
|
-
resolver(update.dict)
|
|
197
|
-
case .failure(let error):
|
|
198
|
-
rejector(nil, nil, error)
|
|
199
|
-
}
|
|
200
|
-
}
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
@objc func syncSome(_ appIds: [String], resolver: @escaping RCTPromiseResolveBlock, rejector: RCTPromiseRejectBlock) {
|
|
204
|
-
Task {
|
|
205
|
-
let syncResult = await lum.syncSome(appIds)
|
|
206
|
-
resolver(syncResult.dict)
|
|
207
|
-
}
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
@objc func syncAll(_ resolver: @escaping RCTPromiseResolveBlock, rejector: RCTPromiseRejectBlock) {
|
|
211
|
-
Task {
|
|
212
|
-
let syncResult = await lum.syncAll()
|
|
213
|
-
resolver(syncResult.dict)
|
|
214
|
-
}
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
@objc static func requiresMainQueueSetup() -> Bool { true }
|
|
218
|
-
}
|
|
219
|
-
|
|
220
|
-
extension LiveUpdateManager {
|
|
221
|
-
fileprivate func syncSome(_ appIds: [String]) async -> SyncResults {
|
|
222
|
-
await _syncSome(appIds).syncResults
|
|
223
|
-
}
|
|
224
|
-
|
|
225
|
-
private func _syncSome(_ appIds: [String]) -> AsyncStream<Result<LiveUpdate, LiveUpdateManager.Error>> {
|
|
226
|
-
AsyncStream { continuation in
|
|
227
|
-
sync(appIds: appIds, isParallel: true) {
|
|
228
|
-
continuation.finish()
|
|
229
|
-
} appComplete: { result in
|
|
230
|
-
continuation.yield(result)
|
|
231
|
-
}
|
|
232
|
-
}
|
|
233
|
-
}
|
|
234
|
-
|
|
235
|
-
fileprivate func syncAll() async -> SyncResults {
|
|
236
|
-
await _syncAll().syncResults
|
|
237
|
-
}
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
private func _syncAll() -> AsyncStream<Result<LiveUpdate, LiveUpdateManager.Error>> {
|
|
241
|
-
AsyncStream { continuation in
|
|
242
|
-
sync(isParallel: true) {
|
|
243
|
-
continuation.finish()
|
|
244
|
-
} appComplete: { result in
|
|
245
|
-
continuation.yield(result)
|
|
246
|
-
}
|
|
247
|
-
}
|
|
248
|
-
}
|
|
249
|
-
}
|
|
250
|
-
|
|
251
|
-
extension AsyncStream where Element == Result<LiveUpdate, LiveUpdateManager.Error> {
|
|
252
|
-
fileprivate var syncResults: SyncResults {
|
|
253
|
-
get async {
|
|
254
|
-
await reduce(into: SyncResults(liveUpdates: [], errors: [])) { acc, next in
|
|
255
|
-
switch next {
|
|
256
|
-
case .success(let liveUpdate):
|
|
257
|
-
acc.liveUpdates.append(liveUpdate)
|
|
258
|
-
case .failure(let error):
|
|
259
|
-
acc.errors.append(error)
|
|
260
|
-
}
|
|
261
|
-
}
|
|
262
|
-
}
|
|
263
|
-
}
|
|
264
|
-
}
|