khipu
v1.0.0
Published
A job queue
Downloads
7
Maintainers
Readme
Kipu
Kipu is an event queue, created to ease the sequencing of events in the web browser.
It has a couple of extra features too, most notably auto-discarding and auto-truncating.
Its designed with performance in mind. After an event has been processed, its removal from the queue is a O(1) operation. This enables consistent performance across web browsers.
Usage
This is how you create, add events and process a new Kipu:
kipu = Kipu.new()
kipu.add(myFunction)
kipu.add(anotherFunction, { context: this, options: [arg1, arg2] })
kipu.processQueue()
That's 90% of Kipu right there. Each function will be processed synchronously and wrapped in a promise that contains:
- The results of all operations in a list until you clear the queue
- An optional context to bind the function to
- An
options
array passed as an argument to the function
That sounds cool and all, but why should I use this?
Even if the events are processed synchronously, user interactions, AJAX requests and other events are async.
You end up disabling the UI controls while waiting for a request to be finished. Or adding flag variables to signal when the next action can take place. This gets messy quickly.
Wouldn't it be cool if your event queue could handle this for you? Enter truncating and discarding.
Truncating
Let's create a queue that removes all the events but the first one from the queue after an event is processed:
kipu = Kipu.new()
kipu.add(firstFunc)
kipu.process(kipu.truncate)
kipu.add(secondFunc)
kipu.add(thirdFunc)
kipu.add(fourthFunc)
// firstFunct finished!
kipu.list() // [secondFunc]
Discarding
Discarding works in a similar fashion as truncating: it discards all but the last event in the queue after an event is processed.
kipu = Kipu.new()
kipu.add(firstFunc)
kipu.process(kipu.discard)
kipu.add(secondFunc)
kipu.add(thirdFunc)
kipu.add(fourthFunc)
// firstFunct finished!
kipu.list() // [fourthFunc]