Skip to content

Commit 556f644

Browse files
committed
style(lint): add ts-lint
1 parent a17b1bc commit 556f644

File tree

6 files changed

+75
-50
lines changed

6 files changed

+75
-50
lines changed

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
"scripts": {
99
"build": "microbundle -o ./dist --sourcemap false",
1010
"commit": "git-cz",
11+
"lint-ts": "tslint -c tslint.json 'src/**/*.ts'",
1112
"lint-md": "remark .",
1213
"test": "jest --silent",
1314
"test:coverage": "jest --collect-coverage --silent"
@@ -42,6 +43,7 @@
4243
"remark-lint": "^6.0.4",
4344
"remark-preset-lint-recommended": "^3.0.2",
4445
"ts-jest": "^24.0.0",
46+
"tslint": "^5.14.0",
4547
"typescript": "^3.3.1"
4648
},
4749
"husky": {

src/global.ts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
1-
let State = {}
2-
let Actions = {}
3-
let AsyncState = {}
1+
const State = {}
2+
const Actions = {}
3+
const AsyncState = {}
44
// Communicate between Provider-Consumer and Hooks
5-
let Setter: Setter = {
6-
// classSetter stores the setState from Provider, invoke the classSetter.setState can update the state of Global Provider.
5+
const Setter: Setter = {
6+
// classSetter stores the setState from Provider
7+
// Invoke the classSetter.setState can update the state of Global Provider.
78
classSetter: undefined,
8-
// functionSetter stores the setState returned by useStore. These setStates can invoke the rerender of hooks components.
9+
// functionSetter stores the setState returned by useStore.
10+
// These setStates can invoke the rerender of hooks components.
911
functionSetter: {}
1012
}
1113

12-
let subscriptions = {}
14+
const subscriptions = {}
1315

1416
let devTools: any
1517
let withDevTools = false
@@ -22,7 +24,7 @@ export default {
2224
Setter,
2325
State,
2426
devTools,
25-
withDevTools,
27+
subscriptions,
2628
uid,
27-
subscriptions
29+
withDevTools
2830
} as Global

src/helper.ts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import produce from 'immer'
22
import { createContext } from 'react'
33
import Global from './global'
4-
import { applyMiddlewares, actionMiddlewares } from './middlewares'
4+
import { actionMiddlewares, applyMiddlewares } from './middlewares'
55

66
const initialProviderState: Global['State'] = {}
77
const GlobalContext = createContext(initialProviderState)
@@ -25,15 +25,15 @@ const consumerAction = (
2525
modelContext: { modelName: string }
2626
) => async (params: any, middlewareConfig?: any) => {
2727
const context: InnerContext = {
28-
type: 'outer',
29-
modelName: modelContext.modelName,
30-
actionName: action.name,
3128
Global,
29+
action,
30+
actionName: action.name,
31+
consumerActions,
32+
middlewareConfig,
33+
modelName: modelContext.modelName,
3234
newState: null,
3335
params,
34-
middlewareConfig,
35-
consumerActions,
36-
action
36+
type: 'outer'
3737
}
3838
await applyMiddlewares(actionMiddlewares, context)
3939
}
@@ -42,7 +42,7 @@ const consumerActions = (
4242
actions: Actions,
4343
modelContext: { modelName: string }
4444
) => {
45-
let ret: any = {}
45+
const ret: any = {}
4646
Object.entries<Action>(actions).forEach(([key, action]) => {
4747
ret[key] = consumerAction(action, modelContext)
4848
})
@@ -51,7 +51,9 @@ const consumerActions = (
5151

5252
const setPartialState = (
5353
name: keyof typeof Global['State'],
54-
partialState: typeof Global['State'] | Function
54+
partialState:
55+
| typeof Global['State']
56+
| ((state: typeof Global['State']['name']) => void)
5557
) => {
5658
if (typeof partialState === 'function') {
5759
let state = Global.State[name]

src/index.tsx

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
/// <reference path="./index.d.ts" />
22
import * as React from 'react'
3-
import Global from './global'
43
import { PureComponent, useEffect, useState } from 'react'
4+
import Global from './global'
55
import {
6-
GlobalContext,
76
Consumer,
7+
consumerActions,
88
getInitialState,
9-
consumerActions
9+
GlobalContext
1010
} from './helper'
1111
import { actionMiddlewares, applyMiddlewares, middlewares } from './middlewares'
1212

@@ -34,16 +34,16 @@ const Model = <M extends Models>(models: M, initialState?: Global['State']) => {
3434
}
3535
return {
3636
actions,
37-
useStore,
38-
getState,
39-
getInitialState,
4037
getActions,
38+
getInitialState,
39+
getState,
4140
subscribe,
42-
unsubscribe
41+
unsubscribe,
42+
useStore
4343
} as {
4444
useStore: <K extends keyof M>(
4545
name: K,
46-
depActions?: (keyof Get<M[K], 'actions'>)[]
46+
depActions?: Array<keyof Get<M[K], 'actions'>>
4747
) => [Get<M[K], 'state'>, getConsumerActionsType<Get<M[K], 'actions'>>]
4848
getState: <K extends keyof M>(modelName: K) => Readonly<Get<M[K], 'state'>>
4949
getActions: <K extends keyof M>(
@@ -52,12 +52,14 @@ const Model = <M extends Models>(models: M, initialState?: Global['State']) => {
5252
getInitialState: typeof getInitialState
5353
subscribe: <K extends keyof M>(
5454
modelName: K,
55-
actionName: keyof Get<M[K], 'actions'> | (keyof Get<M[K], 'actions'>)[],
56-
callback: Function
55+
actionName:
56+
| keyof Get<M[K], 'actions'>
57+
| Array<keyof Get<M[K], 'actions'>>,
58+
callback: () => void
5759
) => void
5860
unsubscribe: <K extends keyof M>(
5961
modelName: K,
60-
actionName: keyof Get<M[K], 'actions'> | (keyof Get<M[K], 'actions'>)[]
62+
actionName: keyof Get<M[K], 'actions'> | Array<keyof Get<M[K], 'actions'>>
6163
) => void
6264
actions: {
6365
[K in keyof M]: Readonly<getConsumerActionsType<Get<M[K], 'actions'>>>
@@ -72,7 +74,7 @@ const unsubscribe = (modelName: string, actions: string | string[]) => {
7274
const subscribe = (
7375
modelName: string,
7476
actions: string | string[],
75-
callback?: Function
77+
callback?: () => void
7678
) => {
7779
if (Array.isArray(actions)) {
7880
actions.forEach(actionName => {
@@ -110,15 +112,15 @@ const getActions = (
110112
([key, action]) =>
111113
(updaters[key] = async (params: any, middlewareConfig?: any) => {
112114
const context: InnerContext = {
113-
modelName,
115+
action,
114116
actionName: key,
117+
consumerActions,
118+
middlewareConfig,
119+
modelName,
115120
newState: null,
116121
params,
117-
middlewareConfig,
118-
consumerActions,
119-
action: action,
120-
Global,
121-
...baseContext
122+
...baseContext,
123+
Global
122124
}
123125
await applyMiddlewares(actionMiddlewares, context)
124126
})
@@ -129,13 +131,14 @@ const getActions = (
129131
const useStore = (modelName: string, depActions?: string[]) => {
130132
const setState = useState(Global.State[modelName])[1]
131133
Global.uid += 1
132-
const _hash = '' + Global.uid
133-
if (!Global.Setter.functionSetter[modelName])
134+
const hash = '' + Global.uid
135+
if (!Global.Setter.functionSetter[modelName]) {
134136
Global.Setter.functionSetter[modelName] = {}
135-
Global.Setter.functionSetter[modelName][_hash] = { setState, depActions }
137+
}
138+
Global.Setter.functionSetter[modelName][hash] = { setState, depActions }
136139
useEffect(() => {
137140
return function cleanup() {
138-
delete Global.Setter.functionSetter[modelName][_hash]
141+
delete Global.Setter.functionSetter[modelName][hash]
139142
}
140143
})
141144
const updaters = getActions(modelName, { setState, type: 'function' })

src/middlewares.ts

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { setPartialState, timeout, getCache } from './helper'
1+
import { getCache, setPartialState, timeout } from './helper'
22
// -- Middlewares --
33

44
const tryCatch: Middleware = async (context, restMiddlewares) => {
@@ -52,19 +52,20 @@ const setNewState: Middleware = async (context, restMiddlewares) => {
5252

5353
const stateUpdater: Middleware = async (context, restMiddlewares) => {
5454
const { modelName, next, Global } = context
55-
context.type === 'function' &&
56-
context.setState &&
55+
if (context.type === 'function' && context.setState) {
5756
context.setState(Global.State[modelName])
57+
}
5858
await next(restMiddlewares)
5959
}
6060

6161
const subscription: Middleware = async (context, restMiddlewares) => {
6262
const { modelName, actionName, next, Global } = context
6363
const subscriptions = Global.subscriptions[`${modelName}_${actionName}`]
64-
subscriptions &&
64+
if (subscriptions) {
6565
subscriptions.forEach(callback => {
6666
callback()
6767
})
68+
}
6869
await next(restMiddlewares)
6970
}
7071

@@ -143,15 +144,15 @@ if (process.env.NODE_ENV === 'production') {
143144
}
144145

145146
const middlewares = {
146-
tryCatch,
147+
communicator,
148+
consoleDebugger,
149+
devToolsListener,
147150
getNewState,
148151
getNewStateWithCache,
149152
setNewState,
150153
stateUpdater,
151-
communicator,
152154
subscription,
153-
devToolsListener,
154-
consoleDebugger
155+
tryCatch
155156
}
156157

157158
const applyMiddlewares = async (
@@ -160,9 +161,9 @@ const applyMiddlewares = async (
160161
) => {
161162
context.next = (restMiddlewares: Middleware[]) =>
162163
restMiddlewares.length > 0 &&
163-
restMiddlewares[0](<Context>context, restMiddlewares.slice(1))
164+
restMiddlewares[0](context as Context, restMiddlewares.slice(1))
164165
if (middlewares.length > 0) {
165-
await middlewares[0](<Context>context, middlewares.slice(1))
166+
await middlewares[0](context as Context, middlewares.slice(1))
166167
}
167168
}
168169

tslint.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"defaultSeverity": "error",
3+
"extends": ["tslint:recommended"],
4+
"jsRules": {},
5+
"rules": {
6+
"arrow-parens": false,
7+
"max-classes-per-file": false,
8+
"member-access": false,
9+
"no-console": false,
10+
"quotemark": false,
11+
"semicolon": false,
12+
"trailing-comma": false
13+
},
14+
"rulesDirectory": ["./src/"]
15+
}

0 commit comments

Comments
 (0)