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

stateful-controller-browser-router

v1.2.0

Published

A client-side router for stateful-controller using the history API

Downloads

7

Readme

stateful-controller-browser-router

A client-side router for stateful-controller using the history API

Examples

Read the documentation for stateful-controller first. This example assumes you are using browserify.

Constructing a new Router:

var Router = require('stateful-controller-browser-router');
var Controller = require('stateful-controller');
var window = global.window;
var document = window.document;
var frontController = new Controller();

frontController.enterFoo = function(state, upgrade)
{
	if (upgrade)
	{
		document.body.textContent += ' (from the server!)';
		return;
	}

	document.body.textContent = 'Welcome to foo!';
};

// A Router is not an URL mapper: you will have to provide your own.
var urlStateMap = {
	fromURL: function(path)
	{
		if (path === '/foo')
		{
			return ['foo'];
		}

		return ['pageNotFound']
	},
	toURL: function(stateList)
	{
		if (stateList[0] === 'foo')
		{
			return '/foo';
		}

		throw Error('Unknown state(s)');
	}
};

var router = new Router(window, urlStateMap, frontController);

Upgrading

If the current page represents a state that was generated by the server, and you would like to upgrade it:

// Perform an "upgrade" transition by looking at the current location URL
router.upgradeInitialState();

popstate event

To handle the popstate event (which is fired by the browser if the user uses the back/forward/etc button) you will need to:

// Register the `popstate` event
router.attachPopStateListener();

enterStates

You can trigger state transitions from code (useful for links and buttons). This will update the browser history and the current URL in the location bar:

// Note: Will reject if a state transition is already in progress
router.enterStates(['bar']).then(function()
{
	console.log('Done!');
});

queueEnterStates

A user might trigger a state transition while a previous one is still in progress. In this case you might want to defer/queue this new transition.

// This method will not reject if a state transition is already in progress,
// instead it will trigger this new state right after the previous one has completed.
// If this method is called multiple times, only the last one will be executed.
// (however the promise returned will always resolve)
router.queueEnterStates(['baz']).then(function()
{
	console.log('Done!');
});

replaceStateList

Sometimes you will want to change the URL in the location bar without transitioning to a new state:

router.replaceStateList(['foo', 'bar']);