Last active
October 11, 2017 11:30
-
-
Save MikeMKH/c408d2e58f241e2be65891ef75d399b5 to your computer and use it in GitHub Desktop.
Example using switchMap
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "extra", | |
"version": "1.0.0", | |
"description": "", | |
"main": "test.js", | |
"scripts": { | |
"test": "mocha" | |
}, | |
"author": "Mike Harris", | |
"license": "ISC", | |
"dependencies": { | |
"mocha": "^4.0.1", | |
"rxjs": "^5.4.3" | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const assert = require('assert'); | |
const Rx = require('rxjs/Rx'); | |
const BehaviorSubject = require('rxjs/BehaviorSubject'); | |
require('rxjs/add/operator/map'); | |
require('rxjs/add/operator/switchMap'); | |
require('rxjs/add/operator/first'); | |
require('rxjs/add/operator/filter'); | |
describe('switchMap', () => { | |
it('should pass value on', () => { | |
const value = 42; | |
let actual; | |
Rx.Observable.from([value]) | |
.map(x => x + 1) | |
.switchMap(x => Rx.Observable.from([x + 1])) | |
.first() | |
.subscribe(x => actual = x); | |
assert.equal(actual, value + 1 + 1); | |
}), | |
it('should call next observable on change of first', () => { | |
const change$ = new Rx.BehaviorSubject(0); | |
let actual = null; | |
change$ | |
.filter(x => x !== 0) | |
.switchMap(x => Rx.Observable.from([1, 2, 3, 4])) | |
.subscribe(x => actual = x); | |
// default is 0 | |
assert.equal(actual, null); | |
// every 0 is filtered | |
change$.next(0); | |
assert.equal(actual, null); | |
change$.next(1); | |
assert.equal(actual, 4); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment