(model, existModels)
| 2 | import { isArray, isFunction, isPlainObject } from './utils'; |
| 3 | |
| 4 | export default function checkModel(model, existModels) { |
| 5 | const { |
| 6 | namespace, |
| 7 | reducers, |
| 8 | effects, |
| 9 | subscriptions, |
| 10 | } = model; |
| 11 | |
| 12 | // namespace 必须被定义 |
| 13 | invariant( |
| 14 | namespace, |
| 15 | `[app.model] namespace should be defined`, |
| 16 | ); |
| 17 | // 并且是字符串 |
| 18 | invariant( |
| 19 | typeof namespace === 'string', |
| 20 | `[app.model] namespace should be string, but got ${typeof namespace}`, |
| 21 | ); |
| 22 | // 并且唯一 |
| 23 | invariant( |
| 24 | !existModels.some(model => model.namespace === namespace), |
| 25 | `[app.model] namespace should be unique`, |
| 26 | ); |
| 27 | |
| 28 | // state 可以为任意值 |
| 29 | |
| 30 | // reducers 可以为空,PlainObject 或者数组 |
| 31 | if (reducers) { |
| 32 | invariant( |
| 33 | isPlainObject(reducers) || isArray(reducers), |
| 34 | `[app.model] reducers should be plain object or array, but got ${typeof reducers}`, |
| 35 | ); |
| 36 | // 数组的 reducers 必须是 [Object, Function] 的格式 |
| 37 | invariant( |
| 38 | !isArray(reducers) || (isPlainObject(reducers[0]) && isFunction(reducers[1])), |
| 39 | `[app.model] reducers with array should be [Object, Function]`, |
| 40 | ); |
| 41 | } |
| 42 | |
| 43 | // effects 可以为空,PlainObject |
| 44 | if (effects) { |
| 45 | invariant( |
| 46 | isPlainObject(effects), |
| 47 | `[app.model] effects should be plain object, but got ${typeof effects}`, |
| 48 | ); |
| 49 | } |
| 50 | |
| 51 | if (subscriptions) { |
| 52 | // subscriptions 可以为空,PlainObject |
| 53 | invariant( |
| 54 | isPlainObject(subscriptions), |
| 55 | `[app.model] subscriptions should be plain object, but got ${typeof subscriptions}`, |
| 56 | ); |
| 57 | |
| 58 | // subscription 必须为函数 |
| 59 | invariant( |
| 60 | isAllFunction(subscriptions), |
| 61 | `[app.model] subscription should be function`, |
no test coverage detected
searching dependent graphs…