levent
v0.3.12
Published
A better solution of distributing events in TS and JS. [](https://github.com/zsh2401/eventx/actions/workflows/test.yml) [
Getting Started
pnpm add levent
yarn add levent
npm install --save leventQuick Start
import levent from "levent"
//subscribe event
levent.on("test",(arg:string)=>{
return arg
})
//publish event.
const values = levent.emit("test","I love you.")
console.log(values) // ['I love you.']Strict event types.
import { Levent } from "levent"
// create your own event bus instance
const bus : Levent<{
"example-event":[string,number]
}>
//define the handler
function handler(arg:string){
return arg.length;
}
//subscribe
bus.on("example-event",handler)
// publish event
const values = bus.emit( "example-event","I Love You")
//desubscribe
bus.off("example-event",handler)Async☘️
bus.on("example-event",async (arg)=>{
return await someAsyncOperation(arg)
})
// emit event and wait all handler returns.
const values = await bus.emit("example-event",null,{ async:true })
Hooks
eventx.emit("example-event",null,{
afterEach:(r:any)=>{
console.log("last handler's result: " + r)
}
})
Join in development!🏋🏻♂️
1. Get the source
git clone https://github.com/zsh2401/levent && cd levent2. Go
# Download dependencies
pnpm
# Build it
pnpm build
# Or run unit test
pnpm testAll pull requests are welcomed!🧑💻
