On this page:
Obs
Obs.of
Obs  Or  Value.of
Obs.later_  of
Obs.now_  of
Obs  Or  Value.later_  of
Obs  Or  Value.now_  of
Obs.handle
Obs.value
Obs.observe
Obs.unobserve
Obs.update
<~
Obs.peek
Obs.rename
Obs.map
~>
Obs.debounce
Obs.throttle
Obs.combine
8.17.0.1

1 Observables🔗ℹ

An observable holds a value, where the value in an observable can be updated, and graphical elements can react automatically to update. For example, the label or enable state for a button can be supplied as an observable, and if the obervable’s value changes, then the graphical representation of the button changes automatically to match.

An observable corresponds to #{obs?} from racket/gui/easy.

class

class gui.Obs():

  constructor (v :: Any,

               ~name: name :: String = "anon",

               ~is_derived: is_derived :: Any = #false)

 

annotation

gui.Obs.of(annot)

 

annotation

gui.ObsOrValue.of(annot)

 

annotation

gui.Obs.later_of(annot)

 

annotation

gui.Obs.now_of(annot)

 

annotation

gui.ObsOrValue.later_of(annot)

 

annotation

gui.ObsOrValue.now_of(annot)

The Obs constructor creates an observable whose initial value is v.

The annotation Obs.of(annot) is satisfied by an annotation whose value satisfies annot. The predicate and conversion (if any) associated with annot is not applied immediately, but it is applied every time a value is extracted or put into the observable. The annotation ObsOrValue.of(annot) is satisfied by a value that satisfies either annot or Obs.of(annot).

The Obs.now_of(annot) annotation is Obs.of(annot), but the current value in the observable is checked immediately, and it is not checked later or when a new value is supplied to the observable. In other words, Obs.now_of(annot) is a predicate annotation, while Obs.of(annot) is a converter annotation. The annot argument in Obs.now_of(annot) must be a predicate annotation. The ObsOrValue.now_of(annot) annotation is analogously like ObsOrValue.of(annot).

The Obs.later_of(annot) and ObsOrValue.later_of(annot) annotations are aliases for Obs.of(annot) and ObsOrValue.of(annot), respectively.

property

property (obs :: gui.Obs).handle :: Any

Returns a Racket object that corresponds to the observable for use directly with racket/gui/easy.

property

property

| (obs :: gui.Obs).value :: Any

| (obs :: gui.Obs).value := (v :: Any)

Returns the value via Obs.peek (which you shouldn’t normally do) or updates the value via Obs.update (ignoring the current value).

def o = Obs("apple")

> o.value

"apple"

> o.value := "banana"

> o.value

"banana"

method

method (obs :: gui.Obs).observe(f :: Function.of_arity(1))

  :: Void

Adds f as a function to be called when the value of obs changes.

method

method (obs :: gui.Obs).unobserve(f :: Function.of_arity(1))

  :: Void

Removes f as a function to be called when the value of obs changes.

method

method (obs :: gui.Obs).update(f :: Function.of_arity(1))

  :: Any

 

operator

operator ((obs :: gui.Obs) <~ (f :: Function.of_arity(1)))

  :: Any

Changes the value v of obs to f(v). Returns the new value.

method

method (obs :: gui.Obs).peek() :: Any

Returns the current value of obs.

Normally, instead of peeking an observable’s value, you should either register an observer or pass an observer to a constructor that expects one. For example, when an observer’s value should affect drawing in a Canvas, then the first argument to Canvas should the observer (or one derived from it), and then a rendered canvas will be updated when the observable changes.

method

method (obs :: gui.Obs).rename(name :: String) :: Obs

Returns an observer like obs, but named as name.

method

method (obs :: gui.Obs).map(f :: Function.of_arity(1))

  :: Obs

 

operator

operator ((obs :: gui.Obs) ~> (f :: Function.of_arity(1)))

  :: Obs

Returns an observer whose value changes each time that obs’s value changes, where the new observer’s value is changed to f(v) when obs is changed to v.

method

method (obs :: gui.Obs).debounce(

  ~duration: msec :: NonnegInt = 200

) :: Obs

Returns a new observable whose value changes to the value of obs when there is at least a msec millisecond pause in changes to obs.

method

method (obs :: gui.Obs).throttle(

  ~duration: msec :: NonnegInt = 200

) :: Obs

Returns a new observable whose value changes to the value of obs at most once per msec milliseconds.

function

fun gui.Obs.combine(f :: Function, obs :: Obs, ...)

  :: Obs

 

function

fun gui.Obs.combine({key: obs :: Obs, ...})

  :: Obs

Returns a new observable whose value changes to the value of f(obs.value, ...) or {key: obs.value, ...} when the value of any obs changes.