@UnstableApi
class ListenerSet<T : Any?>


A set of listeners.

Events are guaranteed to arrive in the order in which they happened even if a new event is triggered recursively from another listener.

Events are also guaranteed to be only sent to the listeners registered at the time the event was enqueued and haven't been removed since.

All methods must be called on the Looper passed to the constructor unless indicated otherwise.

Parameters
<T : Any?>

The listener type.

Summary

Nested types

interface ListenerSet.Event<T>

An event sent to a listener.

An event sent to a listener when all other events sent during one Looper message queue iteration were handled by the listener.

Public constructors

ListenerSet(
    looper: Looper!,
    clock: Clock!,
    iterationFinishedEvent: ListenerSet.IterationFinishedEvent<T!>!
)

Creates a new listener set.

Public functions

Unit
add(listener: T!)

Adds a listener to the set.

Unit

Removes all listeners from the set.

ListenerSet<T!>!
copy(
    looper: Looper!,
    iterationFinishedEvent: ListenerSet.IterationFinishedEvent<T!>!
)

Copies the listener set.

ListenerSet<T!>!
copy(
    looper: Looper!,
    clock: Clock!,
    iterationFinishedEvent: ListenerSet.IterationFinishedEvent<T!>!
)

Copies the listener set.

Unit

Notifies listeners of events previously enqueued with queueEvent.

Unit
queueEvent(eventFlag: Int, event: ListenerSet.Event<T!>!)

Adds an event that is sent to the listeners when flushEvents is called.

Unit

Releases the set of listeners immediately.

Unit
remove(listener: T!)

Removes a listener from the set.

Unit
sendEvent(eventFlag: Int, event: ListenerSet.Event<T!>!)

Queues a single event and immediately flushes the event queue to notify all listeners.

Unit
setThrowsWhenUsingWrongThread(throwsWhenUsingWrongThread: Boolean)

This function is deprecated.

Do not use this method and ensure all calls are made from the correct thread.

Int

Returns the number of added listeners.

Public constructors

ListenerSet

ListenerSet(
    looper: Looper!,
    clock: Clock!,
    iterationFinishedEvent: ListenerSet.IterationFinishedEvent<T!>!
)

Creates a new listener set.

Parameters
looper: Looper!

A Looper used to call listeners on. The same Looper must be used to call all other methods of this class unless indicated otherwise.

clock: Clock!

A Clock.

iterationFinishedEvent: ListenerSet.IterationFinishedEvent<T!>!

An IterationFinishedEvent sent when all other events sent during one Looper message queue iteration were handled by the listeners.

Public functions

add

fun add(listener: T!): Unit

Adds a listener to the set.

If a listener is already present, it will not be added again.

This method can be called from any thread.

Parameters
listener: T!

The listener to be added.

clear

fun clear(): Unit

Removes all listeners from the set.

copy

fun copy(
    looper: Looper!,
    iterationFinishedEvent: ListenerSet.IterationFinishedEvent<T!>!
): ListenerSet<T!>!

Copies the listener set.

This method can be called from any thread.

Parameters
looper: Looper!

The new Looper for the copied listener set.

iterationFinishedEvent: ListenerSet.IterationFinishedEvent<T!>!

The new IterationFinishedEvent sent when all other events sent during one Looper message queue iteration were handled by the listeners.

Returns
ListenerSet<T!>!

The copied listener set.

copy

fun copy(
    looper: Looper!,
    clock: Clock!,
    iterationFinishedEvent: ListenerSet.IterationFinishedEvent<T!>!
): ListenerSet<T!>!

Copies the listener set.

This method can be called from any thread.

Parameters
looper: Looper!

The new Looper for the copied listener set.

clock: Clock!

The new Clock for the copied listener set.

iterationFinishedEvent: ListenerSet.IterationFinishedEvent<T!>!

The new IterationFinishedEvent sent when all other events sent during one Looper message queue iteration were handled by the listeners.

Returns
ListenerSet<T!>!

The copied listener set.

flushEvents

fun flushEvents(): Unit

Notifies listeners of events previously enqueued with queueEvent.

queueEvent

fun queueEvent(eventFlag: Int, event: ListenerSet.Event<T!>!): Unit

Adds an event that is sent to the listeners when flushEvents is called.

Parameters
eventFlag: Int

An integer indicating the type of the event, or INDEX_UNSET to report this event without flag.

event: ListenerSet.Event<T!>!

The event.

release

fun release(): Unit

Releases the set of listeners immediately.

This will ensure no events are sent to any listener after this method has been called.

remove

fun remove(listener: T!): Unit

Removes a listener from the set.

If the listener is not present, nothing happens.

Parameters
listener: T!

The listener to be removed.

sendEvent

fun sendEvent(eventFlag: Int, event: ListenerSet.Event<T!>!): Unit

Queues a single event and immediately flushes the event queue to notify all listeners.

Parameters
eventFlag: Int

An integer flag indicating the type of the event, or INDEX_UNSET to report this event without flag.

event: ListenerSet.Event<T!>!

The event.

setThrowsWhenUsingWrongThread

fun setThrowsWhenUsingWrongThread(throwsWhenUsingWrongThread: Boolean): Unit

Sets whether methods throw when using the wrong thread.

Do not use this method unless to support legacy use cases.

Parameters
throwsWhenUsingWrongThread: Boolean

Whether to throw when using the wrong thread.

size

fun size(): Int

Returns the number of added listeners.