Skip to content

Radio (legacy)

Brod edited this page Oct 6, 2018 · 1 revision

Legacy!

The radio feature is no longer available within Jagwah.


The Radio can be used as a simple Event Emitter, it's initialized when the Jagwah constructor is called and available at jagwah.radio.

If Ecmascript ever implements an EventEmitter, this will be replaced by the built-in alternative.

Listen

You can listen for events by using the radio.listen() method, the method will return a reference to itself which can be used for later cleanup / removal.

signature: listen(name: string, handler: () => Promise<void>|void): { name: string, id: string }

Below is a simple example listening for the built in jagwah:start event.

const jagwah = new Jagwah();
jagwah.radio.listen('jagwah:start', () => {
  console.log('jagwah app started');
});
jagwah.start();

Emit

You can emit events by using the radio.emit() method.

signature: emit(name: string, data: any): void

Below is a simple example which emits an event when a user signs in.

@Provider('$account')
class AccountProvider {
  constructor(
    @Inject('$jagwah') private $jagwah: Jagwah,
  ) {}
  public signIn() {
    this.user = ...
    this.$jagwah.radio.emit('$account:sign-in', this.user);
  }
}

Remove

To remove an event listener you must store it's reference, which is returned from the radio.listen() method. Then you can call the radio.remove() method and pass in the listener reference.

signature: remove(reference: { name: string, id: string }): void

const jagwah = new Jagwah();
const listener = jagwah.radio.listen('jagwah:start', () => {
  console.log('jagwah app started');
  jagwah.radio.remove(listener);
});
jagwah.start();