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

cycle-router5

v0.2.0

Published

A router driver for Cycle.js, based on Router5

Downloads

7

Readme

Cycle Router5 Driver

A source/sink router driver for Cycle.js, based on router5.

See the router5 documentation to learn how router5 works. See the Cycle.js documentation to learn how Cycle and its driver system works.

Installation

npm install --save cycle-router5

Usage:

Construction

Create a router driver like this:

var drivers = {
  DOM: makeDOMDriver(...),
  router: makeRouterDriver(...)
}

You can pass the same arguments to makeRouterDriver as you would to the router5 constructor.

Click Handler

I have adapted some of the code from Visionmedia's page.js router to handle automatic intercepting of link clicks. When a link is clicked, a number of verifications are initially performed to validate that the link matches a registered route. If so, the action is cancelled and router5 is called upon to enact the route transition. If no route is matched, the click resolves normally.

If you wish to handle this behaviour, you can pass the option disableClickHandler with the router5 options object when creating the driver, like so:

var drivers = {
  DOM: makeDOMDriver(...),
  router: makeRouterDriver(yourRoutes, {
    disableClickHandler: true,
    // router5 options here
  })
}

Making Calls

In order to play nicely with Cycle.js, the router5 API methods have been divided into two groups; (1) those that cause an instant side effect and just generally return the router5 instance itself, and (2) those that either (a) return a value synchronously without side effects or (b) cause a side effect and take a callback argument.

Methods in group (1) must be called via the "sink" side of the driver. That is, to make such a call, construct your call as an array of arguments, with the first argument being the name of the function to call, and the subsequent arguments being those that you'd like to pass to the function when calling it. If you're not passing any arguments, you can just emit a string instead of an array.

This works just like Cycle's DOM driver works, in that you hook up an event stream to pass out to the driver, and then pass function calls out the event stream as needed.

Rough Example (for illustration purposes only)

import {makeRouterDriver} from 'cycle-router5';

function intent(sources) {
  return {
    clickStart$: sources.dom.get('.start-button', 'click'),
    routeChange$: sources.router.addListener()
  };
}

function model(actions) {
  return actions.clickStart$
    .startWith(null)
    .map(ev => {
      return {
        started: !!ev
      };
    });
}

function view(model$) {
  return model$.map(model => {
    return h('p', [
      model.started ? 'Router starting now.' : 'Router not started yet.',
      h('br'),
      h('button', { className: 'start-button' }, 'Start Router')
      h('br')
    ]);
  });
}

function routing(intent) {
  return intent.clickStart$
    .map(ev => 'start'); // could also be ['start'] or ['start', arg1, ...]
}

function main(sources) {
  var intent = intent(sources);
  return {
    DOM: view(model(intent)),
    router: routing(intent)
  };
}

var routes = [
  { name: 'home', path: '/' }
];

var routerOptions = {
  disableClickHandler: false // obviously you could omit this if false
  // other router5 constructor options go here
};

var [sources, sinks] = Cycle.run(main, {
  DOM: makeDOMDriver('#app'),
  router: makeRouterDriver(routes, routerOptions)
});

API

The following router5 methods can be called from the exposed driver object, i.e. via the sources.router object in my example. Those methods which take a callback instead have the callback wrapped up inside the returned observable stream.

  • start
  • addListener
  • addNodeListener
  • addRouteListener
  • navigate
  • matchPath
  • buildUrl
  • buildPath

Router5 methods which are side-effectful (causing a state change in the router) and would normally only return the router5 object itself, or where the callback is optional, can be called by wrapping the name of the method, along with its arguments, in an array, and returning the array as a request to the driver, as per the return statement for the main function in the example above. The following methods are exposed in this way:

  • add
  • addNode
  • canActivate
  • deregisterComponent
  • navigate
  • registerComponent
  • setOption
  • start
  • stop

Please see the router5 documentation for full API details.