Skip to content

Commit

Permalink
Added support for multiple subscriptions in channels "QUOTES" and "TR…
Browse files Browse the repository at this point in the history
…ADES"

Applied fixes for build issues

Added .vs (Visual Studio meta folder) to .gitignore

Added .vs (Visual Studio meta folder) to .gitignore and removed folder from repo.
  • Loading branch information
Jonte Sehlin committed Nov 15, 2019
1 parent c939a2f commit 7663a87
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 9 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,6 @@ old/

# dist folder only deployed on npm
dist/

#Visual Studio
.vs/
47 changes: 38 additions & 9 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -360,12 +360,24 @@ class Avanza extends EventEmitter {
_socketSubscribe(subscriptionString) {
this._socketSubscriptions[subscriptionString] = null
if (this._socketConnected) {
this._socketSend({
channel: '/meta/subscribe',
clientId: this._socketClientId,
id: this._socketMessageCount,
subscription: subscriptionString
})
const subscriptionStrings = subscriptionString.split(',')
if (subscriptionStrings.length > 1) {
subscriptionStrings.forEach((subString) => {
this._socketSend({
channel: '/meta/subscribe',
clientId: this._socketClientId,
id: this._socketMessageCount,
subscription: subString
})
})
} else {
this._socketSend({
channel: '/meta/subscribe',
clientId: this._socketClientId,
id: this._socketMessageCount,
subscription: subscriptionString
})
}
}
}

Expand Down Expand Up @@ -666,25 +678,42 @@ class Avanza extends EventEmitter {
throw new Error('Expected to be authenticated before subscribing.')
}

const subscriptionStrings = []
if (Array.isArray(ids)) {
if (
channel === Avanza.ORDERS ||
channel === Avanza.DEALS ||
channel === Avanza.POSITIONS
) {
ids = ids.join(',')
const subscriptionString = `/${channel}/${ids}`
subscriptionStrings.push(subscriptionString)
} else if (
channel === Avanza.QUOTES ||
channel === Avanza.TRADES
) {
ids.forEach((id) => {
const subscriptionString = `/${channel}/${id}`
subscriptionStrings.push(subscriptionString)
})
} else {
throw new Error(`Channel ${channel} does not support multiple ids as input.`)
}
} else {
const subscriptionString = `/${channel}/${ids}`
subscriptionStrings.push(subscriptionString)
}

const _this = this;
subscriptionStrings.forEach((subscriptionString) => {
_this.on(subscriptionString, data => callback(data))
})

if (!this._socket) {
this._socketInit()
}

const subscriptionString = `/${channel}/${ids}`
this.on(subscriptionString, data => callback(data))
this._socketSubscribe(subscriptionString)
this._socketSubscribe(subscriptionStrings)
}

/**
Expand Down

0 comments on commit 7663a87

Please sign in to comment.