Code Monkey home page Code Monkey logo

radicchio's Introduction

Radicchio - Version 1.1.0

A distributed timer written with ES6 syntax and implemented with Redis and Lua scripts.

Installation

$ npm install radicchio

Requirements

Requires Redis version 2.8.0 or later (uses keyspace notifications)

Features

  • Timers stored in a Redis set to speed up looking through all of the timers
  • Lua scripts provide efficiency and speed for Redis calls
  • Promises for timer functions
  • Starting a new timer with a specified expiration time (in milliseconds)
  • Attach data to a new timer upon start by passing it an object
  • Suspending and Resuming a timer
  • Deleting a timer
  • Listening for a specific type of timer event
  • Get the time remaining or data attached to an active timer
  • Get an array containing all of the active and suspended timers containing their data or time left

Types of Events

  • 'expired': triggers when a timer expires in Redis
  • 'deleted': triggers when a timer is removed from Redis
  • 'suspended': triggers when a timer is removed from Redis and stored in the global set with the time left
  • 'resumed': triggers when a suspended timer is taken from the global set and set to expire with the remaining time left

API

startTimer(timeInMS, data) - Start a timer with an expiration time and associated data

  • @param {String} timeInMS - The timer length in milliseconds
  • @param {Object} data - data object to be associated with the timer (empty object assigned by default)
  • @returns {Promise<(String|Error)>} - Resolves to the started timer id

deleteTimer(timerId) - Delete a timer

  • @param {String} timerId - The timer id to be deleted
  • @returns {Promise<(Object|Error)>} - Resolves to an object containing associated timer data

suspendTimer(timerId) - Suspend a timer

  • @param {String} timerId - The timer id to be suspended
  • @returns {Promise<(Boolean|Error)>} - Resolves to true if suspended successfully

resumeTimer(timerId) - Resume a timer

  • @param {String} timerId - The timer id to be resumed
  • @returns {Promise<(Boolean|Error)>} - Resolves to true if resumed successfully

getTimeLeft(timerId) - Get the time left on a timer

  • @param {String} timerId - The timer id get the time left on
  • @returns {Promise<(Object(String, Number))|Error>} - Resolves to an object with the timer id and time left in milliseconds

getAllTimesLeft() - Gets all of the times left on all timers (including suspended)

  • @returns {Promise<(Array(Object(String, Number)))|Error>} - Resolves to array of objects with a timer id and time left

on(event, callback) - Sets up event listener for timer events

  • @param {String} event - the supported event name to listen for
  • @param {Function} - the callback function passed to event-emitter

getTimerData(timerId) - Gets the data associated with a timer

  • @param {String} timerId - The timer id to get the associated data for
  • @returns {Promise<(Object)|Error>} - Resolves to an object with the associated timer data

getDataFromAllTimers() - Get the data from all active timers (including suspended timers)

  • @returns {Promise<(Array<(Object(String, Object)))>|Error>} - Resolves to an array of objects with a timer id and data object

Example Usage

const radicchio = require('radicchio')(); // Default. Have Redis listen on port 6379
const radicchio = require('radicchio')(6380); Have Redis listen on port 6380
const radicchio = require('radicchio')('redis://localhost:6379'); // Have Redis listen on a Redis URL

radicchio.startTimer('10000', {name: 'radicchio'})
.then((timerId) => {
  // Keep track of the timerId returned by the promise to use with the other radicchio functions
});

radicchio.deleteTimer(timerId)
.then((timerDataObj) => {
  // timerDataObj contains an object with the following properties:
  // .data - the associated data object
});

radicchio.suspendTimer(timerId)
.then((success) => {
  // success will be a boolean returned by the promise
});

radicchio.resumeTimer(timerId)
.then((success) => {
  // success will be a boolean returned by the promise
});

radicchio.getTimeLeft(timerId)
.then((timerObj) => {
  // timerObj contains an object with the following properties:
  // .timerId - the timer id
  // .timeLeft - the time left in milliseconds
});

radicchio.getAllTimesLeft()
.then((timerObjs) => {
  // timerObjs contains an array of objects each with the following properties:
  // .timerId - the timer id
  // .timeLeft - the time left in milliseconds
});

radicchio.on('expired', function(expiredTimerObj) {
  // expiredTimerObj contains an object with the following properties:
  // .timerId - the timer id
  // .data - the associated data object
});

radicchio.on('deleted', function(deletedTimerId) {
  // deletedTimerId contains the timerId of the timer that was deleted
});

radicchio.on('suspended', function(suspendedTimerId) {
  // suspendedTimerId contains the timerId of the timer that was suspended
});

radicchio.on('resumed', function(resumedTimerId) {
  // resumedTimerId contains the timerId of the timer that was resumed
});

radicchio.getTimerData(timerId)
.then((timerDataObj) => {
  // timerDataObj contains an object with the following properties:
  // .timerId - the timer id
  // .data - the associated data object
});

radicchio.getDataFromAllTimers()
.then((timerDataObjs) => {
  // timerDataObjs contains an array of objects each with the following properties:
  // .timerId - the timer id
  // .data - the associated data object
});

radicchio's People

Contributors

erickipnis avatar

Stargazers

weily avatar  avatar Nate Smith avatar Will Paul avatar Braxton Frederick avatar  avatar  avatar

Watchers

James Cloos avatar  avatar

radicchio's Issues

Remove dependency on user to create unique set and timer keys

https://www.redisgreen.net/blog/intro-to-lua-for-redis-programmers/

  • Can generate a random unique id from Redis INCR command in lua script (but would end up being integers (probably not great security-wise).
  • Can generate a random unique id fom 7-14 chars long using shortId
  • Create a new set if the current global set is null and add to it every time startTimer is called
  • Also add that key to redis on an expire in milliseconds
  • If update finds that the set is empty, or all timers in the set have expired, it will set the global setId to null. This will also trigger redis to delete the set automatically

Add functionality to store different timers in different sets

Example use:

Storing timers for a sale on a website vs. storing timers for active promo codes.

Could be a useful feature in a future version.

Would have to figure out possible way to implement (might have to rely on users storing the setIds and timerIds)

Could potentially figure out what set a timerId belonged too by searching through all active SetIds

Create additional/better Unit Tests

  • Test radicchio.on function
  • Making sure a set is removed from global set list after last timer in set expires or is disabled
  • Using the same setId will override the current set if it exists

Add option for Suspend and Resume a timer with leftover TTL

  • suspendTimer() should become the new pause functionality.
  • Find the TTL on the timer key trying to be suspended
  • Delete the timer key from Redis (could cause potential extra time in TTL since TTL is called first on key)
  • Will set the value of the timer key in the global set to the found TTL (if not 0)
  • Can call resumeTimer() again and will check the global set for the existing timer id value (if not 0)
  • Then reinsert the timer key into redis with an expire again

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.