On this page:
Obs
Obs.of
Maybe  Obs.of
Obs.handle
Obs.value
Obs.observe
Obs.unobserve
Obs.update
<~
Obs.peek
Obs.rename
Obs.map
~>
Obs.debounce
Obs.throttle
Obs.combine
8.12

1 Observables🔗ℹ

class

class Obs():

  constructor (v :: Any,

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

               ~is_derived: is_derived :: Any = #false)

 

annotation

Obs.of(annot)

 

annotation

MaybeObs.of(annot)

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

The annotation Obs.of(annot) is satisfied by an annotation whose current value satisfies annot. The annotation MaybeObs.of(annot) is satisfied by a value that satisfies either annot or Obs.of(annot).

property

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

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

property

property

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

| (obs :: 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 :: 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 :: 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 :: Obs).update(f :: Function.of_arity(1))

  :: Any

 

operator

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

  :: Any

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

method

method (obs :: 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 :: Obs).rename(name :: String) :: Obs

Returns an observer like obs, but named as name.

method

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

  :: Obs

 

operator

operator ((obs :: 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 :: 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 :: 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 Obs.combine(f :: Function.of_arity(1), obs :: Obs, ...)

  :: Obs

 

function

fun 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.