# Strongly Typed Events 0.3.0 &#8211; now with Signals

**Date:** 2016-07-10  
**Author:** Kees C. Bakker  
**Categories:** Strongly Typed Events, TypeScript  
**Original:** https://keestalkstech.com/strongly-typed-events-0-3-0-now-signals/

![Strongly Typed Events 0.3.0 &#8211; now with Signals](https://keestalkstech.com/wp-content/uploads/2016/07/45315_kitchen-timer-056.jpg)

---

Turns out that I needed an even smaller type of event: the signal. It is an event that has no data; it just fires. The Strongly Typed Events project [started](https://keestalkstech.com/2016/03/strongly-typed-event-handlers-in-typescript-part-1/) with the `IEvent` that was styled after the way .Net does event handling. Then the `ISimpleEvent` was [added in 0.2.0](https://keestalkstech.com/2016/05/strongly-typed-events-0-2-0-now-with-simpleevents/), for scenarios when no sender is necessary. Now I've added the `ISignal` to [version 0.3.0](https://github.com/KeesCBakker/Strongly-Typed-Events-for-TypeScript).

## Why would we need another event type?

While programming a new TypeScript project, I got the need for a timer mechanism. I only need an event without data - much like a simple egg-timer. Implementing those events as an `ISimpleEvent` looks ridiculous, that's why I created the `ISignal` and its counterparts.

## Traffic light example

Its usage is best illustrated with a piece of example code. Let's create a somewhat static traffic light that will switch colors based on time. Remember: I'm Dutch, so the middle color will be orange!

```ts
class TrafficLight {

    private _signals = new SignalList();
    private _color = 'red';

    public constructor(
        public greenMs: number,
        public orangeMs: number,
        public redMs: number
    ) {
        this.onSwitchToGreen.subscribe(() => this._color = 'green');
        this.onSwitchToOrange.subscribe(() => this._color = 'orange');
        this.onSwitchToRed.subscribe(() => this._color = 'red');

        this.internalStart();
    }

    public get color(): string {
        return this._color;
    }

    public get onSwitchToGreen(): ISignal {
        return this._signals.get('green').asEvent();
    }

    public get onSwitchToRed(): ISignal {
        return this._signals.get('red').asEvent();
    }

    public get onSwitchToOrange(): ISignal {
        return this._signals.get('orange').asEvent();
    }

    private internalStart(): void {
        window.setTimeout(() => {
            this._signals.get('green').dispatch();
            window.setTimeout(() => {
                this._signals.get('orange').dispatch();
                window.setTimeout(() => {
                    this._signals.get('red').dispatch();
                    this.internalStart();
                }, this.orangeMs);
            }, this.greenMs)
        }, this.redMs)
    }
}
```

You can subscribe and unsubscribe from the signals in the usual way:

```ts
var light = new TrafficLight(5000, 1500, 10000);
light.onSwitchToGreen.subscribe(() => console.log('Light is green.'));
light.onSwitchToOrange.subscribe(() => console.log('Light is orange.'));
light.onSwitchToRed.subscribe(() => console.log('Light is red.'));
```

## The usual suspects

All of the [usual suspects](https://github.com/KeesCBakker/Strongly-Typed-Events-for-TypeScript/blob/master/documentation/OnEventsDispatchersAndLists.md) are present:

## Other changes

I've removed the version numbers from the file names which makes updating easier. The tests are now wrapped in modules and have a string comment attached to them.

Check [Strongly Typed Events for TypeScript on GitHub](https://github.com/KeesCBakker/Strongly-Typed-Events-for-TypeScript).
