This repository was archived by the owner on Jan 30, 2024. It is now read-only.
tx/EventQueue.js
Folders and files
| Name | Name | Last commit date | ||
|---|---|---|---|---|
Repository files navigation
EventQueue.js - A small Javascript action queuing system.
EventQueue.js allows you to queue name-spaced actions and attach
a countdown value. When the countdown value reaches zero
the code attached to the action will be executed unless
another action has occurred within the same name space.
Optionally you can supply the tick value which represents the
granularity of the clock, as well as a delimiter for controlling more
complex name spaces.
Example Usage:
var queue = new EventQueue();
/*
* Add an action that will execute every 5 seconds.
*/
queue.enqueue({
name: 'Foo:bar', //method bar for module Foo
timeout: 5, // Wait 5s before executing
repeat: true, // repeat indefinitely
immediate: true, // start immediately
action: function(){
console.log('Hello, from the future!');
}
});
//remove all methods related to Foo
queue.dequeue({name: 'Foo'});
/*
* Annoyingly, fire an alert every 10 seconds
*/
queue.enqueue({
name: 'Annoying',
timeout: 10,
repeat: true,
immediate: true, // start immediately
action: function(){
alert('ack, dequeue me');
}
});
// This is an, admittedly, contrived example on how the optional
// context argument is useful.
var Context = function(){
var now = (new Date()).getTime();
var print = function(){
console.log("I started at " + now + " it is now " + (new Date()).getTime());
}
this.printMessage = print;
this.doContextTest = function(){
queue.enqueue({
name: 'Context',
timeout: 1,
repeat: true,
action: function(){
this.printMessage();
}
}, this);
}
}
var ctx = new Context();
...
<input type="button" value="Run context test" onclick="return ctx.doContextTest();" />
Copyright (c) 2013, Morgan Todd
All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided
that the following conditions are met:
Redistributions of source code must retain the above copyright notice, this list of conditions and
the following disclaimer.
Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
the following disclaimer in the documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.