Skip to content

Commit d5b1484

Browse files
authored
Merge pull request ReactiveCocoa#28 from Lightricks/feature/finally-on-disposal
RACSignal+Operations: call -finally: block on disposal.
2 parents afc23f6 + 45f673b commit d5b1484

File tree

3 files changed

+19
-8
lines changed

3 files changed

+19
-8
lines changed

ReactiveObjC/RACSignal+Operations.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ extern const NSInteger RACSignalErrorNoMatchingCase;
125125
/// of the receiver.
126126
- (RACSignal<ValueType> *)initially:(void (^)(void))block RAC_WARN_UNUSED_RESULT;
127127

128-
/// Executes the given block when the signal completes or errors.
128+
/// Executes the given block when the signal completes, errs or the subscription is disposed.
129129
- (RACSignal<ValueType> *)finally:(void (^)(void))block RAC_WARN_UNUSED_RESULT;
130130

131131
/// Divides the receiver's `next`s into buffers which deliver every `interval`

ReactiveObjC/RACSignal+Operations.m

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -313,14 +313,20 @@ - (RACSignal *)initially:(void (^)(void))block {
313313
- (RACSignal *)finally:(void (^)(void))block {
314314
NSCParameterAssert(block != NULL);
315315

316-
return [[[self
317-
doError:^(NSError *error) {
318-
block();
319-
}]
320-
doCompleted:^{
316+
return [[RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
317+
RACDisposable *selfDisposable = [self subscribeNext:^(id x) {
318+
[subscriber sendNext:x];
319+
} error:^(NSError *error) {
320+
[subscriber sendError:error];
321+
} completed:^{
322+
[subscriber sendCompleted];
323+
}];
324+
325+
return [RACDisposable disposableWithBlock:^{
321326
block();
322-
}]
323-
setNameWithFormat:@"[%@] -finally:", self.name];
327+
[selfDisposable dispose];
328+
}];
329+
}] setNameWithFormat:@"[%@] -finally:", self.name];
324330
}
325331

326332
- (RACSignal *)bufferWithTime:(NSTimeInterval)interval onScheduler:(RACScheduler *)scheduler {

ReactiveObjCTests/RACSignalSpec.m

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3762,6 +3762,11 @@ + (void)configure:(Configuration *)configuration {
37623762
[subject sendError:nil];
37633763
expect(@(finallyInvoked)).to(beTruthy());
37643764
});
3765+
3766+
qck_it(@"should run finally upon disposal", ^{
3767+
[disposable dispose];
3768+
expect(@(finallyInvoked)).to(beTruthy());
3769+
});
37653770
});
37663771
});
37673772

0 commit comments

Comments
 (0)