Unverified Commit 4388b911 authored by Mikael Mello's avatar Mikael Mello
Browse files

Add way to send additional info about the client using the SDK

parent 49dbfe30
......@@ -96,6 +96,11 @@ export let clientCommands: ICollection
*/
export let commandHandlers: IClientCommandHandlerMap = {}
/**
* Custom Data set by the client that is using the SDK
*/
export let customClientData: object = {}
/**
* Allow override of default logging with adapter's log instance
*/
......@@ -486,6 +491,17 @@ async function subscribeToCommands (): Promise<ICollection> {
return clientCommands
}
/**
* Data set by the SDK to indicate which operations the server can execute on the client
*/
function getSDKData(): object {
return {
framework: 'Rocket.Chat JS SDK',
canPauseResumeMsgStream: true,
canListenToHeartbeat: true
}
}
/**
* Once a subscription is created, using `subscribeToCommands` this method
* can be used to attach a callback to changes in the clientCommands stream.
......@@ -496,14 +512,16 @@ async function subscribeToCommands (): Promise<ICollection> {
*/
async function reactToCommands (callback: ICallback): Promise<void> {
const clientCommands = await subscribeToCommands()
const clientData = Object.assign(getSDKData(), customClientData);
await asyncCall('setCustomClientData', clientData);
logger.info(`[reactive] Listening for change events in collection ${clientCommands.name}`)
clientCommands.reactiveQuery({}).on('change', (_id: string) => {
const changedCommandQuery = clientCommands.reactiveQuery({ _id })
if (changedCommandQuery.result && changedCommandQuery.result.length > 0) {
const changedCommand = changedCommandQuery.result[0]
callback(null, changedCommand)
} else {
logger.debug('[received] Reactive query at command ${ _id } without results')
}
})
}
......@@ -587,6 +605,14 @@ export function registerCommandHandler (key: string, callback: IClientCommandHan
commandHandlers[key] = callback
}
/**
* Sets additional data about the client using the SDK
* @param clientData Object containing additional data about the client using the SDK
*/
export function setCustomClientData (clientData: object) {
customClientData = clientData;
}
/**
* Get every new element added to DDP in Asteroid (v2)
* @todo Resolve this functionality within Rocket.Chat with team
......
......@@ -6,9 +6,11 @@ const delay = (ms: number) => new Promise((resolve, reject) => setTimeout(resolv
// Start subscription to log message stream (used for e2e test and demo)
async function start () {
driver.setCustomClientData({
framework: 'SDK Start Script'
})
await driver.connect()
await driver.login({ username: botUser.username, password: botUser.password })
await driver.callMethod('updateBotData', { framework: 'Rocket.Chat JS SDK' })
await driver.subscribeToMessages()
await driver.respondToMessages((err, msg, msgOpts) => {
if (err) throw err
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment