Giter Site home page Giter Site logo

kebab's Introduction

kebab Build Status

Half queue half pubsub. Super small (< 30 loc) and simple queue that supports subscribers.

var kebab = require('kebab')
  , kb = kebab()
  , items = 10 
  , consumers = 4;
                      
function produce () {
  var id = --items;

  if (id) {
    setTimeout(function () {
      console.log('Producer enqueueing item %d', id);
      kb.enqueue({ now: new Date() }, id);
      produce();
    }, 50);
  }
}

function consume (num) {
  kb.once(function (info, id) { 
    console.log( 'Consumer %s handling item %d produced at (%ds:%dms).'
      , num, id, info.now.getSeconds(), info.now.getMilliseconds()); 
    
    // younger consumers are lazy and therefore only consume once
    if (num > 1) setTimeout(function () { consume(num); }, 180);
  });
}

for (var i = 0; i < consumers; i++) consume(i);

produce();

Outputs:

Producer enqueueing item 9
Consumer 0 handling item 9 at (3s:879ms).
Producer enqueueing item 8
Consumer 1 handling item 8 at (3s:942ms).
Producer enqueueing item 7
Consumer 2 handling item 7 at (3s:995ms).
Producer enqueueing item 6
Consumer 3 handling item 6 at (4s:48ms).
Producer enqueueing item 5
Producer enqueueing item 4
Consumer 2 handling item 5 at (4s:99ms).
Producer enqueueing item 3
Consumer 3 handling item 4 at (4s:150ms).
Producer enqueueing item 2
Producer enqueueing item 1
Consumer 2 handling item 3 at (4s:202ms).
Consumer 3 handling item 2 at (4s:252ms).
Consumer 2 handling item 1 at (4s:303ms).

Install

npm install kebab

Run Tests

npm test

Run example

With nodejs

npm run-script example

In the Browser

open example.html

Features

  • works server side
  • works in the browser
  • supports async module loaders like requirejs

API

create a kebab

var kebab = require('kebab')
  , kb = kebab();

enqueue

kebab.enqueue(arg1 [, arg2, .., argn])

arg1 .. argn are the arguments you want to pass when a subscriber callback is called.

Example:

kb.enqueue(1, 'hello world');

once

kebab.once(callback)

Subscribe to be called back with queued arguments.

If queue is currently holding arguments, callback will be invoked with them immediately.

Otherwise the callback will be invoked one time when arguments are enqueued in the future.

Example:

kb.once(function (num, s) { console.log('working on num: %s - %s', num, s); });

kebab's People

Contributors

thlorenz avatar

Watchers

Geert avatar James Cloos avatar

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.