Class ObservableTracker
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.
- Since:
- 1.0
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic 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 istrue
, causes subsequent calls togetterCalled(IObservable)
andobservableCreated(IObservable)
to be ignored on the current thread.
-
Constructor Details
-
ObservableTracker
public ObservableTracker()
-
-
Method Details
-
runAndMonitor
public 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. If the runnable calls this method recursively, the result will not contain IObservables that were used within the inner runnable.- Parameters:
runnable
- runnable to executechangeListener
- listener to register with all accessed observables, ornull
if no change listener is to be registeredstaleListener
- listener to register with all accessed observables, ornull
if no stale listener is to be registered- Returns:
- an array of unique observable objects
-
runAndCollect
Invokes the given runnable, and returns the set of IObservables that were created by the runnable. If the runnable calls this method recursively, the result will not contain IObservables that were created within the inner 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.
- Parameters:
runnable
- runnable to execute- Returns:
- an array of unique observable objects
- Since:
- 1.2
-
setIgnore
public static void setIgnore(boolean ignore) If the argument istrue
, causes subsequent calls togetterCalled(IObservable)
andobservableCreated(IObservable)
to be ignored on the current thread. When the flag is set tofalse
, calls togetterCalled(IObservable)
andobservableCreated(IObservable)
will resume gathering observables. Nested calls to this method are stacked.- Parameters:
ignore
- the new ignore state- Throws:
IllegalStateException
- ifignore
is false and the ignore count is already zero.- Since:
- 1.3
- See Also:
-
runAndIgnore
Runs the given runnable without tracking dependencies.- Parameters:
runnable
- the runnable to execute- Since:
- 1.1
- See Also:
-
getterCalled
Notifies the ObservableTracker that an observable was read from. The JavaDoc for methods that invoke this method should include the following tag: "@TrackedGetter This method will notify ObservableTracker that the receiver has been read from". This lets callers know that they can rely on automatic updates from the object without explicitly attaching a listener.- Parameters:
observable
- the observable whose getter was called
-
observableCreated
Notifies the ObservableTracker that an observable was created.- Parameters:
observable
- the observable that was created- Since:
- 1.2
-