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

react-uptown

v1.0.11

Published

High-Performance React state management

Downloads

3

Readme

Uptown Stream State

High-Performance React State Management

Uptown Stream state uses event streaming across components, radically reducing the complexity compared to other React state management frameworks, such as Redux. By streaming events, complex and annoying consumer/producer nesting is no longer required, and any component is liberated to emit or listen to state events.

Uptown Stream state connects easily to a remote websocket router such as Crossbar.io for one to many, and many to many state management needs, making Uptown very useful for gaming and currency exchange functionality.

Getting Started

1. Create an uptown folder in your react project src:

/src
    ...
    /uptown
       example.js
       index.js
    App.js
    index.js
    ...

2. Install the uptown module:

npm i -D react-uptown

3. Create an index file to export Uptown and event functions.

The base uptown index must always export the Uptown object as default. The uptown index will also contain event functions that can be imported anywhere in the application to update state.

uptown/index.js

import { UptownStream } from 'react-uptown'

const Uptown = new UptownStream()

export default Uptown

export const changeMessage = (msg) =>
    Uptown.emit('example-event', { event: 'change-message', data: msg })

4. Start by defining the strucutre of the uptown state file.

Uptown should include the following:

  • StateModel
  • constructor
  • start
  • handle
  • update
  • upstream
  • stats

uptown/example.js


// state model
export const ExampleState = {}

// constructor function
export default function Example () {}

// start, initializes the state
Example.prototype.start = function () {}

// handles events from emitters
Example.prototype.handle = function () {}

// updates state to listeners
Example.prototype.update = function () {}

// broadcasts state to upstream websockets
Example.prototype.upstream = function () {}

// debug logging
Example.prototype.stats = function () {}

That is the basic structure of an Uptown state object. All Uptown state files should include these methods as a matter of naming convention. Use prototyping instead of es6 class strucuture for performance.

5. You can now fill in the content of the uptown state file.

uptown/example.js

// state model
export const ExampleState = {
    message: 'Moose Beaver'
}

// constructor function
export default function Example () {
    this.uptown = Uptown
    this.state = ExampleState
    this.start()
}

// start, initializes the state
Example.prototype.start = function () {
    // stick to this naming convention for
    // the main event for the state file
    // '{name of state object}-event'
    this.uptown.on('example-event', this.handle, this)
}

// Handle is a switch statement to handle events 
Example.prototype.handle = function (payload) {
    //  The payload of the state events
    //  must always follow this structure:
    // { event: 'change-message', data: 'Eagle Pig' }
    // These event functions live in uptown/index.js.
    // The handle switches between the event types and 
    // parses and updates the state accordingly with
    // any logic that is required.
    const { event, data } = payload
    switch(event) {
        case 'change-message': {
            // add middleware here to parse or transform
            // the data, or emit other events as needed.
            this.state.message = Object.assign(data)
            // Always return this.update() from switch cases.
            return this.update()
        }
        // switch statements require a default
        default: {
            return
        }
    }
}

// emits a state update event to listeners
Example.prototype.update = function () {
    this.uptown.emit('example-state', this.state)
    this.stats()
}

// listener/emits state to upstream websocket listeners
Example.prototype.upstream = function () {
    // TODO
}

// debug logging
Example.prototype.stats = function () {
    // always add your state events
    // here to test for memory leaks
    // and to have visibility
    console.log(
        'Example State\n',
        'state\n',
        this.state,
        'example-state\n',
        this.uptown.listeners('example-state'),
        'example-events\n',
        this.uptown.listeners('example-events'),
        
    )
}

6. After you have the uptown state completed, implement the state into React.

Start with App.js and add the following:

src/App.js

import React, { Component } from 'react'

import Uptown, { changeMessage } from './uptown'
import Example, { ExampleState } from './uptown/example'

class App extends Component {

    constructor(props) {
        super(props)

        this.example = new Example(Uptown)
        this.state = { ExampleState }

        this.uptown = Uptown
        // create a listener function, and call
        // it using the format as follows:
        this.App_example_L1 = (ctx) => this.setState({ExampleState: ctx})
    }

    componentDidMount() {
        // we must add listeners to uptown for the 'example-state' events
        // always add 'this' context to the listener.
        this.uptown.addListener('example-state', this.App_example_L1, this)
    }

    componentWillUnmount() {
        // It's very important to remove the listener from uptown when 
        // the component unmounts to avoid memory leaks!
        this.uptown.removeListener('example-state', this.App_example_L1)
    }

    render() {
        return (
            <div>
                <p>{this.state.ExampleState.message}</p>

                <button onClick={() => changeMessage('EaglePig Soars!!!')}>
                  Eagle Pig
                </button>
                <button onClick={() => changeMessage('MooseBeaver stalks!!')}>
                  Moose Beaver
                </button>
            </div>
        )
    }
}