aboutsummaryrefslogtreecommitdiffstats
path: root/components/script/horribly_inefficient_timers.rs
blob: 6f3774d3dca8f6c96831bddcfcf8c8945d55dff8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

/// A quick hack to work around the removal of [`std::old_io::timer::Timer`](
/// http://doc.rust-lang.org/1.0.0-beta/std/old_io/timer/struct.Timer.html )

use std::sync::mpsc::{channel, Receiver};
use std::thread::{spawn, sleep_ms};

pub fn oneshot(duration_ms: u32) -> Receiver<()> {
    let (tx, rx) = channel();
    spawn(move || {
        sleep_ms(duration_ms);
        let _ = tx.send(());
    });
    rx
}

pub fn periodic(duration_ms: u32) -> Receiver<()> {
    let (tx, rx) = channel();
    spawn(move || {
        loop {
            sleep_ms(duration_ms);
            if tx.send(()).is_err() {
                break
            }
        }
    });
    rx
}