Git Product home page Git Product logo

load-queue's Introduction

Load queue

Designed to allow running task in a queue with parallel support. Can be used for image (package) or file loading.

  • Small library: 6kb minified.
  • Simple queue with custom task defined with simple function

Queues types:

  1. Queue - Standard load queue, the default export.
  2. CachedQueue -- Cached load queue - when adding already loaded url, it will not use queue and call success immediately (errors are not cached)

Install

npm install load-queue --save

Browser

Library is exported via UMD. From LoadQueue object you can access to default queue, Queue and CachedQueue.

<script src="./dist/load-queue.js" type="text/javascript"></script>

<script>
var queue = new LoadQueue.default(loaderTask)
var queue2 = new LoadQueue.Queue(loaderTask, 1)
var queue3 = new LoadQueue.CachedQueue(loaderTask, 4)
</script>

Import / Require

Default (Queue)

import Queue from 'load-queue'

var queue = new Queue(loaderTask)

Queue

import {Queue} from 'load-queue'

var queue = new Queue(loaderTask)

CachedQueue

import {CachedQueue} from 'load-queue'

var queue = new CachedQueue(loaderTask)

Usage

Before constructing queue, you must provide your own task implementation. The function will accept 3 arguments:

  1. entry - An QueueEntry with an url entry.url
  2. success - A callback that accepts any custom arguments that will be passed to your custom callback
  3. failure - A callback for failure that accepts an error Error object
/**
 * @param {QueueEntry} entry
 * @param {function} success
 * @param {function} failure
 */
var loaderTask = function (entry, success, failure) {
        // ... loading entry.url
        setTimeout(function () {
            if (entry.url === 'url1') {
                failure(new Error('Failed!'))
            } else {
                success('my custom var', 'custom var 2')
                // or just success()
            }
        }, 1000)
    }

Queue construct accepts:

  1. The task function.
  2. Number of concurrent jobs that can be ran (default 1).
  3. start timeout - defines if the start will use timeout function to throttle calls and give time for start -> cancel use case (when user scrolls in list and etc). Set null to turn it off. Default is 50ms (which is enough for fast scroll)
var queue = new Queue(loaderTask)
// or 
var queue = new Queue(loaderTask, 2)
// or
var queue = new CachedQueue(loaderTask)

To add a new url to load queue, just call add(url, success, error). The add method will return the QueueEntry that holds given url.

var entry = queue.add('url', function(url, customVar, customVar2) {
    console.log(url, customVar, customVar2)
}, function(error) {
  console.log(error)
})
console.log(entry.url)

// Or cancel the request
entry.cancel()

QueueEntry

You can access to url and the cancel method. For internal use you can access to running task entry.task and call success/error callbacks (the callbacks that that executes).

Cancelling

You can cancel given url from queue at any time (even when loading - the callbacks wont be called). There are 2 ways how to cancel request.

  1. Calling cancel(url) on queue object. Like queue.cancel('test.cz')
  2. Calling cancel() on QueueEntry from add function.

Task cancel

If your task implementation needs to handle the cancel (like cancel the http request) then the queue will use it's own logic and then call cancel on the task.

var loaderTask = function (entry, success, failure) {
        // ... loading entry.url
        var timeout = setTimeout(function () {
            if (entry.url === 'url1') {
                failure(new Error('Failed!'))
            } else {
                success('my custom var', 'custom var 2')
                // or just success()
            }
        }, 5000)
        
        // Cancel the timeout
        this.cancel = function () {
            clearTimeout(timeout)
        }
    }

Copyright and License

load-queue was written by Martin Kluska and is released under the MIT License.

Copyright (c) 2017 Martin Kluska

load-queue's People

Contributors

pionl avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

Forkers

farfromrefug

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.