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

vargate

v0.9.4

Published

Async Javascript library for code that should only run after data becomes available.

Downloads

9

Readme

VarGate

npm version npm downloads

VarGate is an async library designed around data instead of files.

Including VarGate

Browser

<script src="path/to/vargate.min.js"></script>

VarGate is now available via window.VarGate.

Node

Run npm install vargate --save-dev.

If using ES6:

import VarGate from "vargate";

CommonJS:

var VarGate = require('vargate');

AMD:

define(['vargate'], function(VarGate) {

});

Overview

Think of VarGate as RequireJS for variables instead of files. It turns this:

// jQuery used for brevity. This is not a jQuery library
var someVar, anotherVar, aThirdVar;
$.get('/some-endpoint')
    .done(function(resp) {
        someVar = resp.data;
        return $.get('/another-endpoint');
    }).done(function(resp) {
        anotherVar = resp.data;
        return $.get('/a-third-endpoint');
    }).done(function(resp) {
        aThirdVar = resp.data;
        console.log("I have some " + someVar + ", as well as some " + anotherVar + " and " + aThirdVar + ".");
    });

into this:

VarGate.when([
    'someVar',
    'anotherVar',
    'aThirdVar'
], function(someVar, anotherVar, aThirdVar) {
    console.log("I have some " + someVar + ", as well as some " + anotherVar + " and " + aThirdVar + ".");
});
$.get('/some-endpoint', function(resp) {
    VarGate.set('someVar', resp.data);
});
$.get('/another-endpoint', function(resp) {
    VarGate.set('anotherVar', resp.data);
});
$.get('/a-third-endpoint', function(resp) {
    VarGate.set('aThirdVar', resp.data);
});

Usage

When developing, it is recommended to set window.DEBUG_MODE and window.DEV_MODE.

window.DEV_MODE = 'strict'; // Errors will stop execution. Recommended for local development.
window.DEV_MODE = 'warn';   // Errors will log, but execution will continue. Recommended for staging environments.
window.DEBUG_MODE = 'verbose'; // Will log the trace of every action
window.DEBUG_MODE = 'static'; // Same as verbose, but prints a static copy of the values passed through
window.DEBUG_MODE = 'minimal'; // Will only log `VarGate.set` actions

Without setting window.DEV_MODE, errors will be ignored, as it is assumed you are in a production environment. Likewise, not setting window.DEBUG_MODE will silence the log statements from VarGate.

When waiting on a single variable to be defined

VarGate.when('oneVar', func);

When waiting on multiple variables to be defined

VarGate.when(['oneVar', 'twoVar'], func);

When waiting on multiple variables to meet specific conditions, follow the convention [key, operator, value]

VarGate.when([['oneVar', '===',  7], ['twoVar', '>', 4]], func);

When waiting on one variable to meet a specific condition (in this case, equaling another variable)

VarGate.when([['oneVar', '===', '@twoVar']],  func);

When waiting on the nested property of a variable to be defined:

VarGate.when([[someObject, 'someProperty']],  func); //note: this will return `someObject` in the callback

Functions will only run once. To run a function every time data is changed, use the following:

VarGate.on('someVar', func); // Will run every time `someVar` is set
VarGate.on([['someVar', '===', 3]], func); // Will run every time `someVar` is set to 3
VarGate.on(['someVar', 'anotherVar'], on); // Will run every time `someVar` and `anotherVar` are set

To un-set a variable, just do VarGate.unset('someVar'), which will set the variable to undefined.

To clear all data in a given module, use VarGate.clear(). Note that this will not clear sub-modules.

To clear all data in a given module, as well as all sub-modules, use VarGate.clearAll().

You can namespace sub-modules to avoid name conflicts

var subGate = VarGate.register('subgate');
subGate.set('someVar', someValue);
var otherGate = subGate.register('othergate');
otherGate.set('someVar', anotherValue); // will error out if window.DEV_MODE == 'strict'
console.log(subGate.get('someVar') == otherGate.get('someVar')); // prints `false`

The parent can get and set values for its children

VarGate.set('subgate.value', anotherValue);
VarGate.get('othergate.someVar');

Sub-modules can only get values from the parent

VarGate.set('value', 1);
var subGate = VarGate.register('subgate');
subGate.get('value'); // returns 1
subGate.set('value', 3); // will error out if window.DEV_MODE == 'strict'
subGate.get('value'); // returns 3
VarGate.get('value'); // returns 1

To override a value set in a parent module, use the override function:

var subGate = VarGate.register('subgate');
VarGate.set('val', 3);
subGate.override('val', 'ten'); // will not error out!
subGate.get('val'); // returns 'ten'
VarGate.get('val'); // returns 3