org.appcelerator.kroll.common
Class TiMessenger

java.lang.Object
  extended by org.appcelerator.kroll.common.TiMessenger
All Implemented Interfaces:
Handler.Callback

public class TiMessenger
extends Object
implements Handler.Callback

A messenger interface that maintains a MessageQueue, and Looper but with better primitives for blocking and single loop iteration. The TiMessenger also provides information on the main and runtime threads and supports posting runnable's on both threads. TiMessengers have one instance per thread tied by a ThreadLocal. The main thread's TiMessenger can be retrieved by calling getMainMessenger(). The runtime thread's TiMessenger can be retrieved by calling getRuntimeMessenger(). A TiMessenger can be lazily created/queried for the current Thread by calling getMessenger(). To simply send a message, see sendMessage(Message) and post(Runnable). In situations where the current thread needs to be blocked while waiting on another thread to process a message, see sendBlockingMainMessage(Message, Object) and sendBlockingRuntimeMessage(Message, Object). To process and dispatch a single message from the message queue, see dispatchMessage().


Method Summary
 Looper getLooper()
           
static TiMessenger getMainMessenger()
           
static TiMessenger getRuntimeMessenger()
           
static Object sendBlockingMainMessage(Message message)
          Sends a message to an ArrayBlockingQueue, and dispatch messages on the current queue while blocking on the passed in AsyncResult.
static Object sendBlockingMainMessage(Message message, Object asyncArg)
          Sends a message to an ArrayBlockingQueue, and dispatch messages on the current queue while blocking on the passed in AsyncResult.
static Object sendBlockingRuntimeMessage(Message message)
          Sends a message to an ArrayBlockingQueue, and dispatch messages on the current queue while blocking on the passed in AsyncResult.
static Object sendBlockingRuntimeMessage(Message message, Object asyncArg)
          Sends a message to an ArrayBlockingQueue, and dispatch messages on the current queue while blocking on the passed in AsyncResult.
 

Method Detail

getMainMessenger

public static TiMessenger getMainMessenger()
Returns:
the main TiMessenger instance. This is used for sending messages to the Main thread. See sendBlockingMainMessage(Message, Object) for more details.

getRuntimeMessenger

public static TiMessenger getRuntimeMessenger()
Returns:
the KrollRuntime TiMessenger instance. This is used for sending messages to the KrollRuntime thread. See sendBlockingRuntimeMessage(Message, Object) for more details.

sendBlockingMainMessage

public static Object sendBlockingMainMessage(Message message)
Sends a message to an ArrayBlockingQueue, and dispatch messages on the current queue while blocking on the passed in AsyncResult. The blocking is done on the Main thread.

Parameters:
message - the message to send.
Returns:
The getResult() value of the AsyncResult put on the message.

sendBlockingMainMessage

public static Object sendBlockingMainMessage(Message message,
                                             Object asyncArg)
Sends a message to an ArrayBlockingQueue, and dispatch messages on the current queue while blocking on the passed in AsyncResult. The blocking is done on the Main thread.

Parameters:
message - the message to send.
asyncArg - argument to be added to the AsyncResult.
Returns:
The getResult() value of the AsyncResult put on the message.

sendBlockingRuntimeMessage

public static Object sendBlockingRuntimeMessage(Message message)
Sends a message to an ArrayBlockingQueue, and dispatch messages on the current queue while blocking on the passed in AsyncResult. The blocking is done on the KrollRuntime thread.

Parameters:
message - the message to send.
Returns:
The getResult() value of the AsyncResult put on the message.

sendBlockingRuntimeMessage

public static Object sendBlockingRuntimeMessage(Message message,
                                                Object asyncArg)
Sends a message to an ArrayBlockingQueue, and dispatch messages on the current queue while blocking on the passed in AsyncResult. The blocking is done on the KrollRuntime thread.

Parameters:
message - the message to send.
asyncArg - the argument to be added to AsyncResult.
Returns:
The getResult() value of the AsyncResult put on the message.

getLooper

public Looper getLooper()
Returns:
the native looper. See Looper for more details.


Copyright © 2010-2012 Appcelerator, Inc. Licensed under the Apache License 2.0