midio_ui/observables/observables

Types

Subscriber[T] = ref object
  onNext: (T) -> void
  onCompleted: Option[proc (): void]
  onError: Option[proc (i0: Error): void] ## \
                                     ## A subscriber is just a procedure that takes in the new value of the observable
  
Subscription = ref object
  dispose: () -> void
Observable[T] = ref object
  onSubscribe: (Subscriber[T]) -> Subscription ## \
                                           ## An observable is a procedure which when called with a subscriber as its argument
                                           ## creates a subscription, which causes the subscriber proc to get called whenever
                                           ## the value of the observable changes.
                                           ## 
                                           ## Note that currently, the observable doesn't have any way of removing subscriptions.
                                           ## This must be added in the future as this feature becomes necessity.
  
Subject[T] = ref object
  source*: Observable[T]
  value*: T
  didComplete: bool
  subscribers: seq[Subscriber[T]]
A subject is an object that contains an observable source, maintains a list of subscribers and also keeps a reference or copy to the last value of the observable source. One has to use either a behaviorSubject or normal subject in order to create an observable over a value.

Procs

proc subscribe[T](self: Observable[T]; subscriber: Subscriber[T]): Subscription
proc subscribe[T](self: Subject[T]; subscriber: Subscriber[T]): Subscription
proc subscribe[T](self: Observable[T]; onNext: (T) -> void): Subscription
proc subscribe[T](self: Observable[T]; onNext: (T) -> void; onCompleted: () -> void): Subscription
proc subscribe[T](self: Observable[T]; onNext: (T) -> void;
                 onCompleted: Option[proc (): void];
                 onError: Option[proc (i0: Error): void]): Subscription
proc subscribe[T](self: Subject[T]; onNext: (T) -> void): Subscription
proc behaviorSubject[T](value: T): Subject[T]
Creates a behaviorSubject with the initial value of value. Behavior subjects notifies as soon as a subscription is created.
proc create[T](onSubscribe: (Subscriber[T]) -> Subscription): Observable[T]
proc create[T](values: seq[T]): Observable[T]
proc then[T](first: Observable[T]; second: Observable[T]): Observable[T]
proc behaviorSubject[T](source: Observable[T]): Subject[T]
Creates a behaviorSubject from another observable. This is useful when one has an observable which one would like to use as a value, exposing the latest value through the subjects .value field.
proc subject[T](): Subject[T]
Creates a normal subject, which has no value, and only notifies their subscriber the next time a new value is pushed to it.
proc complete[T](self: Subject[T]): void
proc next[T](self: Subject[T]; newVal: T): void
Used to push a new value to the subject, causing it to notify all its subscribers/observers.
proc next[T](self: Subject[T]; transformer: (T) -> T): void
Used to push a new value to the subject, causing it to notify all its subscribers/observers. This overload is useful if one wants to transform the current value using some mapping function.
proc map[T, R](self: Observable[T]; mapper: (T) -> R): Observable[R]
Returns a new Observable which maps values from the source Observable to a new type and value.
proc filter[T](self: Observable[T]; predicate: (T) -> bool): Observable[T]
proc combineLatest[A, B, R](a: Observable[A]; b: Observable[B]; mapper: (A, B) -> R): Observable[
    R]
Combines two observables, pushing both their values through a mapper function that maps to a new Observable type. The new observable triggers when either A or B changes.
proc merge[A](a: Observable[A]; b: Observable[A]): Observable[A]
Combines two observables, pushing both their values through a mapper function that maps to a new Observable type. The new observable triggers when either A or B changes.
proc merge[A](observables: Observable[Observable[A]]): Observable[A]
Subscribes to each observable as they arrive, emitting their values as they are emitted
proc switch[A](observables: Observable[Observable[A]]): Observable[A]
Subscribes to each observable as they arrive after first unsubscribing from the second, emitting their values as they arrive.

Converters

converter toObservable[T](subject: Subject[T]): Observable[T]
Gets the source observable from the subject, letts one treat a subject as it it was just a normal observable.

Templates

template extract[T](self: Observable[T]; prop: untyped): untyped