stream-chat-react-native-core
Version:
The official React Native and Expo components for Stream Chat, a service for building chat applications
38 lines • 1.37 kB
JavaScript
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.useStateStore = useStateStore;
var _react = require("react");
var _shim = require("use-sync-external-store/shim");
var noop = () => {};
function useStateStore(store, selector) {
var wrappedSubscription = (0, _react.useCallback)(onStoreChange => {
var unsubscribe = store?.subscribeWithSelector(selector, onStoreChange);
return unsubscribe ?? noop;
}, [store, selector]);
var wrappedSnapshot = (0, _react.useMemo)(() => {
var cachedTuple;
return () => {
var currentValue = store?.getLatestValue();
if (!currentValue) return undefined;
if (cachedTuple && cachedTuple[0] === currentValue) {
return cachedTuple[1];
}
var newlySelected = selector(currentValue);
if (cachedTuple) {
var selectededAreEqualToCached = true;
for (var key in cachedTuple[1]) {
if (cachedTuple[1][key] === newlySelected[key]) continue;
selectededAreEqualToCached = false;
break;
}
if (selectededAreEqualToCached) return cachedTuple[1];
}
cachedTuple = [currentValue, newlySelected];
return cachedTuple[1];
};
}, [store, selector]);
var state = (0, _shim.useSyncExternalStore)(wrappedSubscription, wrappedSnapshot);
return state;
}
//# sourceMappingURL=useStateStore.js.map