Posts Tagged ‘Signals’

  • 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:

    this.updated.dispatch(this._point.x, this._point.y, this._z, this.cssTranslate3d, this.cssLeft, this.cssTop);

    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:

    this.position.updated.add(this._updatePosition, this);

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

    Read More