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

orwell

v0.11.0

Published

Higher-order component that can listen to Probe cursors for changes, and update the component whenever any cursor emit a change.

Downloads

18

Readme

orwell

orwell is a higher-order component that can listen to Probe cursors for changes, and re-renders the wrapped component whenever any cursor emits a change.

Usage

$ npm install --save orwell
const OrwellWrapped = orwell(Component, {

    // any of these orwell lifecycle functions are optional

    watchCursors(props, manual, context) {

        const {cursor, otherCursor} = props;

        // add validation step to decide to re-render OrwellWrapped
        manual(function(update) {
            const unsubscribe = otherCursor.on(event, function(newValue, oldValue) {

                if(condition) {
                    update();
                }
            });

            // return a cleanup step; this will be called whenever OrwellWrapped unmounts
            return unsubscribe;
        });

        // return an array of cursors to be observed.
        // whenever these cursors emit an event, OrwellWrapped will re-render.
        return [cursor];

        // or return a single cursor directly; sugar to [cursor]
        return cursor;
    },

    shouldRewatchCursors(props, context) {

        // this is called whenever a subscribed cursor emits an event, or when the parent component renders.
        // 
        // in the former case: props === this.props and context === this.context
        // in the latter case: props === nextProps and context === nextContext (passed via parent component)
        //
        // these props and context are then passed to watchCursors(props, manual, context)

        // return false by default
        
        // if return true, then watchCursors(props, manual, context) is called with appropriate props and context.
    },

    assignNewProps(props, context) {

        const {valueCursor} = props;

        // return a plain object which will be merged with this.props (whenever a subscribed cursor emits)
        // or nextProps (when the parent component renders).
        return {
            value: valueCursor.deref()
        };
    },

    shouldAssignNewProps(props, context) {

        // this is called whenever a subscribed cursor emits an event, or when the parent component renders.
        // 
        // in the former case: props === this.props and context === this.context
        // in the latter case: props === nextProps and context === nextContext (passed via parent component)
        // 
        // these props and context are then passed to assignNewProps(props, context)

        // returns true by default
        
        // if return true, then watchCursors(props, manual, context) is called with appropriate props and context.
    }

});

// static methods

// set custom shouldComponentUpdate(nextProps, nextState).
// returns same wrapped component
const OrwellWrapped2 = OrwellWrapped.shouldComponentUpdate(customShouldComponentUpdate);

// sets shouldComponentUpdate(nextProps, nextState) to perform shallow equal on props. 
// any two Probe cursors it compares are compared as currentProbe.deref() === nextProbe.deref()
// returns same wrapped component
const OrwellWrapped3 = OrwellWrapped.shallow();

// sets shouldComponentUpdate(nextProps, nextState) to perform deep equal on props.
// any two Probe cursors it compares are compared as currentProbe.deref() === nextProbe.deref()
// returns same wrapped component
const OrwellWrapped4 = OrwellWrapped.deep();

// inject object to be merged with component spec of the OrwellWrapped component
// returns newly wrapped component
const OrwellWrapped4 = OrwellWrapped.inject({
    // the only time to do this is passing `contextTypes` to access `context` from the orwell lifecycle methods
    contextTypes: {
        context: React.PropTypes.object.isRequired
    }
});

// enable debug mode
// returns same wrapped component
const OrwellWrapped5 = OrwellWrapped.debug();
const OrwellWrapped6 = OrwellWrapped.debug(true); // enable
const OrwellWrapped7 = OrwellWrapped.debug(false); // disable

shouldComponentUpdate

By default, orwell uses a shouldComponentUpdate() that is equivalent to React's PureRenderMixin, but compares Probe different by doing something like:

!(currentProbe.deref() === nextProbe.deref())

The props passed to shouldComponentUpdate(nextProps, nextState) is equivalent to Object.assign({}, props, assignNewProps(props, context)).

Inspiration

orwell has been inspired by connectToStores code written by @gaearon in his article promoting usage of Higher-order components: https://medium.com/@dan_abramov/mixins-are-dead-long-live-higher-order-components-94a0d2f9e750

License

MIT