Skip to content

Commit

Permalink
feat(selector): export useSelector and createSelector
Browse files Browse the repository at this point in the history
  • Loading branch information
smmoosavi committed Jun 18, 2020
1 parent 75024ba commit 586a0e9
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
35 changes: 35 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@ declare type CB<T> = (t: T) => void;

declare type CovarianceGuard<T> = [T, unknown];

export declare function createSelector<V, Fns = {}>(
options: WritableSelectorOptions<V>,
): Wire<V, Fns>;

export declare function createSelector<V, Fns = {}>(
options: ReadOnlySelectorOptions<V>,
): ReadonlyWire<V, Fns>;

export declare function createWire<V, Fns = {}>(initialValue: V): Wire<V, Fns>;

declare type Defined<T> = T extends undefined ? never : T;
Expand All @@ -30,6 +38,8 @@ declare interface FnsWireGuard<Fns> {
' fns-wire': StrictMethodsGuard<Fns>;
}

declare type GetWireValue = <V>(wire: ReadonlyStateWire<V>) => V;

declare type InitializerOrValue<V> = V | (() => V);

export declare type Interceptor<Value> = (
Expand All @@ -49,6 +59,10 @@ declare type Methods<T> = Pick<T, MethodKeys<T>>;

declare type NonNever<T> = IsNever<T> extends true ? any : T;

declare interface ReadOnlySelectorOptions<V> {
get: (options: { get: GetWireValue }) => V;
}

export declare interface ReadonlyStateWire<V>
extends ReadonlyStateWireGuard<V> {
/**
Expand All @@ -71,6 +85,8 @@ declare interface ReadonlyStateWireGuard<V> {
export declare type ReadonlyWire<V, Fns = {}> = ReadonlyStateWire<V> &
FnsWire<Fns>;

declare type SetWireValue = <V>(wire: StateWire<V>, value: Defined<V>) => void;

export declare interface StateWire<V> extends StateWireGuard<V> {
/**
* get current value
Expand Down Expand Up @@ -160,6 +176,14 @@ export declare function useInterceptor<W extends StateWire<any>>(
interceptor: Interceptor<WireState<W>>,
): W;

export declare function useSelector<V, Fns = {}>(
options: WritableSelectorOptions<V>,
): Wire<V, Fns>;

export declare function useSelector<V, Fns = {}>(
options: ReadOnlySelectorOptions<V>,
): ReadonlyWire<V, Fns>;

export declare function useWire<V, Fns = {}>(
upLink: Wire<V, Fns>,
): Wire<V, Fns>;
Expand Down Expand Up @@ -236,4 +260,15 @@ export declare type WireState<
W extends ReadonlyStateWire<any>
> = W extends ReadonlyStateWire<infer V> ? V : never;

declare interface WritableSelectorOptions<V> {
get: (options: { get: GetWireValue }) => V;
set: (
options: {
get: GetWireValue;
set: SetWireValue;
},
value: V,
) => void;
}

export {};
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,5 @@ export { useWireValue } from './state-wire/use-wire-value';
export { useInterceptor } from './interceptor/use-interceptor';
export { useFn } from './fn-wire/use-fn';
export { createWire } from './wire/create-wire';
export { createSelector } from './selector/create-selector';
export { useSelector } from './selector/use-selector';

0 comments on commit 586a0e9

Please sign in to comment.