This repository has been archived by the owner on Jan 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #15 from UrbanCompass/just
Add Just stuff
- Loading branch information
Showing
3 changed files
with
60 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// Copyright © 2016 Compass. All rights reserved. | ||
|
||
import Foundation | ||
|
||
public class Just<T>: Observable<T> { | ||
private let value: T | ||
|
||
public init(_ value: T) { | ||
self.value = value | ||
super.init() | ||
} | ||
|
||
public override func subscribe(_ handler: @escaping (Event<T>) -> Void) { | ||
handler(.next(value)) | ||
handler(.done) | ||
} | ||
|
||
public override func subscribe(onNext: ((T) -> Void)? = nil, onError: ((Error) -> Void)? = nil, onDone: (() -> Void)? = nil) { | ||
onNext?(value) | ||
onDone?() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// Copyright © 2016 Compass. All rights reserved. | ||
|
||
import Foundation | ||
import XCTest | ||
@testable import Snail | ||
|
||
class JustTests: XCTestCase { | ||
func testJustOnNext() { | ||
var result: Int? | ||
let subject = Just(1) | ||
subject.subscribe( | ||
onNext: { value in result = value} | ||
) | ||
XCTAssertEqual(1, result) | ||
} | ||
|
||
func testJustEvent() { | ||
var result: Int? | ||
let subject = Just(1) | ||
subject.subscribe { event in | ||
if case .next(let value) = event { | ||
result = value | ||
} | ||
} | ||
XCTAssertEqual(1, result) | ||
} | ||
} |