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
45 changes: 40 additions & 5 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@jolocom/web-service-client",
"version": "1.0.1",
"version": "1.0.2",
"description": "Browser client for services based on @jolocom/web-service-base",
"main": "dist/index.js",
"repository": "https://github.com/jolocom/web-service-client",
Expand Down
63 changes: 57 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ export class JolocomWebServiceClient {
}

async sendRPC(rpcName: string, request?: any, pathPrefix='/rpc'): Promise<any> {
return new Promise((resolve, reject) => {
const followUpMsg: {
processed?: Promise<any>
resolve?: (value: any) => void
} = {}
const processed = new Promise((resolve, reject) => {
console.log(
'sending RPC call',
{ rpcName, request },
Expand All @@ -35,8 +39,12 @@ export class JolocomWebServiceClient {
this.rpcWS.send(JSON.stringify(msg))
this.messages[msgID] = {
...msg,
resolve
followUps: [],
}

followUpMsg.resolve = resolve

this.messages[msgID].followUps.push(followUpMsg)
} else {
resolve(
fetch(`${this.serviceHttpUrl}${pathPrefix}`, {
Expand All @@ -56,12 +64,17 @@ export class JolocomWebServiceClient {
)
}
})

if (this.rpcWS) followUpMsg.processed = processed

return processed
}

disconnectWs() {
this.rpcWS && this.rpcWS.close()
delete this.rpcWS
delete this.rpcWsUrl
console.log('WebSocket Disconnected')
}

async connectWs(pathPrefix='/rpc') {
Expand Down Expand Up @@ -89,13 +102,51 @@ export class JolocomWebServiceClient {
const ws = new WebSocket(rpcWsUrl)

ws.onmessage = (evt) => {
console.log('received websocket data', evt.data)
let msg
let msg: any
try {
msg = JSON.parse(evt.data)
this.messages[msg.id].resolve(msg.response)
this._finalizeMessage(msg.id)
console.log('received websocket data', msg)
let storedMsg: any
// If this is the first ever response in a thread then `this.messages[msg.id]` is there
if (this.messages[msg.id]) {

const msgIndex = msg.id
storedMsg = this.messages[msgIndex]

if(msg.response && msg.response.id) {
storedMsg.responseId = msg.response.id

}
} else {
// This is a follow-up message in a thread. Get the related original stored message
storedMsg = Object.keys(this.messages)
.map((o) => this.messages[o])
.find((m: any) => m.responseId == msg.id)
}

// Resolve the previous follow-up
storedMsg.followUps[storedMsg.followUps.length - 1].resolve({
...msg.response,
originalMsg: storedMsg,
})

// Prepare to for the next follow-up
const followUpMsg: {
processed?: Promise<void>
resolve?: () => void
} = {}
followUpMsg.processed = new Promise((resolve, reject) => {
followUpMsg.resolve = resolve
})
storedMsg.followUps.push(followUpMsg)

//TODO: if there is no 'response id' on the first response (msg.response?.id is not there).
// Or, if message status success or failer (there is more messages to receive),
// Then:
// Do not prepare to for the next follow-up message.
// Call: this._finalizeMessage(msgIndex)
} catch (err) {
console.log('received websocket data', evt.data)
console.error('error while processing websocket data', err)
}
}
Expand Down