UNPKG

livereactload-chrome

Version:

Hot reload all the parts of a chrome extension

46 lines (36 loc) 1.23 kB
'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); exports['default'] = startClient; var client = require('livereactload/lib/client/startClient'), scripts = new Map(); function startClient(scope, onMsg) { // Handle background script itself client(scope, onMsg); // Handle content scripts chrome.runtime.onConnect.addListener(function (port) { if (port.name !== '__livereactFwd') return; var tabId = port.sender.tab.id; port.onMessage.addListener(function (options) { // Identify the script and its server by address var address = options.host + ':' + options.port, tabs = scripts.get(address); if (tabs === undefined) { scripts.set(address, tabs = new Map()); // Forward websocket messages to content script contexts var change = function change(msg) { return tabs.forEach(function (post) { return post(msg); }); }; client({ options: options }, { change: change }); } tabs.set(tabId, port.postMessage.bind(port)); port.onDisconnect.addListener(function () { tabs['delete'](tabId); }); }); }); } module.exports = exports['default'];