# Dispatcher QML Type

Message Dispatcher. More...

Inherited By: | AppDispatcher

- List of all members, including inherited members

## Signals

- dispatched(string type, object message)

## Methods

- int addListener(func callback)
- dispatch(string type, object message)
- removeListener(int listenerId)
- waitFor(int listenerId)

## Detailed Description

    import QuickFlux 1.1

Dispatcher is a component for delivering action message.

Usually you don't need to declare a Dispatcher component by yourself beside writing test case. It is suggested to use AppDispatcher directly.

It is added since QuickFlux v1.1

## Signal Documentation

dispatched(string type, object message)

This signal is emitted when an action message is ready to dispatch by Dispatcher.

There has several methods to listen this signal:

Method 1 - Using Store component

It is the suggested method. (Since QuickFlux 1.1)

    import QuickFlux 1.1

    Dispatcher {
      id: dispatcher
    }

    Store {
      bindSource: dispatcher

      Filter {
        type: ActionTypes.openItem
        onDispatched: {
          // ..
        }
      }
    }

Method 2 - Using Connections component

    import QuickFlux 1.1

    Dispatcher {
      id: dispatcher
    }

    Connections {
        target: dispatcher
        onDispatched: {
            switch (type) {
                case "OpenItem";
                    // ...
                    break;
                case "DeleteItem";
                    // ...
                    break;
            }
        }
    }

Note: The corresponding handler is `onDispatched`.

## Method Documentation

int addListener(func callback)

This method is deprecated

Registers a callback to be invoked with every dispatched message. Returns a listener ID that can be used with waitFor().

dispatch(string type, object message)

Dispatch an action by the Dispatcher. An action consists two parts: The type and message.

The action may not be dispatched immediately. It will first pass to registered Middleware. They may modify / remove the action.

If there has more then one pending action, it will be placed on a queue to guarantees the order of messages are arrived in sequence to store (First come first served)

    Store {

      Filter {
        type: ActionTypes.askToRemoveItem
        onDispatched: {
            if (options.skipRemoveConfirmation) {
                AppDispatcher.dispatch(ActionTypes.removeItem, message);
                // Because AppDispatcher is still dispatching ActionTypes.askToRemoveItem,
                // ActionTypes.removeItem will be placed in a queue and will dispatch when
                // all the listeners received current message.
            }
        }
      }
    }

removeListener(int listenerId)

This method is deprecated

Remove a callback by the listenerId returned by addListener

waitFor(int listenerId)

This method is deprecated

Waits for a callback specifed via the listenerId to be executed before continue execution of current callback. You should call this method only by a callback registered via addListener.