-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathIrisClient.js
More file actions
161 lines (131 loc) · 4.8 KB
/
IrisClient.js
File metadata and controls
161 lines (131 loc) · 4.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
const WebSocket = require('isomorphic-ws')
const IrisClientSubscription = require('./IrisClientSubscription')
const Channels = require('./Channels')
class IrisClient {
constructor(endpoint) {
this.endpoint = endpoint
this.subscriptions = {}
this.requests = {}
this.rpcId = 0
}
async connect() {
this.wsClient = new WebSocket(this.endpoint)
await new Promise((resolve) => {
this.wsClient.onopen = resolve
})
this.wsClient.onmessage = this.onMessage.bind(this)
// TODO: Handle onclose and retry, keeping subscriptions around to resubscribe with
}
disconnect() {
for (let subscription in this.subscriptions) {
// TODO: unsubscribe
}
this.wsClient.close()
}
sendRPC(id, method, params, responseHandler) {
this.wsClient.send(JSON.stringify(this.getRPC(id + "", method, params)))
this.requests[id] = responseHandler
}
getRPC(id, method, params) {
return {
jsonrpc: '2.0',
id,
method,
params
}
}
handleIncoming(messageObj) {
if (!messageObj.jsonrpc || messageObj.jsonrpc != '2.0') {
console.error(`Recieved message that was not a JSON RPC 2.0 format: ${message}`)
return
}
if (messageObj.id && this.requests.hasOwnProperty(messageObj.id)) {
if (typeof this.requests[messageObj.id] === 'function') {
console.log(`Recieved RPC response for id ${messageObj.id}`)
this.requests[messageObj.id](messageObj)
delete this.requests[messageObj.id]
}
}
if (messageObj.method == 'subscription')
this.handleSubscription(messageObj)
}
onMessage(message) {
try {
let messageObj = JSON.parse(message.data)
if (Array.isArray(messageObj)) {
messageObj.forEach((msg, idx) => {
this.handleIncoming(msg)
})
} else (
this.handleIncoming(messageObj)
)
} catch (e) {
console.error(`Failed parsing message as JSON: ${e}`)
}
}
async handleSubscription(messageObj) {
let params = messageObj.params;
let channel = params.channel
let topic = params.topic
if (!this.subscriptions.hasOwnProperty(channel)) {
console.error(`No registered subscriptions for channel ${channel}`)
return
}
if (!this.subscriptions[channel].hasOwnProperty(topic)) {
console.error(`No registered subscriptions for channel ${channel} with topic ${topic}`)
return
}
this.subscriptions[channel][topic].handle(params.message)
}
subscribe(subscription) {
if (!subscription.isValid()) {
console.error(`Invalid subscription: ${JSON.stringify(subscription)}`)
return
}
let channel = subscription.getChannel()
let topic = subscription.getTopic()
let alreadySubscribed = true
if (!this.subscriptions.hasOwnProperty(channel)) {
this.subscriptions[channel] = {}
alreadySubscribed = false
}
if (!this.subscriptions[channel].hasOwnProperty(topic)) {
this.subscriptions[channel][topic] = subscription
alreadySubscribed = false
}
if (alreadySubscribed)
throw new Error(`Already a subscription for channel ${channel} with topic ${topic}`)
this.sendRPC(this.rpcId++, 'subscribe', subscription.toJSON(), (response) => {
if (response.subscriptionId)
subscription.responseReceived()
})
console.log(`Sent subscription request for channel ${channel} and topic ${topic}`)
}
subscribeAction(topic, handler) {
if (!topic) {
console.error('subscribeAction called without a topic')
return
}
let clientSubscription = new IrisClientSubscription(Channels.ACTION, topic, handler)
this.subscribe(clientSubscription)
return clientSubscription
}
subscribeRow(code, scope, table, handler) {
if (code === undefined) {
console.error('subscribeRow called with undefined code')
return
}
if (scope === undefined) {
console.error('subscribeRow called with undefined scope')
return
}
if (table === undefined) {
console.error('subscribeRow called with undefined table')
return
}
let clientSubscription = new IrisClientSubscription(Channels.ROW, `${code}::${scope}::${table}`, handler)
this.subscribe(clientSubscription)
return clientSubscription
}
}
module.exports = IrisClient