diff --git a/submodules/TelegramCore/Sources/State/CallSessionManager.swift b/submodules/TelegramCore/Sources/State/CallSessionManager.swift index 6251ed0950..93fd01e995 100644 --- a/submodules/TelegramCore/Sources/State/CallSessionManager.swift +++ b/submodules/TelegramCore/Sources/State/CallSessionManager.swift @@ -369,6 +369,7 @@ private final class CallSessionManagerContext { private let ringingSubscribers = Bag<([CallSessionRingingState]) -> Void>() private var contexts: [CallSessionInternalId: CallSessionContext] = [:] + private var futureContextSubscribers: [CallSessionInternalId: Bag<(CallSession) -> Void>] = [:] private var contextIdByStableId: [CallSessionStableId: CallSessionInternalId] = [:] private var enqueuedSignalingData: [Int64: [Data]] = [:] @@ -443,7 +444,10 @@ private final class CallSessionManagerContext { return Signal { [weak self] subscriber in let disposable = MetaDisposable() queue.async { - if let strongSelf = self, let context = strongSelf.contexts[internalId] { + guard let strongSelf = self else { + return + } + if let context = strongSelf.contexts[internalId] { let index = context.subscribers.add { next in subscriber.putNext(next) } @@ -459,8 +463,22 @@ private final class CallSessionManagerContext { } }) } else { - subscriber.putNext(CallSession(id: internalId, stableId: nil, isOutgoing: false, type: .audio, state: .terminated(id: nil, reason: .error(.generic), options: []), isVideoPossible: true)) - subscriber.putCompletion() + if strongSelf.futureContextSubscribers[internalId] == nil { + strongSelf.futureContextSubscribers[internalId] = Bag() + } + let index = strongSelf.futureContextSubscribers[internalId]?.add({ session in + subscriber.putNext(session) + }) + if let index = index { + disposable.set(ActionDisposable { + queue.async { + guard let strongSelf = self else { + return + } + strongSelf.futureContextSubscribers[internalId]?.remove(index) + } + }) + } } } return disposable @@ -517,6 +535,11 @@ private final class CallSessionManagerContext { for subscriber in context.subscribers.copyItems() { subscriber(session) } + if let futureSubscribers = self.futureContextSubscribers[internalId] { + for subscriber in futureSubscribers.copyItems() { + subscriber(session) + } + } } }