Skip to content
This repository has been archived by the owner on Feb 14, 2023. It is now read-only.

Commit

Permalink
resolves #10
Browse files Browse the repository at this point in the history
  • Loading branch information
Charles Stover - Vendor committed Nov 27, 2018
1 parent c7327e9 commit 7bc939b
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 3 deletions.
58 changes: 58 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import * as React from 'react';

interface AnyObject {
[key: string]: any;
}

type GlobalCallback = (global: GlobalState) => void;

// TypeScript does not allow props P to be passed to static methods.
declare class GlobalComponent<P = {}, S = {}> extends React.Component<P, S> {
static getDerivedGlobalFromProps?: (props: AnyObject, prevGloba: GlobalState, prevState: AnyObject) => NewGlobal;
private _globalCallback: () => void;
readonly global: Readonly<GlobalState>;
setGlobal(newGlobal: NewGlobal, callback?: GlobalCallback): Promise<void> | void;
}

declare class GlobalPureComponent<P = {}, S = {}> extends React.PureComponent<P, S> {
static getDerivedGlobalFromProps?: (props: AnyObject, prevGloba: GlobalState, prevState: AnyObject) => NewGlobal;
private _globalCallback: () => void;
readonly global: Readonly<GlobalState>;
setGlobal(newGlobal: NewGlobal, callback?: GlobalCallback): Promise<void> | void;
}

type GlobalReducer = (state: GlobalState, ...args: any[]) => NewGlobal;

type GlobalPropertySetter = (value: any) => void;

interface GlobalState {
[key: string]: any;
}

type GlobalStateSetter = (newGlobal: NewGlobal, callback?: GlobalCallback) => Promise<void> | void;

type LocalReducer = (...args: any[]) => void;

type MapGlobalToProps<ComponentProps, NewProps> = (global: GlobalState, props: ComponentProps) => NewProps;

type NewGlobal = NewGlobalFunction | null | Partial<GlobalState> | Promise<NewGlobalFunction> | Promise<null> | Promise<Partial<GlobalState>>;

type NewGlobalFunction = (global: GlobalState) => NewGlobal;

interface ReactN extends React {
(Component: React.ComponentType): GlobalComponent;
addReducer(name: string, reducer: GlobalReducer): void;
Component: GlobalComponent;
default: ReactN;
PureComponent: GlobalPureComponent;
resetGlobal(): void;
setGlobal(newGlobal: NewGlobal, callback?: GlobalCallback): Promise<void> | void;
useGlobal(): [ GlobalState, GlobalStateSetter ];
useGlobal<T>(property: keyof GlobalState, setterOnly?: boolean): [ T, GlobalPropertySetter ];
useGlobal(reducer: GlobalReducer): LocalReducer;
withGlobal<CP, NP>(getGlobal: MapGlobalToProps<CP, NP>): (Component: React.ComponentType<CP & NP>) => GlobalPureComponent<CP, never>;
}

declare const _: ReactN;
export = _;
export as namespace ReactN;
4 changes: 1 addition & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"license": "MIT",
"main": "index.js",
"repository": "https://github.com/CharlesStover/reactn.git",
"types": "./index.d.ts",
"scripts": {
"build": "webpack",
"dev": "set NODE_ENV=development&& webpack",
Expand All @@ -27,8 +28,5 @@
"mocha": "^5.2.0",
"webpack": "^4.20.2",
"webpack-cli": "^3.1.2"
},
"peerDependencies": {
"react": "^16.7.0-alpha.0"
}
}

0 comments on commit 7bc939b

Please sign in to comment.