Skip to content

Commit

Permalink
Indicate optional parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
jcbrand committed Dec 2, 2023
1 parent b8780a2 commit 9dcd0fa
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 41 deletions.
50 changes: 17 additions & 33 deletions src/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -737,10 +737,10 @@ class Connection {
* stanza with the same id (for example when leaving a chat room).
*
* @param {Element} stanza - The stanza to send.
* @param {Function} callback - The callback function for a successful request.
* @param {Function} errback - The callback function for a failed or timed
* @param {Function} [callback] - The callback function for a successful request.
* @param {Function} [errback] - The callback function for a failed or timed
* out request. On timeout, the stanza will be null.
* @param {number} timeout - The time specified in milliseconds for a
* @param {number} [timeout] - The time specified in milliseconds for a
* timeout to occur.
* @return {string} The id used to send the presence.
*/
Expand All @@ -759,18 +759,13 @@ class Connection {

if (typeof callback === 'function' || typeof errback === 'function') {
const handler = this.addHandler(
/**
* @param {Element} stanza
*/
/** @param {Element} stanza */
(stanza) => {
// remove timeout handler if there is one
if (timeoutHandler) {
this.deleteTimedHandler(timeoutHandler);
}
if (timeoutHandler) this.deleteTimedHandler(timeoutHandler);

if (stanza.getAttribute('type') === 'error') {
if (errback) {
errback(stanza);
}
errback?.(stanza);
} else if (callback) {
callback(stanza);
}
Expand All @@ -787,9 +782,7 @@ class Connection {
// get rid of normal handler
this.deleteHandler(handler);
// call errback on timeout with null stanza
if (errback) {
errback(null);
}
errback?.(null);
return false;
});
}
Expand All @@ -802,10 +795,10 @@ class Connection {
* Helper function to send IQ stanzas.
*
* @param {Element|Builder} stanza - The stanza to send.
* @param {Function} callback - The callback function for a successful request.
* @param {Function} errback - The callback function for a failed or timed
* @param {Function} [callback] - The callback function for a successful request.
* @param {Function} [errback] - The callback function for a failed or timed
* out request. On timeout, the stanza will be null.
* @param {number} timeout - The time specified in milliseconds for a
* @param {number} [timeout] - The time specified in milliseconds for a
* timeout to occur.
* @return {string} The id used to send the IQ.
*/
Expand All @@ -824,23 +817,16 @@ class Connection {

if (typeof callback === 'function' || typeof errback === 'function') {
const handler = this.addHandler(
/**
* @param {Element} stanza
*/
/** @param {Element} stanza */
(stanza) => {
// remove timeout handler if there is one
if (timeoutHandler) {
this.deleteTimedHandler(timeoutHandler);
}
if (timeoutHandler) this.deleteTimedHandler(timeoutHandler);

const iqtype = stanza.getAttribute('type');
if (iqtype === 'result') {
if (callback) {
callback(stanza);
}
callback?.(stanza);
} else if (iqtype === 'error') {
if (errback) {
errback(stanza);
}
errback?.(stanza);
} else {
const error = new Error(`Got bad IQ type of ${iqtype}`);
error.name = 'StropheError';
Expand All @@ -859,9 +845,7 @@ class Connection {
// get rid of normal handler
this.deleteHandler(handler);
// call errback on timeout with null stanza
if (errback) {
errback(null);
}
errback?.(null);
return false;
});
}
Expand Down
16 changes: 8 additions & 8 deletions src/types/connection.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -818,26 +818,26 @@ declare class Connection {
* stanza with the same id (for example when leaving a chat room).
*
* @param {Element} stanza - The stanza to send.
* @param {Function} callback - The callback function for a successful request.
* @param {Function} errback - The callback function for a failed or timed
* @param {Function} [callback] - The callback function for a successful request.
* @param {Function} [errback] - The callback function for a failed or timed
* out request. On timeout, the stanza will be null.
* @param {number} timeout - The time specified in milliseconds for a
* @param {number} [timeout] - The time specified in milliseconds for a
* timeout to occur.
* @return {string} The id used to send the presence.
*/
sendPresence(stanza: Element, callback: Function, errback: Function, timeout: number): string;
sendPresence(stanza: Element, callback?: Function, errback?: Function, timeout?: number): string;
/**
* Helper function to send IQ stanzas.
*
* @param {Element|Builder} stanza - The stanza to send.
* @param {Function} callback - The callback function for a successful request.
* @param {Function} errback - The callback function for a failed or timed
* @param {Function} [callback] - The callback function for a successful request.
* @param {Function} [errback] - The callback function for a failed or timed
* out request. On timeout, the stanza will be null.
* @param {number} timeout - The time specified in milliseconds for a
* @param {number} [timeout] - The time specified in milliseconds for a
* timeout to occur.
* @return {string} The id used to send the IQ.
*/
sendIQ(stanza: Element | Builder, callback: Function, errback: Function, timeout: number): string;
sendIQ(stanza: Element | Builder, callback?: Function, errback?: Function, timeout?: number): string;
/**
* Queue outgoing data for later sending. Also ensures that the data
* is a DOMElement.
Expand Down

0 comments on commit 9dcd0fa

Please sign in to comment.