@proscom/prostore-react
Advanced tools
Comparing version 0.1.2 to 0.1.3
import { DependencyList } from 'react'; | ||
import { CheckRequestResult, CheckRequestStateFn, IRequestState, RequestStore } from '@proscom/prostore'; | ||
import { CheckRequestResult, CheckRequestStateFn, IRequestState, IRequestStoreOptions, RequestStore } from '@proscom/prostore'; | ||
export interface UseRequestStoreResult<State> { | ||
@@ -8,2 +8,5 @@ check: CheckRequestResult; | ||
} | ||
export interface UseRequestOptions<Vars, Data> { | ||
checkRequest?: CheckRequestStateFn<Vars, Data>; | ||
} | ||
/** | ||
@@ -15,6 +18,7 @@ * Attached component to the provided store and requests data based on the variables and options | ||
* @param options - Additional request options. They are not observed | ||
* @param checkRequest - Optional function which may be used to override default behavior of determining | ||
* when to fire the request | ||
* @param hookOptions - Additional hook options: | ||
* - checkRequest - Optional function which may be used to override default behavior of determining | ||
* when to fire the request | ||
*/ | ||
export declare function useRequestStore<Vars, Data, Options>(storeOrName: string | RequestStore<Vars, Data, Options>, variables: Vars, options?: Partial<Options>, checkRequest?: CheckRequestStateFn<Vars, Data>): UseRequestStoreResult<IRequestState<Vars, Data>>; | ||
export declare function useRequestStore<Vars, Data, Options extends IRequestStoreOptions<Vars, Data>>(storeOrName: string | RequestStore<Vars, Data, Options>, variables: Vars, options?: Options, hookOptions?: UseRequestOptions<Vars, Data>): UseRequestStoreResult<IRequestState<Vars, Data>>; | ||
/** | ||
@@ -27,2 +31,2 @@ * The same as useRequestStore, but it uses variableCreator which is supplied to useMemo and | ||
*/ | ||
export declare function useRequestStoreVars<Vars, Data>(storeOrName: string | RequestStore<Vars, Data, any>, variableCreator: () => Vars, args: DependencyList): UseRequestStoreResult<IRequestState<Vars, Data>>; | ||
export declare function useRequestStoreVars<Vars, Data, Options extends IRequestStoreOptions<Vars, Data>>(storeOrName: string | RequestStore<Vars, Data, Options>, variableCreator: () => Vars, args: DependencyList): UseRequestStoreResult<IRequestState<Vars, Data>>; |
@@ -1,5 +0,5 @@ | ||
import { useCallback, useEffect, useMemo, useRef, useState } from 'react'; | ||
import { checkRequestState } from '@proscom/prostore'; | ||
import { useCallback, useContext, useEffect, useMemo, useRef, useState } from 'react'; | ||
import { useConnectStore } from './useConnectStore'; | ||
import { useContextStore } from './ProstoreContext'; | ||
import { ProstoreSsrContext } from './ProstoreSsrContext'; | ||
/** | ||
@@ -11,8 +11,10 @@ * Attached component to the provided store and requests data based on the variables and options | ||
* @param options - Additional request options. They are not observed | ||
* @param checkRequest - Optional function which may be used to override default behavior of determining | ||
* when to fire the request | ||
* @param hookOptions - Additional hook options: | ||
* - checkRequest - Optional function which may be used to override default behavior of determining | ||
* when to fire the request | ||
*/ | ||
export function useRequestStore(storeOrName, variables, options, checkRequest) { | ||
if (options === void 0) { options = null; } | ||
if (checkRequest === void 0) { checkRequest = checkRequestState; } | ||
export function useRequestStore(storeOrName, variables, options, hookOptions) { | ||
if (options === void 0) { options = undefined; } | ||
if (hookOptions === void 0) { hookOptions = {}; } | ||
var checkRequest = hookOptions.checkRequest; | ||
// Inject store from context | ||
@@ -43,2 +45,8 @@ var store = useContextStore(storeOrName); | ||
}); | ||
// Attach ssrContext | ||
var ssrContext = useContext(ProstoreSsrContext); | ||
// Perform request if server rendering | ||
if (store.ssrId && ssrContext && ssrContext.isServer && queryCheck.request) { | ||
loadData().catch(function (e) { return console.error(e); }); | ||
} | ||
// Return stuff which may be useful in the component | ||
@@ -45,0 +53,0 @@ return { check: queryCheck, state: state, load: loadData }; |
import { DependencyList } from 'react'; | ||
import { CheckRequestResult, CheckRequestStateFn, IRequestState, RequestStore } from '@proscom/prostore'; | ||
import { CheckRequestResult, CheckRequestStateFn, IRequestState, IRequestStoreOptions, RequestStore } from '@proscom/prostore'; | ||
export interface UseRequestStoreResult<State> { | ||
@@ -8,2 +8,5 @@ check: CheckRequestResult; | ||
} | ||
export interface UseRequestOptions<Vars, Data> { | ||
checkRequest?: CheckRequestStateFn<Vars, Data>; | ||
} | ||
/** | ||
@@ -15,6 +18,7 @@ * Attached component to the provided store and requests data based on the variables and options | ||
* @param options - Additional request options. They are not observed | ||
* @param checkRequest - Optional function which may be used to override default behavior of determining | ||
* when to fire the request | ||
* @param hookOptions - Additional hook options: | ||
* - checkRequest - Optional function which may be used to override default behavior of determining | ||
* when to fire the request | ||
*/ | ||
export declare function useRequestStore<Vars, Data, Options>(storeOrName: string | RequestStore<Vars, Data, Options>, variables: Vars, options?: Partial<Options>, checkRequest?: CheckRequestStateFn<Vars, Data>): UseRequestStoreResult<IRequestState<Vars, Data>>; | ||
export declare function useRequestStore<Vars, Data, Options extends IRequestStoreOptions<Vars, Data>>(storeOrName: string | RequestStore<Vars, Data, Options>, variables: Vars, options?: Options, hookOptions?: UseRequestOptions<Vars, Data>): UseRequestStoreResult<IRequestState<Vars, Data>>; | ||
/** | ||
@@ -27,2 +31,2 @@ * The same as useRequestStore, but it uses variableCreator which is supplied to useMemo and | ||
*/ | ||
export declare function useRequestStoreVars<Vars, Data>(storeOrName: string | RequestStore<Vars, Data, any>, variableCreator: () => Vars, args: DependencyList): UseRequestStoreResult<IRequestState<Vars, Data>>; | ||
export declare function useRequestStoreVars<Vars, Data, Options extends IRequestStoreOptions<Vars, Data>>(storeOrName: string | RequestStore<Vars, Data, Options>, variableCreator: () => Vars, args: DependencyList): UseRequestStoreResult<IRequestState<Vars, Data>>; |
@@ -5,5 +5,5 @@ "use strict"; | ||
var react_1 = require("react"); | ||
var prostore_1 = require("@proscom/prostore"); | ||
var useConnectStore_1 = require("./useConnectStore"); | ||
var ProstoreContext_1 = require("./ProstoreContext"); | ||
var ProstoreSsrContext_1 = require("./ProstoreSsrContext"); | ||
/** | ||
@@ -15,8 +15,10 @@ * Attached component to the provided store and requests data based on the variables and options | ||
* @param options - Additional request options. They are not observed | ||
* @param checkRequest - Optional function which may be used to override default behavior of determining | ||
* when to fire the request | ||
* @param hookOptions - Additional hook options: | ||
* - checkRequest - Optional function which may be used to override default behavior of determining | ||
* when to fire the request | ||
*/ | ||
function useRequestStore(storeOrName, variables, options, checkRequest) { | ||
if (options === void 0) { options = null; } | ||
if (checkRequest === void 0) { checkRequest = prostore_1.checkRequestState; } | ||
function useRequestStore(storeOrName, variables, options, hookOptions) { | ||
if (options === void 0) { options = undefined; } | ||
if (hookOptions === void 0) { hookOptions = {}; } | ||
var checkRequest = hookOptions.checkRequest; | ||
// Inject store from context | ||
@@ -47,2 +49,8 @@ var store = ProstoreContext_1.useContextStore(storeOrName); | ||
}); | ||
// Attach ssrContext | ||
var ssrContext = react_1.useContext(ProstoreSsrContext_1.ProstoreSsrContext); | ||
// Perform request if server rendering | ||
if (store.ssrId && ssrContext && ssrContext.isServer && queryCheck.request) { | ||
loadData().catch(function (e) { return console.error(e); }); | ||
} | ||
// Return stuff which may be useful in the component | ||
@@ -49,0 +57,0 @@ return { check: queryCheck, state: state, load: loadData }; |
{ | ||
"name": "@proscom/prostore-react", | ||
"version": "0.1.2", | ||
"version": "0.1.3", | ||
"description": "> TODO: description", | ||
@@ -29,3 +29,3 @@ "author": "Andrew Starostin <a.starostin@proscom.ru>", | ||
"dependencies": { | ||
"@proscom/prostore": "^0.1.2" | ||
"@proscom/prostore": "^0.1.3" | ||
}, | ||
@@ -40,3 +40,3 @@ "devDependencies": { | ||
"sideEffects": false, | ||
"gitHead": "843db22013e3501b4a92cbae0b12367680a2f386" | ||
"gitHead": "eb04c659c22618c082700bfe8ae74d432ebe7b2c" | ||
} |
import { | ||
DependencyList, | ||
useCallback, | ||
useContext, | ||
useEffect, | ||
@@ -11,5 +12,5 @@ useMemo, | ||
CheckRequestResult, | ||
checkRequestState, | ||
CheckRequestStateFn, | ||
IRequestState, | ||
IRequestStoreOptions, | ||
RequestStore | ||
@@ -19,2 +20,3 @@ } from '@proscom/prostore'; | ||
import { useContextStore } from './ProstoreContext'; | ||
import { ProstoreSsrContext } from './ProstoreSsrContext'; | ||
@@ -27,2 +29,6 @@ export interface UseRequestStoreResult<State> { | ||
export interface UseRequestOptions<Vars, Data> { | ||
checkRequest?: CheckRequestStateFn<Vars, Data>; | ||
} | ||
/** | ||
@@ -34,15 +40,20 @@ * Attached component to the provided store and requests data based on the variables and options | ||
* @param options - Additional request options. They are not observed | ||
* @param checkRequest - Optional function which may be used to override default behavior of determining | ||
* when to fire the request | ||
* @param hookOptions - Additional hook options: | ||
* - checkRequest - Optional function which may be used to override default behavior of determining | ||
* when to fire the request | ||
*/ | ||
export function useRequestStore<Vars, Data, Options>( | ||
export function useRequestStore< | ||
Vars, | ||
Data, | ||
Options extends IRequestStoreOptions<Vars, Data> | ||
>( | ||
storeOrName: string | RequestStore<Vars, Data, Options>, | ||
variables: Vars, | ||
options: Partial<Options> = null, | ||
checkRequest: CheckRequestStateFn<Vars, Data> = checkRequestState | ||
options: Options = undefined, | ||
hookOptions: UseRequestOptions<Vars, Data> = {} | ||
): UseRequestStoreResult<IRequestState<Vars, Data>> { | ||
const { checkRequest } = hookOptions; | ||
// Inject store from context | ||
const store: RequestStore<Vars, Data, Options> = useContextStore( | ||
storeOrName | ||
) as RequestStore<Vars, Data, Options>; | ||
const store = useContextStore<RequestStore<Vars, Data, Options>>(storeOrName); | ||
@@ -79,2 +90,10 @@ // Local copy of store state | ||
// Attach ssrContext | ||
const ssrContext = useContext(ProstoreSsrContext); | ||
// Perform request if server rendering | ||
if (store.ssrId && ssrContext && ssrContext.isServer && queryCheck.request) { | ||
loadData().catch((e) => console.error(e)); | ||
} | ||
// Return stuff which may be useful in the component | ||
@@ -91,4 +110,8 @@ return { check: queryCheck, state, load: loadData }; | ||
*/ | ||
export function useRequestStoreVars<Vars, Data>( | ||
storeOrName: string | RequestStore<Vars, Data, any>, | ||
export function useRequestStoreVars< | ||
Vars, | ||
Data, | ||
Options extends IRequestStoreOptions<Vars, Data> | ||
>( | ||
storeOrName: string | RequestStore<Vars, Data, Options>, | ||
variableCreator: () => Vars, | ||
@@ -95,0 +118,0 @@ args: DependencyList |
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
64046
1130
Updated@proscom/prostore@^0.1.3