๐
This file contains hidden or 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
private func setupAVSession() { | |
captureSession.beginConfiguration() | |
captureSession.sessionPreset = .high | |
defer { | |
captureSession.commitConfiguration() | |
} | |
// input | |
guard |
This file contains hidden or 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
let signal = PullSignal<String> { completion in | |
// There should be some long running operation here | |
completion(Result.value(value: "test")) | |
} | |
signal.map { value in | |
value.count | |
}.start { event in | |
if case let .value(value) = event { | |
print(value) |
This file contains hidden or 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
public struct PullSignal<T> { | |
let operation: ((Result<T>) -> Void) -> Void | |
public init(operation: @escaping ((Result<T>) -> Void) -> Void) { | |
self.operation = operation | |
} | |
public func start(completion: (Result<T>) -> Void) { | |
operation() { event in | |
completion(event) |
This file contains hidden or 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
let signal = PushSignal<String>() | |
_ = signal.map { value in | |
return value.count | |
}.subscribe { event in | |
if case let .value(value) = event { | |
print(value) | |
} else { | |
print("error") | |
} |
This file contains hidden or 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
public final class PushSignal<T> { | |
var event: Result<T>? | |
var callbacks: [(Result<T>) -> Void] = [] | |
let lockQueue = DispatchQueue(label: "Serial Queue") | |
func notify() { | |
guard let event = event else { | |
return | |
} |
This file contains hidden or 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
enum Result<T> { | |
case value(value: T) | |
case failure(error: Error) | |
// Sync | |
public func map<U>(f: (T) -> U) -> Result<U> { | |
switch self { | |
case let .value(value): | |
return .value(value: f(value)) | |
case let .failure(error): |
This file contains hidden or 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
class MyActivity : AppCompatActivity() { | |
override fun onCreate(savedInstanceState: Bundle?, persistentState: PersistableBundle?) { | |
super.onCreate(savedInstanceState, persistentState) | |
val model = ViewModelProviders.of(this).get(MyViewModel::class.java) | |
model.getUsers().observe(this, { users -> | |
// update UI | |
}) | |
} | |
} |
This file contains hidden or 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
// Sync | |
func sum(a: Int, b: Int) -> Int { | |
return a + b | |
} | |
// Async | |
func sum(a: Int, b: Int, completion: Int -> Void) { | |
// Assumed it is a very long task to get the result | |
let result = a + b |
This file contains hidden or 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
class ProfileViewController: BaseViewController<ProfileView> { | |
let viewModel: ProfileViewModelType | |
init(viewModel: ProfileViewModelType) { | |
self.viewModel = viewModel | |
} | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
// Input | |
viewModel.input.fetch.onNext(()) | |
// Output |
This file contains hidden or 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
class ViewModel { | |
class Input { | |
let fetch = PublishSubject<()>() | |
} | |
class Output { | |
let friends: Driver<[User]> | |
} | |
let apiClient: APIClient | |
let input: Input | |
let output: Output |