godot-start/zfoo/scheduler/SchedulerBus.gd

50 lines
1.5 KiB
GDScript
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

extends Object
const Runnable = preload("res://zfoo/common/Runnable.gd")
const SchedulerDefinition = preload("res://zfoo/scheduler/SchedulerDefinition.gd")
const TimeUtils = preload("res://zfoo/scheduler/TimeUtils.gd")
const ArrayUtils = preload("res://zfoo/util/ArrayUtils.gd")
const CollectionUtils = preload("res://zfoo/util/CollectionUtils.gd")
const schedulerMap: Dictionary = {}
# 固定延迟执行的任务delay默认为毫秒
static func schedule(runnable: Runnable, delay: int) -> void:
var triggerTimestamp = TimeUtils.currentTimeMillis() + delay
var definition = SchedulerDefinition.new(runnable, delay, triggerTimestamp, false)
schedulerMap[definition] = null
# 不断执行的周期循环任务delay默认为毫秒
static func scheduleAtFixRate(runnable: Runnable, delay: int) -> void:
var triggerTimestamp = TimeUtils.currentTimeMillis() + delay
var definition = SchedulerDefinition.new(runnable, delay, triggerTimestamp, false)
schedulerMap[definition] = null
static func triggerPerSecond() -> void:
var timestamp = TimeUtils.currentTimeMillis()
if CollectionUtils.isEmpty(schedulerMap):
return
var deleteSchedulers = []
for scheduler in schedulerMap.keys():
if timestamp < scheduler.triggerTimestamp:
continue
if scheduler.repteated:
scheduler.triggerTimestamp += timestamp
else:
deleteSchedulers.append(scheduler)
scheduler.runnable.run()
if ArrayUtils.isEmpty(deleteSchedulers):
return
for scheduler in deleteSchedulers:
schedulerMap.erase(scheduler)
scheduler.runnable.free()
scheduler.free()