react-unistore
Version:
778b connector between React and unistore
115 lines • 4.09 kB
JavaScript
;
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
exports.__esModule = true;
var react_1 = require("react");
function mapActions(actions, store) {
if (typeof actions === "function") {
actions = actions(store);
}
var mapped = {};
for (var i in actions) {
mapped[i] = store.action(actions[i]);
}
return mapped;
}
exports.mapActions = mapActions;
function select(properties) {
if (typeof properties === "string") {
properties = properties.split(/\s*,\s*/);
}
return function (state) {
var selected = {};
for (var i = 0; i < properties.length; i++) {
selected[properties[i]] = state[properties[i]];
}
return selected;
};
}
exports.select = select;
var UnistoreContext = react_1.createContext(null);
exports.Provider = UnistoreContext.Provider;
exports.useStore = function () {
var store = react_1.useContext(UnistoreContext);
if (!store) {
throw new Error("Missing context. Ensure you've rendered a Provider.");
}
return store;
};
exports.useAction = function (action) { return exports.useStore().action(action); };
exports.useSelector = function (selector, equalityFn) {
var store = exports.useStore();
var _a = react_1.useReducer(function (x) { return x + 1; }, 0), forceUpdate = _a[1];
var resultRef = react_1.useRef(null);
resultRef.current = selector(store.getState());
react_1.useEffect(function () {
var listener = function (state) {
var result = selector(state);
if (equalityFn
? !equalityFn(resultRef.current, result)
: resultRef.current !== result) {
forceUpdate({});
}
};
store.subscribe(listener);
return function () {
store.unsubscribe(listener);
};
}, []);
return resultRef.current;
};
function connect(mapStateToProps, actions) {
if (typeof mapStateToProps !== "function") {
mapStateToProps = select(mapStateToProps || []);
}
return function (Child) {
function Wrapper(props, context) {
var _this = this;
react_1.Component.call(this, props, context);
var store = context;
var state = mapStateToProps(store ? store.getState() : {}, props);
var boundActions = actions ? mapActions(actions, store) : { store: store };
var update = function () {
var mapped = mapStateToProps(store ? store.getState() : {}, props);
for (var i in mapped) {
if (mapped[i] !== state[i]) {
state = mapped;
return _this.forceUpdate();
}
}
for (var i in state) {
if (!(i in mapped)) {
state = mapped;
return _this.forceUpdate();
}
}
};
this.UNSAFE_componentWillReceiveProps = function (p) {
props = p;
update();
};
this.componentDidMount = function () {
store.subscribe(update);
};
this.componentWillUnmount = function () {
store.unsubscribe(update);
};
this.render = function () {
return react_1.createElement(Child, __assign(__assign(__assign({}, boundActions), _this.props), state));
};
}
Wrapper.contextType = UnistoreContext;
return ((Wrapper.prototype = Object.create(react_1.Component.prototype)).constructor = Wrapper);
};
}
exports.connect = connect;
//# sourceMappingURL=index.js.map