react-redux-query
Advanced tools
Comparing version 0.1.1 to 0.2.0
{ | ||
"name": "react-redux-query", | ||
"version": "0.1.1", | ||
"version": "0.2.0", | ||
"author": "Kyle Bebak <kylebebak@gmail.com>", | ||
@@ -5,0 +5,0 @@ "description": "React hooks and functions for SWR-style data fetching, caching and automatic updates, backed by Redux", |
45
query.js
@@ -13,2 +13,21 @@ "use strict"; | ||
}; | ||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); | ||
}) : (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
o[k2] = m[k]; | ||
})); | ||
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { | ||
Object.defineProperty(o, "default", { enumerable: true, value: v }); | ||
}) : function(o, v) { | ||
o["default"] = v; | ||
}); | ||
var __importStar = (this && this.__importStar) || function (mod) { | ||
if (mod && mod.__esModule) return mod; | ||
var result = {}; | ||
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); | ||
__setModuleDefault(result, mod); | ||
return result; | ||
}; | ||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
@@ -62,6 +81,7 @@ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.useResponse = exports.getResponse = exports.usePoll = exports.useQuery = exports.query = void 0; | ||
var react_1 = require("react"); | ||
exports.useResponse = exports.getResponse = exports.usePoll = exports.useQuery = exports.query = exports.ConfigContext = void 0; | ||
var react_1 = __importStar(require("react")); | ||
var react_redux_1 = require("react-redux"); | ||
var actions_1 = require("./actions"); | ||
exports.ConfigContext = react_1.default.createContext({}); | ||
var fetchStateByKey = {}; | ||
@@ -75,3 +95,3 @@ /** | ||
* - If response.queryResponse is set, save queryResponse | ||
* - If response.queryResponse is set but is undefined or null, don't save anything | ||
* - If response.queryResponse is set but is null or undefined, don't save anything | ||
* | ||
@@ -140,6 +160,7 @@ * @param key - Key in query branch under which to store response | ||
var dispatch = react_redux_1.useDispatch(); | ||
var _b = react_1.useContext(exports.ConfigContext), _c = _b.branchName, branchName = _c === void 0 ? 'query' : _c, restConfig = __rest(_b, ["branchName"]); | ||
var response = react_redux_1.useSelector(function (state) { | ||
if (!key) | ||
return; | ||
return state.query[key]; | ||
return state[branchName][key]; | ||
}); | ||
@@ -150,3 +171,3 @@ react_1.useEffect(function () { | ||
if (fetcher && key) | ||
query(key, fetcher, __assign(__assign({}, rest), { dispatch: dispatch })); | ||
query(key, fetcher, __assign(__assign(__assign({}, restConfig), rest), { dispatch: dispatch })); | ||
}, [key, refetchKey]); // eslint-disable-line | ||
@@ -181,2 +202,3 @@ return response; | ||
var dispatch = react_redux_1.useDispatch(); | ||
var _a = react_1.useContext(exports.ConfigContext), _b = _a.branchName, branchName = _b === void 0 ? 'query' : _b, restConfig = __rest(_a, ["branchName"]); | ||
var pollId = react_1.useRef(0); | ||
@@ -195,3 +217,3 @@ react_1.useEffect(function () { | ||
return [2 /*return*/]; | ||
return [4 /*yield*/, query(key, fetcher, __assign(__assign({}, rest), { dispatch: dispatch }))]; | ||
return [4 /*yield*/, query(key, fetcher, __assign(__assign(__assign({}, restConfig), rest), { dispatch: dispatch }))]; | ||
case 1: | ||
@@ -213,3 +235,3 @@ _a.sent(); | ||
return; | ||
return state.query[key]; | ||
return state[branchName][key]; | ||
}); | ||
@@ -222,3 +244,3 @@ return response; | ||
* | ||
* @param query - Current query branch of state tree | ||
* @param queryState - Current query branch of state tree | ||
* @param key - Key in query branch | ||
@@ -228,6 +250,6 @@ * | ||
*/ | ||
function getResponse(state, key) { | ||
function getResponse(queryState, key) { | ||
if (!key) | ||
return; | ||
return state.query[key]; | ||
return queryState[key]; | ||
} | ||
@@ -244,4 +266,5 @@ exports.getResponse = getResponse; | ||
function useResponse(key) { | ||
return react_redux_1.useSelector(function (state) { return getResponse(state, key); }); | ||
var _a = react_1.useContext(exports.ConfigContext).branchName, branchName = _a === void 0 ? 'query' : _a; | ||
return react_redux_1.useSelector(function (state) { return getResponse(state[branchName], key); }); | ||
} | ||
exports.useResponse = useResponse; |
32
query.ts
@@ -1,2 +0,2 @@ | ||
import { useEffect, useRef } from 'react' | ||
import React, { useEffect, useRef, useContext } from 'react' | ||
import { useSelector, useDispatch } from 'react-redux' | ||
@@ -7,5 +7,10 @@ import { Dispatch } from 'redux' | ||
export interface State<QR extends {} = { [property: string]: unknown }> { | ||
query: { [key: string]: QueryResponse<QR> } | ||
export const ConfigContext = React.createContext<{ branchName?: string; dedupe?: boolean; dedupeMs?: number }>({}) | ||
export interface QueryState<QR extends {} = { [property: string]: unknown }> { | ||
[key: string]: QueryResponse<QR> | ||
} | ||
interface State { | ||
query: QueryState | ||
} | ||
@@ -30,3 +35,3 @@ const fetchStateByKey: { [key: string]: { sentMs: number } | undefined } = {} | ||
* - If response.queryResponse is set, save queryResponse | ||
* - If response.queryResponse is set but is undefined or null, don't save anything | ||
* - If response.queryResponse is set but is null or undefined, don't save anything | ||
* | ||
@@ -93,6 +98,7 @@ * @param key - Key in query branch under which to store response | ||
const dispatch = useDispatch() | ||
const { branchName = 'query', ...restConfig } = useContext(ConfigContext) | ||
const response = useSelector((state: State) => { | ||
if (!key) return | ||
return state.query[key] as QueryResponse<QR> | ||
return state[branchName as 'query'][key] as QueryResponse<QR> | ||
}) | ||
@@ -102,3 +108,3 @@ | ||
if (response && noRefetch) return | ||
if (fetcher && key) query(key, fetcher, { ...rest, dispatch }) | ||
if (fetcher && key) query(key, fetcher, { ...restConfig, ...rest, dispatch }) | ||
}, [key, refetchKey]) // eslint-disable-line | ||
@@ -137,2 +143,3 @@ | ||
const dispatch = useDispatch() | ||
const { branchName = 'query', ...restConfig } = useContext(ConfigContext) | ||
@@ -149,3 +156,3 @@ const pollId = useRef(0) | ||
if (pollId.current === 0 || pollId.current !== pid) return | ||
await query(key, fetcher, { ...rest, dispatch }) | ||
await query(key, fetcher, { ...restConfig, ...rest, dispatch }) | ||
setTimeout(() => poll(pid), intervalMs) | ||
@@ -163,3 +170,3 @@ } | ||
if (!key) return | ||
return state.query[key] as QueryResponse<QR> | ||
return state[branchName as 'query'][key] as QueryResponse<QR> | ||
}) | ||
@@ -172,3 +179,3 @@ return response | ||
* | ||
* @param query - Current query branch of state tree | ||
* @param queryState - Current query branch of state tree | ||
* @param key - Key in query branch | ||
@@ -178,5 +185,5 @@ * | ||
*/ | ||
export function getResponse<QR>(state: State, key: string | null | undefined) { | ||
export function getResponse<QR>(queryState: QueryState, key: string | null | undefined) { | ||
if (!key) return | ||
return state.query[key] as QueryResponse<QR> | ||
return queryState[key] as QueryResponse<QR> | ||
} | ||
@@ -193,3 +200,4 @@ | ||
export function useResponse<QR>(key: string | null | undefined) { | ||
return useSelector((state: State) => getResponse<QR>(state, key)) | ||
const { branchName = 'query' } = useContext(ConfigContext) | ||
return useSelector((state: State) => getResponse<QR>(state[branchName as 'query'], key)) | ||
} |
@@ -1,2 +0,2 @@ | ||
import { State } from './query' | ||
import { QueryState } from './query' | ||
import { Action } from './actions' | ||
@@ -12,3 +12,3 @@ | ||
*/ | ||
export default function reduce(state: State['query'] = {}, action: Action): State['query'] { | ||
export default function reduce(state: QueryState = {}, action: Action): QueryState { | ||
const receivedMs = Date.now() | ||
@@ -15,0 +15,0 @@ |
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
28540
603