Skip to content

Commit

Permalink
add dependencies argument to usePromistate
Browse files Browse the repository at this point in the history
  • Loading branch information
MZanggl committed Jan 24, 2022
1 parent 1fd8c1d commit e512f0a
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 6 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,9 @@ export default function App() {
}
```

You can pass promistate options as the second argument `usePromistate(somePromise, { defaultValue: "" })`,
and promise depedencies as the third argument `usePromistate(somePromise, { }, [dep1, dep2])`.

### Typescript

To type the result of the promise you can make use of generics.
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "promistate",
"version": "1.7.0",
"version": "1.7.3",
"description": "Manage promise state easily",
"main": "index.js",
"types": "index.d.ts",
Expand Down
3 changes: 2 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ function promistate<T>(callback: Callback<T>, options: Options<T> = {}) : Result
timesInitiated: 0,
timesSettled: 0,
_value: defaultValue,
_callback: callback,
isPending: false,
isDelayOver: false,
_error: null,
Expand Down Expand Up @@ -86,7 +87,7 @@ function promistate<T>(callback: Callback<T>, options: Options<T> = {}) : Result
}, delay)
}

return Promise.resolve(callback.apply(this, args))
return Promise.resolve(this._callback.apply(this, args))
.then((result: T) => {
if (shouldIgnore()) {
return Status.IGNORED
Expand Down
11 changes: 8 additions & 3 deletions src/lib/react/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { useRef, useState } from 'react'
import { useRef, useState, useEffect } from 'react'
import promistate, { PromistateResult, PromistateOptions } from '../..'
import { Callback } from '../../types'

type PromistateResultKeys = 'isEmpty' | 'value' | 'timesSettled' | 'isPending' | 'error' | 'isDelayOver'
export type PromistateReactState<T> = Pick<PromistateResult<T>, PromistateResultKeys>
Expand All @@ -25,17 +26,21 @@ type UsePromistateReturnType<T> = [
}
]

export function usePromistate<T>(promise: (...args: any[]) => Promise<T>, options: PromistateOptions<T> = {}): UsePromistateReturnType<T> {
export function usePromistate<T>(callback: Callback<T>, options: PromistateOptions<T> = {}, deps = []): UsePromistateReturnType<T> {
let setState: React.Dispatch<React.SetStateAction<PromistateReactState<T>>> | undefined;

const promiseRef = useRef(promistate<T>(promise, {
const promiseRef = useRef(promistate<T>(callback, {
...options,
listen() {
setState && setState(extractStyles<T>(promiseRef.current))
options.listen && options.listen()
}
}))

useEffect(() => {
promiseRef.current._callback = callback
}, deps)

const [state, setStateCopy] = useState(extractStyles(promiseRef.current))
setState = setStateCopy

Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export type CallbackArgs = any[]
export interface Result<T> {
timesInitiated: number;
timesSettled: number;
_callback: Callback<T>;
value: T | null;
_value: T | null;
_error: Error | null;
Expand Down

0 comments on commit e512f0a

Please sign in to comment.