Giter VIP home page Giter VIP logo

picobus's Introduction

picobus

Tiniest event bus possible, type-safe

// common base
import picobus from `picobus`
const bus = picobus<PayloadType>()

// subscriber
const callback = payload => handle(payload)
bus.listen(callback)
//…later, when it's not needed anymore
bus.unlisten(callback)

// publisher
bus.dispatch(payload)

Create a new bus for every event type:

const localeBus = picobus<Locale>()
const themeBus = picobus<Theme>()
const currentUserBus = picobus<UserData>()

Type Checking

Base case

const bus = picobus<boolean>()

bus.dispatch(true) // works
bus.dispatch(null) // TS error: Argument of type 'null' is not assignable to parameter of type 'boolean'.
bus.dispatch() // TS error: Expected 1 arguments, but got 0.

No payload

const bus = picobus()

bus.dispatch() // works
bus.dispatch(false) // TS error: Expected 0 arguments, but got 1.
bus.dispatch(null) // TS error: Expected 0 arguments, but got 1.

Optional payload

const bus = picobus<boolean | undefined>()

bus.dispatch(true) // works
bus.dispatch() // works
bus.dispatch(null) // TS error: Argument of type 'null' is not assignable to parameter of type 'boolean'.

Typing the Consumer

import { Picobus } from `picobus`

function consumeBus (bus : Picobus<boolean>) {
	bus.listen(payload => {
		// payload is now correctly typed 👍
		handleBoolean(payload)
	})
}

You can also import Picolistener and Picodispatch, depending on your use-case.

Need more features? Get a bigger bus:

picobus's People

Contributors

c-vetter avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.