Михаил Капелько 1 year ago
parent
commit
5d9122cdba
2 changed files with 11 additions and 19 deletions
  1. +0
    -1
      src/App.swift
  2. +11
    -18
      src/Bus.swift

+ 0
- 1
src/App.swift View File

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



+ 11
- 18
src/Bus.swift View File

@@ -1,20 +1,13 @@
import Combine
import SwiftUI
import Foundation

public enum Bus { }

extension Bus {
public final class Service {
static private(set) weak var singleton: Service?
final class Service {
static let singleton = Service()
let broadcaster = PassthroughSubject<(key: String, value: Any), Never>()
deinit {
Self.singleton = nil
}
public init() {
Self.singleton = self
}
var subscriptions = Set<AnyCancellable>()
func send(_ key: String, _ value: Any) {
/**/print("ИГР BusS.send key/value: '\(key)'/'\(value)'")
@@ -29,7 +22,7 @@ public extension Bus {
_ keys: Set<String>,
_ handler: @escaping ((String, T) -> Void)
) {
Service.singleton?.broadcaster
Service.singleton.broadcaster
.compactMap { v -> (String, T)? in
guard
keys.contains(v.key),
@@ -49,7 +42,7 @@ public extension Bus {
_ keys: Set<String>,
_ handler: @escaping ((String, T) -> Void)
) {
Service.singleton?.broadcaster
Service.singleton.broadcaster
.compactMap { v -> (String, T)? in
guard
keys.contains(v.key),
@@ -70,7 +63,7 @@ public extension Bus {
) {
node
.receive(on: DispatchQueue.main)
.sink { v in Service.singleton?.send(key, v) }
.sink { v in Service.singleton.send(key, v) }
.store(in: &subscriptions)
}
@@ -80,12 +73,12 @@ public extension Bus {
_ node: AnyPublisher<T, Never>
) {
node
.sink { v in Service.singleton?.send(key, v) }
.sink { v in Service.singleton.send(key, v) }
.store(in: &subscriptions)
}
static func sendOnce(_ key: String, _ value: Any) {
Service.singleton?.send(key, value)
Service.singleton.send(key, value)
}
}

@@ -96,7 +89,7 @@ public extension Bus {
_ keyOut: String,
_ handler: @escaping ((Src) -> Dst?)
) {
Service.singleton?.broadcaster
Service.singleton.broadcaster
.compactMap {
guard
$0.key == keyIn,
@@ -106,7 +99,7 @@ public extension Bus {
}
return handler(vIn)
}
.sink { vOut in Service.singleton?.send(keyOut, vOut) }
.sink { vOut in Service.singleton.send(keyOut, vOut) }
.store(in: &subscriptions)
}
}

Loading…
Cancel
Save