1.1.0 • Published 3 years ago

stimulus-events v1.1.0

Weekly downloads
4
License
MIT
Repository
-
Last release
3 years ago

Stimulus Events

Stimulus events is a simple Typescript library that uses the power of decorators to easily create an easy eventing system for communication between your different Stimulus controllers.

Never forget to call and manage removeEventListener on disconnect() again!

First, let's define an Events.ts file for our event constants! Helps us keep track of what's out there.

export const UserCountChanged = "UserCountChanged"

This isn't strictly necessary, but it helps keep your code more maintainable and is highly recommended.

import {UserCountChanged} from './Events';

class MyController extends Controller {
    
    @subscribeTo(UserCountChanged)
    updateUserCount(payload: Payload) {
        let {count} = payload
        // And yer off!
        // We'll automatically subscribe on connect() and unsubscribe on disconnect()
    }
}

And, triggering this from anywhere in your application is as easy as...

MainBus.send(UserCountChanged, {count: 3})

BOOM! That's it. All done. Time for a tea break.