This commit is contained in:
Михаил Капелько
2023-12-16 11:22:56 +03:00
parent 5d9122cdba
commit a77e1c82b4
4 changed files with 29 additions and 23 deletions

View File

@@ -3,7 +3,6 @@ import UIKit
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate
{
let fmt = MeetupId.MeetupIdFormatter()
var window: UIWindow?
func application(

View File

@@ -85,9 +85,9 @@ public extension Bus {
public extension Bus {
static func processSync<Src, Dst>(
_ subscriptions: inout Set<AnyCancellable>,
_ handler: @escaping ((Src) -> Dst?),
_ keyIn: String,
_ keyOut: String,
_ handler: @escaping ((Src) -> Dst?)
_ keyOut: String
) {
Service.singleton.broadcaster
.compactMap {
@@ -102,4 +102,23 @@ public extension Bus {
.sink { vOut in Service.singleton.send(keyOut, vOut) }
.store(in: &subscriptions)
}
static func processSyncG<Src, Dst>(
_ handler: @escaping ((Src) -> Dst?),
_ keyIn: String,
_ keyOut: String
) {
Service.singleton.broadcaster
.compactMap {
guard
$0.key == keyIn,
let vIn = $0.value as? Src
else {
return nil
}
return handler(vIn)
}
.sink { vOut in Service.singleton.send(keyOut, vOut) }
.store(in: &Service.singleton.subscriptions)
}
}

View File

@@ -2,7 +2,7 @@ import Combine
import Foundation
enum MeetupId {
enum Keys: String, RawRepresentable {
enum K: String, RawRepresentable {
case meetupIdTextApp
case meetupIdTextUI
}
@@ -24,21 +24,9 @@ enum MeetupId {
return r
}
final class MeetupIdFormatter {
var subscriptions = Set<AnyCancellable>()
deinit {
/**/print("ИГР MeetupIF.DEinit")
}
init() {
Bus.processSync(
&subscriptions,
Keys.meetupIdTextUI.rawValue,
Keys.meetupIdTextApp.rawValue,
shouldFormat
)
/**/print("ИГР MeetupIF.init")
}
}
Bus.processSyncG(
shouldFormat, K.meetupIdTextUI.rawValue, K.meetupIdTextApp.rawValue
)
}

View File

@@ -9,7 +9,7 @@ final class VM: ObservableObject {
init() {
Bus.sendSync(
&subscriptions,
MeetupId.Keys.meetupIdTextUI.rawValue,
MeetupId.K.meetupIdTextUI.rawValue,
$text
// Исключаем конфликты от UI и App путём игнорирования спама.
.debounce(for: .seconds(0.3), scheduler: DispatchQueue.main)
@@ -22,7 +22,7 @@ final class VM: ObservableObject {
Bus.receiveAsync(
&subscriptions,
[MeetupId.Keys.meetupIdTextApp.rawValue],
[MeetupId.K.meetupIdTextApp.rawValue],
{ [weak self] (_, v: String) in self?.text = "a:\(v)" }
)
}