Alex Mikhalev
7 years ago
12 changed files with 118 additions and 68 deletions
@ -1,3 +1,3 @@
@@ -1,3 +1,3 @@
|
||||
export { UiMessage, UiStore } from "./ui"; |
||||
export * from "./inject"; |
||||
export { UiMessage, UiStore } from "./UiStore"; |
||||
export * from "./reactContext"; |
||||
export { default as StateBase } from "./StateBase"; |
||||
|
@ -1,44 +0,0 @@
@@ -1,44 +0,0 @@
|
||||
import * as PropTypes from "prop-types"; |
||||
import * as React from "react"; |
||||
|
||||
import { StateBase } from "@app/state"; |
||||
|
||||
interface IProvidedStateContext { |
||||
providedState: StateBase; |
||||
} |
||||
|
||||
const providedStateContextTypes: PropTypes.ValidationMap<any> = { |
||||
providedState: PropTypes.object, |
||||
}; |
||||
|
||||
export class ProvideState extends React.Component<{ |
||||
state: StateBase, |
||||
}> implements React.ChildContextProvider<IProvidedStateContext> { |
||||
static childContextTypes = providedStateContextTypes; |
||||
|
||||
getChildContext(): IProvidedStateContext { |
||||
return { |
||||
providedState: this.props.state, |
||||
}; |
||||
} |
||||
|
||||
render() { |
||||
return React.Children.only(this.props.children); |
||||
} |
||||
} |
||||
|
||||
type Diff<T extends string | number | symbol, U extends string | number | symbol> = |
||||
({[P in T]: P } & {[P in U]: never } & { [x: string]: never })[T]; |
||||
type Omit<T, K extends keyof T> = {[P in Diff<keyof T, K>]: T[P]}; |
||||
|
||||
export function injectState<P extends { "state": StateBase }>(Component: React.ComponentType<P>) { |
||||
return class extends React.Component<Omit<P, "state">> { |
||||
static contextTypes = providedStateContextTypes; |
||||
context!: IProvidedStateContext; |
||||
|
||||
render() { |
||||
const state = this.context.providedState; |
||||
return <Component {...this.props} state={state} />; |
||||
} |
||||
}; |
||||
} |
@ -0,0 +1,50 @@
@@ -0,0 +1,50 @@
|
||||
import * as React from "react"; |
||||
|
||||
import { StateBase } from "@app/state"; |
||||
|
||||
const StateContext = React.createContext<StateBase | null>(null); |
||||
|
||||
export interface ProvideStateProps { |
||||
state: StateBase; |
||||
children: React.ReactNode; |
||||
} |
||||
|
||||
export function ProvideState({state, children}: ProvideStateProps) { |
||||
return ( |
||||
<StateContext.Provider value={state}> |
||||
{children} |
||||
</StateContext.Provider> |
||||
); |
||||
} |
||||
|
||||
export interface ConsumeStateProps { |
||||
children: (state: StateBase) => React.ReactNode; |
||||
} |
||||
|
||||
export function ConsumeState({children}: ConsumeStateProps) { |
||||
const consumeState = (state: StateBase | null) => { |
||||
if (state == null) { |
||||
throw new Error("Component with ConsumeState must be mounted inside ProvideState"); |
||||
} |
||||
return children(state); |
||||
}; |
||||
return <StateContext.Consumer>{consumeState}</StateContext.Consumer>; |
||||
} |
||||
|
||||
type Diff<T extends string | number | symbol, U extends string | number | symbol> = |
||||
({[P in T]: P } & {[P in U]: never } & { [x: string]: never })[T]; |
||||
type Omit<T, K extends keyof T> = {[P in Diff<keyof T, K>]: T[P]}; |
||||
|
||||
export function injectState<P extends { state: StateBase }>(Component: React.ComponentType<P>) { |
||||
return class extends React.Component<Omit<P, "state">> { |
||||
render() { |
||||
const consumeState = (state: StateBase | null) => { |
||||
if (state == null) { |
||||
throw new Error("Component with injectState must be mounted inside ProvideState"); |
||||
} |
||||
return <Component {...this.props} state={state}/>; |
||||
}; |
||||
return <StateContext.Consumer>{consumeState}</StateContext.Consumer>; |
||||
} |
||||
}; |
||||
} |
@ -0,0 +1,9 @@
@@ -0,0 +1,9 @@
|
||||
export enum ErrorCode { |
||||
BadRequest = 100, |
||||
NotSpecified = 101, |
||||
Parse = 102, |
||||
Range = 103, |
||||
InvalidData = 104, |
||||
Internal = 200, |
||||
Timeout = 300, |
||||
} |
Loading…
Reference in new issue