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

@bit-about/event

v2.1.1

Published

Tiny and powerfull hook-based event system library.

Downloads

17

Readme

Install

npm i @bit-about/event

Migrations

Events dispatch approach has been changed. There are no longer functions executed using their names in string.

✖️ old one:

const dispatch = useEvent()
dispatch('onBobPress', 'hello') 

✅ new one:

const { onBobPress } = useEvent()
onBobPress('hello')

Features

  • 100% Idiomatic React
  • 100% Typescript with event types deduction
  • Listen or dispatch events from a hook...
  • ...or utilise static access
  • No centralized event provider
  • Tiny - only 0.6kB
  • Just works

➡️ Check demo

Usage

1️⃣ Define your events by defining their payload middlewares

import { events } from '@bit-about/event'

const [EventProvider, useEvents] = events({
  buttonClicked: (payload: string) => payload,
  userLogged: () => {},
  modalClosed: () => {},
})

2️⃣ Wrap your components in EventProvider

const App = () => (
  <EventProvider>
    ...
  </EventProvider>
)

🗣️ Dispatch your events in one place...

const Button = () => {
  const { buttonClicked } = useEvents()
  
  return (
    <button onClick={() => buttonClicked('Hello')}>
      Call event
    </button>
  )
}

👂 ...and listen for them in another

const Component = () => {
  const [message, setMessage] = React.useState('')

  useEvents({
    buttonClicked: (payload: string) => setMessage(payload)
  })
  
  return <p>{message}</p> // "Hello"
}

Static access

The third result element of events() is object providing access in static manner (without hook).

const [AppEventProvider, useAppEvents, { subscribe, dispatcher }] = events(...)

and then

// 🗣️ Dispatch event
dispatcher.buttonClicked('Hello Allice!')

// 👂 Subscribe and listen on new events
const subscriber = subscribe({
  buttonClicked: (payload: string) => console.log(payload)
})
  
// remember to unsubscribe!
subscriber.unsubscribe()

👉 Re-render

Neither listeners nor events dispatch your components render. A component will only be rerendered if it's state is explicitly changed (in e.g. React.useState).

const Component = () => {
  const [message, setMessage] = React.useState('')

  useEvents({
    aliceClicked: () => console.log('I DO NOT rerender this component!'),
    bobClicked: () => setMessage('I DO rerender this component!')
  })
  
  return <p>{message}</p>
}

Event Middlewares

Events in events() are payload middlewares. They can transform payload into another.

const [EventProvider, useEvents] = events({
  buttonClicked: (payload) => `Hello ${message}!`, // Transforms string payload to another
  avatarClicked: () => `Bob!`,                     // Provides default payload
})

const { buttonClicked, avatarClicked } = useEvents({
  buttonClicked: (payload) => console.log(payload), // prints "Hello Alice!",
  avatarClicked: (payload) => console.log(payload), // prints "Bob!"
})

buttonClicked('Alice')
avatarClicked()

NOTE: The library is full type-safe, so Typescript will inform you when you use wrong payload anywhere.

BitAboutEvent 💛 BitAboutState

Are you tired of sending logic to a related components? Move your bussiness logic to hook-based state using @bit-about/state + @bit-about/event.

Now you've got completely type-safe side-effects. Isn't that cool?

import { state } from '@bit-about/state'
import { useEvents } from './auth-events' // Hook generated from events()
import User from '../models/user'

const [UserProvider, useUser] = state(
  () => {
    const [user, setUser] = React.useState<User | null>(null)
    
    useEvents({
      userLogged: (user: User) => setUser(user),
      userLoggout: () => setUser(null)
    })
    
    return user
  }
)

Partners

Credits

License

MIT © Maciej Olejnik 🇵🇱

Support me

If you use my library and you like it... it would be nice if you put the name BitAboutEvent in the work experience section of your resume. Thanks 🙇🏻!