Observes a Subject; when the Subject broadcasts a SubjectEvent then this executes a specified function.

Example 1

Here is an example of a GenericObserver that prints any event broadcast by a Clock:

var obs = new GenericObserver(clock, evt => println('event='+evt));

Paste that code into the Terminal command line of any application, or try this link. which contains the above code running in the pendulum simulation. Click the rewind, play, and step buttons to see events in the Terminal output area.

Use the following Terminal command to turn off the GenericObserver:

clock.removeObserver(obs);

Example 2

This prints only when a particular Clock event occurs:

var obs = new GenericObserver(clock, evt => {
if (evt.nameEquals(Clock.CLOCK_PAUSE)) {
println('event='+evt);
}
});

Paste that code into the Terminal command line of any application, or try this link which contains the above code running in the pendulum simulation. Click the pause button to see events in the Terminal output area.

Example 3

This sets color of a contact force line according to gap distance: red = zero distance, green = max distance. This is useful to study the effects of using different settings for ExtraAccel.

new GenericObserver(displayList, evt => {
if (evt.nameEquals(DisplayList.OBJECT_ADDED)) {
var obj = evt.getValue();
if (obj instanceof DisplayLine) {
var f = obj.getSimObjects()[0];
if (f.getName().match(/^CONTACT_FORCE1/)) {
var pct = Math.max(0,
Math.min(1, f.contactDistance/f.contactTolerance));
obj.setColor(Util.colorString3(1-pct, pct, 0));
}
}
}
});

The above script can be entered into the Terminal command line of most applications which use ContactSim. Or try this link which contains the above code running in ContactApp. That link also sets EXTRA_ACCEL=none so you will see the gap distance color vary periodically.

Implements

Constructors

  • Parameters

    • subject: Subject

      the Subject to observe

    • observeFn: ((s) => void)

      function to execute when a SubjectEvent is broadcast by Subject, takes a single argument of type SubjectEvent

    • Optional opt_purpose: string

      Describes what this Observer does, for debugging

    Returns GenericObserver

Properties

purpose_: string

Describes what this Observer does, for debugging

Methods

  • Disconnects this GenericObserver from the Subject.

    Returns void

  • Notifies this Observer that a change has occurred in the Subject.

    Parameters

    • event: SubjectEvent

      contains information about what has changed in the Subject: typically either a one-time GenericEvent, or a change to the value of a Parameter

    Returns void

  • Returns a minimal string representation of this object, usually giving just identity information like the class name and name of the object.

    For an object whose main purpose is to represent another Printable object, it is recommended to include the result of calling toStringShort on that other object. For example, calling toStringShort() on a DisplayShape might return something like this:

    DisplayShape{polygon:Polygon{'chain3'}}
    

    Returns string

    a minimal string representation of this object.

Generated using TypeDoc