@dura/async-loading
Advanced tools
| import { create } from "@dura/core"; | ||
| import { DuraStore } from "@dura/types"; | ||
| import { EffectAPI, AsyncDuraStore, createAsyncPlugin } from "@dura/async"; | ||
| import { ExtractLoadingState, LoadingMeta, createLoadingPlugin } from "../src/index"; | ||
@@ -5,0 +4,0 @@ |
+4
-4
| { | ||
| "name": "@dura/async-loading", | ||
| "version": "2.0.0-alpha.5", | ||
| "version": "2.0.0-alpha.6", | ||
| "description": "", | ||
@@ -15,9 +15,9 @@ "main": "./lib/index.js", | ||
| "devDependencies": { | ||
| "@dura/core": "^2.0.0-alpha.5" | ||
| "@dura/core": "^2.0.0-alpha.6" | ||
| }, | ||
| "dependencies": { | ||
| "@dura/async": "^2.0.0-alpha.5", | ||
| "@dura/types": "^2.0.0-alpha.5" | ||
| "@dura/types": "^2.0.0-alpha.6" | ||
| }, | ||
| "gitHead": "c9be92da1833b5359abd3af83fd442e44af90a75" | ||
| "gitHead": "c77cd4f6128954e9a44d6024e073c00d58d818cb" | ||
| } |
+23
-23
| /** | ||
| * 自动loading | ||
| */ | ||
| import { RootModel, Model, Meta, Payload } from "@dura/types"; | ||
| import { EffectAPI, Effects, AsyncModel } from "@dura/async"; | ||
| import { RootModel, Model, EffectAPI, ExcludeTypeAction, EffectMap } from "@dura/types"; | ||
| import _ from "lodash"; | ||
| function extractLoadingModelInitialStateByEffect<S extends Model<any>>( | ||
| model: S | ||
| ): { [k in keyof S["effects"]]: boolean } { | ||
| return _.keys(model.effects || {}) | ||
| .map((effectName: string) => ({ [effectName]: false })) | ||
| .reduce(_.merge, {}); | ||
| } | ||
| export const createLoadingPlugin = function(rootModel: RootModel) { | ||
| const extractEffect = (model: Model & AsyncModel) => | ||
| Object.keys(model.effects || {}) | ||
| .map((effectName: string) => ({ [effectName]: false })) | ||
| .reduce((prev, next) => ({ ...prev, ...next }), {}); | ||
| const state = Object.keys(rootModel) | ||
| const initialState = _.keys(rootModel) | ||
| .map((modelName: string) => ({ | ||
| [modelName]: extractEffect(rootModel[modelName]) | ||
| [modelName]: extractLoadingModelInitialStateByEffect(rootModel[modelName]) | ||
| })) | ||
| .reduce((prev, next) => ({ ...prev, ...next }), {}); | ||
| .reduce(_.merge, {}); | ||
@@ -21,3 +26,3 @@ return { | ||
| model: { | ||
| state, | ||
| state: initialState, | ||
| reducers: { | ||
@@ -42,3 +47,3 @@ start(state, action: { payload: { modelName: string; effectName: string } }) { | ||
| }, | ||
| onWrapModel: (name: string, model: Model & AsyncModel) => { | ||
| onWrapModel: (name: string, model: Model<any>) => { | ||
| if (name === "loading") { | ||
@@ -56,15 +61,10 @@ return model; | ||
| .map((key: string) => ({ | ||
| [key]: async (action, request: EffectAPI) => { | ||
| const effectFn = async () => await effects[key](action, request); | ||
| const loadingHoc = async effectFn => { | ||
| [key]: async (request: EffectAPI, action: ExcludeTypeAction) => { | ||
| //兼容 | ||
| if (action && action.meta && action.meta.loading) { | ||
| request.dispatch(start(key)); | ||
| await effectFn(); | ||
| await effects[key](request, action); | ||
| request.dispatch(end(key)); | ||
| }; | ||
| //兼容 | ||
| if (action && action.meta && action.meta.loading) { | ||
| loadingHoc(effectFn); | ||
| } else { | ||
| await effectFn(); | ||
| await effects[key](request, action); | ||
| } | ||
@@ -83,5 +83,5 @@ } | ||
| type ConvertFnToBoolean<E extends Effects> = { [key in keyof E]: boolean }; | ||
| type ConvertFnToBoolean<E extends EffectMap> = { [key in keyof E]: boolean }; | ||
| export type ExtractLoadingState<RMT extends RootModel<Model & AsyncModel>> = { | ||
| export type ExtractLoadingState<RMT extends RootModel> = { | ||
| loading: { [key in keyof RMT]: ConvertFnToBoolean<RMT[key]["effects"]> }; | ||
@@ -88,0 +88,0 @@ }; |
74314
-0.3%820
-0.36%Updated