Skip to content

Commit f46a54d

Browse files
committed
Replaces share with default implementation of share(replay:scope:).
1 parent fd947cb commit f46a54d

File tree

4 files changed

+73
-31
lines changed

4 files changed

+73
-31
lines changed

RxSwift/Observables/Multicast.swift

-16
Original file line numberDiff line numberDiff line change
@@ -110,22 +110,6 @@ extension ConnectableObservableType {
110110
}
111111
}
112112

113-
extension ObservableType {
114-
115-
/**
116-
Returns an observable sequence that shares a single subscription to the underlying sequence.
117-
118-
This operator is a specialization of publish which creates a subscription when the number of observers goes from zero to one, then shares that subscription with all subsequent observers until the number of observers returns to zero, at which point the subscription is disposed.
119-
120-
- seealso: [share operator on reactivex.io](http://reactivex.io/documentation/operators/refcount.html)
121-
122-
- returns: An observable sequence that contains the elements of a sequence produced by multicasting the source sequence.
123-
*/
124-
public func share() -> Observable<E> {
125-
return self.publish().refCount()
126-
}
127-
}
128-
129113
extension ObservableType {
130114

131115
/**

RxSwift/Observables/ShareReplayScope.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ extension ObservableType {
138138

139139
- returns: An observable sequence that contains the elements of a sequence produced by multicasting the source sequence.
140140
*/
141-
public func share(replay: Int = 0, scope: SubjectLifetimeScope)
141+
public func share(replay: Int = 0, scope: SubjectLifetimeScope = .whileConnected)
142142
-> Observable<E> {
143143
switch scope {
144144
case .forever:

Sources/AllTestz/main.swift

+7-6
Original file line numberDiff line numberDiff line change
@@ -1217,12 +1217,13 @@ final class ObservableShareReplayScopeTests_ : ObservableShareReplayScopeTests,
12171217
#endif
12181218

12191219
static var allTests: [(String, (ObservableShareReplayScopeTests_) -> () -> ())] { return [
1220-
("testReplay_forever_receivesCorrectElements", ObservableShareReplayScopeTests.testReplay_forever_receivesCorrectElements),
1221-
("testReplay_whileConnected_receivesCorrectElements", ObservableShareReplayScopeTests.testReplay_whileConnected_receivesCorrectElements),
1222-
("testReplay_forever_error", ObservableShareReplayScopeTests.testReplay_forever_error),
1223-
("testReplay_whileConnected_error", ObservableShareReplayScopeTests.testReplay_whileConnected_error),
1224-
("testReplay_forever_completed", ObservableShareReplayScopeTests.testReplay_forever_completed),
1225-
("testReplay_whileConnected_completed", ObservableShareReplayScopeTests.testReplay_whileConnected_completed),
1220+
("test_testDefaultArguments", ObservableShareReplayScopeTests.test_testDefaultArguments),
1221+
("test_forever_receivesCorrectElements", ObservableShareReplayScopeTests.test_forever_receivesCorrectElements),
1222+
("test_whileConnected_receivesCorrectElements", ObservableShareReplayScopeTests.test_whileConnected_receivesCorrectElements),
1223+
("test_forever_error", ObservableShareReplayScopeTests.test_forever_error),
1224+
("test_whileConnected_error", ObservableShareReplayScopeTests.test_whileConnected_error),
1225+
("test_forever_completed", ObservableShareReplayScopeTests.test_forever_completed),
1226+
("test_whileConnected_completed", ObservableShareReplayScopeTests.test_whileConnected_completed),
12261227
] }
12271228
}
12281229

Tests/RxSwiftTests/Observable+ShareReplayScopeTests.swift

+65-8
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,64 @@ class ObservableShareReplayScopeTests : RxTest {
1414
}
1515

1616
extension ObservableShareReplayScopeTests {
17-
func testReplay_forever_receivesCorrectElements() {
17+
func test_testDefaultArguments() {
18+
let scheduler = TestScheduler(initialClock: 0)
19+
20+
let xs = scheduler.createHotObservable([
21+
next(210, 1),
22+
next(220, 2),
23+
next(230, 3),
24+
next(240, 4),
25+
next(250, 5),
26+
next(320, 6),
27+
next(550, 7)
28+
])
29+
30+
var subscription1: Disposable! = nil
31+
var subscription2: Disposable! = nil
32+
var subscription3: Disposable! = nil
33+
34+
let res1 = scheduler.createObserver(Int.self)
35+
let res2 = scheduler.createObserver(Int.self)
36+
let res3 = scheduler.createObserver(Int.self)
37+
38+
var ys: Observable<Int>! = nil
39+
40+
scheduler.scheduleAt(Defaults.created) { ys = xs.share() }
41+
42+
scheduler.scheduleAt(200) { subscription1 = ys.subscribe(res1) }
43+
scheduler.scheduleAt(300) { subscription2 = ys.subscribe(res2) }
44+
45+
scheduler.scheduleAt(350) { subscription1.dispose() }
46+
scheduler.scheduleAt(400) { subscription2.dispose() }
47+
48+
scheduler.scheduleAt(500) { subscription3 = ys.subscribe(res3) }
49+
scheduler.scheduleAt(600) { subscription3.dispose() }
50+
51+
scheduler.start()
52+
53+
XCTAssertEqual(res1.events, [
54+
next(210, 1),
55+
next(220, 2),
56+
next(230, 3),
57+
next(240, 4),
58+
next(250, 5),
59+
next(320, 6)
60+
])
61+
62+
let replayedEvents2 = (0 ..< 0).map { next(300, 6 - 0 + $0) }
63+
64+
XCTAssertEqual(res2.events, replayedEvents2 + [next(320, 6)])
65+
XCTAssertEqual(res3.events, [next(550, 7)])
66+
67+
XCTAssertEqual(xs.subscriptions, [
68+
Subscription(200, 400),
69+
Subscription(500, 600)
70+
])
71+
}
72+
73+
74+
func test_forever_receivesCorrectElements() {
1875
for i in 0 ..< 5 {
1976
let scheduler = TestScheduler(initialClock: 0)
2077

@@ -73,7 +130,7 @@ extension ObservableShareReplayScopeTests {
73130
}
74131
}
75132

76-
func testReplay_whileConnected_receivesCorrectElements() {
133+
func test_whileConnected_receivesCorrectElements() {
77134
for i in 0 ..< 5 {
78135
let scheduler = TestScheduler(initialClock: 0)
79136

@@ -131,7 +188,7 @@ extension ObservableShareReplayScopeTests {
131188
}
132189
}
133190

134-
func testReplay_forever_error() {
191+
func test_forever_error() {
135192
for i in 0 ..< 5 {
136193
let scheduler = TestScheduler(initialClock: 0)
137194

@@ -219,7 +276,7 @@ extension ObservableShareReplayScopeTests {
219276
}
220277
}
221278

222-
func testReplay_whileConnected_error() {
279+
func test_whileConnected_error() {
223280
for i in 0 ..< 5 {
224281
let scheduler = TestScheduler(initialClock: 0)
225282

@@ -304,7 +361,7 @@ extension ObservableShareReplayScopeTests {
304361
}
305362
}
306363

307-
func testReplay_forever_completed() {
364+
func test_forever_completed() {
308365
for i in 0 ..< 5 {
309366
let scheduler = TestScheduler(initialClock: 0)
310367

@@ -392,7 +449,7 @@ extension ObservableShareReplayScopeTests {
392449
}
393450
}
394451

395-
func testReplay_whileConnected_completed() {
452+
func test_whileConnected_completed() {
396453
for i in 0 ..< 5 {
397454
let scheduler = TestScheduler(initialClock: 0)
398455

@@ -478,14 +535,14 @@ extension ObservableShareReplayScopeTests {
478535
}
479536

480537
#if TRACE_RESOURCES
481-
func testShareReplayScopeReleasesResourcesOnComplete() {
538+
func testReleasesResourcesOnComplete() {
482539
for i in 0 ..< 5 {
483540
_ = Observable<Int>.just(1).share(replay: i, scope: .forever).subscribe()
484541
_ = Observable<Int>.just(1).share(replay: i, scope: .whileConnected).subscribe()
485542
}
486543
}
487544

488-
func testShareReplayScopeReleasesResourcesOnError() {
545+
func testReleasesResourcesOnError() {
489546
for i in 0 ..< 5 {
490547
_ = Observable<Int>.error(testError).share(replay: i, scope: .forever).subscribe()
491548
_ = Observable<Int>.error(testError).share(replay: i, scope: .whileConnected).subscribe()

0 commit comments

Comments
 (0)