Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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