Giter Site home page Giter Site logo

atayahmet / storage-based-queue Goto Github PK

View Code? Open in Web Editor NEW
43.0 7.0 5.0 1.75 MB

Javascript queue library with persistent storage based queue mechanism for the browsers environments. Specially designed for offline.

License: MIT License

JavaScript 100.00%
storage queue browser worker-queue offline offline-first persistent-queue async parallel-tasks ionic

storage-based-queue's Introduction

npm version Build Status Coverage Status Dependency Status devDependencies Status Known Vulnerabilities GitHub license GitHub issues

Persistent Queue For Browsers

Introduction

Storage based queue processing mechanism. Today, many backend technology is a simple derivative of the queuing systems used in the browser environment.

You can run jobs over the channels as asynchronous that saved regularly.

This library just a solution method for some use cases. Today, there are different technologies that fulfill the similar process.

Reminders:

  • Designed for only browser environments.
  • Built-in error handling.
  • ES6/ES7 features.
  • Full control over the workers.
  • React Native support. (require few minor config)
  • Native browser worker. (with polyfill)

How it works?

Data regularly store (localstorage, inmemory or custom storage drivers) added to queue pool. Storing queue data is also inspired by the JSON-RPC method. When the queue is started, the queues start to be processed sequentially in the specified range according to the sorting algorithm.

If any exceptions occur while the worker classes are processing, the current queue is reprocessed to try again. The task is frozen when it reaches the defined retry value.

Channels

You need to create at least one channel. One channel can be created as many channels as desired. Channels run independently of each other. The areas where each channel will store tasks are also separate. The area where tasks are stored is named with the channel name and prefix.

The important thing to remember here is that each newly created channel is actually a new copy of the Queue class. So a new instance is formed, but the dependencies of the channels are still alive as singletons.

Example; You created two channels. Their names are channelA and channelB. If you make a setting in the channelA instance, this change will also be reflected in channelB and all other channels.

Workers

You can create countless worker. Worker classes should return boolean (true / false) data with the Promise class as the return value. The return Promise / resolve (true) must be true if a task is successfully completed and you want to pass the next task. A possible exception should also be tried again: Promise / resolve (false). If we do not want the task to be retried and we want to pass the next task: Promise / reject ('any value')

Also you can use native browser worker. If you are browser does not support Worker, Browser Worker polyfil will add a pseudo-worker function to window object.

Plase check the docs: Workers

Installation

$ npm install storage-based-queue --save

import:

import Queue from "storage-based-queue";

Script Tag:

<script src="https://unpkg.com/[email protected]/dist/queue.min.js" />

Basic Usage

Worker class:

class MessageSenderWorker {
  handle(message) {
    // If return value is false, this task will retry until retry value 5.
    // If retry value is 5 in worker, current task will be as failed and freezed in the task pool.
    retry = 5;

    // Should return true or false value (boolean) that end of the all process
    // If process rejected, current task will be removed from task pool in worker.
    return new Promise((resolve, reject) => {
      // A function is called in this example.
      // The async operation is started by resolving the promise class with the return value.
      const result = someMessageSenderFunc(message);
      if (result) {
        // Task will be completed successfully
        resolve(true);
      } else {
        // Task will be failed.
        // If retry value i not equal to 5,
        // If the retry value was not 5, it is being sent back to the pool to try again.
        resolve(false);
      }
    });
  }
}

Register worker:

Queue.workers({ MessageSenderWorker });

Create channel:

// create new queue instance with default config
const queue = new Queue();
// create a new channel
const channel = queue.create("send-message");

Add task to channel:

channel
  .add({
    label: "Send message",
    handler: "SendMessageWorker",
    args: "Hello world!",
  })
  .then(result => {
    // do something...
  });

Start queue:

channel.start();

That's it!

Documentaion

Click for detailed documentation

Tests

$ npm test

Browser Support

Name Version
Chrome 32 +
Firefox 29 +
Safari 8 +
Opera 19 +
IE 11
Edge all

Note: Listed above list by pormise support.

You can testing all others browser version at BrowserStack

BrowserStack

Change log

See CHANGELOG.md

License

MIT license

storage-based-queue's People

Contributors

atayahmet avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

storage-based-queue's Issues

Support Batching

Hi. im Andy
I have a question that there is any plan to support this feature?
Thanks

Is project compatible with UglifyJs?

Hi
I am currently experiencing some issues with packaging my project using uglifyjs. Seems like this project is throwing some errors when building with uglifyjs:

ERROR in vendor.js from UglifyJs
Unexpected token: name (r) [xxxxxxxx]

Could you help us to make it uglifyjs-compatible?

Thanks

stop processing newly arrived tasks upon forcestop or stop

HI @atayahmet
Could we enforce channel to be completely stopped after stop/forcestop is called? I mean currently if I call stop/forcestop, the channel is like 'paused'. But it gets automatically resumed after another task is added. Is there any quick enhancement we could do to give API an option to completely stop the channel until the next start() is called?

Thanks for help

Don't think channel.on is working as described on Wiki

I am trying to listen to the after event for tag called 'dummy'. When I do this:
this.channel.on("[dummy]:after", function() {
console.log('test test');
});

It just throws an error. I think there is a bug when determine the event type.

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.