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 #107 from UrbanCompass/skip-first
Browse files Browse the repository at this point in the history
Observable Skip First
  • Loading branch information
russellbstephens authored Nov 21, 2018
2 parents 2bab48a + 7bf6b90 commit 1b43c93
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
17 changes: 17 additions & 0 deletions Snail/Observable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,23 @@ public class Observable<T> : ObservableType {
return observable
}

public func skip(first: UInt) -> Observable<T> {
let observable = Observable<T>()
var count = first

subscribe(onNext: {
if count == 0 {
observable.on(.next($0))
}
count = UInt(max(Int(count) - 1, 0))
}, onError: {
observable.on(.error($0))
}, onDone: {
observable.on(.done)
})
return observable
}

func notify(subscriber: Subscriber<T>, event: Event<T>) {
guard let queue = subscriber.queue else {
subscriber.handler(event)
Expand Down
16 changes: 16 additions & 0 deletions SnailTests/ObservableTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -238,4 +238,20 @@ class ObservableTests: XCTestCase {
XCTAssert(received.first == "3")
}
}

func testSkipFirst() {
let observable = Observable<String>()
var received: [String] = []

observable.skip(first: 2).subscribe(onNext: { string in
received.append(string)

XCTAssert(received.count == 1)
XCTAssert(received.first == "3")
})

observable.on(.next("1"))
observable.on(.next("2"))
observable.on(.next("3"))
}
}

0 comments on commit 1b43c93

Please sign in to comment.