Skip to content

Commit 9b946fa

Browse files
authored
Introduce Lifetime.Token.dispose. (#641)
1 parent 9889269 commit 9b946fa

File tree

3 files changed

+25
-4
lines changed

3 files changed

+25
-4
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
*Please add new entries at the top.*
44

5+
1. `Lifetime` may now be manually ended using `Lifetime.Token.dispose()`, in addition to the existing when-token-deinitializes semantic. (#641, kudos to @andersio)
56
1. For Swift 4.1 and above, `BindingSource` conformances are required to have `Error` parameterized as exactly `NoError`. As a result, `Signal` and `SignalProducer` are now conditionally `BindingSource`. (#590, kudos to @NachoSoto and @andersio)
67
1. For Swift 4.1 and above, `Signal.Event` and `ActionError` are now conditionally `Equatable`. (#590, kudos to @NachoSoto and @andersio)
78
1. New method `collect(every:on:skipEmpty:discardWhenCompleted:)` which delivers all values that occurred during a time interval (#619, kudos to @Qata)

Sources/Lifetime.swift

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,8 @@ extension Lifetime {
8686
}
8787

8888
extension Lifetime {
89-
/// A token object which completes its signal when it deinitializes.
89+
/// A token object which completes its associated `Lifetime` when
90+
/// it deinitializes, or when `dispose()` is called.
9091
///
9192
/// It is generally used in conjuncion with `Lifetime` as a private
9293
/// deinitialization trigger.
@@ -97,15 +98,18 @@ extension Lifetime {
9798
/// }
9899
/// ```
99100
public final class Token {
100-
/// A signal that sends a Completed event when the lifetime ends.
101101
fileprivate let disposables: CompositeDisposable
102102

103103
public init() {
104104
disposables = CompositeDisposable()
105105
}
106106

107-
deinit {
107+
public func dispose() {
108108
disposables.dispose()
109109
}
110+
111+
deinit {
112+
dispose()
113+
}
110114
}
111115
}

Tests/ReactiveSwiftTests/LifetimeSpec.swift

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import Result
66
final class LifetimeSpec: QuickSpec {
77
override func spec() {
88
describe("Lifetime") {
9-
it("should complete its lifetime ended signal when the it deinitializes") {
9+
it("should complete its lifetime ended signal when the token deinitializes") {
1010
let object = MutableReference(TestObject())
1111

1212
var isCompleted = false
@@ -18,6 +18,18 @@ final class LifetimeSpec: QuickSpec {
1818
expect(isCompleted) == true
1919
}
2020

21+
it("should complete its lifetime ended signal when the token is disposed of") {
22+
let object = MutableReference(TestObject())
23+
24+
var isCompleted = false
25+
26+
object.value!.lifetime.ended.observeCompleted { isCompleted = true }
27+
expect(isCompleted) == false
28+
29+
object.value!.disposeToken()
30+
expect(isCompleted) == true
31+
}
32+
2133
it("should complete its lifetime ended signal even if the lifetime object is being retained") {
2234
let object = MutableReference(TestObject())
2335
let lifetime = object.value!.lifetime
@@ -91,4 +103,8 @@ internal final class MutableReference<Value: AnyObject> {
91103
internal final class TestObject {
92104
private let token = Lifetime.Token()
93105
var lifetime: Lifetime { return Lifetime(token) }
106+
107+
func disposeToken() {
108+
token.dispose()
109+
}
94110
}

0 commit comments

Comments
 (0)