Skip to content

BrandonQDixon/QEventBroker

Repository files navigation

QEventBroker

QEventBroker is an API which provides event broker functionality: dispatch and subscribe to events via observables.

NOTE: this is a work in progress and should not yet be used in any production software.

Statements Coverage Functions Branches

Install

npm install q-event-broker

Usage

Creating the event broker:

const eventBroker = new QEventBroker();

Subscribing to an event type:

//subscribe to an event of type "color"
eventBroker.event$(color).subscribe((data) => {
	console.log(data);
});

Subscribing to all events in a single observable:

//subscribe to all events
eventBroker.events$.subscribe((event: IQNodeEvent) => {

	switch (event.type) {
		case "color":
			console.log(event.data);
			break;
	}

});

Dispatching an event

//dispatch event directly
eventBroker.dispatch(<IQEvent>{
	type: "color",
	data: "green"
});

//dispatch event via class method decorator
class Test {

	@DispatchOnReturn({
		eventBroker: eventBroker,
		type: "color",
	})
	getColor() {
		return "green";
	}
	
	@DispatchOnReturn({
		eventBroker: eventBroker,
		type: "color",
		mapReturnValue: (data) => data.color,
	})
	getColorMapDispatch() {
		return {
			color: "green"
		};
	}
}

About

Event broker API in typescript

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published