redux-ext
Version:
This simple package allow you to use Redux store all across the webextension.
131 lines (107 loc) • 4.01 kB
JavaScript
Object.defineProperty(exports, "__esModule", {
value: true
});
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
var _constants = require('../constants');
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
var ContentProxyStore = function () {
function ContentProxyStore(name) {
var _this = this;
_classCallCheck(this, ContentProxyStore);
this.dispatch = function (action) {
switch (typeof action === 'undefined' ? 'undefined' : _typeof(action)) {
case 'object':
_constants.webextApi.runtime.sendMessage({
_type: _constants.DISPATCH,
_name: _this.name,
_data: action
});
return action;
case 'function':
action(_this.dispatch, _this.getState);
break;
}
};
this.getState = function () {
return Object.assign({}, _this.state);
};
this.subscribe = function (observer) {
var id = Object.keys(_this.observers).length;
_this.observers[id] = observer;
return function () {
delete _this.observers[id];
};
};
this.name = name;
this.state = {};
this.observers = {};
_constants.webextApi.runtime.onMessage.addListener(function (request, sender, sendResponse) {
if (request && request._type && request._type === _constants.STATE && request._name && request._name === _this.name && request._data) {
_this.state = request._data;
for (var id in _this.observers) {
typeof _this.observers[id] === 'function' && _this.observers[id]();
}
}
});
}
_createClass(ContentProxyStore, [{
key: 'ready',
value: function ready() {
var _this2 = this;
return new Promise(function (resolve, reject) {
_constants.webextApi.runtime.sendMessage({
_type: _constants.STATE,
_name: _this2.name,
_data: false
}, function (state) {
try {
_this2.state = state;
} catch (e) {
reject(e);
}
resolve(_this2);
});
});
}
}]);
return ContentProxyStore;
}();
var PopupProxyStore = function () {
function PopupProxyStore(name) {
_classCallCheck(this, PopupProxyStore);
this.name = name;
}
_createClass(PopupProxyStore, [{
key: 'ready',
value: function ready() {
var _this3 = this;
return new Promise(function (resolve, reject) {
var proceed = function proceed(win) {
var store = !!win && win['__' + _this3.name];
if (store) {
var dispatch = store.dispatch,
getState = store.getState,
subscribe = store.subscribe;
_this3.dispatch = dispatch.bind(_this3);
_this3.getState = getState.bind(_this3);
_this3.subscribe = subscribe.bind(_this3);
resolve(store);
} else {
reject();
}
};
if (_constants.webextApi.extension.getBackgroundPage) {
proceed(_constants.webextApi.extension.getBackgroundPage());
} else if (_constants.webextApi.browser.getBackgroundWindow) {
_constants.webextApi.browser.getBackgroundWindow(proceed);
} else {
reject();
}
});
}
}]);
return PopupProxyStore;
}();
exports.default = _constants.isPopup && _constants.browserName === 'safari' ? PopupProxyStore : ContentProxyStore;
;