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
.removeDuplicates()
.receive(on: DispatchQueue.main)
.sink {
.sink { [weak self] in
if $0 == .connected {
pp_log.info("VPN successful connection, report to Reviewer")
self.reviewer.reportEvent()
self?.reviewer.reportEvent()
}
}.store(in: &cancellables)
productManager.didRefundProducts
.receive(on: DispatchQueue.main)
.sink {
.sink { [weak self] in
Task {
pp_log.info("Refunds detected, uninstalling VPN profile")
await self.coreContext.vpnManager.uninstall()
await self?.coreContext.vpnManager.uninstall()
}
}.store(in: &cancellables)
}

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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