-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(empty): empty() returns the same instance
- Adds more robust tests - Ensures that all calls to empty without a scheduler return a single instance - Removes EmptyObservable - Updates other tests and code using EmptyObservable EmptyObservable BREAKING CHANGE: `empty()` without a scheduler will return the same instance every time. BREAKING CHANGE: In TypeScript, `empty()` no longer accepts a generic argument, as it returns `Observable<never>`
- Loading branch information
Showing
10 changed files
with
101 additions
and
112 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,42 @@ | ||
import * as Rx from '../../src/Rx'; | ||
import marbleTestingSignature = require('../helpers/marble-testing'); // tslint:disable-line:no-require-imports | ||
import { empty } from '../../src/create'; | ||
import { expect } from 'chai'; | ||
|
||
declare const { asDiagram }; | ||
declare const asDiagram: any; | ||
declare const expectObservable: typeof marbleTestingSignature.expectObservable; | ||
|
||
const Observable = Rx.Observable; | ||
declare const rxTestScheduler: any; | ||
|
||
/** @test {empty} */ | ||
describe('Observable.empty', () => { | ||
describe('empty', () => { | ||
asDiagram('empty')('should create a cold observable with only complete', () => { | ||
const expected = '|'; | ||
const e1 = Observable.empty(); | ||
const e1 = empty(); | ||
expectObservable(e1).toBe(expected); | ||
}); | ||
|
||
it('should return the same instance EMPTY', () => { | ||
const s1 = empty(); | ||
const s2 = empty(); | ||
expect(s1).to.equal(s2); | ||
}); | ||
|
||
it('should be synchronous by default', () => { | ||
const source = empty(); | ||
let hit = false; | ||
source.subscribe({ | ||
complete() { hit = true; } | ||
}); | ||
expect(hit).to.be.true; | ||
}); | ||
|
||
it('should take a scheduler', () => { | ||
const source = empty(rxTestScheduler); | ||
let hit = false; | ||
source.subscribe({ | ||
complete() { hit = true; } | ||
}); | ||
expect(hit).to.be.false; | ||
rxTestScheduler.flush(); | ||
expect(hit).to.be.true; | ||
}); | ||
}); |
This file contains 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
This file contains 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
This file was deleted.
Oops, something went wrong.
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,55 @@ | ||
import { EmptyObservable } from './EmptyObservable'; | ||
import { Observable } from '../Observable'; | ||
import { IScheduler } from '../Scheduler'; | ||
|
||
export const empty = EmptyObservable.create; | ||
export const EMPTY = new Observable<never>(subscriber => subscriber.complete()); | ||
|
||
/** | ||
* Creates an Observable that emits no items to the Observer and immediately | ||
* emits a complete notification. | ||
* | ||
* <span class="informal">Just emits 'complete', and nothing else. | ||
* </span> | ||
* | ||
* <img src="./img/empty.png" width="100%"> | ||
* | ||
* This static operator is useful for creating a simple Observable that only | ||
* emits the complete notification. It can be used for composing with other | ||
* Observables, such as in a {@link mergeMap}. | ||
* | ||
* @example <caption>Emit the number 7, then complete.</caption> | ||
* var result = Rx.Observable.empty().startWith(7); | ||
* result.subscribe(x => console.log(x)); | ||
* | ||
* @example <caption>Map and flatten only odd numbers to the sequence 'a', 'b', 'c'</caption> | ||
* var interval = Rx.Observable.interval(1000); | ||
* var result = interval.mergeMap(x => | ||
* x % 2 === 1 ? Rx.Observable.of('a', 'b', 'c') : Rx.Observable.empty() | ||
* ); | ||
* result.subscribe(x => console.log(x)); | ||
* | ||
* // Results in the following to the console: | ||
* // x is equal to the count on the interval eg(0,1,2,3,...) | ||
* // x will occur every 1000ms | ||
* // if x % 2 is equal to 1 print abc | ||
* // if x % 2 is not equal to 1 nothing will be output | ||
* | ||
* @see {@link create} | ||
* @see {@link never} | ||
* @see {@link of} | ||
* @see {@link throw} | ||
* | ||
* @param {Scheduler} [scheduler] A {@link IScheduler} to use for scheduling | ||
* the emission of the complete notification. | ||
* @return {Observable} An "empty" Observable: emits only the complete | ||
* notification. | ||
* @static true | ||
* @name empty | ||
* @owner Observable | ||
*/ | ||
export function empty(scheduler?: IScheduler) { | ||
return scheduler ? emptyScheduled(scheduler) : EMPTY; | ||
} | ||
|
||
export function emptyScheduled(scheduler: IScheduler) { | ||
return new Observable<never>(subscriber => scheduler.schedule(() => subscriber.complete())); | ||
} |
This file contains 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
This file contains 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
This file contains 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
This file contains 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