|
110 | 110 | return [
|
111 | 111 | { name: 'newFilter', call: newFilter },
|
112 | 112 | { name: 'uninstallFilter', call: 'uninstallFilter' },
|
113 |
| - { name: 'changed', call: 'changed' }, |
114 | 113 | { name: 'getMessages', call: 'getMessages' }
|
115 | 114 | ];
|
116 | 115 | };
|
|
119 | 118 | return [
|
120 | 119 | { name: 'newFilter', call: 'shhNewFilter' },
|
121 | 120 | { name: 'uninstallFilter', call: 'shhUninstallFilter' },
|
122 |
| - { name: 'changed', call: 'shhChanged' }, |
123 | 121 | { name: 'getMessage', call: 'shhGetMessages' }
|
124 | 122 | ];
|
125 | 123 | };
|
|
213 | 211 | },
|
214 | 212 |
|
215 | 213 | fromAscii: function(str, pad) {
|
216 |
| - if(pad === undefined) { |
217 |
| - pad = 32 |
218 |
| - } |
219 |
| - |
| 214 | + pad = pad === undefined ? 32 : pad; |
220 | 215 | var hex = this.toHex(str);
|
221 |
| - |
222 | 216 | while(hex.length < pad*2)
|
223 | 217 | hex += "00";
|
224 |
| - |
225 | 218 | return hex
|
226 | 219 | },
|
227 | 220 |
|
|
243 | 236 | }
|
244 | 237 | },
|
245 | 238 |
|
246 |
| - on: function(event, cb) { |
| 239 | + on: function(event, id, cb) { |
247 | 240 | if(web3._events[event] === undefined) {
|
248 |
| - web3._events[event] = []; |
| 241 | + web3._events[event] = {}; |
249 | 242 | }
|
250 | 243 |
|
251 |
| - web3._events[event].push(cb); |
252 |
| - |
| 244 | + web3._events[event][id] = cb; |
253 | 245 | return this
|
254 | 246 | },
|
255 | 247 |
|
256 |
| - off: function(event, cb) { |
| 248 | + off: function(event, id) { |
257 | 249 | if(web3._events[event] !== undefined) {
|
258 |
| - var callbacks = web3._events[event]; |
259 |
| - for(var i = 0; i < callbacks.length; i++) { |
260 |
| - if(callbacks[i] === cb) { |
261 |
| - delete callbacks[i]; |
262 |
| - } |
263 |
| - } |
| 250 | + delete web3._events[event][id]; |
264 | 251 | }
|
265 | 252 |
|
266 | 253 | return this
|
267 | 254 | },
|
268 | 255 |
|
269 |
| - trigger: function(event, data) { |
| 256 | + trigger: function(event, id, data) { |
270 | 257 | var callbacks = web3._events[event];
|
271 |
| - if(callbacks !== undefined) { |
272 |
| - for(var i = 0; i < callbacks.length; i++) { |
273 |
| - // Figure out whether the returned data was an array |
274 |
| - // array means multiple return arguments (multiple params) |
275 |
| - if(data instanceof Array) { |
276 |
| - callbacks[i].apply(this, data); |
277 |
| - } else { |
278 |
| - callbacks[i].call(this, undefined, data); |
279 |
| - } |
280 |
| - } |
| 258 | + if (!callbacks || !callbacks[id]) { |
| 259 | + return; |
281 | 260 | }
|
| 261 | + var cb = callbacks[id]; |
| 262 | + cb(data); |
282 | 263 | },
|
283 | 264 | };
|
284 | 265 |
|
|
288 | 269 | setupMethods(web3.db, dbMethods());
|
289 | 270 | setupMethods(web3.shh, shhMethods());
|
290 | 271 |
|
291 |
| - var ethWatch = {}; |
| 272 | + var ethWatch = { |
| 273 | + changed: 'changed' |
| 274 | + }; |
292 | 275 | setupMethods(ethWatch, ethWatchMethods());
|
293 |
| - var shhWatch = {}; |
| 276 | + var shhWatch = { |
| 277 | + changed: 'shhChanged' |
| 278 | + }; |
294 | 279 | setupMethods(shhWatch, shhWatchMethods());
|
295 | 280 |
|
296 | 281 | var ProviderManager = function() {
|
|
316 | 301 |
|
317 | 302 | ProviderManager.prototype.send = function(data, cb) {
|
318 | 303 | data._id = this.id;
|
319 |
| - if(cb) { |
| 304 | + if (cb) { |
320 | 305 | web3._callbacks[data._id] = cb;
|
321 | 306 | }
|
322 | 307 |
|
323 |
| - if(data.args === undefined) { |
324 |
| - data.args = []; |
325 |
| - } |
326 |
| - |
| 308 | + data.args = data.args || []; |
327 | 309 | this.id++;
|
328 | 310 |
|
329 | 311 | if(this.provider !== undefined) {
|
|
378 | 360 | web3.provider.sendQueued();
|
379 | 361 | };
|
380 | 362 |
|
381 |
| - var filters = []; |
382 | 363 | var Filter = function(options, impl) {
|
383 |
| - filters.push(this); |
384 |
| - |
385 | 364 | this.impl = impl;
|
386 | 365 | this.callbacks = [];
|
387 | 366 |
|
388 |
| - var self = this; // Cheaper than binding |
| 367 | + var self = this; |
389 | 368 | this.promise = impl.newFilter(options);
|
390 | 369 | this.promise.then(function (id) {
|
391 | 370 | self.id = id;
|
392 |
| - web3.provider.startPolling({call: 'changed', args: [id]}, id); |
| 371 | + web3.on(impl.changed, id, self.trigger.bind(self)); |
| 372 | + web3.provider.startPolling({call: impl.changed, args: [id]}, id); |
393 | 373 | });
|
394 | 374 | };
|
395 | 375 |
|
|
400 | 380 | });
|
401 | 381 | };
|
402 | 382 |
|
403 |
| - Filter.prototype.trigger = function(messages, id) { |
404 |
| - if(id == this.id) { |
405 |
| - for(var i = 0; i < this.callbacks.length; i++) { |
406 |
| - this.callbacks[i].call(this, messages); |
407 |
| - } |
| 383 | + Filter.prototype.trigger = function(messages) { |
| 384 | + for(var i = 0; i < this.callbacks.length; i++) { |
| 385 | + this.callbacks[i].call(this, messages); |
408 | 386 | }
|
409 | 387 | };
|
410 | 388 |
|
|
413 | 391 | this.promise.then(function (id) {
|
414 | 392 | self.impl.uninstallFilter(id);
|
415 | 393 | web3.provider.stopPolling(id);
|
| 394 | + web3.off(impl.changed, id); |
416 | 395 | });
|
417 | 396 | };
|
418 | 397 |
|
|
423 | 402 | });
|
424 | 403 | };
|
425 | 404 |
|
426 |
| - // Register to the messages callback. "messages" will be emitted when new messages |
427 |
| - // from the client have been created. |
428 |
| - web3.on("messages", function(messages, id) { |
429 |
| - for(var i = 0; i < filters.length; i++) { |
430 |
| - filters[i].trigger(messages, id); |
431 |
| - } |
432 |
| - }); |
433 |
| - |
434 | 405 | function messageHandler(data) {
|
435 | 406 | if(data._event !== undefined) {
|
436 |
| - web3.trigger(data._event, data.data); |
437 |
| - } else { |
438 |
| - if(data._id) { |
439 |
| - var cb = web3._callbacks[data._id]; |
440 |
| - if(cb) { |
441 |
| - cb.call(this, data.data) |
442 |
| - |
443 |
| - // Remove the "trigger" callback |
444 |
| - delete web3._callbacks[data._id]; |
445 |
| - } |
| 407 | + web3.trigger(data._event, data._id, data.data); |
| 408 | + return; |
| 409 | + } |
| 410 | + |
| 411 | + if(data._id) { |
| 412 | + var cb = web3._callbacks[data._id]; |
| 413 | + if (cb) { |
| 414 | + cb.call(this, data.data) |
| 415 | + delete web3._callbacks[data._id]; |
446 | 416 | }
|
447 | 417 | }
|
448 | 418 | }
|
|
0 commit comments