Skip to main content

Filter Operators

filter

Filter items emitted by the source Observable by only emitting those that satisfy a specified predicate.

output.svg
import { from, filter } from "@devicescript/observables"

const obs = from([0, 1, 2, 3, 1])

obs
.pipe(filter(v => v > 1))
.subscribe(v => console.log(v))

debounceTime

Emits a notification from the source Observable only after a particular time span has passed without another source emission.

output.svg

The example below debounce a button down event to one every 5 seconds.

import { Button } from "@devicescript/core"
import { debounceTime } from "@devicescript/observables"

const button = new Button()

button.down
.pipe(debounceTime(5000))
.subscribe(() => console.log("click"))

throttleTime

Emits a value from the source Observable, then ignores subsequent source values for duration milliseconds, then repeats this process.

output.svg

The example below debounce a button down event to one every 5 seconds.

import { Button } from "@devicescript/core"
import { throttleTime } from "@devicescript/observables"

const button = new Button()

button.down
.pipe(throttleTime(1000))
.subscribe(() => console.log("click"))

auditTime

Ignores source values for duration milliseconds, then emits the most recent value from the source Observable, then repeats this process

auditTime is similar to throttleTime, but emits the last value from the silenced time window, instead of the first value.

output.svg

The example below debounce a button down event to one every 5 seconds.

import { Button } from "@devicescript/core"
import { auditTime } from "@devicescript/observables"

const button = new Button()

button.down
.pipe(auditTime(1000))
.subscribe(() => console.log("click"))

threshold

Filters numerical data stream within change threshold.

output.svg

The example tracks the temperature returned

import { Temperature } from "@devicescript/core"
import { threshold } from "@devicescript/observables"

const thermometer = new Temperature()

thermometer.reading
.pipe(threshold(2))
.subscribe(t => console.log(t))

distinctUntilChanged

Returns a result Observable that emits all values pushed by the source observable if they are distinct in comparison to the last value the result observable emitted.

This function can be used to create user-friendly transformer. For example, thershold is as follows:

export function threshold<T = number>(
value: number,
...
): OperatorFunction<T, T> {
return distinctUntilChanged<T, number>(
(l, r) => Math.abs(l - r) < value,
keySelector
)
}

Transform operators

map

Applies a converter function to converts the observed value into a new value.

output.svg
import { from, map } from "@devicescript/observables"

const obs = from([1, 2, 3])

obs
.pipe(map(v => v * v))
.subscribe(v => console.log(v))

scan

Applies an accumulator (or "reducer function") to each value from the source.

output.svg
import { from, scan } from "@devicescript/observables"

const obs = from([1, 2, 3])

obs.pipe(
scan((p, c) => p + c, 0)
).subscribe(v => console.log(v))

Signal processing

ewma

Exponentially weighted moving average is a simple PIR filter with a gain parameter. The closer gain to 1 and the more closely the EWMA tracks the original time series.

import { Temperature } from "@devicescript/core"
import { ewma } from "@devicescript/observables"

const thermometer = new Temperature()

thermometer.reading
.pipe(ewma(0.5))
.subscribe(t => console.log(t))

rollingAverage

A windowed rolling average filter.

import { Temperature } from "@devicescript/core"
import { rollingAverage } from "@devicescript/observables"