From 5d9122cdba28c98f98df41e5484ddddfcb1d2004 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: Sat, 16 Dec 2023 11:14:30 +0300 Subject: [PATCH] d --- src/App.swift | 1 - src/Bus.swift | 29 +++++++++++------------------ 2 files changed, 11 insertions(+), 19 deletions(-) diff --git a/src/App.swift b/src/App.swift index 238ee04..c35f2f9 100644 --- a/src/App.swift +++ b/src/App.swift @@ -3,7 +3,6 @@ import UIKit @UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate { - let bus = Bus.Service() let fmt = MeetupId.MeetupIdFormatter() var window: UIWindow? diff --git a/src/Bus.swift b/src/Bus.swift index 19bc2da..9353143 100644 --- a/src/Bus.swift +++ b/src/Bus.swift @@ -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() func send(_ key: String, _ value: Any) { /**/print("ИГР BusS.send key/value: '\(key)'/'\(value)'") @@ -29,7 +22,7 @@ public extension Bus { _ keys: Set, _ 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, _ 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 ) { 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) } }