Reflectly 1.0

Reflectly 1.0

CaoPhuocThanh 维护。



  • Cao Phuoc Thanh

Swift Reflectly

我学习了如何使用闭包创建响应式函数、变量和自定义 UI。我不想使用 "disposableBag"。因此,我从 2015 年开始制作这个库。我知道我的库并不完美,但我学到了很多关于响应式编程的知识。

响应式

  1. Promise:队列中带有操作符的函数响应
  2. 变量:变量在变化时响应式
  3. UI 响应式:通过 Promise 创建的按钮、开关、自定义
  4. 对象缓存响应式(存储、池):从请求中缓存对象,并使 UI 响应式变化以更新 UI

操作符

  • 节流
  • 防抖
  • 过滤
  • 去重
  • 映射

Promise

        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")

变量

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
        }

UI 反应

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)
        }
    }
}

参考

  1. map, flatMap 和 compactMap
  2. Swift中“Future”和“Promise”的内部机制
  3. Google的“Promise”
  4. RxSwift
  5. JavaScript的“Promise”

联系