Git Product home page Git Product logo

load-image-queue's Introduction

Load image queue

Loading image with queue support to allow loading images as you need. Ideal for virtual list (with cancel support). Uses load-queue.

You can use the global queue or create your own.

Install

npm install load-image-queue --save

Browser

Library is exported via UMD. From LoadImageQueue object you can access to default (global object) queue or create new one

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

<script>
// Default global queue
var queue = new LoadQueue.default
// Create custom queue
var queue3 = new LoadQueue.createImageQueue(2)
</script>

Import / Require

Default (Queue)

import globalQueue from 'load-image-queue'

globalQueue.add(...)

createImageQueue

import {createImageQueue} from 'load-image-queue'

var queue = createImageQueue()

Usage

You can use the global queue (uses cached queue of 1 concurrent job) or create your own custom queue via createImageQueue.

createImageQueue(jobs = 1, cached = true) accepts number of concurrent jobs (default 1) as first parameters, second parameter defines, if you want to use cached queue.

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()

More about how to cancel and work with queue, check the load-queue package

React usage

This package is ideal to use with react component (or any virtual dom). It is important to call cancel when component has been updated (or moved)

import {createImageQueue} from 'load-image-queue'

const images = createImageQueue(3)

export default class Image extends React.Component {
    static propTypes = {
        url: PropTypes.string.isRequired
    }
    static defaultProps = {}

    constructor (props, context) {
        super(props, context)

        /**
         * The queue entry
         * @type {QueueEntry}
         */
        this.queueEntry = null
        this.mounted = false
        this.state = {
            loading: true,
            error: null
        }
    }

    /**
     * Load the image
     */
    loadImage () {
        const {loading, error} = this.state
        if (loading === false || error !== null) {
            return
        }

        // Get the thumbnail
        const {url} = this.props

        // Load the image
        this.queueEntry = images.load(url, (url) => {
            if (this.mounted === false) {
                return
            } 
            
            this.setState({
                loading: false,
                error: null
            })
        }, (error) => {
            this.setState({
                error: error
            })
        })
    }

    /**
     * Cancels current load
     */
    cancelLoad () {
        if (this.queueEntry !== null) {
            this.queueEntry.cancel()
            this.queueEntry = null
        }
    }

    // Load the image when the component has been mounted or updated
    componentWillMount () {
        this.mounted = true
        this.loadImage()
    }

    componentDidUpdate () {
        this.loadImage()
    }

    /**
     * Cancel upload when component is being destroyed
     */
    componentWillUnmount () {
        this.mounted = false
        this.cancelLoad()
    }

    /**
     * Handle new props url and load new image
     * @param nextProps
     */
    componentWillReceiveProps (nextProps) {
        if (nextProps.url !== this.props.url) {
            // Cancel the previously loaded image
            this.cancelLoad()

            // Update the state to loading
            this.setState({
                loading: true,
                error: null
            })
        }
    }

    render () {
        const {loading, error} = this.state
        const {url} = this.props

        // Pass src only if loaded
        let src = null
        if (loading === false && error === null) {
            src = `url(${url})`
        }

        return <img src={src} />
    }
}

Copyright and License

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

Copyright (c) 2017 Martin Kluska

load-image-queue's People

Contributors

pionl avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  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.