Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(audit, auditTime): audit and auditTime emit last value after source completes #5799

Merged
merged 5 commits into from
Oct 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 46 additions & 29 deletions spec/operators/audit-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,17 +120,17 @@ describe('audit operator', () => {

it('should handle a busy producer emitting a regular repeating sequence', () => {
testScheduler.run(({ hot, cold, expectObservable, expectSubscriptions }) => {
const e1 = hot(' abcdefabcdefabcdefabcdefa|');
const e1subs = ' ^------------------------!';
const e2 = cold(' -----| ');
const e1 = hot(' abcdefabcdefabcdefabcdefa| ');
const e1subs = ' ^------------------------! ';
const e2 = cold(' -----| ');
const e2subs = [
' ^----! ',
' ------^----! ',
' ------------^----! ',
' ------------------^----! ',
' ------------------------^!'
' ^----! ',
' ------^----! ',
' ------------^----! ',
' ------------------^----! ',
' ------------------------^----!'
];
const expected = '-----f-----f-----f-----f-|';
const expected = '-----f-----f-----f-----f-----(a|)';

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

Expand Down Expand Up @@ -168,13 +168,13 @@ describe('audit operator', () => {
});
});

it('should emit no values if duration is a never', () => {
it('should emit no values and never complete if duration is a never', () => {
testScheduler.run(({ hot, cold, expectObservable, expectSubscriptions }) => {
const e1 = hot(' ----abcdefabcdefabcdefabcdefa|');
const e1subs = ' ^----------------------------!';
const e2 = cold(' -');
const e2subs = ' ----^------------------------!';
const expected = '-----------------------------|';
const e2subs = ' ----^-------------------------';
const expected = '------------------------------';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmmm... I'll need to think about this. I think this makes the most sense. Given that there are items to audit, and we're waiting for a duration that just never arrives... But I want a second opinion: @cartant

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I think this makes sense, too. Above all, the behaviour needs to be consistent between operators and, AFAICT, delayWhen doesn't complete if its notifier is NEVER:

it('should not emit if selector never emits', () => {
const e1 = hot('--a--b--|');
const expected = '-';
const subs = '^ !';
const selector = cold( '-');
const selectorSubs = [' ^ ',
' ^ '];
const result = e1.pipe(delayWhen((x: any) => selector));
expectObservable(result).toBe(expected);
expectSubscriptions(e1.subscriptions).toBe(subs);
expectSubscriptions(selector.subscriptions).toBe(selectorSubs);
});

So this LGTM.


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

Expand Down Expand Up @@ -232,23 +232,23 @@ describe('audit operator', () => {

it('should audit using durations of varying lengths', () => {
testScheduler.run(({ hot, cold, expectObservable, expectSubscriptions }) => {
const e1 = hot(' abcdefabcdabcdefghabca|');
const e1subs = ' ^---------------------!';
const e1 = hot(' abcdefabcdabcdefghabca| ');
const e1subs = ' ^---------------------! ';
const e2 = [
cold(' -----| '),
cold(' ---| '),
cold(' -------| '),
cold(' --| '),
cold(' ----| ')
cold(' -----| '),
cold(' ---| '),
cold(' -------| '),
cold(' --| '),
cold(' ----| ')
];
const e2subs = [
' ^----! ',
' ------^--! ',
' ----------^------! ',
' ------------------^-! ',
' ---------------------^! '
' ^----! ',
' ------^--! ',
' ----------^------! ',
' ------------------^-! ',
' ---------------------^---! '
];
const expected = '-----f---d-------h--c-| ';
const expected = '-----f---d-------h--c----(a|)';

let i = 0;
const result = e1.pipe(audit(() => e2[i++]));
Expand Down Expand Up @@ -391,12 +391,10 @@ describe('audit operator', () => {

it('should audit by promise resolves', (done: MochaDone) => {
const e1 = interval(10).pipe(take(5));
const expected = [0, 1, 2, 3];
const expected = [0, 1, 2, 3, 4];

e1.pipe(
audit(() => {
return new Promise((resolve: any) => { resolve(42); });
})
audit(() => Promise.resolve(42))
).subscribe(
(x: number) => {
expect(x).to.equal(expected.shift()); },
Expand Down Expand Up @@ -455,4 +453,23 @@ describe('audit operator', () => {

expect(sideEffects).to.deep.equal([0, 1, 2]);
});

it('should emit last value after duration completes if source completes first', () => {
testScheduler.run(({ hot, cold, expectObservable, expectSubscriptions }) => {
const e1 = hot('-a--------xy| ');
const e1subs = ' ^-----------! ';
const e2 = cold(' ----| ');
const e2subs = [
' -^---! ',
' ----------^---!'
];
const expected = '-----a--------(y|)';

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

expectObservable(result).toBe(expected);
expectSubscriptions(e1.subscriptions).toBe(e1subs);
expectSubscriptions(e2.subscriptions).toBe(e2subs);
});
});
});
10 changes: 5 additions & 5 deletions spec/operators/auditTime-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ describe('auditTime operator', () => {
testScheduler.run(({ hot, expectObservable, expectSubscriptions }) => {
const e1 = hot(' -a-x-y----b---x-cx---|');
const subs = ' ^--------------------!';
const expected = '------y--------x-----|';
const expected = '------y--------x-----(x|)';

const result = e1.pipe(auditTime(5, testScheduler));

Expand All @@ -26,11 +26,11 @@ describe('auditTime operator', () => {
});

it('should auditTime events by 5 time units', (done: MochaDone) => {
const expected = 3;
of(1, 2, 3).pipe(
auditTime(5)
).subscribe((x: number) => {
done(new Error('should not be called'));
}, null, () => {
expect(x).to.equal(expected);
done();
});
});
Expand All @@ -50,7 +50,7 @@ describe('auditTime operator', () => {
testScheduler.run(({ hot, expectObservable, expectSubscriptions }) => {
const e1 = hot(' -a--------b-----c----|');
const subs = ' ^--------------------!';
const expected = '------a--------b-----|';
const expected = '------a--------b-----(c|)';

expectObservable(e1.pipe(auditTime(5, testScheduler))).toBe(expected);
expectSubscriptions(e1.subscriptions).toBe(subs);
Expand All @@ -61,7 +61,7 @@ describe('auditTime operator', () => {
testScheduler.run(({ hot, expectObservable, expectSubscriptions }) => {
const e1 = hot(' abcdefabcdefabcdefabcdefa|');
const subs = ' ^------------------------!';
const expected = '-----f-----f-----f-----f-|';
const expected = '-----f-----f-----f-----f-----(a|)';

expectObservable(e1.pipe(auditTime(5, testScheduler))).toBe(expected);
expectSubscriptions(e1.subscriptions).toBe(subs);
Expand Down
26 changes: 18 additions & 8 deletions src/internal/operators/audit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ export function audit<T>(durationSelector: (value: T) => SubscribableOrPromise<a
let hasValue = false;
let lastValue: T | null = null;
let durationSubscriber: Subscriber<any> | null = null;
let isComplete = false;

const endDuration = () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cartant, do we want to fix the duration notifier treating completion like a notification here? Or shall we get that at a later time? The whole thing is weird. Like [] "Here's your value, in this array!". My gut tells me it should be a noop, and I think we all agreed on that.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, we should fix it. Whether it's fixed here or someplace else, I don't mind. I intend to do another pass through all of the operators that take a notifier to make sure their behaviour is consistent, etc.

durationSubscriber?.unsubscribe();
Expand All @@ -66,18 +67,27 @@ export function audit<T>(durationSelector: (value: T) => SubscribableOrPromise<a
lastValue = null;
subscriber.next(value);
}
isComplete && subscriber.complete();
};

source.subscribe(
new OperatorSubscriber(subscriber, (value) => {
hasValue = true;
lastValue = value;
if (!durationSubscriber) {
innerFrom(durationSelector(value)).subscribe(
(durationSubscriber = new OperatorSubscriber(subscriber, endDuration, undefined, endDuration))
);
new OperatorSubscriber(
subscriber,
(value) => {
hasValue = true;
lastValue = value;
if (!durationSubscriber) {
innerFrom(durationSelector(value)).subscribe(
(durationSubscriber = new OperatorSubscriber(subscriber, endDuration, undefined, endDuration))
);
}
},
undefined,
() => {
isComplete = true;
(!hasValue || !durationSubscriber || durationSubscriber.closed) && subscriber.complete();
}
})
)
);
});
}