我正在尝试创建日期调度程序以观察某些事件。但这行不通。我已经浏览过
协议DateScheduler
据说,操作将通过某些方法协议DateScheduler 在 currentDate 发生。我尝试在10秒后执行此操作。以下是我的自定义时间表的示例。
var lifeA = 500
var lifeB = 250
var powerA = 2
var powerB = 4
var increasepowerA = Math.floor(Math.random() * 5)
var increasepowerB = Math.floor(Math.random() * 10)
var chanceattack = 0.5
var x = 0
function getx(){
const z = Math.random()
if (z <= chanceattack){
x = 1
}else{x = 0}
}
getx()
function execute(){
if(x = 1){attackA()
}else{attackB()}
}
function attackB(){
lifeA -= powerB;
powerB += increasepowerB;
chanceattack += 0.075;
if (lifeA <= 0){
console.log("A has dead")
}else{getx(); execute()}
}
function attackA(){
lifeB -= powerA;
powerA += increasepowerA;
chanceattack -= 0.075;
if (lifeB <= 0){
console.log("B has dead")
}else{getx(); execute()}
}
execute()
}
然后创建绑定以观察事件
class SomeDateScheduler : DateScheduler {
var currentDate: Date
init() {
self.currentDate = Date(timeIntervalSinceNow: 10)
}
func schedule(after date: Date, action: @escaping () -> Void) -> Disposable? {
print(#function)
print(date)
return nil
}
func schedule(after date: Date, interval: DispatchTimeInterval, leeway: DispatchTimeInterval, action: @escaping () -> Void) -> Disposable? {
print(#function)
print(date)
print(interval)
print(leeway)
return nil
}
func schedule(_ action: @escaping () -> Void) -> Disposable? {
print(#function)
return nil
}
我正在将其导入 AppDelegate 。 ObserverSchedular 和 signalSchedular 是全局属性。请向我解释如何调用所有方法 DateScheduler 。
答案 0 :(得分:0)
我已经知道我在哪里犯错了。要解决此问题,我接下来要做:
已创建绑定以在didFinishLaunchingWithOptions
中进行观察
reactive.makeBindingTarget { appDeleg, value in
print("SUCCESS")
print(value)
} <~ signalSchedular.customObserve(on: schedular, interval: 10, leeway: 1)
private let (signalSchedular, observerSchedular) = Signal<String, Never>.pipe(),
let schedular = SomeDateScheduler()