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

command-queue-module

v1.0.0

Published

Create command queue proxies for modules

Downloads

451

Readme

Command Queue Module

License: MIT Minified Size

Create simple command queue proxies for modules.

Motivation

You can boost the initial load performance of a page by requesting some non-crucial scripts asynchronously, but at the same time you might need to queue some calls to these libraries early on.

A common example is event / error tracking - it's not necessary to start sending events right after load, but it's beneficial to start collecting them as early as possible.

This project enables you to create proxy module for any library with the exact same API as the original, but the method calls are stored as commands and invoked only after the actual implementation is loaded.

API

createCommandQueueModule(methodNames, loadCallback)

methodNames

Type: Array<string>

Array of method names that will be proxied by the command queue. Other methods will not be available neither before or after load.

loadCallback

Type: (onLoad: (actualModule) => void) => void

Callback called right after calling createCommandQueueModule. It should accept onLoad function as it's only argument.

onLoad should be called with the actual module object when it's available.

Examples

Dynamic import()

const createCommandQueueModule = require('command-queue-module');

const myTrackingLibrary = createCommandQueueModule(['trackEvent'], (onLoad) => {
    import('my-tracking-library').then(onLoad);
}));

// Works no matter if library is already loaded or not
myTrackingLibrary.trackEvent('Hello world');

Dynamically inserted <script> tag

const createCommandQueueModule = require('command-queue-module');

const myTrackingLibrary = createCommandQueueModule(['trackEvent'], (onLoad) => {
    const script = document.createElement('script');
    script.src = 'https://example.org/my-tracking-library.umd.js';
    script.onload = () => {
        onLoad(window.MyTrackingLibrary)
    };

    document.body.append(script);
}));

// Works no matter if library is already loaded or not
myTrackingLibrary.trackEvent('Hello world');

Prior art

  • lazy-async is a much more complex implementation with similar API. Additional features cause the script to be 20x larger than this one.

License

MIT