From 5b6d3f38421f0a39f405a5acb895430b3cd58df5 Mon Sep 17 00:00:00 2001 From: Eric Rabil Date: Fri, 10 Sep 2021 20:25:08 -0400 Subject: [PATCH] Invalidate publisher promise on first fire --- Sources/Pwomise/Extensions/Publisher+Promise.swift | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/Sources/Pwomise/Extensions/Publisher+Promise.swift b/Sources/Pwomise/Extensions/Publisher+Promise.swift index efea494..e38db50 100644 --- a/Sources/Pwomise/Extensions/Publisher+Promise.swift +++ b/Sources/Pwomise/Extensions/Publisher+Promise.swift @@ -18,16 +18,22 @@ public extension Publisher { var promise: Promise { let promise = Promise() - sink(receiveCompletion: { + var cancellable: AnyCancellable? + + cancellable = sink(receiveCompletion: { switch $0 { case .failure(let err): promise.result = .resolved(.failure(err)) + cancellable?.cancel() + cancellable = nil default: break } }, receiveValue: { value in promise.result = .resolved(.success(value)) - }).store(in: &cancellables) + cancellable?.cancel() + cancellable = nil + }) return promise }