Skip to content

Commit

Permalink
fix(use-wire-state): support initializer function returning undefined
Browse files Browse the repository at this point in the history
  • Loading branch information
smmoosavi committed Nov 7, 2019
1 parent 0fc7036 commit 5e50241
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 20 deletions.
2 changes: 1 addition & 1 deletion docs/api/react-wire.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ connect react components with wire
| Function | Description |
| ------------------------------------------------------------------ | ---------------------------------------------------------- |
| [useWire(upLink, initialValue)](./react-wire.usewire.md) | creates and returns a new wire. |
| [useWireState(wire)](./react-wire.usewirestate.md) | same as react useState but synced with wire. |
| [useWireState(wire, initialValue)](./react-wire.usewirestate.md) | same as react useState but synced with wire. |
| [useWireState(wire, initialValue)](./react-wire.usewirestate_1.md) | same as react useState but synced with wire. |
| [useWireValue(wire)](./react-wire.usewirevalue.md) | returns wire value and subscribe to wire for value updates |
| [useWireValue(wire, defaultValue)](./react-wire.usewirevalue_1.md) | returns wire value and subscribe to wire for value updates |
Expand Down
18 changes: 12 additions & 6 deletions docs/api/react-wire.usewirestate.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,29 @@ same as react useState but synced with wire.
```typescript
export declare function useWireState<Value>(
wire: Subscribable<Value> | null | undefined,
): [Value | undefined, Dispatch<SetStateAction<Value>>];
initialValue: Value | (() => Value),
): [Value, Dispatch<SetStateAction<Defined<Value>>>];
```

## Parameters

| Parameter | Type | Description |
| --------- | ------------------------------------------------------------------- | ----------------------------------------------- |
| wire | <code>Subscribable&lt;Value&gt; &#124; null &#124; undefined</code> | the wire to be connected to and sync value with |
| Parameter | Type | Description |
| ------------ | ------------------------------------------------------------------- | ----------------------------------------------- |
| wire | <code>Subscribable&lt;Value&gt; &#124; null &#124; undefined</code> | the wire to be connected to and sync value with |
| initialValue | <code>Value &#124; (() =&gt; Value)</code> | initial value or initializer function |

<b>Returns:</b>

`[Value | undefined, Dispatch<SetStateAction<Value>>]`
`[Value, Dispatch<SetStateAction<Defined<Value>>>]`

state and setState.

## Remarks

if wire is null, it should behave like useState
if wire is null, it should behave like `useState`

if `wire` param provided and wire has a value, the state respect wire value and ignore its own initial value.

if `wire` param provided and wire hasn't a value (has `undefined` value), wire value will be updated

please always pass same wire and avoid changing wire, it can cause strange behavior and bad intermediate values
14 changes: 7 additions & 7 deletions docs/api/react-wire.usewirestate_1.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,20 @@ same as react useState but synced with wire.
```typescript
export declare function useWireState<Value>(
wire: Subscribable<Value> | null | undefined,
initialValue: Value | (() => Value),
): [Value, Dispatch<SetStateAction<Value>>];
initialValue?: undefined | Value | (() => Value | undefined),
): [Value | undefined, Dispatch<SetStateAction<Value>>];
```

## Parameters

| Parameter | Type | Description |
| ------------ | ------------------------------------------------------------------- | ----------------------------------------------- |
| wire | <code>Subscribable&lt;Value&gt; &#124; null &#124; undefined</code> | the wire to be connected to and sync value with |
| initialValue | <code>Value &#124; (() =&gt; Value)</code> | initial value or initializer function |
| Parameter | Type | Description |
| ------------ | ---------------------------------------------------------------------------- | ----------------------------------------------- |
| wire | <code>Subscribable&lt;Value&gt; &#124; null &#124; undefined</code> | the wire to be connected to and sync value with |
| initialValue | <code>undefined &#124; Value &#124; (() =&gt; Value &#124; undefined)</code> | initial value or initializer function |

<b>Returns:</b>

`[Value, Dispatch<SetStateAction<Value>>]`
`[Value | undefined, Dispatch<SetStateAction<Value>>]`

state and setState.

Expand Down
1 change: 1 addition & 0 deletions src/type-utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export type Defined<T> = T extends undefined ? never : T;
19 changes: 13 additions & 6 deletions src/use-wire-state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,37 @@ import {
} from 'react';
import { isSetStateAction } from './is-set-state-action';
import { Subscribable } from './subscribable';
import { Defined } from './type-utils';
import { useWire } from './use-wire';

/**
* same as react useState but synced with wire.
*
* @param wire - the wire to be connected to and sync value with
* @param initialValue - initial value or initializer function
*
* @returns state and setState.
*
* @remarks
* if wire is null, it should behave like useState
* if wire is null, it should behave like `useState`
*
* if `wire` param provided and wire has a value, the state respect wire value and ignore its own initial value.
*
* if `wire` param provided and wire hasn't a value (has `undefined` value), wire value will be updated
*
* please always pass same wire and avoid changing wire, it can cause strange behavior and bad intermediate values
*
*/
export function useWireState<Value>(
wire: Subscribable<Value> | null | undefined,
): [Value | undefined, Dispatch<SetStateAction<Value>>];
initialValue: Value | (() => Value),
): [Value, Dispatch<SetStateAction<Defined<Value>>>];

/**
* same as react useState but synced with wire.
*
* @param wire - the wire to be connected to and sync value with
* @param initialValue - initial value or initializer function
*
* @returns state and setState.
*
* @remarks
Expand All @@ -46,11 +53,11 @@ export function useWireState<Value>(
*/
export function useWireState<Value>(
wire: Subscribable<Value> | null | undefined,
initialValue: Value | (() => Value),
): [Value, Dispatch<SetStateAction<Value>>];
initialValue?: undefined | Value | (() => Value | undefined),
): [Value | undefined, Dispatch<SetStateAction<Value>>];
export function useWireState<Value>(
wire: Subscribable<Value> | null | undefined,
initialValue?: undefined | Value | (() => Value),
initialValue?: undefined | Value | (() => Value | undefined),
): [Value | undefined, Dispatch<SetStateAction<Value>>] {
const innerWire = useWire<Value>(wire, initialValue);
const [stateValue, setStateValue] = useState<Value | undefined>(() =>
Expand Down

0 comments on commit 5e50241

Please sign in to comment.