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

@castore/event-storage-adapter-redux

v2.3.1

Published

DRY Castore EventStorageAdapter implementation using a Redux store

Downloads

106

Readme

Redux Event Storage Adapter

DRY Castore EventStorageAdapter implementation using a Redux store.

📥 Installation

# npm
npm install @castore/event-storage-adapter-redux

# yarn
yarn add @castore/event-storage-adapter-redux

This package has @castore/core, @reduxjs/toolkit (above v1.9) and react-redux (above v8) as peer dependencies, so you will have to install them as well:

# npm
npm install @castore/core @reduxjs/toolkit react-redux

# yarn
yarn add @castore/core @reduxjs/toolkit react-redux

👩‍💻 Usage

Direct usage

If you do not already use Redux in your app, you can simply use the configureCastore util:

import { Provider } from 'react-redux';

import { configureCastore } from '@castore/event-storage-adapter-redux';

const store = configureCastore({
  eventStores: [pokemonsEventStore, trainersEventStore],
});

const MyReactApp = () => (
  <Provider store={store}>
    <App />
  </Provider>
);

And that's it 🙌 configureCastore not only configures the Redux store but also connects it to the event stores by replacing their eventStorageAdapter.

You can use the pushEvent method as usual:

const CatchPokemonButton = ({ pokemonId }) => (
  <Button
    onClick={async () => {
      await pokemonsEventStore.pushEvent({
        aggregateId: pokemonId,
        type: 'POKEMON_CAUGHT',
        version: currentPokemonVersion + 1,
      });
    }}
  />
);

You can also use the other methods, but it's simpler to use the following built-in hooks.

Hooks

You can use the useAggregateEvents, useAggregate, useExistingAggregate and useAggregateIds hooks to read data from the store. Their interface is the same as the event store methods, but synchronous.

import { useAggregateIds } from '@castore/event-storage-adapter-redux';

const AggregateIdsList = () => {
  // 🙌 Will synchronously return the store data, as well as hook the component to it
  const { aggregateIds } = useAggregateIds(pokemonsEventStore, { limit: 20 });

  return aggregateIds.map(aggregateId => (
    <Aggregate key={aggregateId} aggregateId={aggregateId} />
  ));
};

const Aggregate = ({ aggregateId }) => {
  const { aggregate } = useExistingAggregate(pokemonsEventStore, aggregateId);

  // 🙌 aggregate is correctly typed
  return <p>{aggregate.name}</p>;
};

Thanks to the magic of Redux, pushing a new event to an aggregate will only trigger re-renders of components hooked to the said aggregate. The same goes when listing aggregate ids: Only creating a new aggregate will trigger a re-render.

Configure with another store

If you already use Redux, you can merge the Castore Redux store with your own.

First, know that event stores events are stored as Redux "slices". Their name is their eventStoreId, prefixed by a customizable string (@castore by default).

You can use the getCastoreReducers util to generate the Castore Redux reducers, and merge them with your own:

import { Provider } from 'react-redux';

import {
  ReduxEventStorageAdapter,
  getCastoreReducers,
} from '@castore/event-storage-adapter-redux';

const castoreReducers = getCastoreReducers({
  eventStores: [pokemonsEventStore, trainersEventStore],
  // 👇 Optional
  prefix: 'customPrefix',
});

const store = configureStore({
  reducer: {
    ...castoreReducers,
    customReducer,
  },
});

// 👇 Connect the event stores to the store
eventStores.forEach(eventStore => {
  eventStore.eventStorageAdapter = new ReduxEventStorageAdapter({
    store,
    eventStoreId: eventStore.eventStoreId,
    // 👇 Don't forget the prefix if one has been provided
    prefix: 'customPrefix',
  });
});

const MyReactApp = () => (
  <Provider store={store}>
    <App />
  </Provider>
);