Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add jetstream pull wrappers (#482)
- Loading branch information
1 parent
b294a8a
commit e893e9f
Showing
8 changed files
with
186 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { camelCase, getMessageType, realizeParametersForChannelWrapper, renderJSDocParameters, messageHasNullPayload, realizeChannelName} from '../../utils/index'; | ||
// eslint-disable-next-line no-unused-vars | ||
import { Message, ChannelParameter } from '@asyncapi/parser'; | ||
import { unwrap } from './ChannelParameterUnwrap'; | ||
|
||
/** | ||
* Component which returns a subscribe to function for the client | ||
* | ||
* @param {string} defaultContentType | ||
* @param {string} channelName to publish to | ||
* @param {Message} message which is being received | ||
* @param {Object.<string, ChannelParameter>} channelParameters parameters to the channel | ||
*/ | ||
export function JetstreamPull(channelName, message, channelParameters) { | ||
const messageType = getMessageType(message); | ||
let parameters = []; | ||
parameters = Object.entries(channelParameters).map(([parameterName]) => { | ||
return `${camelCase(parameterName)}Param`; | ||
}); | ||
const hasNullPayload = messageHasNullPayload(message.payload()); | ||
|
||
//Determine the callback process when receiving messages. | ||
//If the message payload is null no hooks are called to process the received data. | ||
let whenReceivingMessage = `onDataCallback(undefined, null ${parameters.length > 0 && `, ${parameters.join(',')}`});`; | ||
if (!hasNullPayload) { | ||
whenReceivingMessage = ` | ||
let receivedData: any = codec.decode(msg.data); | ||
onDataCallback(undefined, ${messageType}.unmarshal(receivedData) ${parameters.length > 0 && `, ${parameters.join(',')}`}, msg); | ||
`; | ||
} | ||
return ` | ||
/** | ||
* Internal functionality to setup jetstrema pull on the \`${channelName}\` channel | ||
* | ||
* @param onDataCallback to call when messages are received | ||
* @param js client to pull with | ||
* @param codec used to convert messages | ||
${renderJSDocParameters(channelParameters)} | ||
*/ | ||
export function jetStreamPull( | ||
onDataCallback: ( | ||
err ? : NatsTypescriptTemplateError, | ||
msg ? : ${messageType} | ||
${realizeParametersForChannelWrapper(channelParameters, false)}, | ||
jetstreamMsg?: Nats.JsMsg) => void, | ||
js: Nats.JetStreamClient, | ||
codec: Nats.Codec < any > | ||
${realizeParametersForChannelWrapper(channelParameters)}, | ||
) { | ||
const stream = ${realizeChannelName(channelParameters, channelName)}; | ||
(async () => { | ||
const msg = await js.pull(stream, 'durableName'); | ||
${unwrap(channelName, channelParameters)} | ||
${whenReceivingMessage} | ||
})(); | ||
} | ||
`; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { camelCase, getMessageType, realizeParametersForChannelWrapper, renderJSDocParameters, realizeParametersForChannelWithoutType, pascalCase} from '../../utils/index'; | ||
|
||
export function JetstreamPull(channelName, message, messageDescription, channelParameters) { | ||
return ` | ||
/** | ||
* JetStream pull function. | ||
* | ||
* Pull message from \`${channelName}\` | ||
* | ||
* ${messageDescription} | ||
* | ||
* @param onDataCallback to call when messages are received | ||
${renderJSDocParameters(channelParameters)} | ||
* @param options to pull message with, bindings from the AsyncAPI document overwrite these if specified | ||
*/ | ||
public jetStreamPull${pascalCase(channelName)}( | ||
onDataCallback: ( | ||
err ? : NatsTypescriptTemplateError, | ||
msg?: ${getMessageType(message)} | ||
${realizeParametersForChannelWrapper(channelParameters, false)}, | ||
jetstreamMsg?: Nats.JsMsg) => void | ||
${realizeParametersForChannelWrapper(channelParameters)} | ||
): void { | ||
if (!this.isClosed() && this.nc !== undefined && this.codec !== undefined && this.js !== undefined) { | ||
${camelCase(channelName)}Channel.jetStreamPull( | ||
onDataCallback, | ||
this.js, | ||
this.codec | ||
${Object.keys(channelParameters).length ? ` ,${realizeParametersForChannelWithoutType(channelParameters)},` : ''} | ||
); | ||
} else { | ||
throw NatsTypescriptTemplateError.errorForCode(ErrorCode.NOT_CONNECTED); | ||
} | ||
} | ||
`; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters