Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(compat): add next rxjs version's exports for forward compatibility
* Also adds throwIfEmpty operator
- Loading branch information
Showing
11 changed files
with
205 additions
and
5 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
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 |
---|---|---|
@@ -0,0 +1,122 @@ | ||
import { expect } from 'chai'; | ||
import { cold, expectObservable, expectSubscriptions } from '../helpers/marble-testing'; | ||
import * as Rx from '../../dist/package/Rx'; | ||
|
||
/** @test {timeout} */ | ||
describe('throwIfEmpty', () => { | ||
describe('with errorFactory', () => { | ||
it('should throw if empty', () => { | ||
const error = new Error('So empty inside'); | ||
let thrown: any; | ||
|
||
Rx.Observable.empty().throwIfEmpty(() => error) | ||
.subscribe({ | ||
error(err) { | ||
thrown = err; | ||
} | ||
}); | ||
|
||
expect(thrown).to.equal(error); | ||
}); | ||
|
||
it('should NOT throw if NOT empty', () => { | ||
const error = new Error('So empty inside'); | ||
let thrown: any; | ||
|
||
Rx.Observable.of('test').throwIfEmpty(() => error) | ||
.subscribe({ | ||
error(err) { | ||
thrown = err; | ||
} | ||
}); | ||
|
||
// tslint:disable-next-line:no-unused-expression | ||
expect(thrown).to.be.undefined; | ||
}); | ||
|
||
it('should pass values through', () => { | ||
const source = cold('----a---b---c---|'); | ||
const sub1 = '^ !'; | ||
const expected = '----a---b---c---|'; | ||
expectObservable( | ||
source.throwIfEmpty(() => new Error('test')) | ||
).toBe(expected); | ||
expectSubscriptions(source.subscriptions).toBe([sub1]); | ||
}); | ||
|
||
it('should never when never', () => { | ||
const source = cold('-'); | ||
const sub1 = '^'; | ||
const expected = '-'; | ||
expectObservable( | ||
source.throwIfEmpty(() => new Error('test')) | ||
).toBe(expected); | ||
expectSubscriptions(source.subscriptions).toBe([sub1]); | ||
}); | ||
|
||
it('should error when empty', () => { | ||
const source = cold('----|'); | ||
const sub1 = '^ !'; | ||
const expected = '----#'; | ||
expectObservable( | ||
source.throwIfEmpty(() => new Error('test')) | ||
).toBe(expected, undefined, new Error('test')); | ||
expectSubscriptions(source.subscriptions).toBe([sub1]); | ||
}); | ||
}); | ||
|
||
describe('without errorFactory', () => { | ||
it('should throw EmptyError if empty', () => { | ||
let thrown: any; | ||
|
||
Rx.Observable.empty().throwIfEmpty() | ||
.subscribe({ | ||
error(err) { | ||
thrown = err; | ||
} | ||
}); | ||
|
||
expect(thrown).to.be.instanceof(Rx.EmptyError); | ||
}); | ||
|
||
it('should NOT throw if NOT empty', () => { | ||
let thrown: any; | ||
|
||
Rx.Observable.of('test').throwIfEmpty() | ||
.subscribe({ | ||
error(err) { | ||
thrown = err; | ||
} | ||
}); | ||
|
||
// tslint:disable-next-line:no-unused-expression | ||
expect(thrown).to.be.undefined; | ||
}); | ||
|
||
it('should pass values through', () => { | ||
const source = cold('----a---b---c---|'); | ||
const sub1 = '^ !'; | ||
const expected = '----a---b---c---|'; | ||
expectObservable(source.throwIfEmpty()).toBe(expected); | ||
expectSubscriptions(source.subscriptions).toBe([sub1]); | ||
}); | ||
|
||
it('should never when never', () => { | ||
const source = cold('-'); | ||
const sub1 = '^'; | ||
const expected = '-'; | ||
expectObservable(source.throwIfEmpty()).toBe(expected); | ||
expectSubscriptions(source.subscriptions).toBe([sub1]); | ||
}); | ||
|
||
it('should error when empty', () => { | ||
const source = cold('----|'); | ||
const sub1 = '^ !'; | ||
const expected = '----#'; | ||
expectObservable( | ||
source.throwIfEmpty() | ||
).toBe(expected, undefined, new Rx.EmptyError()); | ||
expectSubscriptions(source.subscriptions).toBe([sub1]); | ||
}); | ||
}); | ||
}); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
|
||
import { Observable } from '../../Observable'; | ||
import { throwIfEmpty } from '../../operator/throwIfEmpty'; | ||
|
||
Observable.prototype.throwIfEmpty = throwIfEmpty; | ||
|
||
declare module '../../Observable' { | ||
interface Observable<T> { | ||
throwIfEmpty: typeof throwIfEmpty; | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export { ajax } from './observable/dom/ajax'; | ||
export { AjaxRequest, AjaxResponse, AjaxError, AjaxTimeoutError } from './observable/dom/AjaxObservable'; |
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 |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { Observable } from '../Observable'; | ||
import { throwIfEmpty as higherOrder, defaultErrorFactory } from '../operators/throwIfEmpty'; | ||
|
||
export function throwIfEmpty<T>(this: Observable<T>, | ||
errorFactory: (() => any) = defaultErrorFactory): Observable<T> { | ||
return higherOrder(errorFactory)(this) as Observable<T>; | ||
} |
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 |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { tap } from './tap'; | ||
import { EmptyError } from '../util/EmptyError'; | ||
/* tslint:disable:no-unused-variable */ | ||
import { Observable } from '../Observable'; | ||
/* tslint:enable:no-unused-variable */ | ||
|
||
/** | ||
* If the source observable completes without emitting a value, it will emit | ||
* an error. The error will be created at that time by the optional | ||
* `errorFactory` argument, otherwise, the error will be {@link ErrorEmpty}. | ||
* | ||
* @example | ||
* | ||
* const click$ = fromEvent(button, 'click'); | ||
* | ||
* clicks$.pipe( | ||
* takeUntil(timer(1000)), | ||
* throwIfEmpty( | ||
* () => new Error('the button was not clicked within 1 second') | ||
* ), | ||
* ) | ||
* .subscribe({ | ||
* next() { console.log('The button was clicked'); }, | ||
* error(err) { console.error(err); }, | ||
* }); | ||
* @param {Function} [errorFactory] A factory function called to produce the | ||
* error to be thrown when the source observable completes without emitting a | ||
* value. | ||
*/ | ||
export const throwIfEmpty = | ||
<T>(errorFactory: (() => any) = defaultErrorFactory) => tap<T>({ | ||
hasValue: false, | ||
next(this: any) { this.hasValue = true; }, | ||
complete(this: any) { | ||
if (!this.hasValue) { | ||
throw errorFactory(); | ||
} | ||
} | ||
} as any); | ||
|
||
export function defaultErrorFactory() { | ||
return new EmptyError(); | ||
} |
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
export { TestScheduler } from './testing/TestScheduler'; |
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
export { webSocket as websocket } from './observable/dom/webSocket'; |