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

[event-hubs] better type guards for event position #7718

Merged
merged 1 commit into from
Mar 9, 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
32 changes: 28 additions & 4 deletions sdk/eventhub/event-hubs/src/eventPosition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,6 @@ export function validateEventPositions(
return;
}

const offsetPresent = position.offset != undefined;
const sequenceNumberPresent = position.sequenceNumber != undefined;
const enqueuedOnPresent = position.enqueuedOn != undefined;
const keys = Object.keys(position);

if (!keys.length) {
Expand All @@ -129,7 +126,7 @@ export function validateEventPositions(
);
}

if (offsetPresent || sequenceNumberPresent || enqueuedOnPresent || !keys.length) {
if (isEventPosition(position)) {
validateEventPosition(position);
return;
}
Expand All @@ -142,6 +139,33 @@ export function validateEventPositions(
}
}

/**
* Determines whether a position is an EventPosition.
* Does not validate that the position is allowed.
* @param position
* @ignore
* @internal
*/
export function isEventPosition(position: any): position is EventPosition {
if (!position) {
return false;
}

if (position.offset != undefined) {
return true;
}

if (position.sequenceNumber != undefined) {
return true;
}

if (position.enqueuedOn != undefined) {
return true;
}

return false;
}

function validateEventPosition(position: EventPosition) {
if (position == undefined) {
return;
Expand Down
8 changes: 2 additions & 6 deletions sdk/eventhub/event-hubs/src/eventProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { logger, logErrorStackTrace } from "./log";
import { FairPartitionLoadBalancer, PartitionLoadBalancer } from "./partitionLoadBalancer";
import { PartitionProcessor, Checkpoint } from "./partitionProcessor";
import { SubscriptionEventHandlers } from "./eventHubConsumerClientModels";
import { EventPosition, latestEventPosition } from "./eventPosition";
import { EventPosition, latestEventPosition, isEventPosition } from "./eventPosition";
import { delayWithoutThrow } from "./util/delayWithoutThrow";
import { CommonEventProcessorOptions } from "./models/private";
import { CloseReason } from "./models/public";
Expand Down Expand Up @@ -587,11 +587,7 @@ function getStartPosition(
return latestEventPosition;
}

if (
startPositions.offset != undefined ||
startPositions.sequenceNumber != undefined ||
startPositions.enqueuedOn != undefined
) {
if (isEventPosition(startPositions)) {
return startPositions;
}

Expand Down