Skip to content

Commit

Permalink
fix(audit): don't signal on complete
Browse files Browse the repository at this point in the history
BREAKING CHANGE: the observable returned by the audit operator's duration selector must emit a next notification to end the duration. Complete notifications no longer end the duration.
  • Loading branch information
cartant authored and benlesh committed Nov 3, 2020
1 parent 374138e commit 54cb428
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 21 deletions.
38 changes: 19 additions & 19 deletions spec/operators/audit-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ describe('audit operator', () => {
testScheduler.run(({ hot, cold, expectObservable, expectSubscriptions }) => {
const e1 = hot(' -a-xy-----b--x--cxxx-|');
const e1subs = ' ^--------------------!';
const e2 = cold(' ----| ');
const e2 = cold(' ----x ');
const e2subs = [
' -^---! ',
' ----------^---! ',
Expand All @@ -36,7 +36,7 @@ describe('audit operator', () => {
testScheduler.run(({ hot, cold, expectObservable, expectSubscriptions }) => {
const e1 = hot(' -a--------b-----c----|');
const e1subs = ' ^--------------------!';
const e2 = cold(' ----| ');
const e2 = cold(' ----x ');
const e2subs = [
' -^---! ',
' ----------^---! ',
Expand Down Expand Up @@ -122,7 +122,7 @@ describe('audit operator', () => {
testScheduler.run(({ hot, cold, expectObservable, expectSubscriptions }) => {
const e1 = hot(' abcdefabcdefabcdefabcdefa| ');
const e1subs = ' ^------------------------! ';
const e2 = cold(' -----| ');
const e2 = cold(' -----x ');
const e2subs = [
' ^----! ',
' ------^----! ',
Expand All @@ -140,11 +140,11 @@ describe('audit operator', () => {
});
});

it('should mirror source if durations are always empty', () => {
it('should mirror source if durations are immediate', () => {
testScheduler.run(({ hot, cold, expectObservable, expectSubscriptions }) => {
const e1 = hot(' abcdefabcdefabcdefabcdefa|');
const e1subs = ' ^------------------------!';
const e2 = cold(' |');
const e2 = cold(' x');
const expected = 'abcdefabcdefabcdefabcdefa|';

const result = e1.pipe(audit(() => e2));
Expand All @@ -154,12 +154,12 @@ describe('audit operator', () => {
});
});

it('should mirror source if durations are EMPTY', () => {
it('should emit no values if durations are EMPTY', () => {
testScheduler.run(({ hot, expectObservable, expectSubscriptions }) => {
const e1 = hot('abcdefabcdefabcdefabcdefa|');
const e1subs = '^------------------------!';
const e2 = EMPTY;
const expected = 'abcdefabcdefabcdefabcdefa|';
const expected = '-------------------------|';

const result = e1.pipe(audit(() => e2));

Expand Down Expand Up @@ -235,11 +235,11 @@ describe('audit operator', () => {
const e1 = hot(' abcdefabcdabcdefghabca| ');
const e1subs = ' ^---------------------! ';
const e2 = [
cold(' -----| '),
cold(' ---| '),
cold(' -------| '),
cold(' --| '),
cold(' ----| ')
cold(' -----x '),
cold(' ---x '),
cold(' -------x '),
cold(' --x '),
cold(' ----x ')
];
const e2subs = [
' ^----! ',
Expand All @@ -266,8 +266,8 @@ describe('audit operator', () => {
const e1 = hot(' abcdefabcdabcdefghabca|');
const e1subs = ' ^----------------! ';
const e2 = [
cold(' -----| '),
cold(' ---| '),
cold(' -----x '),
cold(' ---x '),
cold(' -------# ')
];
const e2subs = [
Expand All @@ -293,13 +293,13 @@ describe('audit operator', () => {
const e1 = hot('abcdefabcdabcdefghabca| ');
const e1subs = '^---------! ';
const e2 = [
cold(' -----| '),
cold(' ---| '),
cold(' -------| ')
cold(' -----x '),
cold(' ---x '),
cold(' -------x ')
];
const e2subs = [
' ^----! ',
' ------^--! '
' ------^--! '
];
const expected = '-----f---d# ';

Expand Down Expand Up @@ -458,7 +458,7 @@ describe('audit operator', () => {
testScheduler.run(({ hot, cold, expectObservable, expectSubscriptions }) => {
const e1 = hot('-a--------xy| ');
const e1subs = ' ^-----------! ';
const e2 = cold(' ----| ');
const e2 = cold(' ----x ');
const e2subs = [
' -^---! ',
' ----------^---!'
Expand Down
9 changes: 7 additions & 2 deletions src/internal/operators/audit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import { OperatorSubscriber } from './OperatorSubscriber';
* Initially, the timer is disabled. As soon as the first source value arrives,
* the timer is enabled by calling the `durationSelector` function with the
* source value, which returns the "duration" Observable. When the duration
* Observable emits a value or completes, the timer is disabled, then the most
* Observable emits a value, the timer is disabled, then the most
* recent source value is emitted on the output Observable, and this process
* repeats for the next source value.
*
Expand Down Expand Up @@ -69,6 +69,11 @@ export function audit<T>(durationSelector: (value: T) => ObservableInput<any>):
isComplete && subscriber.complete();
};

const cleanupDuration = () => {
durationSubscriber = null;
isComplete && subscriber.complete();
};

source.subscribe(
new OperatorSubscriber(
subscriber,
Expand All @@ -77,7 +82,7 @@ export function audit<T>(durationSelector: (value: T) => ObservableInput<any>):
lastValue = value;
if (!durationSubscriber) {
innerFrom(durationSelector(value)).subscribe(
(durationSubscriber = new OperatorSubscriber(subscriber, endDuration, undefined, endDuration))
(durationSubscriber = new OperatorSubscriber(subscriber, endDuration, undefined, cleanupDuration))
);
}
},
Expand Down

0 comments on commit 54cb428

Please sign in to comment.