Skip to content

Commit

Permalink
fix: improve useWireValue typing
Browse files Browse the repository at this point in the history
  • Loading branch information
smmoosavi committed Mar 29, 2020
1 parent 104fb97 commit a4475a4
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 37 deletions.
6 changes: 3 additions & 3 deletions src/state-wire/use-wire-value.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ describe('useWireValue', () => {
describe('without initial value', () => {
it('should return undefined', () => {
const { result } = renderHook(() => {
const wire = useStateWire(null);
const wire = useStateWire<number>(null);
const value = useWireValue(wire);
return { value };
});
Expand All @@ -42,7 +42,7 @@ describe('useWireValue', () => {
describe('with initial value on sibling', () => {
it('should return wire initial value', () => {
const { result } = renderHook(() => {
const parent = useStateWire(null);
const parent = useStateWire<number>(null);
const wire = useStateWire(parent, 4);
const value = useWireValue(wire);
const sibling = useStateWire(parent, 5);
Expand All @@ -57,7 +57,7 @@ describe('useWireValue', () => {
describe('with default value', () => {
it('should returns default value', () => {
const { result } = renderHook(() => {
const wire = useStateWire(null);
const wire = useStateWire<number>(null);
const value = useWireValue(wire, 5);
return { value };
});
Expand Down
58 changes: 24 additions & 34 deletions src/state-wire/use-wire-value.ts
Original file line number Diff line number Diff line change
@@ -1,40 +1,30 @@
import { useDebugValue, useEffect, useState } from 'react';
import { Defined } from '../utils/type-utils';
import { useStabilityGuard } from '../utils/use-stability-guard';
import { StateWire } from './state-wire';
import { StateWire, WireState } from './state-wire';

/**
*
* returns wire value and subscribe to wire for value updates
*
* @param wire
*
* @remarks
* please always pass same wire param and avoid changing wire param. if wire argument changed, an error will be thrown.
*
*/
export function useWireValue<Value>(
wire: StateWire<Value> | null | undefined,
): Value | undefined;
/**
*
* returns wire value and subscribe to wire for value updates
*
* @param wire
* @param defaultValue - return value if wire value is undefined
*
* @remarks
*
* please always pass same wire param and avoid changing wire param. if wire argument changed, an error will be thrown.
*
*/
export function useWireValue<Value>(
wire: StateWire<Value> | null | undefined,
defaultValue: Value,
): Value | undefined;
export function useWireValue<Value>(
wire: StateWire<Value> | null | undefined,
defaultValue?: Value,
): Value | undefined {
export function useWireValue(
wire: null | undefined,
defaultValue?: unknown,
): undefined;
export function useWireValue<W extends StateWire<any>>(wire: W): WireState<W>;
export function useWireValue<W extends StateWire<any>>(
wire: W | null | undefined,
defaultValue: Defined<WireState<W>>,
): Defined<WireState<W>>;
export function useWireValue<W extends StateWire<any>>(
wire: W,
defaultValue?: WireState<W> | undefined,
): WireState<W>;
export function useWireValue<W extends StateWire<any>>(
wire: W | null | undefined,
defaultValue?: WireState<W>,
): WireState<W> | undefined;
export function useWireValue<W extends StateWire<any>>(
wire: W | null | undefined,
defaultValue?: WireState<W>,
): WireState<W> | undefined {
type Value = WireState<W>;
useStabilityGuard(wire);
const wireValue = wire?.getValue();
const valueToReturn = wireValue === undefined ? defaultValue : wireValue;
Expand Down

0 comments on commit a4475a4

Please sign in to comment.