From 98c6fffbbd3d90f679011f6805f3befabe7a9a99 Mon Sep 17 00:00:00 2001 From: Tung Wu Date: Wed, 18 Oct 2023 13:52:56 +0800 Subject: [PATCH 1/6] [Latte] Send signal to webview on reset password complated --- Authgear.xcodeproj/project.pbxproj | 4 +++ Sources/EventBus.swift | 44 ++++++++++++++++++++++++++++++ Sources/Latte+Flows.swift | 30 +++++++++++++++++--- Sources/Latte+WKWebView.swift | 14 ++++++++++ Sources/Latte.swift | 6 ++++ 5 files changed, 94 insertions(+), 4 deletions(-) create mode 100644 Sources/EventBus.swift diff --git a/Authgear.xcodeproj/project.pbxproj b/Authgear.xcodeproj/project.pbxproj index 08ff60a8..e4535bb6 100644 --- a/Authgear.xcodeproj/project.pbxproj +++ b/Authgear.xcodeproj/project.pbxproj @@ -22,6 +22,7 @@ A652CEBB2A822D19006E789F /* Dictionary+encodeAsQuery.swift in Sources */ = {isa = PBXBuildFile; fileRef = A652CEBA2A822D19006E789F /* Dictionary+encodeAsQuery.swift */; }; A652CEE72A84DE72006E789F /* ASN1DERParsing.swift in Sources */ = {isa = PBXBuildFile; fileRef = A652CEE62A84DE72006E789F /* ASN1DERParsing.swift */; }; A652CEFA2A8A2C8F006E789F /* Asn1IntegerConversion.swift in Sources */ = {isa = PBXBuildFile; fileRef = A652CEF92A8A2C8F006E789F /* Asn1IntegerConversion.swift */; }; + A679C83F2ADEAEC1002059D4 /* EventBus.swift in Sources */ = {isa = PBXBuildFile; fileRef = A679C83E2ADEAEC1002059D4 /* EventBus.swift */; }; A68DAD2229AF686000487DAF /* UILocales.swift in Sources */ = {isa = PBXBuildFile; fileRef = A68DAD1F29AF686000487DAF /* UILocales.swift */; }; A68DAD2929AF688E00487DAF /* String+encodeAsQueryComponent.swift in Sources */ = {isa = PBXBuildFile; fileRef = A68DAD2629AF688E00487DAF /* String+encodeAsQueryComponent.swift */; }; A6D692B92A0CCA2B00B457F9 /* Latte+Tokenize.swift in Sources */ = {isa = PBXBuildFile; fileRef = A6D692B82A0CCA2B00B457F9 /* Latte+Tokenize.swift */; }; @@ -76,6 +77,7 @@ A652CEBA2A822D19006E789F /* Dictionary+encodeAsQuery.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Dictionary+encodeAsQuery.swift"; sourceTree = ""; }; A652CEE62A84DE72006E789F /* ASN1DERParsing.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ASN1DERParsing.swift; sourceTree = ""; }; A652CEF92A8A2C8F006E789F /* Asn1IntegerConversion.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Asn1IntegerConversion.swift; sourceTree = ""; }; + A679C83E2ADEAEC1002059D4 /* EventBus.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EventBus.swift; sourceTree = ""; }; A68DAD1F29AF686000487DAF /* UILocales.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UILocales.swift; sourceTree = ""; }; A68DAD2629AF688E00487DAF /* String+encodeAsQueryComponent.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "String+encodeAsQueryComponent.swift"; sourceTree = ""; }; A6D692B82A0CCA2B00B457F9 /* Latte+Tokenize.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Latte+Tokenize.swift"; sourceTree = ""; }; @@ -196,6 +198,7 @@ A6D692B82A0CCA2B00B457F9 /* Latte+Tokenize.swift */, A648BE842975316A002D8EA5 /* Latte+EmailClient.swift */, 95E6733A29A37E5A00C9466C /* Latte+Flows.swift */, + A679C83E2ADEAEC1002059D4 /* EventBus.swift */, A652CEBA2A822D19006E789F /* Dictionary+encodeAsQuery.swift */, 77AE4DD929A72A5B006A3ADB /* Latte+UniversalLink.swift */, 95E67330299F652000C9466C /* Latte+WKWebView.swift */, @@ -319,6 +322,7 @@ 775FF73426047C7700B81ECD /* DeviceInfo.swift in Sources */, F8A657A7250551ED0027888F /* JWT.swift in Sources */, A648BE852975316A002D8EA5 /* Latte+EmailClient.swift in Sources */, + A679C83F2ADEAEC1002059D4 /* EventBus.swift in Sources */, A68DAD2229AF686000487DAF /* UILocales.swift in Sources */, F8A657A52501D31E0027888F /* JWK.swift in Sources */, F8A657A32501845D0027888F /* Decodable+Any.swift in Sources */, diff --git a/Sources/EventBus.swift b/Sources/EventBus.swift new file mode 100644 index 00000000..40cba0ca --- /dev/null +++ b/Sources/EventBus.swift @@ -0,0 +1,44 @@ +import Foundation + +class EventBus { + typealias Listener = () -> Void + typealias Unsubscriber = () -> Void + + private var registry: [EventName:Array] = [:] + + init() {} + + func listen(eventName: EventName, listener: @escaping Listener) -> Unsubscriber { + let listenerRef = ListenerRef(listener: listener) + DispatchQueue.main.async { + var listeners = self.registry[eventName] ?? [] + listeners.append(listenerRef) + self.registry[eventName] = listeners + } + + return { + DispatchQueue.main.async { + var listeners = self.registry[eventName] ?? [] + listeners = listeners.filter({ $0 !== listenerRef }) + self.registry[eventName] = listeners + } + } + } + + func dispatch(eventName: EventName) { + DispatchQueue.main.async { + var listeners = self.registry[eventName] ?? [] + listeners.forEach({ + $0.listener() + }) + } + } + + private class ListenerRef { + var listener: Listener + + init(listener: @escaping Listener) { + self.listener = listener + } + } +} diff --git a/Sources/Latte+Flows.swift b/Sources/Latte+Flows.swift index 7da64c1f..4d5ae3d9 100644 --- a/Sources/Latte+Flows.swift +++ b/Sources/Latte+Flows.swift @@ -108,8 +108,16 @@ public extension Latte { let handle = LatteHandle(task: Task { try await run1() }) @Sendable @MainActor func run1() async throws -> UserInfo { - let result: LatteWebViewResult = try await withCheckedThrowingContinuation { next in + let result: LatteWebViewResult = try await withCheckedThrowingContinuation { [ + weak self] next in + let unsubscribe = self?.eventBus.listen( + eventName: .resetPasswordCompleted, + listener: { + latteVC.dispatchWebViewSignal(signal: .resetPasswordCompleted) + } + ) latteVC.webView.completion = { (_, result) in + unsubscribe?() next.resume(with: result) } } @@ -183,17 +191,24 @@ public extension Latte { @Sendable @MainActor func run1() async throws -> Bool { - let result: Bool = try await withCheckedThrowingContinuation { next in + let result: Bool = try await withCheckedThrowingContinuation { [weak self] next in var isResumed = false func resume(_ result: Result) { guard isResumed == false else { return } isResumed = true next.resume(with: result) } + let unsubscribe = self?.eventBus.listen( + eventName: .resetPasswordCompleted, + listener: { + latteVC.dispatchWebViewSignal(signal: .resetPasswordCompleted) + } + ) latteVC.webView.completion = { (_, result) in do { + unsubscribe?() let finishURL = try result.get().unwrap() - self.authgear.experimental.finishAuthentication(finishURL: finishURL, request: request) { r in + self?.authgear.experimental.finishAuthentication(finishURL: finishURL, request: request) { r in resume(r.flatMap { _ in .success(true) }) @@ -387,7 +402,10 @@ public extension Latte { let handle = LatteHandle(task: Task { try await run1() }) @Sendable @MainActor func run1() async throws { - let result: LatteWebViewResult = try await withCheckedThrowingContinuation { next in + let result: LatteWebViewResult = try await withCheckedThrowingContinuation { [weak self] next in + latteVC.webView.onResetPasswordCompleted = { _ in + self?.eventBus.dispatch(eventName: .resetPasswordCompleted) + } latteVC.webView.completion = { (_, result) in next.resume(with: result) } @@ -572,4 +590,8 @@ class LatteViewController: UIViewController { } } } + + func dispatchWebViewSignal(signal: LatteBuiltInSignals) { + self.webView.dispatchSignal(signal: signal) + } } diff --git a/Sources/Latte+WKWebView.swift b/Sources/Latte+WKWebView.swift index 87188841..38e41994 100644 --- a/Sources/Latte+WKWebView.swift +++ b/Sources/Latte+WKWebView.swift @@ -8,6 +8,11 @@ enum LatteBuiltInEvents: String { case tracking case ready case reauthWithBiometric + case resetPasswordCompleted +} + +enum LatteBuiltInSignals: String { + case resetPasswordCompleted } let initScript = """ @@ -28,6 +33,7 @@ class LatteWKWebView: WKWebView, WKNavigationDelegate { let request: LatteWebViewRequest var onReady: ((_ webview: LatteWKWebView) -> Void)? var onReauthWithBiometric: ((_ webview: LatteWKWebView) -> Void)? + var onResetPasswordCompleted: ((_ webview: LatteWKWebView) -> Void)? var completion: ((_ webview: LatteWKWebView, _ result: Result) -> Void)? weak var viewController: UIViewController? weak var delegate: LatteWebViewDelegate? @@ -77,6 +83,12 @@ class LatteWKWebView: WKWebView, WKNavigationDelegate { func load() { self.initialNavigation = self.load(URLRequest(url: self.request.url)) } + + func dispatchSignal(signal: LatteBuiltInSignals) { + self.evaluateJavaScript(""" + window.dispatchSignal("\(signal.rawValue)"); + """) + } func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) { if navigation == self.initialNavigation { @@ -151,6 +163,8 @@ class LatteWKWebView: WKWebView, WKNavigationDelegate { parent.onReady = nil case LatteBuiltInEvents.reauthWithBiometric.rawValue: parent.onReauthWithBiometric?(parent) + case LatteBuiltInEvents.resetPasswordCompleted.rawValue: + parent.onResetPasswordCompleted?(parent) default: return } diff --git a/Sources/Latte.swift b/Sources/Latte.swift index fc2dc468..ecb53f95 100644 --- a/Sources/Latte.swift +++ b/Sources/Latte.swift @@ -37,6 +37,8 @@ public class Latte: LatteWebViewDelegate { let webviewIsInspectable: Bool let webViewLoadTimeoutMillis: Int public weak var delegate: LatteDelegate? + + let eventBus: EventBus = EventBus() public init( authgear: Authgear, @@ -133,6 +135,10 @@ enum LatteWebViewEvent { case trackingEvent(event: LatteTrackingEvent) } +enum LatteInternalEvent { + case resetPasswordCompleted +} + public struct LatteBiometricOptions { let localizedReason: String let laPolicy: LAPolicy From b19e331202082e99fa9583b6bd37f68d9135f923 Mon Sep 17 00:00:00 2001 From: Tung Wu Date: Wed, 18 Oct 2023 14:57:52 +0800 Subject: [PATCH 2/6] Run make format --- Sources/EventBus.swift | 22 +++++++++++----------- Sources/Latte+Flows.swift | 5 +++-- Sources/Latte+WKWebView.swift | 2 +- Sources/Latte.swift | 2 +- 4 files changed, 16 insertions(+), 15 deletions(-) diff --git a/Sources/EventBus.swift b/Sources/EventBus.swift index 40cba0ca..abaff4b6 100644 --- a/Sources/EventBus.swift +++ b/Sources/EventBus.swift @@ -3,11 +3,11 @@ import Foundation class EventBus { typealias Listener = () -> Void typealias Unsubscriber = () -> Void - - private var registry: [EventName:Array] = [:] - + + private var registry: [EventName: Array] = [:] + init() {} - + func listen(eventName: EventName, listener: @escaping Listener) -> Unsubscriber { let listenerRef = ListenerRef(listener: listener) DispatchQueue.main.async { @@ -15,28 +15,28 @@ class EventBus { listeners.append(listenerRef) self.registry[eventName] = listeners } - + return { DispatchQueue.main.async { var listeners = self.registry[eventName] ?? [] - listeners = listeners.filter({ $0 !== listenerRef }) + listeners = listeners.filter { $0 !== listenerRef } self.registry[eventName] = listeners } } } - + func dispatch(eventName: EventName) { DispatchQueue.main.async { var listeners = self.registry[eventName] ?? [] - listeners.forEach({ + listeners.forEach { $0.listener() - }) + } } } - + private class ListenerRef { var listener: Listener - + init(listener: @escaping Listener) { self.listener = listener } diff --git a/Sources/Latte+Flows.swift b/Sources/Latte+Flows.swift index 4d5ae3d9..3449c3bb 100644 --- a/Sources/Latte+Flows.swift +++ b/Sources/Latte+Flows.swift @@ -109,7 +109,8 @@ public extension Latte { @Sendable @MainActor func run1() async throws -> UserInfo { let result: LatteWebViewResult = try await withCheckedThrowingContinuation { [ - weak self] next in + weak self + ] next in let unsubscribe = self?.eventBus.listen( eventName: .resetPasswordCompleted, listener: { @@ -590,7 +591,7 @@ class LatteViewController: UIViewController { } } } - + func dispatchWebViewSignal(signal: LatteBuiltInSignals) { self.webView.dispatchSignal(signal: signal) } diff --git a/Sources/Latte+WKWebView.swift b/Sources/Latte+WKWebView.swift index 38e41994..f36e3150 100644 --- a/Sources/Latte+WKWebView.swift +++ b/Sources/Latte+WKWebView.swift @@ -83,7 +83,7 @@ class LatteWKWebView: WKWebView, WKNavigationDelegate { func load() { self.initialNavigation = self.load(URLRequest(url: self.request.url)) } - + func dispatchSignal(signal: LatteBuiltInSignals) { self.evaluateJavaScript(""" window.dispatchSignal("\(signal.rawValue)"); diff --git a/Sources/Latte.swift b/Sources/Latte.swift index ecb53f95..b42b1374 100644 --- a/Sources/Latte.swift +++ b/Sources/Latte.swift @@ -37,7 +37,7 @@ public class Latte: LatteWebViewDelegate { let webviewIsInspectable: Bool let webViewLoadTimeoutMillis: Int public weak var delegate: LatteDelegate? - + let eventBus: EventBus = EventBus() public init( From 9dd3b6087e5b099388c86c4b7c8e2ea62066e85f Mon Sep 17 00:00:00 2001 From: Tung Wu Date: Wed, 18 Oct 2023 15:05:29 +0800 Subject: [PATCH 3/6] [Latte] Fix some lint warning by use let --- Sources/EventBus.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/EventBus.swift b/Sources/EventBus.swift index abaff4b6..4ea30338 100644 --- a/Sources/EventBus.swift +++ b/Sources/EventBus.swift @@ -27,7 +27,7 @@ class EventBus { func dispatch(eventName: EventName) { DispatchQueue.main.async { - var listeners = self.registry[eventName] ?? [] + let listeners = self.registry[eventName] ?? [] listeners.forEach { $0.listener() } From 0a36be18fcbe3aba0941de929ff31c3cf9192568 Mon Sep 17 00:00:00 2001 From: Tung Wu Date: Thu, 19 Oct 2023 13:04:16 +0800 Subject: [PATCH 4/6] Use notification center instead of event bus --- Authgear.xcodeproj/project.pbxproj | 4 --- Sources/EventBus.swift | 44 ------------------------------ Sources/Latte+Flows.swift | 35 ++++++++++++++++-------- Sources/Latte.swift | 8 ++++-- 4 files changed, 30 insertions(+), 61 deletions(-) delete mode 100644 Sources/EventBus.swift diff --git a/Authgear.xcodeproj/project.pbxproj b/Authgear.xcodeproj/project.pbxproj index e4535bb6..08ff60a8 100644 --- a/Authgear.xcodeproj/project.pbxproj +++ b/Authgear.xcodeproj/project.pbxproj @@ -22,7 +22,6 @@ A652CEBB2A822D19006E789F /* Dictionary+encodeAsQuery.swift in Sources */ = {isa = PBXBuildFile; fileRef = A652CEBA2A822D19006E789F /* Dictionary+encodeAsQuery.swift */; }; A652CEE72A84DE72006E789F /* ASN1DERParsing.swift in Sources */ = {isa = PBXBuildFile; fileRef = A652CEE62A84DE72006E789F /* ASN1DERParsing.swift */; }; A652CEFA2A8A2C8F006E789F /* Asn1IntegerConversion.swift in Sources */ = {isa = PBXBuildFile; fileRef = A652CEF92A8A2C8F006E789F /* Asn1IntegerConversion.swift */; }; - A679C83F2ADEAEC1002059D4 /* EventBus.swift in Sources */ = {isa = PBXBuildFile; fileRef = A679C83E2ADEAEC1002059D4 /* EventBus.swift */; }; A68DAD2229AF686000487DAF /* UILocales.swift in Sources */ = {isa = PBXBuildFile; fileRef = A68DAD1F29AF686000487DAF /* UILocales.swift */; }; A68DAD2929AF688E00487DAF /* String+encodeAsQueryComponent.swift in Sources */ = {isa = PBXBuildFile; fileRef = A68DAD2629AF688E00487DAF /* String+encodeAsQueryComponent.swift */; }; A6D692B92A0CCA2B00B457F9 /* Latte+Tokenize.swift in Sources */ = {isa = PBXBuildFile; fileRef = A6D692B82A0CCA2B00B457F9 /* Latte+Tokenize.swift */; }; @@ -77,7 +76,6 @@ A652CEBA2A822D19006E789F /* Dictionary+encodeAsQuery.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Dictionary+encodeAsQuery.swift"; sourceTree = ""; }; A652CEE62A84DE72006E789F /* ASN1DERParsing.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ASN1DERParsing.swift; sourceTree = ""; }; A652CEF92A8A2C8F006E789F /* Asn1IntegerConversion.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Asn1IntegerConversion.swift; sourceTree = ""; }; - A679C83E2ADEAEC1002059D4 /* EventBus.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EventBus.swift; sourceTree = ""; }; A68DAD1F29AF686000487DAF /* UILocales.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UILocales.swift; sourceTree = ""; }; A68DAD2629AF688E00487DAF /* String+encodeAsQueryComponent.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "String+encodeAsQueryComponent.swift"; sourceTree = ""; }; A6D692B82A0CCA2B00B457F9 /* Latte+Tokenize.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Latte+Tokenize.swift"; sourceTree = ""; }; @@ -198,7 +196,6 @@ A6D692B82A0CCA2B00B457F9 /* Latte+Tokenize.swift */, A648BE842975316A002D8EA5 /* Latte+EmailClient.swift */, 95E6733A29A37E5A00C9466C /* Latte+Flows.swift */, - A679C83E2ADEAEC1002059D4 /* EventBus.swift */, A652CEBA2A822D19006E789F /* Dictionary+encodeAsQuery.swift */, 77AE4DD929A72A5B006A3ADB /* Latte+UniversalLink.swift */, 95E67330299F652000C9466C /* Latte+WKWebView.swift */, @@ -322,7 +319,6 @@ 775FF73426047C7700B81ECD /* DeviceInfo.swift in Sources */, F8A657A7250551ED0027888F /* JWT.swift in Sources */, A648BE852975316A002D8EA5 /* Latte+EmailClient.swift in Sources */, - A679C83F2ADEAEC1002059D4 /* EventBus.swift in Sources */, A68DAD2229AF686000487DAF /* UILocales.swift in Sources */, F8A657A52501D31E0027888F /* JWK.swift in Sources */, F8A657A32501845D0027888F /* Decodable+Any.swift in Sources */, diff --git a/Sources/EventBus.swift b/Sources/EventBus.swift deleted file mode 100644 index 4ea30338..00000000 --- a/Sources/EventBus.swift +++ /dev/null @@ -1,44 +0,0 @@ -import Foundation - -class EventBus { - typealias Listener = () -> Void - typealias Unsubscriber = () -> Void - - private var registry: [EventName: Array] = [:] - - init() {} - - func listen(eventName: EventName, listener: @escaping Listener) -> Unsubscriber { - let listenerRef = ListenerRef(listener: listener) - DispatchQueue.main.async { - var listeners = self.registry[eventName] ?? [] - listeners.append(listenerRef) - self.registry[eventName] = listeners - } - - return { - DispatchQueue.main.async { - var listeners = self.registry[eventName] ?? [] - listeners = listeners.filter { $0 !== listenerRef } - self.registry[eventName] = listeners - } - } - } - - func dispatch(eventName: EventName) { - DispatchQueue.main.async { - let listeners = self.registry[eventName] ?? [] - listeners.forEach { - $0.listener() - } - } - } - - private class ListenerRef { - var listener: Listener - - init(listener: @escaping Listener) { - self.listener = listener - } - } -} diff --git a/Sources/Latte+Flows.swift b/Sources/Latte+Flows.swift index 3449c3bb..e0351554 100644 --- a/Sources/Latte+Flows.swift +++ b/Sources/Latte+Flows.swift @@ -111,14 +111,19 @@ public extension Latte { let result: LatteWebViewResult = try await withCheckedThrowingContinuation { [ weak self ] next in - let unsubscribe = self?.eventBus.listen( - eventName: .resetPasswordCompleted, - listener: { - latteVC.dispatchWebViewSignal(signal: .resetPasswordCompleted) + guard let nc = self?.eventNotificationCenter else { return } + let observer = nc.addObserver( + forName: LatteInternalEvent.resetPasswordCompleted.notificationName, + object: nil, + queue: nil, + using: { _ in + Task { + await latteVC.dispatchWebViewSignal(signal: .resetPasswordCompleted) + } } ) latteVC.webView.completion = { (_, result) in - unsubscribe?() + nc.removeObserver(observer) next.resume(with: result) } } @@ -193,21 +198,26 @@ public extension Latte { @Sendable @MainActor func run1() async throws -> Bool { let result: Bool = try await withCheckedThrowingContinuation { [weak self] next in + guard let nc = self?.eventNotificationCenter else { return } var isResumed = false func resume(_ result: Result) { guard isResumed == false else { return } isResumed = true next.resume(with: result) } - let unsubscribe = self?.eventBus.listen( - eventName: .resetPasswordCompleted, - listener: { - latteVC.dispatchWebViewSignal(signal: .resetPasswordCompleted) + let observer = nc.addObserver( + forName: LatteInternalEvent.resetPasswordCompleted.notificationName, + object: nil, + queue: nil, + using: { _ in + Task { + await latteVC.dispatchWebViewSignal(signal: .resetPasswordCompleted) + } } ) latteVC.webView.completion = { (_, result) in do { - unsubscribe?() + nc.removeObserver(observer) let finishURL = try result.get().unwrap() self?.authgear.experimental.finishAuthentication(finishURL: finishURL, request: request) { r in resume(r.flatMap { _ in @@ -405,7 +415,10 @@ public extension Latte { func run1() async throws { let result: LatteWebViewResult = try await withCheckedThrowingContinuation { [weak self] next in latteVC.webView.onResetPasswordCompleted = { _ in - self?.eventBus.dispatch(eventName: .resetPasswordCompleted) + self?.eventNotificationCenter.post( + name: LatteInternalEvent.resetPasswordCompleted.notificationName, + object: nil + ) } latteVC.webView.completion = { (_, result) in next.resume(with: result) diff --git a/Sources/Latte.swift b/Sources/Latte.swift index b42b1374..439c56e7 100644 --- a/Sources/Latte.swift +++ b/Sources/Latte.swift @@ -38,7 +38,7 @@ public class Latte: LatteWebViewDelegate { let webViewLoadTimeoutMillis: Int public weak var delegate: LatteDelegate? - let eventBus: EventBus = EventBus() + let eventNotificationCenter = NotificationCenter() public init( authgear: Authgear, @@ -135,8 +135,12 @@ enum LatteWebViewEvent { case trackingEvent(event: LatteTrackingEvent) } -enum LatteInternalEvent { +enum LatteInternalEvent: String { case resetPasswordCompleted + + var notificationName: Notification.Name { + return Notification.Name(self.rawValue) + } } public struct LatteBiometricOptions { From 53f0a81d4aec2b402c2df48ea517f504b6c4d1b7 Mon Sep 17 00:00:00 2001 From: Tung Wu Date: Thu, 19 Oct 2023 13:10:14 +0800 Subject: [PATCH 5/6] [Latte] Call dispatchEvent directly in document --- Sources/Latte+WKWebView.swift | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/Sources/Latte+WKWebView.swift b/Sources/Latte+WKWebView.swift index f36e3150..f65d1502 100644 --- a/Sources/Latte+WKWebView.swift +++ b/Sources/Latte+WKWebView.swift @@ -86,7 +86,11 @@ class LatteWKWebView: WKWebView, WKNavigationDelegate { func dispatchSignal(signal: LatteBuiltInSignals) { self.evaluateJavaScript(""" - window.dispatchSignal("\(signal.rawValue)"); + document.dispatchEvent( + new CustomEvent("latte:signal", { + detail: { type: "\(signal.rawValue)" }, + }) + ); """) } From 455e4f699f54901627072cae6eef6e3b6d7ccdd9 Mon Sep 17 00:00:00 2001 From: Tung Wu Date: Thu, 19 Oct 2023 13:10:39 +0800 Subject: [PATCH 6/6] Run make format --- Sources/Latte.swift | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Sources/Latte.swift b/Sources/Latte.swift index 439c56e7..e4b0636a 100644 --- a/Sources/Latte.swift +++ b/Sources/Latte.swift @@ -137,9 +137,9 @@ enum LatteWebViewEvent { enum LatteInternalEvent: String { case resetPasswordCompleted - + var notificationName: Notification.Name { - return Notification.Name(self.rawValue) + Notification.Name(self.rawValue) } }