API Reference 0.7.1rikulo_messageMessageQueue<Message>

MessageQueue<Message> class

A message queue.

class MessageQueue<Message> {
 final List<_ListenerInfo> _listenerInfos = new List();
 String _uuid;

 MessageQueue();

 /** The UUID of this message queue.
  */
 String get uuid {
   if (_uuid == null) {
     final int appid = ViewUtil.appId;
     _uuid = StringUtil.encodeId(_uuidNext++,
       appid > 0 ? "q${StringUtil.encodeId(appid)}_": "q_");
   }
   return _uuid;
 }
 static int _uuidNext = 0;

 /** Subscribes a message listener to this message queue with an optional filter.
  */
 void subscribe(MessageListener listener, [MessageFilter filter]) {
   _listenerInfos.add(new _ListenerInfo(listener, filter));
 }
 /** Unsubscribes a message listener from this message queue.
  * This method returns true if the listener was added.
  */
 bool unsubscribe(MessageListener listener, [MessageFilter filter]) {
   for (int j = _listenerInfos.length; --j >= 0;) {
     final _ListenerInfo info = _listenerInfos[j];
     if (listener == info.listener && filter == info.filter) {
       _listenerInfos.removeAt(j);
       return true;
     }
   }
   return false;
 }
 /** Sends a message to this queue, such that
  * all subscribers in this message queue will be notified.
  */
 void send(Message message) {
   for (final _ListenerInfo info in _listenerInfos) {
     if (info.filter == null || (message = info.filter(message)) != null) {
       info.listener(message);
     }
   }
 }
 /** Posts a message to this queue.
  * Unlike [send], the subscribers will be invoked later.
  * In other words, this method returned immediately without waiting
  * any subscriber to be invoked.
  */
 void post(Message message) {
   Timer.run(() {send(message);});
     //note: the order of messages is preserved across all message queues
 }

 String toString() => "MessageQueue($uuid)";
}

Constructors

new MessageQueue() #

Creates a new Object instance.

Object instances have no meaningful state, and are only useful through their identity. An Object instance is equal to itself only.

docs inherited from Object
MessageQueue();

Properties

final String uuid #

The UUID of this message queue.

String get uuid {
 if (_uuid == null) {
   final int appid = ViewUtil.appId;
   _uuid = StringUtil.encodeId(_uuidNext++,
     appid > 0 ? "q${StringUtil.encodeId(appid)}_": "q_");
 }
 return _uuid;
}

Methods

void post(Message message) #

Posts a message to this queue. Unlike send, the subscribers will be invoked later. In other words, this method returned immediately without waiting any subscriber to be invoked.

void post(Message message) {
 Timer.run(() {send(message);});
   //note: the order of messages is preserved across all message queues
}

void send(Message message) #

Sends a message to this queue, such that all subscribers in this message queue will be notified.

void send(Message message) {
 for (final _ListenerInfo info in _listenerInfos) {
   if (info.filter == null || (message = info.filter(message)) != null) {
     info.listener(message);
   }
 }
}

void subscribe(MessageListener listener, [MessageFilter filter]) #

Subscribes a message listener to this message queue with an optional filter.

void subscribe(MessageListener listener, [MessageFilter filter]) {
 _listenerInfos.add(new _ListenerInfo(listener, filter));
}

String toString() #

Returns a string representation of this object.

docs inherited from Object
String toString() => "MessageQueue($uuid)";

bool unsubscribe(MessageListener listener, [MessageFilter filter]) #

Unsubscribes a message listener from this message queue. This method returns true if the listener was added.

bool unsubscribe(MessageListener listener, [MessageFilter filter]) {
 for (int j = _listenerInfos.length; --j >= 0;) {
   final _ListenerInfo info = _listenerInfos[j];
   if (listener == info.listener && filter == info.filter) {
     _listenerInfos.removeAt(j);
     return true;
   }
 }
 return false;
}