I learn how to make a reactive function, variable, and custom UI with closure for action. And I don't want to use "disposableBag". So I make this library from 2015. I have known my library not good. But I learn a lot about reactive programming.
- Promise: Function response in queue with operators
- Variable: Variable reactive when it changed
- UI Reactive: Button, Switch, Custome by Promise
- Object Cache Reactive (Store, Pool): Cache object from request and make reactive change to update UI
- Promise Await: wait multi response of Promise function
- throttle
- debounce
- filter
- distinct
- map
- flatMap
- Button
- Switch
- TextField
- TextView
- View Guesture
- View Constraint
- View properties
- ...
- promise.resolve()
let promies: Promise<String?> = Promise<String?>()
promies
.map { $0 }
.throttle(interval: 500)
.debounce(interval: 200)
.filter { ($0?.contains("3") ?? false) }
.distinct()
.observe { (result) in
guard case let .success(vax) = result else { return }
print("result success:", vax)
}
promies.resolve(nil)
promies.resolve("334")
promies.resolve("22")
promies.resolve("44")
promies.resolve("32")
func add(a: Int, b: Int) -> Future<Int> {
let promise = Promise<Int>()
promise.resolve(a + b)
return promise
}
do {
let add1: Int = try await { self.add(a: 8, b: 9) }
print("ober1:", add1)
let add2: Int = try await { self.add(a: 5, b: 15) }
print("ober2:", add2)
print("add1 + add2:", add1 + add2)
} catch let error {
print("error:", error)
}
// Result:
// ober1: 17
// ober2: 20
// add1 + add2: 37
let variable: Variable<Int> = Variable<Int>(0)
variable
.map { $0 + 1212 }
.throttle(interval: 500)
.debounce(interval: 200)
.filter {$0 > 10}
.observe { (result) in
guard case let .success(vax) = result else { return }
print("result success:", vax)
}
DispatchQueue.global(qos: .background).async {
variable.value = 7
usleep(100 * 1000)
variable.value = 2
usleep(100 * 1000)
variable.value = 3
usleep(100 * 1000)
variable.value = 4
usleep(300 * 1000) // waiting a bit longer than the interval
variable.value = 5
usleep(100 * 1000)
variable.value = 6
usleep(100 * 1000)
variable.value = 7
usleep(300 * 1000) // waiting a bit longer than the interval
variable.value = 8
usleep(100 * 1000)
variable.value = 9
usleep(100 * 1000)
variable.value = 10
usleep(100 * 1000)
variable.value = 11
usleep(100 * 1000)
variable.value = 12
}
class ViewController: UIViewController {
let button: Button = {
let button = Button()
button.frame = CGRect(x: 100, y: 100, width: 100, height: 50)
button.setTitle("A", for: .normal)
button.backgroundColor = .red
return button
}()
override func loadView() {
super.loadView()
self.view.backgroundColor = .white
self.view.addSubview(button)
button
.action()
.debounce(interval: 200)
.observe { [weak self] (event) in
guard let `self` = self else { return}
let vc = AViewController()
vc.variable = self.variable
self.navigationController?.pushViewController(vc, animated: true)
}
}
}
- map, flatMap and compactMap
- Under the hood of Futures and Promises in Swift
- Promises by Google
- RxSwift
- Promise Javascript
- Email: [email protected]
- Site: https://onebuffer.com
- Linkedin: https://www.linkedin.com/in/caophuocthanh/