webext-redux
Advanced tools
Comparing version 3.0.0 to 4.0.0
@@ -1246,3 +1246,25 @@ (function (global, factory) { | ||
var createDeferredListener = function createDeferredListener() { | ||
/** | ||
* Returns a function that can be passed as a listener callback to a browser | ||
* API. The listener will queue events until setListener is called. | ||
* | ||
* @param {Function} filter - A function that filters messages to be handled by | ||
* the listener. This is important to avoid telling the browser to expect an | ||
* async response when the message is not intended for this listener. | ||
* | ||
* @example | ||
* const filter = (message, sender, sendResponse) => { | ||
* return message.type === "my_type" | ||
* } | ||
* | ||
* const { listener, setListener } = createDeferredListener(filter); | ||
* chrome.runtime.onMessage.addListener(listener); | ||
* | ||
* // Later, define the listener to handle messages. Messages received | ||
* // before this point are queued. | ||
* setListener((message, sender, sendResponse) => { | ||
* console.log(message); | ||
* }); | ||
*/ | ||
var createDeferredListener = function createDeferredListener(filter) { | ||
var resolve = function resolve() {}; | ||
@@ -1253,2 +1275,5 @@ var fnPromise = new Promise(function (resolve_) { | ||
var listener = function listener(message, sender, sendResponse) { | ||
if (!filter(message, sender, sendResponse)) { | ||
return; | ||
} | ||
fnPromise.then(function (fn) { | ||
@@ -1299,3 +1324,2 @@ fn(message, sender, sendResponse); | ||
* @param {Object} options | ||
* @param {string} options.channelName The name of the channel for this store. | ||
* @param {function} options.dispatchResponder A function that takes the result | ||
@@ -1316,2 +1340,4 @@ * of a store dispatch and optionally implements custom logic for responding to | ||
* messages that woke the service worker. | ||
* @param {Object} options | ||
* @param {string} options.channelName The name of the channel for this store. | ||
* @return {WrapStore} The wrapStore function that accepts a Redux store and | ||
@@ -1321,25 +1347,29 @@ * options. See {@link WrapStore}. | ||
var wrapStore = (function () { | ||
var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : defaultOpts$1, | ||
_ref$channelName = _ref.channelName, | ||
channelName = _ref$channelName === void 0 ? defaultOpts$1.channelName : _ref$channelName; | ||
var browserAPI = getBrowserAPI(); | ||
var filterStateMessages = function filterStateMessages(message) { | ||
return message.type === FETCH_STATE_TYPE && message.channelName === channelName; | ||
}; | ||
var filterActionMessages = function filterActionMessages(message) { | ||
return message.type === DISPATCH_TYPE && message.channelName === channelName; | ||
}; | ||
// Setup message listeners synchronously to avoid dropping messages if the | ||
// extension is woken by a message. | ||
var stateProviderListener = createDeferredListener(); | ||
var actionListener = createDeferredListener(); | ||
var stateProviderListener = createDeferredListener(filterStateMessages); | ||
var actionListener = createDeferredListener(filterActionMessages); | ||
browserAPI.runtime.onMessage.addListener(stateProviderListener.listener); | ||
browserAPI.runtime.onMessage.addListener(actionListener.listener); | ||
return function (store) { | ||
var _ref = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : defaultOpts$1, | ||
_ref$channelName = _ref.channelName, | ||
channelName = _ref$channelName === void 0 ? defaultOpts$1.channelName : _ref$channelName, | ||
_ref$dispatchResponde = _ref.dispatchResponder, | ||
dispatchResponder = _ref$dispatchResponde === void 0 ? defaultOpts$1.dispatchResponder : _ref$dispatchResponde, | ||
_ref$serializer = _ref.serializer, | ||
serializer = _ref$serializer === void 0 ? defaultOpts$1.serializer : _ref$serializer, | ||
_ref$deserializer = _ref.deserializer, | ||
deserializer = _ref$deserializer === void 0 ? defaultOpts$1.deserializer : _ref$deserializer, | ||
_ref$diffStrategy = _ref.diffStrategy, | ||
diffStrategy = _ref$diffStrategy === void 0 ? defaultOpts$1.diffStrategy : _ref$diffStrategy; | ||
if (!channelName) { | ||
throw new Error("channelName is required in options"); | ||
} | ||
var _ref2 = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : defaultOpts$1, | ||
_ref2$dispatchRespond = _ref2.dispatchResponder, | ||
dispatchResponder = _ref2$dispatchRespond === void 0 ? defaultOpts$1.dispatchResponder : _ref2$dispatchRespond, | ||
_ref2$serializer = _ref2.serializer, | ||
serializer = _ref2$serializer === void 0 ? defaultOpts$1.serializer : _ref2$serializer, | ||
_ref2$deserializer = _ref2.deserializer, | ||
deserializer = _ref2$deserializer === void 0 ? defaultOpts$1.deserializer : _ref2$deserializer, | ||
_ref2$diffStrategy = _ref2.diffStrategy, | ||
diffStrategy = _ref2$diffStrategy === void 0 ? defaultOpts$1.diffStrategy : _ref2$diffStrategy; | ||
if (typeof serializer !== "function") { | ||
@@ -1359,16 +1389,14 @@ throw new Error("serializer must be a function"); | ||
var dispatchResponse = function dispatchResponse(request, sender, sendResponse) { | ||
if (request.type === DISPATCH_TYPE && request.channelName === channelName) { | ||
var action = Object.assign({}, request.payload, { | ||
_sender: sender | ||
}); | ||
var dispatchResult = null; | ||
try { | ||
dispatchResult = store.dispatch(action); | ||
} catch (e) { | ||
dispatchResult = Promise.reject(e.message); | ||
console.error(e); | ||
} | ||
dispatchResponder(dispatchResult, sendResponse); | ||
return true; | ||
// Only called with messages that pass the filterActionMessages filter. | ||
var action = Object.assign({}, request.payload, { | ||
_sender: sender | ||
}); | ||
var dispatchResult = null; | ||
try { | ||
dispatchResult = store.dispatch(action); | ||
} catch (e) { | ||
dispatchResult = Promise.reject(e.message); | ||
console.error(e); | ||
} | ||
dispatchResponder(dispatchResult, sendResponse); | ||
}; | ||
@@ -1428,6 +1456,2 @@ | ||
}); | ||
var withPayloadDeserializer = withDeserializer(deserializer); | ||
var shouldDeserialize = function shouldDeserialize(request) { | ||
return request.type === DISPATCH_TYPE && request.channelName === channelName; | ||
}; | ||
@@ -1438,9 +1462,8 @@ /** | ||
stateProviderListener.setListener(function (request, sender, sendResponse) { | ||
// This listener is only called with messages that pass filterStateMessages | ||
var state = store.getState(); | ||
if (request.type === FETCH_STATE_TYPE && request.channelName === channelName) { | ||
sendResponse({ | ||
type: FETCH_STATE_TYPE, | ||
payload: state | ||
}); | ||
} | ||
sendResponse({ | ||
type: FETCH_STATE_TYPE, | ||
payload: state | ||
}); | ||
}); | ||
@@ -1451,3 +1474,4 @@ | ||
*/ | ||
withPayloadDeserializer(actionListener.setListener)(dispatchResponse, shouldDeserialize); | ||
var withPayloadDeserializer = withDeserializer(deserializer); | ||
withPayloadDeserializer(actionListener.setListener)(dispatchResponse, filterActionMessages); | ||
}; | ||
@@ -1454,0 +1478,0 @@ }); |
@@ -1,1 +0,1 @@ | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e=e||self).WebextRedux={})}(this,(function(e){"use strict";function t(e,t){(null==t||t>e.length)&&(t=e.length);for(var r=0,n=Array(t);t>r;r++)n[r]=e[r];return n}function r(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function n(e,t){for(var r=0;t.length>r;r++){var n=t[r];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(e,s(n.key),n)}}function i(e,t){var r="undefined"!=typeof Symbol&&e[Symbol.iterator]||e["@@iterator"];if(!r){if(Array.isArray(e)||(r=l(e))||t&&e&&"number"==typeof e.length){r&&(e=r);var n=0,i=function(){};return{s:i,n:function(){return e.length>n?{done:!1,value:e[n++]}:{done:!0}},e:function(e){throw e},f:i}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var a,o=!0,c=!1;return{s:function(){r=r.call(e)},n:function(){var e=r.next();return o=e.done,e},e:function(e){c=!0,a=e},f:function(){try{o||null==r.return||r.return()}finally{if(c)throw a}}}}function a(e,t,r){return(t=s(t))in e?Object.defineProperty(e,t,{value:r,enumerable:!0,configurable:!0,writable:!0}):e[t]=r,e}function o(e,t){var r=Object.keys(e);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(e);t&&(n=n.filter((function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable}))),r.push.apply(r,n)}return r}function c(e){for(var t=1;arguments.length>t;t++){var r=null!=arguments[t]?arguments[t]:{};t%2?o(Object(r),!0).forEach((function(t){a(e,t,r[t])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(r)):o(Object(r)).forEach((function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(r,t))}))}return e}function u(e){return function(e){if(Array.isArray(e))return t(e)}(e)||function(e){if("undefined"!=typeof Symbol&&null!=e[Symbol.iterator]||null!=e["@@iterator"])return Array.from(e)}(e)||l(e)||function(){throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}function s(e){var t=function(e,t){if("object"!=typeof e||!e)return e;var r=e[Symbol.toPrimitive];if(void 0!==r){var n=r.call(e,t||"default");if("object"!=typeof n)return n;throw new TypeError("@@toPrimitive must return a primitive value.")}return("string"===t?String:Number)(e)}(e,"string");return"symbol"==typeof t?t:t+""}function l(e,r){if(e){if("string"==typeof e)return t(e,r);var n={}.toString.call(e).slice(8,-1);return"Object"===n&&e.constructor&&(n=e.constructor.name),"Map"===n||"Set"===n?Array.from(e):"Arguments"===n||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)?t(e,r):void 0}}var f=/^(?:0|[1-9]\d*)$/;function h(e,t,r){switch(r.length){case 0:return e.call(t);case 1:return e.call(t,r[0]);case 2:return e.call(t,r[0],r[1]);case 3:return e.call(t,r[0],r[1],r[2])}return e.apply(t,r)}var d=Object.prototype,p=d.hasOwnProperty,y=d.toString,v=d.propertyIsEnumerable,b=Math.max;function g(e,t){var r=E(e)||function(e){return function(e){return function(e){return!!e&&"object"==typeof e}(e)&&z(e)}(e)&&p.call(e,"callee")&&(!v.call(e,"callee")||"[object Arguments]"==y.call(e))}(e)?function(e,t){for(var r=-1,n=Array(e);++r<e;)n[r]=t(r);return n}(e.length,String):[],n=r.length,i=!!n;for(var a in e)!t&&!p.call(e,a)||i&&("length"==a||S(a,n))||r.push(a);return r}function m(e,t,r){var n=e[t];p.call(e,t)&&j(n,r)&&(void 0!==r||t in e)||(e[t]=r)}function w(e){if(!N(e))return function(e){var t=[];if(null!=e)for(var r in Object(e))t.push(r);return t}(e);var t,r,n=(t=e)===("function"==typeof(r=t&&t.constructor)&&r.prototype||d),i=[];for(var a in e)("constructor"!=a||!n&&p.call(e,a))&&i.push(a);return i}function S(e,t){return!!(t=null==t?9007199254740991:t)&&("number"==typeof e||f.test(e))&&e>-1&&e%1==0&&t>e}function j(e,t){return e===t||e!=e&&t!=t}var P,O,A,E=Array.isArray;function z(e){return null!=e&&function(e){return"number"==typeof e&&e>-1&&e%1==0&&9007199254740991>=e}(e.length)&&!function(e){var t=N(e)?y.call(e):"";return"[object Function]"==t||"[object GeneratorFunction]"==t}(e)}function N(e){var t=typeof e;return!!e&&("object"==t||"function"==t)}var k=(P=function(e,t){!function(e,t,r,n){r||(r={});for(var i=-1,a=t.length;++i<a;){var o=t[i],c=n?n(r[o],e[o],o,r,e):void 0;m(r,o,void 0===c?e[o]:c)}}(t,function(e){return z(e)?g(e,!0):w(e)}(t),e)},O=function(e,t){var r=-1,n=t.length,i=n>1?t[n-1]:void 0,a=n>2?t[2]:void 0;for(i=P.length>3&&"function"==typeof i?(n--,i):void 0,a&&function(e,t,r){if(!N(r))return!1;var n=typeof t;return!!("number"==n?z(r)&&S(t,r.length):"string"==n&&t in r)&&j(r[t],e)}(t[0],t[1],a)&&(i=3>n?void 0:i,n=1),e=Object(e);++r<n;){var o=t[r];o&&P(e,o,r,i)}return e},A=b(void 0===A?O.length-1:A,0),function(){for(var e=arguments,t=-1,r=b(e.length-A,0),n=Array(r);++t<r;)n[t]=e[A+t];t=-1;for(var i=Array(A+1);++t<A;)i[t]=e[t];return i[A]=n,h(O,this,i)}),x=function(e){return e},M=function(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:x;return c(c({},e),e.payload?{payload:t(e.payload)}:{})},R=function(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:x,r=arguments.length>2?arguments[2]:void 0;return r?function(n){for(var i=arguments.length,a=Array(i>1?i-1:0),o=1;i>o;o++)a[o-1]=arguments[o];return r.apply(void 0,[n].concat(a))?e.apply(void 0,[M(n,t)].concat(a)):e.apply(void 0,[n].concat(a))}:function(r){for(var n=arguments.length,i=Array(n>1?n-1:0),a=1;n>a;a++)i[a-1]=arguments[a];return e.apply(void 0,[M(r,t)].concat(i))}},I=function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:x;return function(t){return function(r,n){return t(R(r,e,n))}}},L=function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:x;return function(t){var r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:0;return function(){for(var n=arguments.length,i=Array(n),a=0;n>a;a++)i[a]=arguments[a];if(r>=i.length)throw Error("Message in request could not be serialized. "+"Expected message in position ".concat(r," but only received ").concat(i.length," args."));return i[r]=M(i[r],e),t.apply(void 0,i)}}};function _(){var e;try{e=self.chrome||self.browser||browser}catch(t){e=browser}if(!e)throw Error("Browser API is not present");return e}var D="\nLooks like there is an error in the background page. You might want to inspect your background page for more details.\n",q={channelName:"webext.channel",state:{},serializer:x,deserializer:x,patchStrategy:function(e,t){var r=Object.assign({},e);return t.forEach((function(e){var t=e.key,n=e.value;switch(e.change){case"updated":r[t]=n;break;case"removed":Reflect.deleteProperty(r,t)}})),r}};function T(){for(var e=arguments.length,t=Array(e),r=0;e>r;r++)t[r]=arguments[r];return 0===t.length?function(e){return e}:1===t.length?t[0]:t.reduce((function(e,t){return function(){return e(t.apply(void 0,arguments))}}))}var C=function(){var e=function(){},t=new Promise((function(t){return e=t}));return{setListener:e,listener:function(e,r,n){return t.then((function(t){t(e,r,n)})),!0}}},F={channelName:"webext.channel",dispatchResponder:function(e,t){Promise.resolve(e).then((function(e){t({error:null,value:e})})).catch((function(e){console.error("error dispatching result:",e),t({error:e.message,value:null})}))},serializer:x,deserializer:x,diffStrategy:function(e,t){var r=[];return Object.keys(t).forEach((function(n){e[n]!==t[n]&&r.push({key:n,value:t[n],change:"updated"})})),Object.keys(e).forEach((function(e){t.hasOwnProperty(e)||r.push({key:e,change:"removed"})})),r}};e.Store=function(){return e=function e(){var t=this,n=arguments.length>0&&void 0!==arguments[0]?arguments[0]:q,i=n.channelName,a=void 0===i?q.channelName:i,o=n.state,c=void 0===o?q.state:o,u=n.serializer,s=void 0===u?q.serializer:u,l=n.deserializer,f=void 0===l?q.deserializer:l,h=n.patchStrategy,d=void 0===h?q.patchStrategy:h;if(r(this,e),!a)throw Error("channelName is required in options");if("function"!=typeof s)throw Error("serializer must be a function");if("function"!=typeof f)throw Error("deserializer must be a function");if("function"!=typeof d)throw Error("patchStrategy must be one of the included patching strategies or a custom patching function");this.channelName=a,this.readyResolved=!1,this.readyPromise=new Promise((function(e){return t.readyResolve=e})),this.browserAPI=_(),this.initializeStore=this.initializeStore.bind(this),this.browserAPI.runtime.sendMessage({type:"webext.fetch_state",channelName:a},void 0,this.initializeStore),this.deserializer=f,this.serializedPortListener=I(f)((function(){var e;return(e=t.browserAPI.runtime.onMessage).addListener.apply(e,arguments)})),this.serializedMessageSender=L(s)((function(){var e;return(e=t.browserAPI.runtime).sendMessage.apply(e,arguments)})),this.listeners=[],this.state=c,this.patchStrategy=d;var p=function(e){return!!e&&"string"==typeof e.type&&e.channelName===t.channelName};this.serializedPortListener((function(e){if(e&&e.channelName===t.channelName)switch(e.type){case"webext.state":t.replaceState(e.payload),t.readyResolved||(t.readyResolved=!0,t.readyResolve());break;case"webext.patch_state":t.patchState(e.payload)}}),p),this.dispatch=this.dispatch.bind(this),this.getState=this.getState.bind(this),this.subscribe=this.subscribe.bind(this)},(t=[{key:"ready",value:function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:null;return null!==e?this.readyPromise.then(e):this.readyPromise}},{key:"subscribe",value:function(e){var t=this;return this.listeners.push(e),function(){t.listeners=t.listeners.filter((function(t){return t!==e}))}}},{key:"patchState",value:function(e){this.state=this.patchStrategy(this.state,e),this.listeners.forEach((function(e){return e()}))}},{key:"replaceState",value:function(e){this.state=e,this.listeners.forEach((function(e){return e()}))}},{key:"getState",value:function(){return this.state}},{key:"replaceReducer",value:function(){}},{key:"dispatch",value:function(e){var t=this;return new Promise((function(r,n){t.serializedMessageSender({type:"webext.dispatch",channelName:t.channelName,payload:e},null,(function(e){if(e){var i=e.error,a=e.value;if(i){var o=Error("".concat(D).concat(i));n(k(o,i))}else r(a&&a.payload)}else{var c=t.browserAPI.runtime.lastError,u=Error("".concat(D).concat(c));n(k(u,c))}}))}))}},{key:"initializeStore",value:function(e){e&&"webext.fetch_state"===e.type&&(this.replaceState(e.payload),this.readyResolved||(this.readyResolved=!0,this.readyResolve()))}}])&&n(e.prototype,t),i&&n(e,i),Object.defineProperty(e,"prototype",{writable:!1}),e;var e,t,i}(),e.alias=function(e){return function(){return function(t){return function(r){var n=e[r.type];return t(n?n(r):r)}}}},e.applyMiddleware=function(e){for(var t=arguments.length,r=Array(t>1?t-1:0),n=1;t>n;n++)r[n-1]=arguments[n];var i=function(){throw Error("Dispatching while constructing your middleware is not allowed. Other middleware would not be applied to this dispatch.")},a={getState:e.getState.bind(e),dispatch:function(){return i.apply(void 0,arguments)}};return r=(r||[]).map((function(e){return e(a)})),i=T.apply(void 0,u(r))(e.dispatch),e.dispatch=i,e},e.createWrapStore=function(){var e=_(),t=C(),r=C();return e.runtime.onMessage.addListener(t.listener),e.runtime.onMessage.addListener(r.listener),function(n){var a=arguments.length>1&&void 0!==arguments[1]?arguments[1]:F,o=a.channelName,c=void 0===o?F.channelName:o,u=a.dispatchResponder,s=void 0===u?F.dispatchResponder:u,l=a.serializer,f=void 0===l?F.serializer:l,h=a.deserializer,d=void 0===h?F.deserializer:h,p=a.diffStrategy,y=void 0===p?F.diffStrategy:p;if(!c)throw Error("channelName is required in options");if("function"!=typeof f)throw Error("serializer must be a function");if("function"!=typeof d)throw Error("deserializer must be a function");if("function"!=typeof y)throw Error("diffStrategy must be one of the included diffing strategies or a custom diff function");var v=function(e,t,r){if("webext.dispatch"===e.type&&e.channelName===c){var i=Object.assign({},e.payload,{_sender:t}),a=null;try{a=n.dispatch(i)}catch(e){a=Promise.reject(e.message),console.error(e)}return s(a,r),!0}},b=L(f)((function(){for(var t,r=arguments.length,n=Array(r),a=0;r>a;a++)n[a]=arguments[a];var o=function(){};return(t=e.runtime).sendMessage.apply(t,n.concat([o])),e.tabs.query({},(function(t){var r,a=i(t);try{for(a.s();!(r=a.n()).done;){var c;(c=e.tabs).sendMessage.apply(c,[r.value.id].concat(n,[o]))}}catch(e){a.e(e)}finally{a.f()}}))})),g=n.getState(),m=function(){var e=n.getState(),t=y(g,e);t.length&&(g=e,b({type:"webext.patch_state",payload:t,channelName:c}))};n.subscribe(m),b({type:"webext.state",payload:g,channelName:c});var w=I(d),S=function(e){return"webext.dispatch"===e.type&&e.channelName===c};t.setListener((function(e,t,r){var i=n.getState();"webext.fetch_state"===e.type&&e.channelName===c&&r({type:"webext.fetch_state",payload:i})})),w(r.setListener)(v,S)}},Object.defineProperty(e,"__esModule",{value:!0})})); | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e=e||self).WebextRedux={})}(this,(function(e){"use strict";function t(e,t){(null==t||t>e.length)&&(t=e.length);for(var r=0,n=Array(t);t>r;r++)n[r]=e[r];return n}function r(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function n(e,t){for(var r=0;t.length>r;r++){var n=t[r];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(e,s(n.key),n)}}function i(e,t){var r="undefined"!=typeof Symbol&&e[Symbol.iterator]||e["@@iterator"];if(!r){if(Array.isArray(e)||(r=l(e))||t&&e&&"number"==typeof e.length){r&&(e=r);var n=0,i=function(){};return{s:i,n:function(){return e.length>n?{done:!1,value:e[n++]}:{done:!0}},e:function(e){throw e},f:i}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var o,a=!0,c=!1;return{s:function(){r=r.call(e)},n:function(){var e=r.next();return a=e.done,e},e:function(e){c=!0,o=e},f:function(){try{a||null==r.return||r.return()}finally{if(c)throw o}}}}function o(e,t,r){return(t=s(t))in e?Object.defineProperty(e,t,{value:r,enumerable:!0,configurable:!0,writable:!0}):e[t]=r,e}function a(e,t){var r=Object.keys(e);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(e);t&&(n=n.filter((function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable}))),r.push.apply(r,n)}return r}function c(e){for(var t=1;arguments.length>t;t++){var r=null!=arguments[t]?arguments[t]:{};t%2?a(Object(r),!0).forEach((function(t){o(e,t,r[t])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(r)):a(Object(r)).forEach((function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(r,t))}))}return e}function u(e){return function(e){if(Array.isArray(e))return t(e)}(e)||function(e){if("undefined"!=typeof Symbol&&null!=e[Symbol.iterator]||null!=e["@@iterator"])return Array.from(e)}(e)||l(e)||function(){throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}function s(e){var t=function(e,t){if("object"!=typeof e||!e)return e;var r=e[Symbol.toPrimitive];if(void 0!==r){var n=r.call(e,t||"default");if("object"!=typeof n)return n;throw new TypeError("@@toPrimitive must return a primitive value.")}return("string"===t?String:Number)(e)}(e,"string");return"symbol"==typeof t?t:t+""}function l(e,r){if(e){if("string"==typeof e)return t(e,r);var n={}.toString.call(e).slice(8,-1);return"Object"===n&&e.constructor&&(n=e.constructor.name),"Map"===n||"Set"===n?Array.from(e):"Arguments"===n||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)?t(e,r):void 0}}var f=/^(?:0|[1-9]\d*)$/;function h(e,t,r){switch(r.length){case 0:return e.call(t);case 1:return e.call(t,r[0]);case 2:return e.call(t,r[0],r[1]);case 3:return e.call(t,r[0],r[1],r[2])}return e.apply(t,r)}var d=Object.prototype,p=d.hasOwnProperty,y=d.toString,v=d.propertyIsEnumerable,b=Math.max;function g(e,t){var r=z(e)||function(e){return function(e){return function(e){return!!e&&"object"==typeof e}(e)&&E(e)}(e)&&p.call(e,"callee")&&(!v.call(e,"callee")||"[object Arguments]"==y.call(e))}(e)?function(e,t){for(var r=-1,n=Array(e);++r<e;)n[r]=t(r);return n}(e.length,String):[],n=r.length,i=!!n;for(var o in e)!t&&!p.call(e,o)||i&&("length"==o||S(o,n))||r.push(o);return r}function m(e,t,r){var n=e[t];p.call(e,t)&&j(n,r)&&(void 0!==r||t in e)||(e[t]=r)}function w(e){if(!k(e))return function(e){var t=[];if(null!=e)for(var r in Object(e))t.push(r);return t}(e);var t,r,n=(t=e)===("function"==typeof(r=t&&t.constructor)&&r.prototype||d),i=[];for(var o in e)("constructor"!=o||!n&&p.call(e,o))&&i.push(o);return i}function S(e,t){return!!(t=null==t?9007199254740991:t)&&("number"==typeof e||f.test(e))&&e>-1&&e%1==0&&t>e}function j(e,t){return e===t||e!=e&&t!=t}var P,O,A,z=Array.isArray;function E(e){return null!=e&&function(e){return"number"==typeof e&&e>-1&&e%1==0&&9007199254740991>=e}(e.length)&&!function(e){var t=k(e)?y.call(e):"";return"[object Function]"==t||"[object GeneratorFunction]"==t}(e)}function k(e){var t=typeof e;return!!e&&("object"==t||"function"==t)}var x=(P=function(e,t){!function(e,t,r,n){r||(r={});for(var i=-1,o=t.length;++i<o;){var a=t[i],c=n?n(r[a],e[a],a,r,e):void 0;m(r,a,void 0===c?e[a]:c)}}(t,function(e){return E(e)?g(e,!0):w(e)}(t),e)},O=function(e,t){var r=-1,n=t.length,i=n>1?t[n-1]:void 0,o=n>2?t[2]:void 0;for(i=P.length>3&&"function"==typeof i?(n--,i):void 0,o&&function(e,t,r){if(!k(r))return!1;var n=typeof t;return!!("number"==n?E(r)&&S(t,r.length):"string"==n&&t in r)&&j(r[t],e)}(t[0],t[1],o)&&(i=3>n?void 0:i,n=1),e=Object(e);++r<n;){var a=t[r];a&&P(e,a,r,i)}return e},A=b(void 0===A?O.length-1:A,0),function(){for(var e=arguments,t=-1,r=b(e.length-A,0),n=Array(r);++t<r;)n[t]=e[A+t];t=-1;for(var i=Array(A+1);++t<A;)i[t]=e[t];return i[A]=n,h(O,this,i)}),N=function(e){return e},M=function(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:N;return c(c({},e),e.payload?{payload:t(e.payload)}:{})},R=function(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:N,r=arguments.length>2?arguments[2]:void 0;return r?function(n){for(var i=arguments.length,o=Array(i>1?i-1:0),a=1;i>a;a++)o[a-1]=arguments[a];return r.apply(void 0,[n].concat(o))?e.apply(void 0,[M(n,t)].concat(o)):e.apply(void 0,[n].concat(o))}:function(r){for(var n=arguments.length,i=Array(n>1?n-1:0),o=1;n>o;o++)i[o-1]=arguments[o];return e.apply(void 0,[M(r,t)].concat(i))}},I=function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:N;return function(t){return function(r,n){return t(R(r,e,n))}}},L=function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:N;return function(t){var r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:0;return function(){for(var n=arguments.length,i=Array(n),o=0;n>o;o++)i[o]=arguments[o];if(r>=i.length)throw Error("Message in request could not be serialized. "+"Expected message in position ".concat(r," but only received ").concat(i.length," args."));return i[r]=M(i[r],e),t.apply(void 0,i)}}};function _(){var e;try{e=self.chrome||self.browser||browser}catch(t){e=browser}if(!e)throw Error("Browser API is not present");return e}var D="\nLooks like there is an error in the background page. You might want to inspect your background page for more details.\n",T={channelName:"webext.channel",state:{},serializer:N,deserializer:N,patchStrategy:function(e,t){var r=Object.assign({},e);return t.forEach((function(e){var t=e.key,n=e.value;switch(e.change){case"updated":r[t]=n;break;case"removed":Reflect.deleteProperty(r,t)}})),r}};function q(){for(var e=arguments.length,t=Array(e),r=0;e>r;r++)t[r]=arguments[r];return 0===t.length?function(e){return e}:1===t.length?t[0]:t.reduce((function(e,t){return function(){return e(t.apply(void 0,arguments))}}))}var C=function(e){var t=function(){},r=new Promise((function(e){return t=e}));return{setListener:t,listener:function(t,n,i){if(e(t,n,i))return r.then((function(e){e(t,n,i)})),!0}}},F={channelName:"webext.channel",dispatchResponder:function(e,t){Promise.resolve(e).then((function(e){t({error:null,value:e})})).catch((function(e){console.error("error dispatching result:",e),t({error:e.message,value:null})}))},serializer:N,deserializer:N,diffStrategy:function(e,t){var r=[];return Object.keys(t).forEach((function(n){e[n]!==t[n]&&r.push({key:n,value:t[n],change:"updated"})})),Object.keys(e).forEach((function(e){t.hasOwnProperty(e)||r.push({key:e,change:"removed"})})),r}};e.Store=function(){return e=function e(){var t=this,n=arguments.length>0&&void 0!==arguments[0]?arguments[0]:T,i=n.channelName,o=void 0===i?T.channelName:i,a=n.state,c=void 0===a?T.state:a,u=n.serializer,s=void 0===u?T.serializer:u,l=n.deserializer,f=void 0===l?T.deserializer:l,h=n.patchStrategy,d=void 0===h?T.patchStrategy:h;if(r(this,e),!o)throw Error("channelName is required in options");if("function"!=typeof s)throw Error("serializer must be a function");if("function"!=typeof f)throw Error("deserializer must be a function");if("function"!=typeof d)throw Error("patchStrategy must be one of the included patching strategies or a custom patching function");this.channelName=o,this.readyResolved=!1,this.readyPromise=new Promise((function(e){return t.readyResolve=e})),this.browserAPI=_(),this.initializeStore=this.initializeStore.bind(this),this.browserAPI.runtime.sendMessage({type:"webext.fetch_state",channelName:o},void 0,this.initializeStore),this.deserializer=f,this.serializedPortListener=I(f)((function(){var e;return(e=t.browserAPI.runtime.onMessage).addListener.apply(e,arguments)})),this.serializedMessageSender=L(s)((function(){var e;return(e=t.browserAPI.runtime).sendMessage.apply(e,arguments)})),this.listeners=[],this.state=c,this.patchStrategy=d;var p=function(e){return!!e&&"string"==typeof e.type&&e.channelName===t.channelName};this.serializedPortListener((function(e){if(e&&e.channelName===t.channelName)switch(e.type){case"webext.state":t.replaceState(e.payload),t.readyResolved||(t.readyResolved=!0,t.readyResolve());break;case"webext.patch_state":t.patchState(e.payload)}}),p),this.dispatch=this.dispatch.bind(this),this.getState=this.getState.bind(this),this.subscribe=this.subscribe.bind(this)},(t=[{key:"ready",value:function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:null;return null!==e?this.readyPromise.then(e):this.readyPromise}},{key:"subscribe",value:function(e){var t=this;return this.listeners.push(e),function(){t.listeners=t.listeners.filter((function(t){return t!==e}))}}},{key:"patchState",value:function(e){this.state=this.patchStrategy(this.state,e),this.listeners.forEach((function(e){return e()}))}},{key:"replaceState",value:function(e){this.state=e,this.listeners.forEach((function(e){return e()}))}},{key:"getState",value:function(){return this.state}},{key:"replaceReducer",value:function(){}},{key:"dispatch",value:function(e){var t=this;return new Promise((function(r,n){t.serializedMessageSender({type:"webext.dispatch",channelName:t.channelName,payload:e},null,(function(e){if(e){var i=e.error,o=e.value;if(i){var a=Error("".concat(D).concat(i));n(x(a,i))}else r(o&&o.payload)}else{var c=t.browserAPI.runtime.lastError,u=Error("".concat(D).concat(c));n(x(u,c))}}))}))}},{key:"initializeStore",value:function(e){e&&"webext.fetch_state"===e.type&&(this.replaceState(e.payload),this.readyResolved||(this.readyResolved=!0,this.readyResolve()))}}])&&n(e.prototype,t),i&&n(e,i),Object.defineProperty(e,"prototype",{writable:!1}),e;var e,t,i}(),e.alias=function(e){return function(){return function(t){return function(r){var n=e[r.type];return t(n?n(r):r)}}}},e.applyMiddleware=function(e){for(var t=arguments.length,r=Array(t>1?t-1:0),n=1;t>n;n++)r[n-1]=arguments[n];var i=function(){throw Error("Dispatching while constructing your middleware is not allowed. Other middleware would not be applied to this dispatch.")},o={getState:e.getState.bind(e),dispatch:function(){return i.apply(void 0,arguments)}};return r=(r||[]).map((function(e){return e(o)})),i=q.apply(void 0,u(r))(e.dispatch),e.dispatch=i,e},e.createWrapStore=function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:F,t=e.channelName,r=void 0===t?F.channelName:t,n=_(),o=function(e){return"webext.fetch_state"===e.type&&e.channelName===r},a=function(e){return"webext.dispatch"===e.type&&e.channelName===r},c=C(o),u=C(a);return n.runtime.onMessage.addListener(c.listener),n.runtime.onMessage.addListener(u.listener),function(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:F,o=t.dispatchResponder,s=void 0===o?F.dispatchResponder:o,l=t.serializer,f=void 0===l?F.serializer:l,h=t.deserializer,d=void 0===h?F.deserializer:h,p=t.diffStrategy,y=void 0===p?F.diffStrategy:p;if("function"!=typeof f)throw Error("serializer must be a function");if("function"!=typeof d)throw Error("deserializer must be a function");if("function"!=typeof y)throw Error("diffStrategy must be one of the included diffing strategies or a custom diff function");var v=function(t,r,n){var i=Object.assign({},t.payload,{_sender:r}),o=null;try{o=e.dispatch(i)}catch(e){o=Promise.reject(e.message),console.error(e)}s(o,n)},b=L(f)((function(){for(var e,t=arguments.length,r=Array(t),o=0;t>o;o++)r[o]=arguments[o];var a=function(){};return(e=n.runtime).sendMessage.apply(e,r.concat([a])),n.tabs.query({},(function(e){var t,o=i(e);try{for(o.s();!(t=o.n()).done;){var c;(c=n.tabs).sendMessage.apply(c,[t.value.id].concat(r,[a]))}}catch(e){o.e(e)}finally{o.f()}}))})),g=e.getState(),m=function(){var t=e.getState(),n=y(g,t);n.length&&(g=t,b({type:"webext.patch_state",payload:n,channelName:r}))};e.subscribe(m),b({type:"webext.state",payload:g,channelName:r}),c.setListener((function(t,r,n){n({type:"webext.fetch_state",payload:e.getState()})}));var w=I(d);w(u.setListener)(v,a)}},Object.defineProperty(e,"__esModule",{value:!0})})); |
@@ -95,3 +95,2 @@ import * as redux from "redux"; | ||
configuration?: { | ||
channelName?: string; | ||
dispatchResponder?( | ||
@@ -110,3 +109,5 @@ dispatchResult: any, | ||
A extends redux.Action = redux.AnyAction | ||
>(): WrapStore<S, A>; | ||
>(configuration?: { | ||
channelName?: string; | ||
}): WrapStore<S, A>; | ||
@@ -113,0 +114,0 @@ export function alias(aliases: { |
@@ -7,3 +7,25 @@ "use strict"; | ||
exports.createDeferredListener = void 0; | ||
var createDeferredListener = exports.createDeferredListener = function createDeferredListener() { | ||
/** | ||
* Returns a function that can be passed as a listener callback to a browser | ||
* API. The listener will queue events until setListener is called. | ||
* | ||
* @param {Function} filter - A function that filters messages to be handled by | ||
* the listener. This is important to avoid telling the browser to expect an | ||
* async response when the message is not intended for this listener. | ||
* | ||
* @example | ||
* const filter = (message, sender, sendResponse) => { | ||
* return message.type === "my_type" | ||
* } | ||
* | ||
* const { listener, setListener } = createDeferredListener(filter); | ||
* chrome.runtime.onMessage.addListener(listener); | ||
* | ||
* // Later, define the listener to handle messages. Messages received | ||
* // before this point are queued. | ||
* setListener((message, sender, sendResponse) => { | ||
* console.log(message); | ||
* }); | ||
*/ | ||
var createDeferredListener = exports.createDeferredListener = function createDeferredListener(filter) { | ||
var resolve = function resolve() {}; | ||
@@ -14,2 +36,5 @@ var fnPromise = new Promise(function (resolve_) { | ||
var listener = function listener(message, sender, sendResponse) { | ||
if (!filter(message, sender, sendResponse)) { | ||
return; | ||
} | ||
fnPromise.then(function (fn) { | ||
@@ -16,0 +41,0 @@ fn(message, sender, sendResponse); |
@@ -48,3 +48,2 @@ "use strict"; | ||
* @param {Object} options | ||
* @param {string} options.channelName The name of the channel for this store. | ||
* @param {function} options.dispatchResponder A function that takes the result | ||
@@ -65,2 +64,4 @@ * of a store dispatch and optionally implements custom logic for responding to | ||
* messages that woke the service worker. | ||
* @param {Object} options | ||
* @param {string} options.channelName The name of the channel for this store. | ||
* @return {WrapStore} The wrapStore function that accepts a Redux store and | ||
@@ -70,25 +71,29 @@ * options. See {@link WrapStore}. | ||
var _default = exports["default"] = function _default() { | ||
var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : defaultOpts, | ||
_ref$channelName = _ref.channelName, | ||
channelName = _ref$channelName === void 0 ? defaultOpts.channelName : _ref$channelName; | ||
var browserAPI = (0, _util.getBrowserAPI)(); | ||
var filterStateMessages = function filterStateMessages(message) { | ||
return message.type === _constants.FETCH_STATE_TYPE && message.channelName === channelName; | ||
}; | ||
var filterActionMessages = function filterActionMessages(message) { | ||
return message.type === _constants.DISPATCH_TYPE && message.channelName === channelName; | ||
}; | ||
// Setup message listeners synchronously to avoid dropping messages if the | ||
// extension is woken by a message. | ||
var stateProviderListener = (0, _listener.createDeferredListener)(); | ||
var actionListener = (0, _listener.createDeferredListener)(); | ||
var stateProviderListener = (0, _listener.createDeferredListener)(filterStateMessages); | ||
var actionListener = (0, _listener.createDeferredListener)(filterActionMessages); | ||
browserAPI.runtime.onMessage.addListener(stateProviderListener.listener); | ||
browserAPI.runtime.onMessage.addListener(actionListener.listener); | ||
return function (store) { | ||
var _ref = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : defaultOpts, | ||
_ref$channelName = _ref.channelName, | ||
channelName = _ref$channelName === void 0 ? defaultOpts.channelName : _ref$channelName, | ||
_ref$dispatchResponde = _ref.dispatchResponder, | ||
dispatchResponder = _ref$dispatchResponde === void 0 ? defaultOpts.dispatchResponder : _ref$dispatchResponde, | ||
_ref$serializer = _ref.serializer, | ||
serializer = _ref$serializer === void 0 ? defaultOpts.serializer : _ref$serializer, | ||
_ref$deserializer = _ref.deserializer, | ||
deserializer = _ref$deserializer === void 0 ? defaultOpts.deserializer : _ref$deserializer, | ||
_ref$diffStrategy = _ref.diffStrategy, | ||
diffStrategy = _ref$diffStrategy === void 0 ? defaultOpts.diffStrategy : _ref$diffStrategy; | ||
if (!channelName) { | ||
throw new Error("channelName is required in options"); | ||
} | ||
var _ref2 = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : defaultOpts, | ||
_ref2$dispatchRespond = _ref2.dispatchResponder, | ||
dispatchResponder = _ref2$dispatchRespond === void 0 ? defaultOpts.dispatchResponder : _ref2$dispatchRespond, | ||
_ref2$serializer = _ref2.serializer, | ||
serializer = _ref2$serializer === void 0 ? defaultOpts.serializer : _ref2$serializer, | ||
_ref2$deserializer = _ref2.deserializer, | ||
deserializer = _ref2$deserializer === void 0 ? defaultOpts.deserializer : _ref2$deserializer, | ||
_ref2$diffStrategy = _ref2.diffStrategy, | ||
diffStrategy = _ref2$diffStrategy === void 0 ? defaultOpts.diffStrategy : _ref2$diffStrategy; | ||
if (typeof serializer !== "function") { | ||
@@ -108,16 +113,14 @@ throw new Error("serializer must be a function"); | ||
var dispatchResponse = function dispatchResponse(request, sender, sendResponse) { | ||
if (request.type === _constants.DISPATCH_TYPE && request.channelName === channelName) { | ||
var action = Object.assign({}, request.payload, { | ||
_sender: sender | ||
}); | ||
var dispatchResult = null; | ||
try { | ||
dispatchResult = store.dispatch(action); | ||
} catch (e) { | ||
dispatchResult = Promise.reject(e.message); | ||
console.error(e); | ||
} | ||
dispatchResponder(dispatchResult, sendResponse); | ||
return true; | ||
// Only called with messages that pass the filterActionMessages filter. | ||
var action = Object.assign({}, request.payload, { | ||
_sender: sender | ||
}); | ||
var dispatchResult = null; | ||
try { | ||
dispatchResult = store.dispatch(action); | ||
} catch (e) { | ||
dispatchResult = Promise.reject(e.message); | ||
console.error(e); | ||
} | ||
dispatchResponder(dispatchResult, sendResponse); | ||
}; | ||
@@ -180,6 +183,2 @@ | ||
}); | ||
var withPayloadDeserializer = (0, _serialization.withDeserializer)(deserializer); | ||
var shouldDeserialize = function shouldDeserialize(request) { | ||
return request.type === _constants.DISPATCH_TYPE && request.channelName === channelName; | ||
}; | ||
@@ -190,9 +189,8 @@ /** | ||
stateProviderListener.setListener(function (request, sender, sendResponse) { | ||
// This listener is only called with messages that pass filterStateMessages | ||
var state = store.getState(); | ||
if (request.type === _constants.FETCH_STATE_TYPE && request.channelName === channelName) { | ||
sendResponse({ | ||
type: _constants.FETCH_STATE_TYPE, | ||
payload: state | ||
}); | ||
} | ||
sendResponse({ | ||
type: _constants.FETCH_STATE_TYPE, | ||
payload: state | ||
}); | ||
}); | ||
@@ -203,4 +201,5 @@ | ||
*/ | ||
withPayloadDeserializer(actionListener.setListener)(dispatchResponse, shouldDeserialize); | ||
var withPayloadDeserializer = (0, _serialization.withDeserializer)(deserializer); | ||
withPayloadDeserializer(actionListener.setListener)(dispatchResponse, filterActionMessages); | ||
}; | ||
}; |
{ | ||
"name": "webext-redux", | ||
"version": "3.0.0", | ||
"version": "4.0.0", | ||
"description": "A set of utilities for building Redux applications in Web Extensions.", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -437,6 +437,2 @@ # WebExt Redux | ||
* [Advanced Usage](https://github.com/tshaddix/webext-redux/wiki/Advanced-Usage) | ||
* [API](https://github.com/tshaddix/webext-redux/wiki/API) | ||
* [Store](https://github.com/tshaddix/webext-redux/wiki/Store) | ||
* [createWrapStore](https://github.com/tshaddix/webext-redux/wiki/createWrapStore) | ||
* [alias](https://github.com/tshaddix/webext-redux/wiki/alias) | ||
@@ -443,0 +439,0 @@ ## Who's using this? |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
151974
3101
466