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(withlatestfrom): allow synchronous source #5828

Merged
merged 1 commit 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
12 changes: 12 additions & 0 deletions spec/operators/withLatestFrom-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -260,4 +260,16 @@ describe('withLatestFrom operator', () => {
expect(x).to.deep.equal([1, 4, 6]);
});
});

it('should work with synchronous observables', () => {
const result: Array<Array<number>> = [];
of(1, 2, 3).pipe(withLatestFrom(of(4, 5))).subscribe((x) => {
result.push(x);
});

expect(result.length).to.equal(3);
expect(result[0]).to.deep.equal([1,5]);
expect(result[1]).to.deep.equal([2,5]);
expect(result[2]).to.deep.equal([3,5]);
})
});
22 changes: 11 additions & 11 deletions src/internal/operators/withLatestFrom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,17 +159,6 @@ export function withLatestFrom<T, R>(...inputs: any[]): OperatorFunction<T, R |
// we are ready to start emitting values.
let ready = false;

// Source subscription
source.subscribe(
new OperatorSubscriber(subscriber, (value) => {
if (ready) {
// We have at least one value from the other sources. Go ahead and emit.
const values = [value, ...otherValues];
subscriber.next(project ? project(...values) : values);
}
})
);

// Other sources. Note that here we are not checking `subscriber.closed`,
// this causes all inputs to be subscribed to, even if nothing can be emitted
// from them. This is an important distinction because subscription constitutes
Expand Down Expand Up @@ -197,5 +186,16 @@ export function withLatestFrom<T, R>(...inputs: any[]): OperatorFunction<T, R |
)
);
}

// Source subscription
source.subscribe(
new OperatorSubscriber(subscriber, (value) => {
if (ready) {
// We have at least one value from the other sources. Go ahead and emit.
const values = [value, ...otherValues];
subscriber.next(project ? project(...values) : values);
}
})
);
});
}