@decathlon/moon
Advanced tools
Comparing version 2.0.2 to 2.0.3
@@ -21,5 +21,8 @@ "use strict"; | ||
var _a = React.useState(queryResult), state = _a[0], setState = _a[1]; | ||
store.subscribeToQueryResult(queryId, setState); | ||
var setQueryResult = React.useCallback(function (_queryId, result) { | ||
setState(result); | ||
}, [queryId]); | ||
store.subscribeToQueryResult(queryId, setQueryResult); | ||
React.useEffect(function () { | ||
return function () { return store.unsubscribeFromQueryResult(queryId, setState); }; | ||
return function () { return store.unsubscribeFromQueryResult(queryId, setQueryResult); }; | ||
}, [queryId]); | ||
@@ -32,14 +35,17 @@ return resultToProps ? resultToProps(state) : state; | ||
var currentQueriesIds = usePrevValue(queriesIds).value; | ||
var queriesStates = currentQueriesIds.reduce(function (result, queryId) { | ||
result[queryId] = store.readQuery(queryId).data; | ||
return result; | ||
}, {}); | ||
var _a = React.useState(queriesStates), states = _a[0], setStates = _a[1]; | ||
var setState = function (queryId) { return function (result) { | ||
var readQueriesResults = function () { | ||
return currentQueriesIds.reduce(function (result, queryId) { | ||
result[queryId] = store.readQuery(queryId).data; | ||
return result; | ||
}, {}); | ||
}; | ||
var _a = React.useState(readQueriesResults()), states = _a[0], setStates = _a[1]; | ||
var setQueriesResults = React.useCallback(function (queryId, result) { | ||
var _a; | ||
setStates(__assign(__assign({}, states), (_a = {}, _a[queryId] = result, _a))); | ||
}; }; | ||
var newQueries = readQueriesResults(); | ||
setStates(__assign(__assign({}, newQueries), (_a = {}, _a[queryId] = result, _a))); | ||
}, [currentQueriesIds]); | ||
var queriesHandlers = React.useMemo(function () { | ||
return currentQueriesIds.reduce(function (handlers, queryId) { | ||
handlers[queryId] = setState(queryId); | ||
handlers[queryId] = setQueriesResults; | ||
return handlers; | ||
@@ -58,3 +64,3 @@ }, {}); | ||
}; | ||
}, [currentQueriesIds]); | ||
}, [queriesHandlers]); | ||
return resultsToProps ? resultsToProps(states) : states; | ||
@@ -67,5 +73,8 @@ } | ||
var _a = React.useState(queryState), state = _a[0], setState = _a[1]; | ||
store.subscribeToQuery(queryId, setState); | ||
var setQueryState = React.useCallback(function (_queryId, state) { | ||
setState(state); | ||
}, [queryId]); | ||
store.subscribeToQuery(queryId, setQueryState); | ||
React.useEffect(function () { | ||
return function () { return store.unsubscribeFromQuery(queryId, setState); }; | ||
return function () { return store.unsubscribeFromQuery(queryId, setQueryState); }; | ||
}, [queryId]); | ||
@@ -78,14 +87,17 @@ return stateToProps ? stateToProps(state) : state; | ||
var currentQueriesIds = usePrevValue(queriesIds).value; | ||
var queriesStates = currentQueriesIds.reduce(function (result, queryId) { | ||
result[queryId] = store.readQuery(queryId); | ||
return result; | ||
}, {}); | ||
var _a = React.useState(queriesStates), states = _a[0], setStates = _a[1]; | ||
var setState = function (queryId) { return function (state) { | ||
var readQueriesStates = function () { | ||
return currentQueriesIds.reduce(function (result, queryId) { | ||
result[queryId] = store.readQuery(queryId); | ||
return result; | ||
}, {}); | ||
}; | ||
var _a = React.useState(readQueriesStates), states = _a[0], setStates = _a[1]; | ||
var setQueriesStates = React.useCallback(function (queryId, state) { | ||
var _a; | ||
setStates(__assign(__assign({}, states), (_a = {}, _a[queryId] = state, _a))); | ||
}; }; | ||
var newQueries = readQueriesStates(); | ||
setStates(__assign(__assign({}, newQueries), (_a = {}, _a[queryId] = state, _a))); | ||
}, [currentQueriesIds]); | ||
var queriesHandlers = React.useMemo(function () { | ||
return currentQueriesIds.reduce(function (handlers, queryId) { | ||
handlers[queryId] = setState(queryId); | ||
handlers[queryId] = setQueriesStates; | ||
return handlers; | ||
@@ -104,3 +116,3 @@ }, {}); | ||
}; | ||
}, [currentQueriesIds]); | ||
}, [queriesHandlers]); | ||
return statesToProps ? statesToProps(states) : states; | ||
@@ -107,0 +119,0 @@ } |
@@ -12,4 +12,4 @@ export declare enum MoonNetworkStatus { | ||
} | ||
export declare type OnStateChangeHandler<Data = any> = (state: QueryState<Data>) => void; | ||
export declare type OnResultChangeHandler<Data = any> = (data: Data) => void; | ||
export declare type OnStateChangeHandler<Data = any> = (queryId: string, state: QueryState<Data>) => void; | ||
export declare type OnResultChangeHandler<Data = any> = (queryId: string, data: Data) => void; | ||
interface Query<Data = any> { | ||
@@ -16,0 +16,0 @@ id: string; |
@@ -72,7 +72,7 @@ "use strict"; | ||
currentQuery.onStateChangeHandlers.forEach(function (onStateChangeHandler) { | ||
onStateChangeHandler(newState); | ||
onStateChangeHandler(queryId, newState); | ||
}); | ||
if (!shallow_equal_1.default(newState.data, currentQuery.state.data)) { | ||
currentQuery.onResultChangeHandlers.forEach(function (onResultChangeHandler) { | ||
onResultChangeHandler(newState.data); | ||
onResultChangeHandler(queryId, newState.data); | ||
}); | ||
@@ -79,0 +79,0 @@ } |
{ | ||
"name": "@decathlon/moon", | ||
"version": "2.0.2", | ||
"version": "2.0.3", | ||
"description": "A featured, production ready caching REST client for every React UI", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
Sorry, the diff of this file is not supported yet
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
102486
1168