Skip to content
Snippets Groups Projects
Commit 7d775f99 authored by pjs's avatar pjs
Browse files

refactored cancellation check into private method for readability

parent 5d4aa1ac
No related branches found
No related tags found
No related merge requests found
......@@ -83,25 +83,7 @@ module SimpleKit
@model_time = 0.0
@user_model.init
while (current_event = @event_list.pop)
e = current_event.event
if @cancel_set.key? e
if @cancel_set[e] === nil
@cancel_set[e].delete nil
@cancel_set.delete e if @cancel_set[e].empty?
next
else
arg_match = false
for h in @cancel_set[e] do
if current_event.args >= h
arg_match = true
@cancel_set[e].delete h
@cancel_set.delete e if @cancel_set[e].empty?
break
end
end
next if arg_match
end
end
next if should_cancel?(current_event)
@model_time = current_event.time
if current_event.args.empty?
@user_model.send(current_event.event)
......@@ -116,27 +98,46 @@ module SimpleKit
def halt
@event_list.clear
end
end
private
private
# This is a private helper class for the EventScheduler class.
# Users should never try to access this directly.
class EventNotice
attr_reader :event, :time, :time_stamp, :priority, :args
def initialize(event, time, delay, args)
@event = event
@time_stamp = time
@time = time + delay
@args = args
@priority = (@args && @args.key?(:priority)) ? @args.delete(:priority) : 10
def should_cancel?(event_notice)
e = event_notice.event
if @cancel_set.key? e
if @cancel_set[e] === nil
@cancel_set[e].delete nil
@cancel_set.delete e if @cancel_set[e].empty?
return true
else
for hsh in @cancel_set[e] do
next unless event_notice.args >= hsh
@cancel_set[e].delete hsh
@cancel_set.delete e if @cancel_set[e].empty?
return true
end
end
end
false
end
include Comparable
def <=>(other)
(time <=> other.time).tap do |outcome|
return priority <=> other.priority if outcome == 0
# This is a private helper class for the EventScheduler class.
# Users should never try to access this directly.
class EventNotice
attr_reader :event, :time, :time_stamp, :priority, :args
def initialize(event, time, delay, args)
@event = event
@time_stamp = time
@time = time + delay
@args = args
@priority = @args && @args.key?(:priority) ? @args.delete(:priority) : 10
end
include Comparable
def <=>(other)
(time <=> other.time).tap do |outcome|
return priority <=> other.priority if outcome == 0
end
end
end
end
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment