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

fire-event-store-react

v0.1.0

Published

Use Firestore as an event store for event sourcing

Downloads

5

Readme

fire-event-store-react Travis npm package

This project is an experiment because I wanted to see if I could make a simple API for using Firestore as an event store in an event sourcing application.

The concepts/technology I am playing with here are:

  • Event Sourcing
  • Firebase / Firestore
  • CQRS / Message Bus (one component to write, another to read)
  • React context (using react-broadcast for now, until it changes!!)
  • HOC vs render props vs Function as Child Components

fire-event-store-react provides two Function as Child Components:

  1. <FireEventStore> does a few things:
  • Connects to Firebase and runs the documents at a given key through a reducer
  • Makes the resulting state from that reducer available to children
  • Puts a Firestore reference in context for <EventEmitter> components
  1. <EventEmitter> only does one thing:
  • Sends a new event to Firebase

Please note: As with anything that uses context, this library and react-broadcast which this library is based are both experimental. Either of these may cease working in some future version of React.

Installation

Using yarn:

$ yarn add fire-event-store-react

Then, use as you would anything else:

// using ES6 modules
import { FireEventStore, EventEmitter } from "fire-event-store-react"

// using CommonJS modules
var FireEventStore = require("fire-event-store-react").FireEventStore
var EventEmitter = require("fire-event-store-react").EventEmitter

The UMD build is also available on unpkg:

<script src="https://unpkg.com/fire-event-store-react/umd/fire-event-store-react.min.js"></script>

You can find the library on window.FireEventStoreReact. (Haven't actually tested this!)

Usage

The following is a totally contrived example, but illustrates the basic functionality we're after:

import React from "react"
import {
  initializeApp,
  FireEventStore,
  EventEmitter
} from "fire-event-store-react"

// Setup the Firebase app
initializeApp({
  apiKey: "...",
  authDomain: "...",
  databaseURL: "...",
  projectId: "...",
  storageBucket: "...",
  messagingSenderId: "..."
})

// Create a reducer
const initialState = { counter: 0 }
const reducer = (state = initialState, action) => {
  if (!action) return state
  switch (action.type) {
    case "INCREMENT":
      return { counter: state.counter + 1 }
    case "DECREMENT":
      return { counter: state.counter - 1 }
    default:
      return state
  }
}

// Use <FireEventStore /> around any component you want to be a container that
// is connected to the event stream, this could just be once at the top of the
// component tree and passed down via props
const App = props => (
  <FireEventStore
    stream="counter-events"                    // Any name for your event stream
    firebaseKey="counters/demo/counter-events" // Location of events in Firebase
    reducer={reducer}                          // The reducer from earlier
  >
    {state => (
      <div className="App">
        {/* Use the state as you normally would with this.state */}
        <div>{state.counter}</div>

        {/* Use the <EventEmitter /> component anywhere to emit a new event */}
        <EventEmitter stream="counter-events">
          {emit => (
            <div>
              <button
                className="button"
                onClick={() => emit({ type: "INCREMENT" })}
              >
                +
              </button>
              <button
                className="button"
                onClick={() => emit({ type: "DECREMENT" })}
              >
                -
              </button>
            </div>
          )}
        </EventEmitter>
      </div>
    )}
  </FireEventStore>
)

Enjoy!