Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@germinal-io/redux-as-event-bus

Package Overview
Dependencies
Maintainers
2
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@germinal-io/redux-as-event-bus

`redux-as-event-bus` turns your redux store into a fully-typed event bus.

  • 0.1.2
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
1
Maintainers
2
Weekly downloads
 
Created
Source

redux-as-event-bus

redux-as-event-bus turns your redux store into a fully-typed event bus.

Doing so it allows you to manage your side effects in a clean and testable way.

Getting started

Install

$ npm i redux-as-event-bus  

Create the event bus and add the middleware to your store

import { prepareEventBus } from 'redux-as-event-bus'  
import { applyMiddleware, createStore, Store } from 'redux'  
  
type AppEvent = FirstEvent | SecondEvent  
type AppStore = Store<AppState, AppEvent>  
    
const { eventBus, eventBusMiddleware } = prepareEventBus<AppEvent>()  
const store: AppStore = createStore(rootReducer, applyMiddleware(eventBusMiddleware))  

Listen for a specific event

eventBus.onEvent('FirstEvent', (event: FirstEvent) => {  
    console.log(event.type) // 'FirstEvent'  
})

Listen for all events

eventBus.onAllEvents((event: AppEvent) => {  
    console.log(event.type) // 'FirstEvent' or 'SecondEvent'  
})

Inject dependencies

interface Dependencies {  
    getState: () => AppState  
    dispatch: Dispatch<AppEvent>  
    userGateway: UserGateway  
}  
  
const deps: Dependencies = {  
    ...store,  
  userGateway: new GraphQlUserGateway()  
}  
  
eventBus.onEvent('CreateProject', handler(deps))  
  
function handler(deps: Dependencies) {  
    return async (event: CreateProject) => {
        // do async logic using store values, then dispatch new event
        const userId = userIdSelector(deps.getState())  
        const project = prepareProjectToCreate(userId)
        await deps.userGateway.createProject(project)  
        deps.dispatch(ProjectCreated(project))  
    }  
}

Catch handlers execution errors

eventBus.onError((error: Error) => { 
    console.log(error) // Some error
})

Access handlers results

type HandlerResult = AppEvent[]

const { eventBus, eventBusMiddleware } = prepareEventBus<
    AppEvent, 
    HandlerResult // to ensure typing
>()

eventBus.onResult((result: HandlerResult) => {
    result.map(store.dispatch) // if results are Event you may dispatch them
})

Licence

MIT

FAQs

Package last updated on 26 Feb 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc