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

mobx-event-bus2

v2.0.0

Published

Event bus based on MobX pub/sub model.

Downloads

720

Readme

mobx-event-bus · Build Status

Tiny library allows publish-subscribe-style communication between components without requiring the components to explicitly register with one another. Based on original implementation by 7upcat, this fork includes Typescript bindings. Additional enhancements are planned in the future.

Installation

npm install mobx-event-bus2 --save

Usage

Basic

import { eventBus, subscribe } from 'mobx-event-bus'

class DemoStore {
  constructor () {
    // Registers subscriber the receive events
    eventBus.register(this)
  }

  // Mark listener1 as a event subscriber handle all 'topic1' events
  @subscribe('topic1')
  lisener1 (event) {
    console.log(`listener1: Hello ${event.payload}`)
  }

  // Mark listener2 as a event subscriber handle 'topic1' events payload match 'Martin'
  @subscribe('topic1', event => event.payload === 'Martin')
  listener2 (event) {
    console.log(`listener2: Hello ${event.payload}`)
  }
}

export const demoStore = new DemoStore()

// post event with topic 'topic1' and payload 'Tommy'
eventBus.post('topic1', 'Tommy')

// post event with topic 'topic1' and payload 'Martin'
eventBus.post('topic1', 'Martin')

// unregister from receiving future events
eventBus.unregister(demoStore);

Integration with react-router

RouterStore.js

import { createBrowserHistory } from 'history'
import { eventBus } from 'mobx-event-bus'

export default class RouterStore {
  constructor () {
    this.history.listen((location, action) => {
      eventBus.post('router', { ...location})
    })
  }

  history = createBrowserHistory()
}

DomainStore.js

import { eventBus, subscribe } from 'mobx-event-bus'

export default class DomainStore {
  constructor () {
    eventBus.register(this, {
      fireImmediately: false
    })
  }

  @subscribe('router', {payload}=> payload.pathname === '/')
  setup( event ) {
    // init data
  }
}

Handle Keyboard Events

React Component

import eventBus from 'mobx-event-bus'
// 
export default ()=>{
  const handleKeyPress = (event)=>{
    eventBus.post('keyboard', event)
  }
  return <input type="text" id="one" onKeyPress={handleKeyPress} />
}

DomainStore.js

import { eventBus, subscribe } from 'mobx-event-bus'

export default class DomainStore {
  constructor () {
    eventBus.register(this)
  }
  
  @subscribe('keyboard', ({payload}) => payload.key === 'Enter')
  handleEnterPress( event ) {
    // your business code.
  }
}

API Documents

eventBus

Dispatch events to listeners, and provides ways for subscribers register themselves.

import { eventBus } from 'mobx-event-bus'

register(store, options?)

Registers all subscribers methods on store instances to receive events. Accepts optional config with the following options:

Options

  • fireImmediately: Boolean that indicates that the subscriber function should immediately be triggered if event for that topic were fired before. false by default.

unregister(store)

Unregister store from receiving future events.

post(topic , payload)

Posts specified topic events to registered subscribers.

// event data structure
{
  topic: 'theTopic',
  payload: 'the data'
}

@subscribe

Mark a store class method as an event subscriber.

Usage:

    @subscribe (topic, selector)
    handler() {
    
    }