Skip to content

Commit dd9c876

Browse files
committed
refactor(concatIfEmpty): Rename.
1 parent cb83b92 commit dd9c876

File tree

4 files changed

+32
-27
lines changed

4 files changed

+32
-27
lines changed

source/operators/defaultObservableIfEmpty-spec.ts renamed to source/operators/concatIfEmpty-spec.ts

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,10 @@
44
*/
55
/*tslint:disable:no-unused-expression*/
66

7-
import { expect } from "chai";
87
import { marbles } from "rxjs-marbles";
9-
import { defaultObservableIfEmpty, switchIfEmpty } from "./defaultObservableIfEmpty";
8+
import { concatIfEmpty } from "./concatIfEmpty";
109

11-
describe("defaultObservableIfEmpty", () => {
12-
13-
it("should export an alias", () => {
14-
15-
expect(switchIfEmpty).to.equal(defaultObservableIfEmpty);
16-
});
10+
describe("concatIfEmpty", () => {
1711

1812
it("should return the source if not empty", marbles((m) => {
1913

@@ -22,7 +16,7 @@ describe("defaultObservableIfEmpty", () => {
2216
const def = m.cold("--d--|");
2317
const expected = m.cold("--a--b--c--|");
2418

25-
const destination = source.pipe(defaultObservableIfEmpty(def));
19+
const destination = source.pipe(concatIfEmpty(def));
2620
m.expect(destination).toBeObservable(expected);
2721
m.expect(source).toHaveSubscriptions(subs);
2822
m.expect(def).toHaveSubscriptions([]);
@@ -36,7 +30,7 @@ describe("defaultObservableIfEmpty", () => {
3630
const defSubs = "----^----!";
3731
const expected = m.cold("------d--|");
3832

39-
const destination = source.pipe(defaultObservableIfEmpty(def));
33+
const destination = source.pipe(concatIfEmpty(def));
4034
m.expect(destination).toBeObservable(expected);
4135
m.expect(source).toHaveSubscriptions(sourceSubs);
4236
m.expect(def).toHaveSubscriptions(defSubs);

source/operators/concatIfEmpty.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* @license Use of this source code is governed by an MIT-style license that
3+
* can be found in the LICENSE file at https://github.com/cartant/rxjs-etc
4+
*/
5+
6+
import { EMPTY, merge, Observable, OperatorFunction } from "rxjs";
7+
import { isEmpty, mergeMap, publish } from "rxjs/operators";
8+
9+
export function concatIfEmpty<T, D = T>(
10+
observable: Observable<D>
11+
): OperatorFunction<T, D | T> {
12+
13+
return source => source.pipe(
14+
publish(sharedSource => merge(
15+
sharedSource,
16+
sharedSource.pipe(
17+
isEmpty(),
18+
mergeMap(empty => empty ? observable : EMPTY)
19+
)
20+
))
21+
);
22+
}

source/operators/defaultObservableIfEmpty.ts

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,10 @@
33
* can be found in the LICENSE file at https://github.com/cartant/rxjs-etc
44
*/
55

6-
import { EMPTY, merge, Observable, OperatorFunction } from "rxjs";
7-
import { isEmpty, mergeMap, publish } from "rxjs/operators";
6+
import { concatIfEmpty } from "./concatIfEmpty";
87

9-
export function defaultObservableIfEmpty<T, D = T>(
10-
defaultObservable: Observable<D>
11-
): OperatorFunction<T, D | T> {
8+
/** @deprecated Renamed to concatIfEmpty */
9+
export const defaultObservableIfEmpty = concatIfEmpty;
1210

13-
return (source: Observable<T>) => source.pipe(
14-
publish(sharedSource => merge(
15-
sharedSource,
16-
sharedSource.pipe(
17-
isEmpty(),
18-
mergeMap((b: boolean) => b ? defaultObservable : EMPTY)
19-
)
20-
))
21-
);
22-
}
23-
24-
export const switchIfEmpty = defaultObservableIfEmpty;
11+
/** @deprecated Renamed to concatIfEmpty */
12+
export const switchIfEmpty = concatIfEmpty;

source/operators/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
*/
55

66
export * from "./bufferRecent";
7+
export * from "./concatIfEmpty";
78
export * from "./debounceAfter";
89
export * from "./debounceTimeSubsequent";
910
export * from "./deferFinalize";

0 commit comments

Comments
 (0)