From 5617da4d79dd74f5ff824377b04033e449efd475 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B8=D1=85=D0=B0=D0=B8=D0=BB=20=D0=9A=D0=B0=D0=BF?= =?UTF-8?q?=D0=B5=D0=BB=D1=8C=D0=BA=D0=BE?= Date: Tue, 26 Dec 2023 12:21:01 +0300 Subject: [PATCH] d --- src/App.swift | 6 +++-- src/Bus.swift | 61 ++++++++++++++++++++++++++++----------------------- 2 files changed, 37 insertions(+), 30 deletions(-) diff --git a/src/App.swift b/src/App.swift index 9755bf7..6f40164 100644 --- a/src/App.swift +++ b/src/App.swift @@ -18,8 +18,10 @@ class AppDelegate: UIResponder, UIApplicationDelegate window?.makeKeyAndVisible() - Bus.processSyncG( - MeetupId.shouldFormat, MeetupId.K.meetupIdTextUI.rawValue, MeetupId.K.meetupIdTextApp.rawValue + Bus.process( + MeetupId.K.meetupIdTextUI.rawValue, + MeetupId.K.meetupIdTextApp.rawValue, + MeetupId.shouldFormat ) return true diff --git a/src/Bus.swift b/src/Bus.swift index e33a09d..6f9e162 100644 --- a/src/Bus.swift +++ b/src/Bus.swift @@ -22,6 +22,20 @@ extension Bus { } } +private extension Bus { + static func subscribe( + _ subscription: AnyCancellable?, + _ sub: UnsafeMutablePointer<[AnyCancellable]>? + ) { + guard let subscription else { return } + if let sub = sub { + sub.pointee.append(subscription) + } else { + Service.singleton.subscriptions.append(subscription) + } + } +} + public extension Bus { static func receive( _ keys: Set, @@ -80,41 +94,32 @@ public extension Bus { } } -private extension Bus { - static func subscribe( - _ subscription: AnyCancellable?, - _ sub: UnsafeMutablePointer<[AnyCancellable]>? - ) { - guard let subscription else { return } - if let sub = sub { - sub.pointee.append(subscription) - } else { - Service.singleton.subscriptions.append(subscription) - } - } -} - public extension Bus { - static func processSync( - _ subscriptions: inout [AnyCancellable], - _ handler: @escaping ((Src) -> Dst?), + static func process( _ keyIn: String, - _ keyOut: String + _ keyOut: String, + _ handler: @escaping ((Src) -> Dst?), + opt: [Option] = [], + sub: UnsafeMutablePointer<[AnyCancellable]>? = nil ) { - Service.singleton.events + var subscription: AnyCancellable? + let isAsync = opt.contains(.async) + + // Async. + if isAsync { + subscription = Service.singleton.events .compactMap { processKeyValue($0, keyIn, handler) } + .receive(on: DispatchQueue.main) .sink { vOut in Service.singleton.send(keyOut, vOut) } - .store(in: &subscriptions) - } + } - static func processSyncG( - _ handler: @escaping ((Src) -> Dst?), - _ keyIn: String, - _ keyOut: String - ) { - Service.singleton.events + // Sync. + if !isAsync { + subscription = Service.singleton.events .compactMap { processKeyValue($0, keyIn, handler) } .sink { vOut in Service.singleton.send(keyOut, vOut) } - .store(in: &Service.singleton.subscriptions) + } + + subscribe(subscription, sub) } }