sample

signature: sample(sampler: Observable): Observable

Sample from source when provided observable emits.

Examples

Example 1: Sample source every 2 seconds

( jsBin | jsFiddle )

import { interval } from 'rxjs/observable/interval';
import { sample } 'rxjs/operators';

//emit value every 1s
const source = interval(1000);
//sample last emitted value from source every 2s
const example = source.sample.pipe(interval(2000));
//output: 2..4..6..8..
const subscribe = example.subscribe(val => console.log(val));
Example 2: Sample source when interval emits

( jsBin | jsFiddle )

import { interval } from 'rxjs/observable/interval';
import { zip } from 'rxjs/observable/zip';
import { from } from 'rxjs/observable/from';
import { sample } 'rxjs/operators';

const source = zip(
  //emit 'Joe', 'Frank' and 'Bob' in sequence
  from(['Joe', 'Frank', 'Bob']),
  //emit value every 2s
  interval(2000)
);
//sample last emitted value from source every 2.5s
const example = source.pipe(sample(interval(2500)));
//output: ["Joe", 0]...["Frank", 1]...........
const subscribe = example.subscribe(val => console.log(val));
Example 3: Distinguish between drag and click

From Stack Overflow By Dorus

( jsBin | jsFiddle )

import { fromEvent } from 'rxjs/observable/interval';
import { sample, mapTo } 'rxjs/operators';

const listener = merge(
    fromEvent(element, 'mousedown').mapTo(false),
    fromEvent(element, 'mousemove').mapTo(true)
  )
  .pipe(sample(fromEvent(element, 'mouseup')))
  .subscribe(isDragging => {
    console.log('Were you dragging?', isDragging);
  })

Additional Resources


:file_folder: Source Code: https://github.com/ReactiveX/rxjs/blob/master/src/internal/operators/sample.ts

results matching ""

    No results matching ""