Happa 是一个库,用于控制何时以封闭作用域的上下文执行函数。
Happa 通过 CocoaPods 提供。
将以下内容添加到 Podfile
platform :ios, '8.0'
use_frameworks!
pod "Happa"
然后,运行命令
$ pod install
这是一个简单的用法。有关更复杂的示例,请参阅 示例
import Happa
let context = Happa<Int>(
state: 2,
cond: { (current: Int) -> Bool in
return current <= 0
},
fire: { (current: Int) -> Void in
print("current value: \(current)")
}
)
asyncFunc1(completion: {
// not fire
context.update { (current: Int) -> Int in
return current - 1
}
})
asyncFunc2(completion: {
// fire
context.update { (current: Int) -> Int in
return current - 1
}
})
asyncFunc3(completion: {
// more fire
context.update { (current: Int) -> Int in
return current - 1
}
})
Happa 在 MIT 许可证下可用。有关更多信息,请参阅 LICENSE 文件。