Skip to content

Commit

Permalink
Fix RCTCallableJSModules is not set bug
Browse files Browse the repository at this point in the history
  • Loading branch information
frontegg-david committed Aug 27, 2024
1 parent 5d93ead commit ff0d285
Showing 1 changed file with 38 additions and 19 deletions.
57 changes: 38 additions & 19 deletions ios/FronteggRN.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import Combine
class FronteggRN: RCTEventEmitter {

public let fronteggApp = FronteggApp.shared
var hasListeners: Bool = false
var pendingObservingState:Bool = false
var cancellables = Set<AnyCancellable>()

override func constantsToExport() -> [AnyHashable : Any]! {
Expand All @@ -17,7 +19,17 @@ class FronteggRN: RCTEventEmitter {
"bundleId": Bundle.main.bundleIdentifier as Any
]
}

override func startObserving() {
self.hasListeners = true
if(self.pendingObservingState){
self.pendingObservingState = false
self.sendEventToJS()
}
}

override func stopObserving() {
self.hasListeners = false
}
@objc
func subscribe() -> [AnyHashable : Any]! {

Expand All @@ -38,31 +50,38 @@ class FronteggRN: RCTEventEmitter {

anyChange.sink(receiveValue: { () in
DispatchQueue.global(qos: .userInteractive).asyncAfter(deadline: .now() + 0.1) {

let auth = self.fronteggApp.auth

var jsonUser: [String: Any]? = nil
if let userData = try? JSONEncoder().encode(auth.user) {
jsonUser = try? JSONSerialization.jsonObject(with: userData, options: .allowFragments) as? [String: Any]
if(self.hasListeners){
self.sendEventToJS()
} else {
self.pendingObservingState = true
}

let body: [String: Any?] = [
"accessToken": auth.accessToken,
"refreshToken": auth.refreshToken,
"user": jsonUser,
"isAuthenticated": auth.isAuthenticated,
"isLoading": auth.isLoading,
"initializing": auth.initializing,
"showLoader": auth.showLoader,
"appLink": auth.appLink
]
self.sendEvent(withName: "onFronteggAuthEvent", body: body)
}

}).store(in: &cancellables)

return ["status": "OK"]
}

func sendEventToJS() {
let auth = self.fronteggApp.auth

var jsonUser: [String: Any]? = nil
if let userData = try? JSONEncoder().encode(auth.user) {
jsonUser = try? JSONSerialization.jsonObject(with: userData, options: .allowFragments) as? [String: Any]
}

let body: [String: Any?] = [
"accessToken": auth.accessToken,
"refreshToken": auth.refreshToken,
"user": jsonUser,
"isAuthenticated": auth.isAuthenticated,
"isLoading": auth.isLoading,
"initializing": auth.initializing,
"showLoader": auth.showLoader,
"appLink": auth.appLink
]
self.sendEvent(withName: "onFronteggAuthEvent", body: body)
}

@objc
func logout() -> [AnyHashable : Any]! {
Expand Down

0 comments on commit ff0d285

Please sign in to comment.