Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Delta Between Two Patch Sets: background.js

Issue 29370996: Issue 4783 - Use Port API for the messageResponder (Closed)
Left Patch Set: Use EventEmitter Created Jan. 16, 2017, 2:25 p.m.
Right Patch Set: Improve messaging implementation in background.js Created Jan. 16, 2017, 3:15 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « no previous file | messageResponder.js » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
1 /* 1 /*
2 * This file is part of Adblock Plus <https://adblockplus.org/>, 2 * This file is part of Adblock Plus <https://adblockplus.org/>,
3 * Copyright (C) 2006-2016 Eyeo GmbH 3 * Copyright (C) 2006-2016 Eyeo GmbH
4 * 4 *
5 * Adblock Plus is free software: you can redistribute it and/or modify 5 * Adblock Plus is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 3 as 6 * it under the terms of the GNU General Public License version 3 as
7 * published by the Free Software Foundation. 7 * published by the Free Software Foundation.
8 * 8 *
9 * Adblock Plus is distributed in the hope that it will be useful, 9 * Adblock Plus is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
(...skipping 320 matching lines...) Expand 10 before | Expand all | Expand 10 after
331 addonName: "adblockplus", 331 addonName: "adblockplus",
332 addonVersion: "2.6.7" 332 addonVersion: "2.6.7"
333 }; 333 };
334 updateFromURL(modules.info); 334 updateFromURL(modules.info);
335 335
336 modules.subscriptionInit = { 336 modules.subscriptionInit = {
337 reinitialized: params.filterlistsReinitialized 337 reinitialized: params.filterlistsReinitialized
338 }; 338 };
339 339
340 modules.messaging = { 340 modules.messaging = {
341 port: { 341 port: new EventEmitter()
342 _eventEmitter: new EventEmitter(), 342 };
Sebastian Noack 2017/01/16 15:00:03 Couldn't you do it like that: modules.messaging
kzar 2017/01/16 15:16:31 Done.
343 on(name, callback) { this._eventEmitter.on(name, callback); }, 343
344 off(name, callback) { this._eventEmitter.off(name, callback); }, 344 window.addEventListener("message", event =>
345 _onMessage(event) 345 {
346 { 346 if (event.data.type != "message")
347 if (event.data.type != "message") 347 return;
348 return; 348 let message = event.data.payload;
349 let message = event.data.payload; 349 let messageId = event.data.messageId;
350 let messageId = event.data.messageId; 350 let sender = {
351 let sender = { 351 page: new ext.Page(event.source)
352 page: new ext.Page(event.source) 352 };
353 }; 353
354 354 let listeners = modules.messaging.port._listeners[message.type];
355 function reply(message) 355 if (!listeners)
356 { 356 return;
357 event.source.postMessage({ 357
358 type: "response", 358 function reply(message)
359 messageId: messageId, 359 {
360 payload: message 360 event.source.postMessage({
361 }, "*"); 361 type: "response",
362 } 362 messageId: messageId,
363 363 payload: message
364 let port = modules.messaging.port; 364 }, "*");
365 let listeners = port._eventEmitter._listeners[message.type]; 365 }
366 if (!listeners) 366
367 return; 367 for (let listener of listeners)
368 368 {
369 for (let listener of listeners) 369 let response = listener(message, sender);
370 { 370 if (response && typeof response.then == "function")
371 let response = listener(message, sender); 371 {
372 if (response && typeof response.then == "function") 372 response.then(
373 { 373 reply,
374 response.then( 374 reason => {
375 reply, 375 console.error(reason);
376 reason => { 376 reply(undefined);
377 console.error(reason);
378 reply(undefined);
379 }
380 );
381 } 377 }
382 else if (typeof response != "undefined") 378 );
383 { 379 }
384 reply(response); 380 else if (typeof response != "undefined")
385 } 381 {
386 } 382 reply(response);
387 } 383 }
388 } 384 }
389 }; 385 });
390 window.addEventListener("message", modules.messaging.port._onMessage);
391 386
392 global.Services = { 387 global.Services = {
393 vc: { 388 vc: {
394 compare: function(v1, v2) 389 compare: function(v1, v2)
395 { 390 {
396 return parseFloat(v1) - parseFloat(v2); 391 return parseFloat(v1) - parseFloat(v2);
397 } 392 }
398 } 393 }
399 }; 394 };
400 395
(...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after
530 525
531 if (params.safariContentBlocker) 526 if (params.safariContentBlocker)
532 { 527 {
533 global.safari = { 528 global.safari = {
534 extension: { 529 extension: {
535 setContentBlocker: function() {} 530 setContentBlocker: function() {}
536 } 531 }
537 }; 532 };
538 } 533 }
539 })(this); 534 })(this);
LEFTRIGHT

Powered by Google App Engine
This is Rietveld