Skip to content

Commit

Permalink
Avoid optional chainging as older TS versions dont support it
Browse files Browse the repository at this point in the history
  • Loading branch information
ramya-rao-a committed Aug 25, 2020
1 parent 503399b commit c12aeed
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 44 deletions.
6 changes: 3 additions & 3 deletions lib/awaitableSender.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ export class AwaitableSender extends BaseSender {
this.connection.id, this.name, this.session.id, this.credit,
this.session.outgoing.available());

if (abortSignal?.aborted) {
if (abortSignal && abortSignal.aborted) {
const err = createAbortError("Send request has been cancelled.");
log.error("[%s] %s", this.connection.id, err.message);
return reject(err);
Expand Down Expand Up @@ -215,7 +215,7 @@ export class AwaitableSender extends BaseSender {
};

const removeAbortListener = () => {
abortSignal?.removeEventListener("abort", onAbort);
if (abortSignal) { abortSignal.removeEventListener("abort", onAbort); }
};

const delivery = (this._link as RheaSender).send(msg, tag, format);
Expand All @@ -231,7 +231,7 @@ export class AwaitableSender extends BaseSender {
timer: timer
});

abortSignal?.addEventListener("abort", onAbort);
if (abortSignal) { abortSignal.addEventListener("abort", onAbort); }
} else {
// Please send the message after some time.
const msg =
Expand Down
50 changes: 27 additions & 23 deletions lib/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ export class Connection extends Entity {
let onOpen: Func<RheaEventContext, void>;
let onClose: Func<RheaEventContext, void>;
let onAbort: Func<void, void>;
const abortSignal = options?.abortSignal;
const abortSignal = options && options.abortSignal;
let waitTimer: any;

const removeListeners: Function = () => {
Expand All @@ -316,7 +316,7 @@ export class Connection extends Entity {
this._connection.removeListener(ConnectionEvents.connectionOpen, onOpen);
this._connection.removeListener(ConnectionEvents.connectionClose, onClose);
this._connection.removeListener(ConnectionEvents.disconnected, onClose);
abortSignal?.removeEventListener("abort", onAbort);
if (abortSignal) { abortSignal.removeEventListener("abort", onAbort); }
};

onOpen = (context: RheaEventContext) => {
Expand Down Expand Up @@ -357,12 +357,13 @@ export class Connection extends Entity {
this._connection.connect();
this.actionInitiated++;

if (abortSignal?.aborted) {
onAbort();
} else {
abortSignal?.addEventListener("abort", onAbort);
if (abortSignal) {
if (abortSignal.aborted) {
onAbort();
} else {
abortSignal.addEventListener("abort", onAbort);
}
}

} else {
return resolve(this);
}
Expand All @@ -389,7 +390,7 @@ export class Connection extends Entity {
let onError: Func<RheaEventContext, void>;
let onDisconnected: Func<RheaEventContext, void>;
let onAbort: Func<void, void>;
const abortSignal = options?.abortSignal;
const abortSignal = options && options.abortSignal;
let waitTimer: any;

const removeListeners = () => {
Expand All @@ -398,7 +399,7 @@ export class Connection extends Entity {
this._connection.removeListener(ConnectionEvents.connectionError, onError);
this._connection.removeListener(ConnectionEvents.connectionClose, onClose);
this._connection.removeListener(ConnectionEvents.disconnected, onDisconnected);
abortSignal?.removeEventListener("abort", onAbort);
if (abortSignal) { abortSignal.removeEventListener("abort", onAbort); }
};

onClose = (context: RheaEventContext) => {
Expand Down Expand Up @@ -445,10 +446,12 @@ export class Connection extends Entity {
this._connection.close();
this.actionInitiated++;

if (abortSignal?.aborted) {
onAbort();
} else {
abortSignal?.addEventListener("abort", onAbort);
if (abortSignal) {
if (abortSignal.aborted) {
onAbort();
} else {
abortSignal.addEventListener("abort", onAbort);
}
}
} else {
return resolve();
Expand Down Expand Up @@ -537,7 +540,7 @@ export class Connection extends Entity {
let onClose: Func<RheaEventContext, void>;
let onDisconnected: Func<RheaEventContext, void>;
let onAbort: Func<void, void>;
const abortSignal = options?.abortSignal;
const abortSignal = options && options.abortSignal;
let waitTimer: any;

const removeListeners = () => {
Expand All @@ -546,7 +549,7 @@ export class Connection extends Entity {
rheaSession.removeListener(SessionEvents.sessionOpen, onOpen);
rheaSession.removeListener(SessionEvents.sessionClose, onClose);
rheaSession.connection.removeListener(ConnectionEvents.disconnected, onDisconnected);
abortSignal?.removeEventListener("abort", onAbort);
if (abortSignal) { abortSignal.removeEventListener("abort", onAbort); }
};

onOpen = (context: RheaEventContext) => {
Expand Down Expand Up @@ -595,11 +598,12 @@ export class Connection extends Entity {
waitTimer = setTimeout(actionAfterTimeout, this.options!.operationTimeoutInSeconds! * 1000);
rheaSession.begin();


if (abortSignal?.aborted) {
onAbort();
} else {
abortSignal?.addEventListener("abort", onAbort);
if (abortSignal) {
if (abortSignal.aborted) {
onAbort();
} else {
abortSignal.addEventListener("abort", onAbort);
}
}
});
}
Expand All @@ -616,7 +620,7 @@ export class Connection extends Entity {
if (options && options.session && options.session.createSender) {
return options.session.createSender(options);
}
const session = await this.createSession({ abortSignal: options?.abortSignal });
const session = await this.createSession({ abortSignal: options && options.abortSignal });
return session.createSender(options);
}

Expand All @@ -636,7 +640,7 @@ export class Connection extends Entity {
if (options && options.session && options.session.createAwaitableSender) {
return options.session.createAwaitableSender(options);
}
const session = await this.createSession({ abortSignal: options?.abortSignal });
const session = await this.createSession({ abortSignal: options && options.abortSignal });
return session.createAwaitableSender(options);
}

Expand All @@ -652,7 +656,7 @@ export class Connection extends Entity {
if (options && options.session && options.session.createReceiver) {
return options.session.createReceiver(options);
}
const session = await this.createSession({ abortSignal: options?.abortSignal });
const session = await this.createSession({ abortSignal: options && options.abortSignal });
return session.createReceiver(options);
}

Expand Down
42 changes: 24 additions & 18 deletions lib/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ export class Session extends Entity {
let onClose: Func<RheaEventContext, void>;
let onDisconnected: Func<RheaEventContext, void>;
let onAbort: Func<void, void>;
const abortSignal = options?.abortSignal;
const abortSignal = options && options.abortSignal;
let waitTimer: any;

const removeListeners = () => {
Expand All @@ -172,7 +172,7 @@ export class Session extends Entity {
this._session.removeListener(SessionEvents.sessionError, onError);
this._session.removeListener(SessionEvents.sessionClose, onClose);
this._session.connection.removeListener(ConnectionEvents.disconnected, onDisconnected);
abortSignal?.removeEventListener("abort", onAbort);
if (abortSignal) { abortSignal.removeEventListener("abort", onAbort); }
};

onClose = (context: RheaEventContext) => {
Expand Down Expand Up @@ -221,10 +221,12 @@ export class Session extends Entity {
this._session.close();
this.actionInitiated++;

if (abortSignal?.aborted) {
onAbort();
} else {
abortSignal?.addEventListener("abort", onAbort);
if (abortSignal) {
if (abortSignal.aborted) {
onAbort();
} else {
abortSignal.addEventListener("abort", onAbort);
}
}
} else {
return resolve();
Expand Down Expand Up @@ -290,7 +292,7 @@ export class Session extends Entity {
let onClose: Func<RheaEventContext, void>;
let onDisconnected: Func<RheaEventContext, void>;
let onAbort: Func<void, void>;
const abortSignal = options?.abortSignal;
const abortSignal = options && options.abortSignal;
let waitTimer: any;

if (options && options.onMessage) {
Expand Down Expand Up @@ -322,7 +324,7 @@ export class Session extends Entity {
rheaReceiver.removeListener(ReceiverEvents.receiverOpen, onOpen);
rheaReceiver.removeListener(ReceiverEvents.receiverClose, onClose);
rheaReceiver.session.connection.removeListener(ConnectionEvents.disconnected, onDisconnected);
abortSignal?.removeEventListener("abort", onAbort);
if (abortSignal) { abortSignal.removeEventListener("abort", onAbort); }
};

onOpen = (context: RheaEventContext) => {
Expand Down Expand Up @@ -372,10 +374,12 @@ export class Session extends Entity {
rheaReceiver.session.connection.on(ConnectionEvents.disconnected, onDisconnected);
waitTimer = setTimeout(actionAfterTimeout, this.connection.options!.operationTimeoutInSeconds! * 1000);

if (abortSignal?.aborted) {
onAbort();
} else {
abortSignal?.addEventListener("abort", onAbort);
if (abortSignal) {
if (abortSignal.aborted) {
onAbort();
} else {
abortSignal.addEventListener("abort", onAbort);
}
}
});
}
Expand Down Expand Up @@ -446,7 +450,7 @@ export class Session extends Entity {
let onClose: Func<RheaEventContext, void>;
let onDisconnected: Func<RheaEventContext, void>;
let onAbort: Func<void, void>;
const abortSignal = options?.abortSignal;
const abortSignal = options && options.abortSignal;
let waitTimer: any;

// listeners provided by the user in the options object should be added
Expand Down Expand Up @@ -480,7 +484,7 @@ export class Session extends Entity {
rheaSender.removeListener(SenderEvents.senderOpen, onSendable);
rheaSender.removeListener(SenderEvents.senderClose, onClose);
rheaSender.session.connection.removeListener(ConnectionEvents.disconnected, onDisconnected);
abortSignal?.removeEventListener("abort", onAbort);
if (abortSignal) { abortSignal.removeEventListener("abort", onAbort); }
};

onSendable = (context: RheaEventContext) => {
Expand Down Expand Up @@ -530,10 +534,12 @@ export class Session extends Entity {
rheaSender.session.connection.on(ConnectionEvents.disconnected, onDisconnected);
waitTimer = setTimeout(actionAfterTimeout, this.connection.options!.operationTimeoutInSeconds! * 1000);

if (abortSignal?.aborted) {
onAbort();
} else {
abortSignal?.addEventListener("abort", onAbort);
if (abortSignal) {
if (abortSignal.aborted) {
onAbort();
} else {
abortSignal.addEventListener("abort", onAbort);
}
}
});
}
Expand Down

0 comments on commit c12aeed

Please sign in to comment.