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

@akoenig/adhoc

v2.0.2

Published

Opinionated higher-order component for establishing ad-hoc stream-based unidirectional data flows.

Downloads

14

Readme

adhoc Build Status

Opinionated higher-order component for establishing ad-hoc stream-based unidirectional data flows.

Demo

Example

import React from "react";
import { compose, withHandlers } from "recompose";

import { withStreams } from "@akoenig/adhoc";

const SearchBoxComponent = ({query, placeholder, onChange}) => (
  <input
    placeholder={placeholder}
    value={query}
    type="text"
    onChange={onChange}
  />
);

const SearchBox = compose(
  withStreams({
    //
    // Define your streams here ...
    //
    // The name of the stream attribute will also be the name of the prop which holds the
    // current value of the stream. Please note that the following `fold` does not
    // distinguish between several action types for streamlining this example, but you
    // could implement fully-fledged Redux-like reducers here.
    //
    query: (stream, ownProps) => stream.fold((state, action) => action.payload, ""),
  }),
  withHandlers({
    onChange: ({ dispatch }) => e => dispatch({type: "an-action-type", payload: e.target.value})
  })
)(SearchBoxComponent)

export { SearchBox };

Motivation

For me, Redux is still the number one choice when it comes to selecting a global state container for scalable applications and I recommended it a lot to my customers. Many of the use-cases out there can be covered with this stack quite well. Even when this technology stack is still popular, I made an interesting observation in my last projects. Recently, I developed a GraphQL API and the corresponding client application with React + Apollo. Everything fitted nicely due to the declarative nature of GraphQL. Anyways, mapping queries and mutations to UI components is cool, but having UI state is of course still real. Therefore, what to do in this case? Sharing the Redux store with the Apollo Client? Well, you can do that. I had different goals in mind:

  • Lightweight solution without a lot of boilerplate
  • Reactive (❤️ RxJS & xstream)
  • Fast implementation iterations when you have to deal with local UI state

The result is this higher-order component which allows you to establish ad-hoc stream-based unidirectional data flows. Let's examine one typical use-case:

I want to have a search box which handles the user input by 1.) storing the current value, 2.) checking if the user entered at least three characters and 3.) there should not be any interaction in the last 500 ms before sending the query to the backend system.

Functional Reactive Programming is one of the best paradigms for handling scenarios like that. Achieving a solution for that use-case above can be express with xstream – a functional reactive stream library – as:

import debounce from "xstream/extra/debounce";
import fromEvent from "xstream/extra/fromEvent";

const stream = fromEvent(document.getElementById('search'), 'keyup')
  .map(e => e.currentTarget.value)
  .filter(value => value.length >= 3)
  .compose(debounce(500))
  .subscribe({
      next: (value) => console.log(`Received debounced search query: ${value}`)
  });

Lovely, isn't it? adhoc helps you to do exactly that within your React components.

API

withStreams()

withStreams({
  streamCreators: {
      [streamName: string]: (stream: Stream, props: Object) => Stream
  }
}),

Each last value of the stream will be mapped and passed as prop to the component. The streamName defines the name of the prop. The HoC will create one dedicated stream for the respective component, but you can define as much stream creators as you want. Those stream creators can be seen as "substreams" of the "über-stream". The underlying stream foundation is xstream.

Additionally, your component will also receive a dispatch prop, which can be used to dispatch actions. Those actions will flow through your stream. The signature of this function awaits a Flux Standard Action: dispatch({type: String, payload: any})

License

MIT © André König