Skip to content

Commit

Permalink
Merge pull request #154 from zulkis/replay-subject-sendNext-bug-with-…
Browse files Browse the repository at this point in the history
…values-array-clean-up

Update order of invocation in -sendNext: RACReplaySubject
  • Loading branch information
mdiep authored Mar 28, 2019
2 parents a621018 + 35aa8e4 commit b62aa6f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
3 changes: 2 additions & 1 deletion ReactiveObjC/RACReplaySubject.m
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,12 @@ - (RACDisposable *)subscribe:(id<RACSubscriber>)subscriber {
- (void)sendNext:(id)value {
@synchronized (self) {
[self.valuesReceived addObject:value ?: RACTupleNil.tupleNil];
[super sendNext:value];

if (self.capacity != RACReplaySubjectUnlimitedCapacity && self.valuesReceived.count > self.capacity) {
[self.valuesReceived removeObjectsInRange:NSMakeRange(0, self.valuesReceived.count - self.capacity)];
}

[super sendNext:value];
}
}

Expand Down
15 changes: 15 additions & 0 deletions ReactiveObjCTests/RACSubjectSpec.m
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,21 @@ - (void)didSubscribeWithDisposable:(RACCompoundDisposable *)disposable {
qck_beforeEach(^{
subject = [RACReplaySubject replaySubjectWithCapacity:1];
});

qck_it(@"should send same latest value to multiple subscribers in another subscription", ^{
__block NSMutableArray *values = [NSMutableArray array];

[subject subscribeNext:^(id _Nullable item1) {
[values addObject:item1];
[[subject take:1] subscribeNext:^(id _Nullable item2) {
[values addObject:item2];
}];
}];
[subject sendNext:@1];
[subject sendNext:@2];

expect(values).to(equal(@[@1, @1, @2, @2]));
});

qck_it(@"should send the last value", ^{
id firstValue = @"blah";
Expand Down

0 comments on commit b62aa6f

Please sign in to comment.