public class ObservableTracker extends Object
If you are implementing an IObservable, invoke getterCalled(this) whenever a getter is called - that is, whenever your observable is read from. You only need to do this once per method call. If one getter delegates to another, the outer getter doesn't need to call the method since the inner one will.
If you want to determine what observables were used in a particular block of code, call runAndMonitor(Runnable). This will execute the given runnable and return the set of observables that were read from.
This can be used to automatically attach listeners. For example, imagine you have a block of code that updates some widget by reading from a bunch of observables. Whenever one of those observables changes, you want to re-run the code and cause the widget to be refreshed. You could do this in the traditional manner by attaching one listener to each observable and re-running your widget update code whenever one of them changes, but this code is repetitive and requires updating the listener code whenever you refactor the widget updating code.
Alternatively, you could use a utility class that runs the code in a runAndMonitor block and automatically attach listeners to any observable used in updating the widget. The advantage of the latter approach is that it, eliminates the code for attaching and detaching listeners and will always stay in synch with changes to the widget update logic.
Constructor and Description |
---|
ObservableTracker() |
Modifier and Type | Method and Description |
---|---|
static void |
getterCalled(IObservable observable)
Notifies the ObservableTracker that an observable was read from.
|
static void |
observableCreated(IObservable observable)
Notifies the ObservableTracker that an observable was created.
|
static IObservable[] |
runAndCollect(Runnable runnable)
Invokes the given runnable, and returns the set of IObservables that were
created by the runnable.
|
static void |
runAndIgnore(Runnable runnable)
Runs the given runnable without tracking dependencies.
|
static IObservable[] |
runAndMonitor(Runnable runnable,
IChangeListener changeListener,
IStaleListener staleListener)
Invokes the given runnable, and returns the set of IObservables that were
read by the runnable.
|
static void |
setIgnore(boolean ignore)
If the argument is
true , causes subsequent calls to
getterCalled(IObservable) and
observableCreated(IObservable) to be ignored on the current
thread. |
public static IObservable[] runAndMonitor(Runnable runnable, IChangeListener changeListener, IStaleListener staleListener)
runnable
- runnable to executechangeListener
- listener to register with all accessed observablesstaleListener
- listener to register with all accessed observables, or
null
if no stale listener is to be registeredpublic static IObservable[] runAndCollect(Runnable runnable)
NOTE: As of 1.2 (Eclipse 3.5), there are unresolved problems with this API, see bug 278550. If we cannot find a way to make this API work, it will be deprecated as of 3.6.
runnable
- runnable to executepublic static void setIgnore(boolean ignore)
true
, causes subsequent calls to
getterCalled(IObservable)
and
observableCreated(IObservable)
to be ignored on the current
thread. When the flag is set to false
, calls to
getterCalled(IObservable)
and
observableCreated(IObservable)
will resume gathering
observables. Nested calls to this method are stacked.ignore
- the new ignore stateIllegalStateException
- if
ignore is false and the ignore count is already zero.
getterCalled(IObservable)
,
observableCreated(IObservable)
public static void runAndIgnore(Runnable runnable)
runnable
- public static void getterCalled(IObservable observable)
observable
- public static void observableCreated(IObservable observable)
observable
- the observable that was created
Copyright (c) 2000, 2014 Eclipse Contributors and others. All rights reserved.Guidelines for using Eclipse APIs.