# AppListener QML Type

- List of all members, including inherited members

## Properties

- alwaysOn : bool
- enabled : bool
- filter : string
- filters : array
- listenerId : int
- waitFor : array

## Signals

- dispatched(string type, object message)

## Methods

- on(string type, func callback)
- removeAllListener(string type)
- removeListener(string type, func callback)

## Detailed Description

AppListener is a helper class to listen dispatched signal from AppDispatcher. It provides an alternative solution other then using Connections component. Moreover, it offers a way to control the order of signal delivery through waitFor property.

Example:

    // Only listen for specific message
    AppListener {
        filter: "messageType1";
        onDispatched: {
          // Your code here
        }
    }

    // Listen for multiple messages.
    AppListener {
        onDispatched: {
            switch (type) {
                case "messageType1":
                    // ...
                    break;
                case "messageType2":
                    // ...
                    break;
            }
        }
    }

    // Alternative method to listen for multiple messages

    AppListener {

      Component.onComponented: {
        on("messageType1",function() {
           /// Your code here.
        });
        on("messageType2",function() {
           /// Your code here.
        });
      }
    }

## Property Documentation

alwaysOn : bool

This property holds a value to indicate if the listener should remain listening message when it is not enabled.

enabled : bool

This property holds whether the listener receives message. If it is false, it won't emit "dispatched" signal and trigger callback registered via "on" function. By default this is true.

The value can be controlled by parent component. Setting this property directly affects the enabled value of child items. When set to false, the enabled values of all child items also become false. When set to true, the enabled values of child items are returned to true, unless they have explicitly been set to false.

filter : string

Set a filter to incoming messages. Only message with type matched with the filter will emit "dispatched" signal. If it is not set, it will dispatch every message.

filters : array

Set a list of filter to incoming messages. Only message with type matched by the filters will emit "dispatched" signal. If it is not set, it will dispatch every message.

listenerId : int

The listener ID of this component. It could be used with AppListener::waitFor / AppDispatcher::waitFor to control the order of message delivery.

waitFor : array

If it is set, it will block the emission of dispatched signal until all the specificed listeners has been invoked.

Example code:

    AppListener {
      id: listener1
    }

    AppListener {
       id: listener2
       waitFor: [listener1.listenerId]
    }

## Signal Documentation

dispatched(string type, object message)

It is a proxy of AppDispatcher.dispatched signal. If the enabled property is set to false, this signal will not be emitted.

Note: The corresponding handler is `onDispatched`.

## Method Documentation

on(string type, func callback)

Add a listener to the end of the listeners array for the specified type of message. Multiple calls passing the same combination of event and listener will result in the listener being added multiple times.

removeAllListener(string type)

Remove all the listeners for a message with type. If type is empty, it will remove all the listeners.

removeListener(string type, func callback)

Remove a listener from the listener array for the specified message.