All Projects → bobbylite → telephone-ts

bobbylite / telephone-ts

Licence: GPL-3.0 license
Telephone-ts: The "Event Emitter-less" TypeScript Event Architecture.

Programming Languages

typescript
32286 projects

Projects that are alternatives of or similar to telephone-ts

nodejs-boilerplate
Clean Architecture for node.js projects (Typescript + Express + TypeORM + Typedi)
Stars: ✭ 199 (+804.55%)
Mutual labels:  dependency-injection, inversion-of-control, inversifyjs
Qmq
QMQ是去哪儿网内部广泛使用的消息中间件,自2012年诞生以来在去哪儿网所有业务场景中广泛的应用,包括跟交易息息相关的订单场景; 也包括报价搜索等高吞吐量场景。
Stars: ✭ 2,420 (+10900%)
Mutual labels:  message-bus, message, message-queue
use-bus
React hook to subscribe and dispatch events accros React components
Stars: ✭ 51 (+131.82%)
Mutual labels:  events, eventbus, message
vue-ioc
IoC and DI for Vue powered by InversifyJS and inspired by Angular Module syntactic sugar.
Stars: ✭ 39 (+77.27%)
Mutual labels:  dependency-injection, inversion-of-control, inversifyjs
OpenSleigh
OpenSleigh is a Saga management library for .NET Core.
Stars: ✭ 198 (+800%)
Mutual labels:  events, message-bus, message-queue
pg-pubsub
Reliable PostgreSQL LISTEN/NOTIFY with inter-process lock support
Stars: ✭ 50 (+127.27%)
Mutual labels:  events, eventbus
avaje-inject
Dependency injection via APT (source code generation) ala "Server side Dagger DI"
Stars: ✭ 114 (+418.18%)
Mutual labels:  dependency-injection, inversion-of-control
event
📆 Strictly typed event emitter with asynciterator support
Stars: ✭ 30 (+36.36%)
Mutual labels:  events, event-emitter
mindjs
Minimalistic, pure Node.js framework superpowered with Dependency Injection 💡 💻 🚀
Stars: ✭ 17 (-22.73%)
Mutual labels:  dependency-injection, inversion-of-control
Alpine
Basic event system framework using functional interfaces
Stars: ✭ 79 (+259.09%)
Mutual labels:  events, eventbus
event-emitter
Event Emitter module for Nest framework (node.js) 🦋
Stars: ✭ 102 (+363.64%)
Mutual labels:  events, event-emitter
psr-container-messenger
Message bus and queue for Mezzio with Symfony Messenger + Enqueue
Stars: ✭ 24 (+9.09%)
Mutual labels:  message-bus, message-queue
tsee
Typed EventEmitter implemented with tsargs
Stars: ✭ 22 (+0%)
Mutual labels:  events, event-emitter
ufw
A minimalist framework for rapid server side applications prototyping in C++ with dependency injection support.
Stars: ✭ 19 (-13.64%)
Mutual labels:  dependency-injection, inversion-of-control
jimple
Just a dependency injection container to NodeJS and to the browser using new ES6 features
Stars: ✭ 72 (+227.27%)
Mutual labels:  dependency-injection, inversion-of-control
dargo
Dependency Injection for GO
Stars: ✭ 26 (+18.18%)
Mutual labels:  dependency-injection, inversion-of-control
di
Simple and yet powerful Dependency Injection for Go
Stars: ✭ 188 (+754.55%)
Mutual labels:  dependency-injection, inversion-of-control
inversify-koa-utils
inversify-koa-utils is a module based on inversify-express-utils. This module has utilities for koa 2 applications development using decorators and IoC Dependency Injection (with inversify)
Stars: ✭ 27 (+22.73%)
Mutual labels:  dependency-injection, inversion-of-control
Griffin.Container
Inversion of control container with (almost) zero configuration
Stars: ✭ 13 (-40.91%)
Mutual labels:  dependency-injection, inversion-of-control
grails-rabbitmq-native
A Grails plugin that provides convenient RabbitMQ functionality using the native Java library for RabbitMQ.
Stars: ✭ 27 (+22.73%)
Mutual labels:  message-bus, message-queue

telephone-ts Tweet

Telephone-ts is a "Event Emitter-less" TypeScript Event Architecture. Without the use of the 'events' module from node, Telephone-ts is an OOP message bus that allows developers to easily register their TypeScript Event Handlers to listen to when an Event message is "Shouted" on the telephone line!

Sure, there are tons of great event modules... I found this way useful when trying to keep my code modular with InversifyJS, which is the IOC DI registering engine behind Telephone-ts.

Run example

There is a test script available to run to see how this repo works. Run the following in your terminal.

Clone the code

git clone https://github.com/bobbylite/telephone-ts.git
cd telephone-ts/

Install dependencies

npm install 
npm run test

How to use in project

Step 1

Create your Event Interfaces. This will explain what you want your event to send over our line!

export interface IHelloEvent {
    msg: string;
}

Step 2

Implement your Event so you can have everything you need. Maybe you want to setup a service for handling an event later?
In this example we'll just use our greeting string we all love.

export class HelloEvent implements IHelloEvent {
    public msg: string = "Hello World!";
}

Step 3

Next we'll create our event handler interfaces. This will explain what is needed to handle the event!

export interface IHelloHandler {
    // anything you want your event handler to have.
}

Step 4

This is where we will implement our event handler. We must make sure to inherit/extend the BaseHandler class provided.

export class HelloHandler extends BaseHandler<IHelloEvent> implements IHelloHandler {

    public constructor() {
        super();
    }

    protected HandleMessage(message: IHelloEvent) : IHelloEvent{
        console.log(message);

        return message;
    }
}

Step 5

We now have everything we need to register, and emit events... Or create our quiet listening wire and shout on that wire! Both Register and Call require the following: TelephonetsInstance.Register("EventInterface", HandlerClassReference); TelephonetsInstance.Call("EventInterface", new EventClass);

const sleep = (ms: number) => new Promise(resolve => setTimeout(resolve, ms));

async function Test() {
    var telephonets = new Telephonets();

    telephonets.Register<INotHelloEvent>("INotHelloEvent", NotHelloHandler);
    telephonets.Register<IHelloEvent>("IHelloEvent", HelloHandler);


    telephonets.Call<IHelloEvent>("IHelloEvent", new HelloEvent); // outputs -> HelloEvent { msg: 'Hello World!' }
    await sleep(1000);
    telephonets.Call<INotHelloEvent>("INotHelloEvent", new NotHelloEvent); // outputs -> NotHelloEvent { msg: 'Not Hello World!' }
}

Behind the scenes

Behind the scenes we have two important files that really auto-wire up the events to the handlers. These two files are the telephonets.ts and BaseHandler.ts. telephonets uses InversifyJs' Container to auto-wire similar to how Autofac or other IOC libraries work. The BaseHandler is what we need our custom handlers to inherit from to ensure the project is structured properly. Take a look below.

telephonets.ts

export class Telephonets implements ITelephonets {

    private container: Container;

    public constructor() {
        this.container = new Container();
    }

    public Register<T>(symbolString: string, Handler: any) : void {
        this.container.bind<T>(symbolString).to(Handler);
    }

    public Call<T>(symbolString: string, message: any) : void {
        this.container.get<IBaseHandler<T>>(symbolString).ReceiveMessage(message);
    }
}

BaseHandler.ts

@injectable()
export abstract class BaseHandler<T> implements IBaseHandler<T> {
    public ReceiveMessage(injection: T) : void {
        try {
            this.HandleMessage(injection);
        } catch(err) {
            console.log(err);
        }
    }
    protected abstract HandleMessage(message: T): T;
}
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].