Skip to content
This repository has been archived by the owner on Jan 24, 2023. It is now read-only.

Commit

Permalink
Merge pull request #92 from 0x107/unique-notifiesInitialOnSubscribe
Browse files Browse the repository at this point in the history
Fix notification for initial value on new subscriptions
  • Loading branch information
wesbillman authored Aug 7, 2018
2 parents 6273bae + a678d5e commit 4ea6047
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
5 changes: 5 additions & 0 deletions Snail/Unique.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,9 @@ public class Unique<T: Equatable>: Variable<T> {
subject.on(.next(newValue))
}
}

public override init(_ value: T) {
super.init(value)
subject.on(.next(value))
}
}
23 changes: 13 additions & 10 deletions SnailTests/UniqueTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@ class UniqueTests: XCTestCase {
subject.value = "2"
subject.value = "2"
subject.value = "2"
XCTAssert(events[0] == "1")
XCTAssert(events[1] == "2")
XCTAssert(events[0] == nil)
XCTAssert(events[1] == "1")
XCTAssert(events[2] == "2")
XCTAssert(subject.value == "2")
XCTAssert(events.count == 2)
XCTAssert(events.count == 3)
}

func testVariableNotifiesOnSubscribe() {
Expand All @@ -35,14 +36,14 @@ class UniqueTests: XCTestCase {
}

func testVariableNotifiesInitialOnSubscribe() {
let subject = Unique<String?>(nil)
let subject = Unique<String?>("initial")
var result: String?

subject.asObservable().subscribe(onNext: { string in
result = string
})

XCTAssertEqual(nil, result)
XCTAssertEqual("initial", result)
}

func testVariableHandlesEquatableArrays() {
Expand All @@ -55,8 +56,9 @@ class UniqueTests: XCTestCase {
subject.value = ["2", "1"]
subject.value = ["2", "1"]
subject.value = ["1", "2"]
XCTAssert(events[0] == ["2", "1"])
XCTAssert(events[1] == ["1", "2"])
XCTAssert(events[0] == ["1", "2"])
XCTAssert(events[1] == ["2", "1"])
XCTAssert(events[2] == ["1", "2"])
}

func testVariableHandlesOptionalArrays() {
Expand All @@ -69,8 +71,9 @@ class UniqueTests: XCTestCase {
subject.value = nil
subject.value = nil
subject.value = ["1", "2"]
XCTAssert(events[0] == ["1", "2"])
XCTAssert(events[1] == nil)
XCTAssert(events[2] == ["1", "2"])
XCTAssert(events[0] == nil)
XCTAssert(events[1] == ["1", "2"])
XCTAssert(events[2] == nil)
XCTAssert(events[3] == ["1", "2"])
}
}

0 comments on commit 4ea6047

Please sign in to comment.