Skip to content

Commit

Permalink
auto merge of #4064 : trevorriles/servo/isintervalenum, r=jdm
Browse files Browse the repository at this point in the history
Created an `IsInterval` enum to improve readability and remove the need for `true // is_interval`

I'm still fairly new to rust. I briefly looked for a way to implement boolean comparisons of the enum but didn't figure out a way. 

Also I'm not attached to any of the names. Let me know what I can fix :)
  • Loading branch information
bors-servo committed Nov 24, 2014
2 parents a1a268c + 0cba3ee commit 90007ee
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 13 deletions.
6 changes: 3 additions & 3 deletions components/script/dom/window.rs
Expand Up @@ -25,7 +25,7 @@ use page::Page;
use script_task::{ExitWindowMsg, ScriptChan, TriggerLoadMsg, TriggerFragmentMsg};
use script_task::FromWindow;
use script_traits::ScriptControlChan;
use timers::{TimerId, TimerManager};
use timers::{Interval, NonInterval, TimerId, TimerManager};

use servo_msg::compositor_msg::ScriptListener;
use servo_msg::constellation_msg::LoadData;
Expand Down Expand Up @@ -228,7 +228,7 @@ impl<'a> WindowMethods for JSRef<'a, Window> {
self.timers.set_timeout_or_interval(callback,
args,
timeout,
false, // is_interval
NonInterval,
FromWindow(self.page.id.clone()),
self.script_chan.clone())
}
Expand All @@ -241,7 +241,7 @@ impl<'a> WindowMethods for JSRef<'a, Window> {
self.timers.set_timeout_or_interval(callback,
args,
timeout,
true, // is_interval
Interval,
FromWindow(self.page.id.clone()),
self.script_chan.clone())
}
Expand Down
6 changes: 3 additions & 3 deletions components/script/dom/workerglobalscope.rs
Expand Up @@ -14,7 +14,7 @@ use dom::workerlocation::WorkerLocation;
use dom::workernavigator::WorkerNavigator;
use dom::window::{base64_atob, base64_btoa};
use script_task::{ScriptChan, FromWorker};
use timers::{TimerId, TimerManager};
use timers::{Interval, NonInterval, TimerId, TimerManager};

use servo_net::resource_task::{ResourceTask, load_whole_resource};
use servo_util::str::DOMString;
Expand Down Expand Up @@ -160,7 +160,7 @@ impl<'a> WorkerGlobalScopeMethods for JSRef<'a, WorkerGlobalScope> {
self.timers.set_timeout_or_interval(callback,
args,
timeout,
false, // is_interval
NonInterval,
FromWorker,
self.script_chan.clone())
}
Expand All @@ -173,7 +173,7 @@ impl<'a> WorkerGlobalScopeMethods for JSRef<'a, WorkerGlobalScope> {
self.timers.set_timeout_or_interval(callback,
args,
timeout,
true, // is_interval
Interval,
FromWorker,
self.script_chan.clone())
}
Expand Down
22 changes: 15 additions & 7 deletions components/script/timers.rs
Expand Up @@ -66,6 +66,14 @@ impl Drop for TimerManager {
}
}

// Enum allowing more descriptive values for the is_interval field
#[jstraceable]
#[deriving(PartialEq, Clone)]
pub enum IsInterval {
Interval,
NonInterval,
}

// Holder for the various JS values associated with setTimeout
// (ie. function value to invoke and all arguments to pass
// to the function when calling it)
Expand All @@ -74,7 +82,7 @@ impl Drop for TimerManager {
#[privatize]
#[deriving(Clone)]
struct TimerData {
is_interval: bool,
is_interval: IsInterval,
funval: Function,
args: Vec<JSVal>
}
Expand All @@ -91,7 +99,7 @@ impl TimerManager {
callback: Function,
arguments: Vec<JSVal>,
timeout: i32,
is_interval: bool,
is_interval: IsInterval,
source: TimerSource,
script_chan: ScriptChan)
-> i32 {
Expand All @@ -104,15 +112,15 @@ impl TimerManager {
let tm = Timer::new().unwrap();
let (cancel_chan, cancel_port) = channel();
let spawn_name = match source {
FromWindow(_) if is_interval => "Window:SetInterval",
FromWorker if is_interval => "Worker:SetInterval",
FromWindow(_) if is_interval == IsInterval::Interval => "Window:SetInterval",
FromWorker if is_interval == IsInterval::Interval => "Worker:SetInterval",
FromWindow(_) => "Window:SetTimeout",
FromWorker => "Worker:SetTimeout",
};
spawn_named(spawn_name, proc() {
let mut tm = tm;
let duration = Duration::milliseconds(timeout as i64);
let timeout_port = if is_interval {
let timeout_port = if is_interval == IsInterval::Interval {
tm.periodic(duration)
} else {
tm.oneshot(duration)
Expand All @@ -131,7 +139,7 @@ impl TimerManager {
timeout_port.recv();
let ScriptChan(ref chan) = script_chan;
chan.send(FireTimerMsg(source, TimerId(handle)));
if !is_interval {
if is_interval == IsInterval::NonInterval {
break;
}
} else if id == cancel_handle.id() {
Expand Down Expand Up @@ -171,7 +179,7 @@ impl TimerManager {
// TODO: Must handle rooting of funval and args when movable GC is turned on
let _ = data.funval.Call_(this, data.args, ReportExceptions);

if !data.is_interval {
if data.is_interval == IsInterval::NonInterval {
self.active_timers.borrow_mut().remove(&timer_id);
}
}
Expand Down

0 comments on commit 90007ee

Please sign in to comment.