npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

load-image-queue

v1.1.0

Published

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

Downloads

170

Readme

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