Skip to content

Commit

Permalink
feat: add types
Browse files Browse the repository at this point in the history
  • Loading branch information
chnliquan committed Nov 26, 2020
1 parent bd05c07 commit dc06836
Show file tree
Hide file tree
Showing 5 changed files with 192 additions and 21 deletions.
26 changes: 13 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
<p align="center"><a href="https://github.com/kwai-test/dox/" target="_blank" rel="noopener noreferrer"><img width="200" src="https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1604638209660&di=e8df94ce63ec253f7b3cfaa849e05452&imgtype=0&src=http%3A%2F%2Fphoto.16pic.com%2F00%2F13%2F82%2F16pic_1382167_b.jpg" alt="Dox logo"></a></p>
<p align="center"><a href="https://github.com/kwai-efe/dobux/" target="_blank" rel="noopener noreferrer"><img width="100" src="https://static.yximgs.com/udata/pkg/ks-ad-fe/efe/dobux-logo.png" alt="Dobux logo"></a></p>

<h1 align="center">Dox</h1>
<h1 align="center">Dobux</h1>

<p align="center">
this is the description of dox
轻量级的响应式状态管理方案
<br><br>
<a href="https://travis-ci.com/kwai-test/dox"><img src="https://travis-ci.com/kwai-test/dox.svg?branch=main" alt="Build Status"></a>
<a href="https://codecov.io/gh/kwai-test/dox"><img src="https://codecov.io/gh/kwai-test/dox/branch/main/graph/badge.svg" alt="Coverage Status"></a>
<a href="https://www.npmjs.com/package/dox-test"><img src="https://img.shields.io/npm/v/dox-test" alt="Version"></a>
<a href="https://www.npmjs.com/package/dox-test"><img src="https://img.shields.io/npm/dm/dox-test" alt="Downloads"></a>
<a href="https://img.shields.io/bundlephobia/minzip/dox-test"><img src="https://img.shields.io/bundlephobia/minzip/dox-test" alt="Bundle Size"></a>
<a href="https://github.com/kwai-test/dox/"><img src="https://img.shields.io/snyk/vulnerabilities/npm/dox-test" alt="Vulnerabilities"></a>
<a href="https://travis-ci.com/kwai-efe/dobux"><img src="https://travis-ci.com/kwai-efe/dobux.svg?branch=main" alt="Build Status"></a>
<a href="https://codecov.io/gh/kwai-efe/dobux"><img src="https://codecov.io/gh/kwai-efe/dobux/branch/main/graph/badge.svg" alt="Coverage Status"></a>
<a href="https://www.npmjs.com/package/dobux"><img src="https://img.shields.io/npm/v/dobux" alt="Version"></a>
<a href="https://www.npmjs.com/package/dobux"><img src="https://img.shields.io/npm/dm/dobux" alt="Downloads"></a>
<a href="https://img.shields.io/bundlephobia/minzip/dobux"><img src="https://img.shields.io/bundlephobia/minzip/dobux" alt="Bundle Size"></a>
<a href="https://github.com/kwai-efe/dobux/"><img src="https://img.shields.io/snyk/vulnerabilities/npm/dobux" alt="Vulnerabilities"></a>
<br>
<img src="https://img.shields.io/npm/dependency-version/dox-test/peer/react" alt="Peer React">
<img src="https://img.shields.io/npm/dependency-version/dox-test/peer/react-dom" alt="Peer React Dom">
<img src="https://img.shields.io/npm/dependency-version/dobux/peer/react" alt="Peer React">
<img src="https://img.shields.io/npm/dependency-version/dobux/peer/react-dom" alt="Peer React Dom">
</p>

## ✨ 特性
Expand All @@ -24,11 +24,11 @@
## 📦 安装

```bash
$ npm i dox-test -S
$ npm i dobux -S
```

```bash
$ yarn add dox-test
$ yarn add dobux
```

## 🔨 快速开始
Expand Down
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
},
"repository": {
"type": "git",
"url": "https://github.com/kwai-ad-fe/dobux"
"url": "https://github.com/kwai-efe/dobux"
},
"keywords": [
"react",
Expand Down Expand Up @@ -68,6 +68,9 @@
]
}
},
"dependencies": {
"immer": "^6.0.9"
},
"peerDependencies": {
"react": "^16.8.0",
"react-dom": "^16.8.0"
Expand Down
12 changes: 6 additions & 6 deletions scripts/conventional-changelog-config/writer-opts.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,22 +40,22 @@ function getWriterOpts() {
commit.type = `⚡ Performance Improvements`
} else if (commit.type === `revert`) {
commit.type = `⏪ Reverts`
} else if (commit.type === `docs`) {
commit.type = `📖 Documentation`
} else if (commit.type === `style`) {
commit.type = `💄 Styles`
} else if (commit.type === `refactor`) {
commit.type = `♻ Code Refactoring`
} else if (commit.type === `test`) {
commit.type = `✅ Tests`
} else if (discard) {
return
} else if (commit.type === `docs`) {
commit.type = `📖 Documentation`
} else if (commit.type === `style`) {
commit.type = `💄 Styles`
} else if (commit.type === `build`) {
commit.type = `📦 Build System`
} else if (commit.type === `ci`) {
commit.type = `🔧 Continuous Integration`
} else if (commit.type === 'chore') {
commit.type = '⚙️ Chores'
} else if (discard) {
return
}

if (commit.scope === '*') {
Expand Down
2 changes: 1 addition & 1 deletion scripts/release.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ async function publishToNpm(nextVersion) {
}

async function githubRelease(tag, body, isPrerelease) {
const repoUrl = repository ? repository.url : 'https://github.com/kwai-ad-fe/dobux'
const repoUrl = repository ? repository.url : 'https://github.com/kwai-efe/dobux'
const url = newGithubReleaseUrl({
repoUrl,
tag,
Expand Down
168 changes: 168 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
import { Dispatch } from 'react'

type Push<L extends any[], T> = ((r: any, ...x: L) => void) extends (...x: infer L2) => void
? { [K in keyof L2]-?: K extends keyof L ? L[K] : T }
: never

// convert a union to an intersection: X | Y | Z ==> X & Y & Z
type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends (k: infer I) => void
? I
: never

// convert a union to an overloaded function X | Y ==> ((x: X)=>void) & ((y:Y)=>void)
type UnionToOvlds<U> = UnionToIntersection<U extends any ? (f: U) => void : never>

// convert a union to a tuple X | Y => [X, Y]
// a union of too many elements will become an array instead
type UnionToTuple<U> = UTT0<U> extends infer T
? T extends any[]
? Exclude<U, T[number]> extends never
? T
: U[]
: never
: never

// each type function below pulls the last element off the union and
// pushes it onto the list it builds
type UTT0<U> = UnionToOvlds<U> extends (a: infer A) => void ? Push<UTT1<Exclude<U, A>>, A> : []
type UTT1<U> = UnionToOvlds<U> extends (a: infer A) => void ? Push<UTT2<Exclude<U, A>>, A> : []
type UTT2<U> = UnionToOvlds<U> extends (a: infer A) => void ? Push<UTT3<Exclude<U, A>>, A> : []
type UTT3<U> = UnionToOvlds<U> extends (a: infer A) => void ? Push<UTT4<Exclude<U, A>>, A> : []
type UTT4<U> = UnionToOvlds<U> extends (a: infer A) => void ? Push<UTT5<Exclude<U, A>>, A> : []
type UTT5<U> = UnionToOvlds<U> extends (a: infer A) => void ? Push<UTTX<Exclude<U, A>>, A> : []
type UTTX<U> = []

export interface Noop<R = any> {
(...args: any[]): R
}

export interface ConfigReducer<S = any> {
(state: S, ...payload: any): void
}

export interface ConfigReducers<S = any> {
[name: string]: ConfigReducer<S>
}

export interface ConfigEffect {
(...payload: any[]): any
}

export interface ConfigEffects<M = any, RM = any> {
(model: M, rootModel: RM): { [key: string]: ConfigEffect }
}

export interface Config<S = any> {
state: S
reducers: ConfigReducers<S>
effects: ConfigEffects
}

export interface Configs {
[key: string]: Config
}

interface ModelEffectState {
loading: boolean
identifier: number
}

interface BuildInReducers<S = any> {
setValue: <K extends keyof S>(key: K, value: S[K]) => void
setValues: (state: Partial<S>) => void
reset: <K extends keyof S>(key?: K) => void
}

type ModelReducer<MR extends ConfigReducer> = MR extends (state: any, ...payload: infer P) => void
? (...payload: P) => void
: any

export type ModelReducers<R extends ConfigReducers, S = any> = {
[K in keyof R]: ModelReducer<R[K]>
} &
BuildInReducers<S>

export type ModelEffect<E extends ConfigEffect> = (E extends (...payload: infer P) => infer R
? (...payload: P) => R
: any) &
ModelEffectState

export type ModelEffects<C extends { [key: string]: ConfigEffect }> = {
[K in keyof C]: ModelEffect<C[K]>
}

export interface Model<C extends Config, S = undefined, R = undefined, E = undefined> {
state: S extends undefined ? C['state'] : S
reducers: R extends undefined
? C['reducers'] extends ConfigReducers
? ModelReducers<C['reducers'], C['state']>
: BuildInReducers<C['state']>
: R
effects: E extends undefined
? C['effects'] extends ConfigEffects
? ModelEffects<ReturnType<C['effects']>>
: Record<string, unknown>
: E
}

export type Models<C extends Configs, S = undefined, R = undefined, E = undefined> = {
[K in keyof C]: {
state: Model<C[K], S, R, E>['state']
reducers: Model<C[K], S, R, E>['reducers']
effects: Model<C[K], S, R, E>['effects']
}
}

export interface ModelConfig<S = any> {
state: S
reducers: ConfigReducers<S>
effects: { [key: string]: ConfigEffect }
}

export interface ContextPropsModel<C extends ModelConfig = any> {
state: C['state']
reducers: C['reducers'] extends ConfigReducers<C['state']>
? ModelReducers<C['reducers'], C['state']>
: BuildInReducers<C['state']>
effects: C['effects'] extends ConfigEffects
? ModelEffects<ReturnType<C['effects']>>
: Record<string, unknown>
}

export interface ModelProviderOptions {
autoReset?: boolean
devTools?: boolean
}

export interface ModelContextProps {
model: ContextPropsModel
options: ModelProviderOptions
}

export type ModelProvider = React.FC<React.PropsWithChildren<ModelProviderOptions>>

export interface StoreProviderOptions<C extends Configs> {
autoReset?: boolean | UnionToTuple<keyof C>
devTools?: boolean | UnionToTuple<keyof C>
}

export type StoreProvider<C extends Configs> = React.FC<
React.PropsWithChildren<StoreProviderOptions<C>>
>

export type HOC<InjectProps = any> = <P>(
Component: React.ComponentType<P & InjectProps>
) => React.ComponentType<P>

export type Optionality<T extends K, K> = Omit<T, keyof K>

export interface Subscriber<T> {
mapStateToProps: any
prevState: T
effects: any
dispatcher: Dispatch<any>
}

export interface MapStateToProps<M extends Model<any>, S = any> {
(state: M['state']): S
}

0 comments on commit dc06836

Please sign in to comment.