1 | import { Config, Model, Models, NamedModel, RematchBag } from './types'
|
2 | import { validateModel } from './validate'
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 | export default function createRematchBag<
|
9 | TModels extends Models<TModels>,
|
10 | TExtraModels extends Models<TModels>
|
11 | >(config: Config<TModels, TExtraModels>): RematchBag<TModels, TExtraModels> {
|
12 | return {
|
13 | models: createNamedModels(config.models),
|
14 | reduxConfig: config.redux,
|
15 | forEachPlugin(method, fn): void {
|
16 | config.plugins.forEach((plugin) => {
|
17 | if (plugin[method]) {
|
18 | fn(plugin[method]!)
|
19 | }
|
20 | })
|
21 | },
|
22 | effects: {},
|
23 | }
|
24 | }
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 | function createNamedModels<TModels extends Models<TModels>>(
|
32 | models: TModels | Partial<TModels>
|
33 | ): NamedModel<TModels>[] {
|
34 | return Object.keys(models).map((modelName: string) => {
|
35 | const model = createNamedModel(modelName, (models as TModels)[modelName])
|
36 | validateModel(model)
|
37 | return model
|
38 | })
|
39 | }
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 | function createNamedModel<TModels extends Models<TModels>>(
|
46 | name: string,
|
47 | model: Model<TModels>
|
48 | ): NamedModel<TModels> {
|
49 | return {
|
50 | name,
|
51 | reducers: {},
|
52 | ...model,
|
53 | }
|
54 | }
|