TypeScript Signals released – Think outside the Event

Signals are a light-weight, strongly typed messaging tool and we use them extensively in our game framework. And we’ve just released our TypeScript implementation for others to benefit from. It’s a conversion of js-signals by Miller Medeiros, which is of course in turn a conversion of AS3-Signals by Robert Penner.

You can get TypeScript-Signals from github.

If you are unfamiliar with Signals, how they work and how they compare to Events then this short summary is well worth a quick read, but to summarise:

  • A Signal is essentially a mini-dispatcher specific to one event, with its own array of listeners.
  • A Signal gives an event a concrete membership in a class.
  • Listeners subscribe to real objects, not to string-based channels.
  • Event string constants are no longer needed.
  • Signals are inspired by C# events and signals/slots in Qt.

I ported over all of the 18 unit tests to TypeScript as well. So you have plenty of examples: from adding a basic listener up to manual binding and dynamic context switching.

Here is an example of how we use a Signal within our Position component in our framework. In this case ‘updated’ is our Signal and when the Position component is updated we dispatch it:

Anything that is listening to the ‘updated’ Signal is notified of the new values being sent. For example our Sprite game object listens for updates:

And a local private method _updatePosition responds to the new values being sent.

The reason we use Signals, aside from the fact that they are blisteringly fast, is that they are not bound to strings but rather to objects. And more importantly they can dispatch a dynamic number of properties which can be strongly typed in TypeScript if you wish. But you don’t have to respond to all of the parmeters if you don’t wish. For example although the updated Signal was sending through x, y, z and other values, the method that listens to the Signal doesn’t have to take all of those parameters.

This is perfectly valid:

But if you only need the x,y values then so is this:

Even though all values are sent it doesn’t matter how many of them you state in your method.

There are lots of other benefits too. The passing of context and priority orders being especially useful. I have to admit that in AS3 I never really found a use for Signals as Events were so tightly coupled with the language, but in TypeScript they make perfect sense. They are one of those things that are really quite hard to explain or make you understand why they’re great to use until you actually do it. But once you do start using them it’s like a “light bulb” moment and you probably won’t want to go back to Events again.

Checkout TypeScript-Signals on github.

Posted on April 3rd 2013 at 2:05 pm by .
View more posts in Projects. Follow responses via the RSS 2.0 feed.

3 Responses

Leave a comment
  • Jake
    November 7th 2013 at 7:32 pm

    Curious if this is still being updated? Grabbed the latest from github and the typescript 0.9 compiler seems to be choking on a few things. A bunch of issues similar to this:

    Signal.ts(169,26): error TS1015: Parameter cannot have question mark and initializer.

    I could probably manually take care of them, but if you are still updating it I’d save myself time.


  • November 8th 2013 at 2:31 am

    Sorry no it’s not. However there is a 0.9.1 version of this as part of KiwiJS: https://github.com/gamelab/kiwi.js

  • Jake
    November 8th 2013 at 3:24 am

    I’ll check it out, thanks!

Make yourself heard