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

Side by Side Diff: background.js

Issue 29370996: Issue 4783 - Use Port API for the messageResponder (Closed)
Patch Set: Add polyfilly for messaging API to background.js Created Jan. 13, 2017, 8 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | messageResponder.js » ('j') | messageResponder.js » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 319 matching lines...) Expand 10 before | Expand all | Expand 10 after
330 applicationVersion: "34.0", 330 applicationVersion: "34.0",
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 let messageListeners = {};
341 modules.messaging = {
Sebastian Noack 2017/01/13 12:12:10 Can you please just use the EventEmitter class her
kzar 2017/01/16 04:27:02 Well I'm not sure I can since the EventEmitter cla
Sebastian Noack 2017/01/16 13:46:38 See line 20 in this file. There is a class called
kzar 2017/01/16 14:25:56 Oh yea, sorry I missed that. Done.
342 port: {
343 on: (name, callback) =>
344 {
345 if (!(name in messageListeners))
346 messageListeners[name] = [];
347 messageListeners[name].push(callback);
348 },
349 off: (name, callback) =>
350 {
351 if (!(name in messageListeners))
352 return;
353
354 let index = messageListeners[name].indexOf(callback);
355 if (index == -1)
356 return;
357
358 if (messageListeners[name].length == 1)
359 delete messageListeners[name];
360 else
361 messageListeners[name].splice(index, 1);
362 }
363 }
364 };
365 window.addEventListener("message", event =>
366 {
367 if (event.data.type != "message")
368 return;
369 let message = event.data.payload;
370 let messageId = event.data.messageId;
371 let sender = {
372 page: new ext.Page(event.source)
373 };
374
375 let listeners = messageListeners[message.type];
376 if (!listeners)
377 return;
378
379 function reply(message)
380 {
381 event.source.postMessage({
382 type: "response",
383 messageId: messageId,
384 payload: message
385 }, "*");
386 }
387
388 for (let listener of listeners)
389 {
390 let response = listener(message, sender);
391 if (response && typeof response.then == "function")
392 {
393 response.then(
394 reply,
395 reason => {
396 console.error(reason);
397 reply(undefined);
398 }
399 );
400 }
401 else if (typeof response != "undefined")
402 {
403 reply(response);
404 }
405 }
406 });
407
340 global.Services = { 408 global.Services = {
341 vc: { 409 vc: {
342 compare: function(v1, v2) 410 compare: function(v1, v2)
343 { 411 {
344 return parseFloat(v1) - parseFloat(v2); 412 return parseFloat(v1) - parseFloat(v2);
345 } 413 }
346 } 414 }
347 }; 415 };
348 416
349 var filters = [ 417 var filters = [
(...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after
478 546
479 if (params.safariContentBlocker) 547 if (params.safariContentBlocker)
480 { 548 {
481 global.safari = { 549 global.safari = {
482 extension: { 550 extension: {
483 setContentBlocker: function() {} 551 setContentBlocker: function() {}
484 } 552 }
485 }; 553 };
486 } 554 }
487 })(this); 555 })(this);
OLDNEW
« no previous file with comments | « no previous file | messageResponder.js » ('j') | messageResponder.js » ('J')

Powered by Google App Engine
This is Rietveld