react-reducer-store
Advanced tools
Comparing version 2.0.5 to 2.1.0
@@ -10,2 +10,16 @@ 'use strict'; | ||
function _typeof(obj) { | ||
if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { | ||
_typeof = function (obj) { | ||
return typeof obj; | ||
}; | ||
} else { | ||
_typeof = function (obj) { | ||
return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; | ||
}; | ||
} | ||
return _typeof(obj); | ||
} | ||
function _slicedToArray(arr, i) { | ||
@@ -83,6 +97,61 @@ return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _nonIterableRest(); | ||
function useStore() { | ||
return React.useContext(StoreContext); | ||
// This was taken from react-redux! | ||
var hasOwn = Object.prototype.hasOwnProperty; | ||
function is(x, y) { | ||
if (x === y) { | ||
return x !== 0 || y !== 0 || 1 / x === 1 / y; | ||
} else { | ||
return x !== x && y !== y; | ||
} | ||
} | ||
function shallowEqual(objA, objB) { | ||
if (is(objA, objB)) return true; | ||
if (_typeof(objA) !== 'object' || objA === null || _typeof(objB) !== 'object' || objB === null) { | ||
return false; | ||
} | ||
var keysA = Object.keys(objA); | ||
var keysB = Object.keys(objB); | ||
if (keysA.length !== keysB.length) return false; | ||
for (var i = 0; i < keysA.length; i++) { | ||
if (!hasOwn.call(objB, keysA[i]) || !is(objA[keysA[i]], objB[keysA[i]])) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
function useStore(mapContextToState) { | ||
var _useState = React.useState(), | ||
_useState2 = _slicedToArray(_useState, 2), | ||
state = _useState2[0], | ||
setState = _useState2[1]; | ||
React.useEffect(function () { | ||
StoreContext.subscribe(handleContextChange); | ||
return function () { | ||
return StoreContext.unsubscribe(handleContextChange); | ||
}; | ||
}, []); | ||
function handleContextChange(context) { | ||
if (typeof mapContextToState === 'function') { | ||
var newState = mapContextToState(context); | ||
if (!shallowEqual(newState, state)) { | ||
setState(newState); | ||
} | ||
} else { | ||
setState(context); | ||
} | ||
} | ||
return state; | ||
} | ||
function useDispatch() { | ||
@@ -89,0 +158,0 @@ return React.useContext(DispatchContext); |
@@ -1,3 +0,17 @@ | ||
import React, { createContext, useReducer, useEffect, useContext, useMemo } from 'react'; | ||
import React, { createContext, useReducer, useEffect, useState, useContext, useMemo } from 'react'; | ||
function _typeof(obj) { | ||
if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { | ||
_typeof = function (obj) { | ||
return typeof obj; | ||
}; | ||
} else { | ||
_typeof = function (obj) { | ||
return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; | ||
}; | ||
} | ||
return _typeof(obj); | ||
} | ||
function _slicedToArray(arr, i) { | ||
@@ -75,6 +89,61 @@ return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _nonIterableRest(); | ||
function useStore() { | ||
return useContext(StoreContext); | ||
// This was taken from react-redux! | ||
var hasOwn = Object.prototype.hasOwnProperty; | ||
function is(x, y) { | ||
if (x === y) { | ||
return x !== 0 || y !== 0 || 1 / x === 1 / y; | ||
} else { | ||
return x !== x && y !== y; | ||
} | ||
} | ||
function shallowEqual(objA, objB) { | ||
if (is(objA, objB)) return true; | ||
if (_typeof(objA) !== 'object' || objA === null || _typeof(objB) !== 'object' || objB === null) { | ||
return false; | ||
} | ||
var keysA = Object.keys(objA); | ||
var keysB = Object.keys(objB); | ||
if (keysA.length !== keysB.length) return false; | ||
for (var i = 0; i < keysA.length; i++) { | ||
if (!hasOwn.call(objB, keysA[i]) || !is(objA[keysA[i]], objB[keysA[i]])) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
function useStore(mapContextToState) { | ||
var _useState = useState(), | ||
_useState2 = _slicedToArray(_useState, 2), | ||
state = _useState2[0], | ||
setState = _useState2[1]; | ||
useEffect(function () { | ||
StoreContext.subscribe(handleContextChange); | ||
return function () { | ||
return StoreContext.unsubscribe(handleContextChange); | ||
}; | ||
}, []); | ||
function handleContextChange(context) { | ||
if (typeof mapContextToState === 'function') { | ||
var newState = mapContextToState(context); | ||
if (!shallowEqual(newState, state)) { | ||
setState(newState); | ||
} | ||
} else { | ||
setState(context); | ||
} | ||
} | ||
return state; | ||
} | ||
function useDispatch() { | ||
@@ -81,0 +150,0 @@ return useContext(DispatchContext); |
{ | ||
"name": "react-reducer-store", | ||
"version": "2.0.5", | ||
"version": "2.1.0", | ||
"main": "dist/index.cjs.js", | ||
@@ -5,0 +5,0 @@ "module": "dist/index.esm.js", |
47437
294