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-announce-fetch

v3.3.2

Published

a react-announce declarative to create REST based data stores

Downloads

102

Readme

react-announce-fetch

Build Status npm semantic-release Coverage Status

An HTTP based reactive data store. Essentially it takes in an input stream which represents the HTTP Request Stream (an Rx.Observable), and returns a Communication Bus (an Rx.Subject). Communication bus is how one can send as well as receive different kinds of events.

Installation

npm install react-announce-fetch --save

Usage

import Rx from 'rx'

import {create, reload} from 'react-announce-fetch'
import {asStream} from 'react-announce'
import {Component} from 'react'

const users = create(Rx.Observable.just(['/api/users', {method: 'GET'}]))

// Using @asStream binds the store to the components lifecycle events.  
@asStream(users)
UsersList extends Component {
  render () {
    return <div>Hi!</div>
  }
}

users.subscribe(x => console.log(x))

How does it work?

It basically makes an HTTP request if one of the components that it is listening to (for lifecycle events) mounts. Once the response is received it is not going to make anymore requests, no matter how many components its linked to via @asStream() until, the request stream fires another distinct new value.

Also, if none of the components are in mounted state and the request stream keeps firing with different values, no HTTP requests are going to be made, UNTILL one of the components mounts. In which case, the params of the last request would be used to make the HTTP request.

API

Events

The following two events are fired on the store —

  • REQUEST: Fired as soon as the request is initiated.

  • RESPONSE: Fired as soon as the response in completely received.

    const users = create(Rx.Observable.just(['/api/users']))
    
    users.filter(x => x.event === 'REQUEST').pluck('args')
    users.filter(x => x.event === 'RESPONSE').pluck('args')

create(observable)

store.create() takes in one parameter — an observable which basically is the request stream that emits notifications in the of following schema format —

Sample Schema for Request Stream

[
  url: `/api/users`,
  {  
    method: `POST` // Defaults to get
    body: JSON.stringify({name: 'Godzilla!', age: 390})
  }
]

reload(store)

reload() Forcefully refreshes the store. By default requests are only made when the request stream fires an event.

import {reload} from 'react-announce-fetch'
reload(store)

toJSON(store)

toJSON() is a simple utility method that simply exposes the store as a stream of JSON responses.

import {toJSON} from 'react-announce-fetch'
toJSON(store).subscribe(x => console.log(x))

isLoading(store0, store1, store...)

isLoading() exposes a stream with a Boolean value representing if a HTTP request is completed or not. It can take any number of stores as an argument and dispatches true if any one request is pending and false when not.

import {isLoading} from 'react-announce-fetch'
isLoading(store0, store1, store2).subscribe(x => console.log(x))