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

datetime-flux-iso

v2.0.0

Published

An isomorphic date/time application example

Downloads

5

Readme

datetime-flux-iso

Isomorphic react application using flux.

Running This

npm install; npm run build; npm start

Then open your browser to localhost:8080 and enjoy.

What

This is a simple ismorphic application which renders the current time and a random number sent from the server.

The purpose of this application is to show how an isomorphic react application using flux could work with iso.

One of the challenges with using flux isomorphically is how flux is structured. In flux, since the data only flows one way, all data changes start via the action triggers. This presents an issue on the server since actions are meant to be fire-and-forget and you can only dispatch one at a time. What this means is that an action has no callback and it's difficult to set off a chain of actions and know when they all completed.

Store's themselves have listeners and you could theoretically use a store's listener along with waitFor to get close, but the React components usually rely on these store listeners in order to set their internal state which kicks off DOM diffing, thus making them unsuitable for usage on the server side.

An isomorphic flux implementation could theoretically add callbacks to actions, but that goes against the spirit of flux, and doesn't look very sexy.

TimeAction.updateTime(Date.now(), function () {
  // do next thing...
})

Another challenge is that in flux stores are singletons. Pairing singleton data stores with concurrent requests is a recipe for disaster. One way of solving this dilemma is to create instances of these stores, but then the trade-off is that you're passing these instances around each component so they have a reference to the data and can use the appropriate store. This is both fragile and cumbersome.

var App = React.createClass({
  render() {
    return <TimeComponent fluxInstance={fluxImplementation} />
  }
})

var TimeComponent = React.createClass({
  getInitialState() {
    return this.props.fluxInstance.getStore('TimeStore').getState()
  },

  render() {
    return <div>{this.state.time}</div>
  }
})

Fortunately, flux's stores work very well when they are synchronous. This means we can seed the stores with data, render our application, and then revert the stores to their previous virgin state. Alt is a flux implementation that facilitates this.

alt uses a method called bootstrap which seeds the stores with data on the server, and then initializes them when the application starts on the client. Turning TimeComponent into something that looks a lot like plain flux.

// yay, references and plain old require!
var TimeStore = require('../stores/TimeStore')

var TimeComponent = React.createClass({
  getInitialState() {
    return TimeStore.getState()
  },

  render() {
    return <div>{this.state.time}</div>
  }
})

Actions then are meant to only be used on the client-side once the application starts. On the server you can perform all the necessary data gathering, and once complete you seed the data.

In this example, the random number sent from the server is in order to test flux's singleton stores. Two concurrent requests won't interfere with each other and the store's data will never collide. The time component allows you to click in order to change the time via React's onClick, this proves that the application was initialized correctly on the client.

License

MIT