forked from linkeddata/rdflib.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdatesVia.js
189 lines (172 loc) · 4.46 KB
/
updatesVia.js
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/*
* Updates-Via
*/
var $rdf
var k
var v
var bind = function (fn, me) {
return function () {
return fn.apply(me, arguments)
}
}
var hasProp = {}.hasOwnProperty
if (typeof $rdf === 'undefined' || $rdf === null) {
$rdf = {}
}
$rdf.UpdatesSocket = (function () {
function UpdatesSocket (parent, via1) {
var error
this.parent = parent
this.via = via1
this.subscribe = bind(this.subscribe, this)
this.onError = bind(this.onError, this)
this.onMessage = bind(this.onMessage, this)
this.onClose = bind(this.onClose, this)
this.onOpen = bind(this.onOpen, this)
this._subscribe = bind(this._subscribe, this)
this._send = bind(this._send, this)
this.connected = false
this.pending = {}
this.subscribed = {}
this.socket = {}
try {
this.socket = new WebSocket(via1)
this.socket.onopen = this.onOpen
this.socket.onclose = this.onClose
this.socket.onmessage = this.onMessage
this.socket.onerror = this.onError
} catch (error1) {
error = error1
this.onError(error)
}
}
UpdatesSocket.prototype._decode = function (q) {
var elt
var i
var k
var r
var ref
var ref1
var v
r = {}
ref = (function () {
var j, len, ref, results
ref = q.split('&')
results = []
for (j = 0, len = ref.length; j < len; j++) {
elt = ref[j]
results.push(elt.split('='))
}
return results
})()
for (i in ref) {
elt = ref[i]
ref1 = [decodeURIComponent(elt[0]), decodeURIComponent(elt[1])]
k = ref1[0]
v = ref1[1]
if (r[k] == null) {
r[k] = []
}
r[k].push(v)
}
return r
}
UpdatesSocket.prototype._send = function (method, uri, data) {
var base, message
message = [method, uri, data].join(' ')
return typeof (base = this.socket).send === 'function' ? base.send(message) : void 0
}
UpdatesSocket.prototype._subscribe = function (uri) {
this._send('sub', uri, '')
this.subscribed[uri] = true
return this.subscribed[uri]
}
UpdatesSocket.prototype.onOpen = function (e) {
var results, uri
this.connected = true
results = []
for (uri in this.pending) {
delete this.pending[uri]
results.push(this._subscribe(uri))
}
return results
}
UpdatesSocket.prototype.onClose = function (e) {
var uri
this.connected = false
for (uri in this.subscribed) {
this.pending[uri] = true
}
this.subscribed = {}
return this.subscribed
}
UpdatesSocket.prototype.onMessage = function (e) {
var base, message
message = e.data.split(' ')
if (message[0] === 'ping') {
return typeof (base = this.socket).send === 'function' ? base.send('pong ' + message.slice(1).join(' ')) : void 0
} else if (message[0] === 'pub') {
return this.parent.onUpdate(message[1], this._decode(message[2]))
}
}
UpdatesSocket.prototype.onError = function (e) {
throw new Error('onError' + e)
}
UpdatesSocket.prototype.subscribe = function (uri) {
if (this.connected) {
return this._subscribe(uri)
} else {
this.pending[uri] = true
return this.pending[uri]
}
}
return UpdatesSocket
})()
$rdf.UpdatesVia = (function () {
function UpdatesVia (fetcher) {
this.fetcher = fetcher
this.onUpdate = bind(this.onUpdate, this)
this.onHeaders = bind(this.onHeaders, this)
this.register = bind(this.register, this)
this.graph = {}
this.via = {}
this.fetcher.addCallback('headers', this.onHeaders)
}
UpdatesVia.prototype.register = function (via, uri) {
if (this.via[via] == null) {
this.via[via] = new $rdf.UpdatesSocket(this, via)
}
return this.via[via].subscribe(uri)
}
UpdatesVia.prototype.onHeaders = function (d) {
var etag, uri, via
if (d.headers == null) {
return true
}
if (typeof WebSocket === 'undefined' || WebSocket === null) {
return true
}
etag = d.headers['etag']
via = d.headers['updates-via']
uri = d.uri
if (etag && via) {
this.graph[uri] = {
etag: etag,
via: via
}
this.register(via, uri)
}
return true
}
UpdatesVia.prototype.onUpdate = function (uri, d) {
return this.fetcher.refresh($rdf.sym(uri))
}
return UpdatesVia
})()
if ((typeof module !== 'undefined' && module !== null ? module.exports : void 0) != null) {
for (k in $rdf) {
if (!hasProp.call($rdf, k)) continue
v = $rdf[k]
module.exports[k] = v
}
}