Fix .sink retain cycles (#345)

This commit is contained in:
Davide De Rosa 2023-09-08 16:20:01 +02:00 committed by GitHub
parent f159252e24
commit b4b2db176c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 33 additions and 24 deletions

View File

@ -79,19 +79,19 @@ private extension AppContext {
coreContext.vpnManager.currentState.$vpnStatus coreContext.vpnManager.currentState.$vpnStatus
.removeDuplicates() .removeDuplicates()
.receive(on: DispatchQueue.main) .receive(on: DispatchQueue.main)
.sink { .sink { [weak self] in
if $0 == .connected { if $0 == .connected {
pp_log.info("VPN successful connection, report to Reviewer") pp_log.info("VPN successful connection, report to Reviewer")
self.reviewer.reportEvent() self?.reviewer.reportEvent()
} }
}.store(in: &cancellables) }.store(in: &cancellables)
productManager.didRefundProducts productManager.didRefundProducts
.receive(on: DispatchQueue.main) .receive(on: DispatchQueue.main)
.sink { .sink { [weak self] in
Task { Task {
pp_log.info("Refunds detected, uninstalling VPN profile") pp_log.info("Refunds detected, uninstalling VPN profile")
await self.coreContext.vpnManager.uninstall() await self?.coreContext.vpnManager.uninstall()
} }
}.store(in: &cancellables) }.store(in: &cancellables)
} }

View File

@ -62,8 +62,8 @@ final class DefaultLightProfileManager: LightProfileManager {
init() { init() {
profileManager.didUpdateProfiles profileManager.didUpdateProfiles
.receive(on: DispatchQueue.main) .receive(on: DispatchQueue.main)
.sink { .sink { [weak self] in
self.delegate?.didUpdateProfiles() self?.delegate?.didUpdateProfiles()
}.store(in: &subscriptions) }.store(in: &subscriptions)
} }

View File

@ -89,8 +89,8 @@ final class DefaultLightProviderManager: LightProviderManager {
init() { init() {
providerManager.didUpdateProviders providerManager.didUpdateProviders
.receive(on: DispatchQueue.main) .receive(on: DispatchQueue.main)
.sink { .sink { [weak self] in
self.delegate?.didUpdateProviders() self?.delegate?.didUpdateProviders()
}.store(in: &subscriptions) }.store(in: &subscriptions)
} }

View File

@ -47,7 +47,10 @@ final class DefaultLightVPNManager: LightVPNManager {
vpnManager.currentState.$isEnabled vpnManager.currentState.$isEnabled
.removeDuplicates() .removeDuplicates()
.receive(on: DispatchQueue.main) .receive(on: DispatchQueue.main)
.sink { .sink { [weak self] in
guard let self else {
return
}
self.didUpdateState( self.didUpdateState(
isEnabled: $0, isEnabled: $0,
vpnStatus: self.vpnManager.currentState.vpnStatus.asLightVPNStatus vpnStatus: self.vpnManager.currentState.vpnStatus.asLightVPNStatus
@ -57,7 +60,10 @@ final class DefaultLightVPNManager: LightVPNManager {
vpnManager.currentState.$vpnStatus vpnManager.currentState.$vpnStatus
.removeDuplicates() .removeDuplicates()
.receive(on: DispatchQueue.main) .receive(on: DispatchQueue.main)
.sink { .sink { [weak self] in
guard let self else {
return
}
self.didUpdateState( self.didUpdateState(
isEnabled: self.vpnManager.currentState.isEnabled, isEnabled: self.vpnManager.currentState.isEnabled,
vpnStatus: $0.asLightVPNStatus vpnStatus: $0.asLightVPNStatus

View File

@ -70,7 +70,10 @@ extension LaunchOnLoginItem {
func subscribe(_ block: @escaping (Bool) -> Void) { func subscribe(_ block: @escaping (Bool) -> Void) {
objectWillChange objectWillChange
.sink { .sink { [weak self] in
guard let self else {
return
}
block(self.persistentlyLaunchesOnLogin) block(self.persistentlyLaunchesOnLogin)
}.store(in: &subscriptions) }.store(in: &subscriptions)
} }

View File

@ -348,14 +348,14 @@ extension ProfileManager {
extension ProfileManager { extension ProfileManager {
public func observeUpdates() { public func observeUpdates() {
$internalActiveProfileId $internalActiveProfileId
.sink { .sink { [weak self] in
self.didUpdateActiveProfile.send($0) self?.didUpdateActiveProfile.send($0)
}.store(in: &cancellables) }.store(in: &cancellables)
profileRepository.willUpdateProfiles() profileRepository.willUpdateProfiles()
.dropFirst() .dropFirst()
.sink { .sink { [weak self] in
self.willUpdateProfiles($0) self?.willUpdateProfiles($0)
}.store(in: &cancellables) }.store(in: &cancellables)
} }

View File

@ -156,19 +156,19 @@ extension VPNManager {
profileManager.didUpdateActiveProfile profileManager.didUpdateActiveProfile
.dropFirst() .dropFirst()
.removeDuplicates() .removeDuplicates()
.sink { newId in .sink { [weak self] newId in
Task { Task {
await self.willUpdateActiveId(newId) await self?.willUpdateActiveId(newId)
} }
}.store(in: &cancellables) }.store(in: &cancellables)
profileManager.currentProfile.$value profileManager.currentProfile.$value
.dropFirst() .dropFirst()
.removeDuplicates() .removeDuplicates()
.sink { newProfile in .sink { [weak self] newProfile in
Task { Task {
do { do {
try await self.willUpdateCurrentProfile(newProfile) try await self?.willUpdateCurrentProfile(newProfile)
} catch { } catch {
pp_log.error("Unable to apply profile update: \(error)") pp_log.error("Unable to apply profile update: \(error)")
} }

View File

@ -126,9 +126,9 @@ extension TunnelKitVPNManagerStrategy {
vpnState vpnState
.removeDuplicates() .removeDuplicates()
.receive(on: DispatchQueue.main) .receive(on: DispatchQueue.main)
.sink { .sink { [weak self] in
self.currentState?.isEnabled = $0.isEnabled self?.currentState?.isEnabled = $0.isEnabled
self.currentState?.vpnStatus = $0.vpnStatus self?.currentState?.vpnStatus = $0.vpnStatus
}.store(in: &cancellables) }.store(in: &cancellables)
} }
@ -285,8 +285,8 @@ private extension TunnelKitVPNManagerStrategy {
} }
dataCountTimer = Timer.TimerPublisher(interval: dataCountInterval, runLoop: .main, mode: .common) dataCountTimer = Timer.TimerPublisher(interval: dataCountInterval, runLoop: .main, mode: .common)
.autoconnect() .autoconnect()
.sink { .sink { [weak self] in
self.onDataCount($0) self?.onDataCount($0)
} }
} }