diff --git a/.fatherrc.js b/.fatherrc.js
index 01b62ebd..9cb72122 100644
--- a/.fatherrc.js
+++ b/.fatherrc.js
@@ -4,7 +4,7 @@ import { join } from 'path';
// utils must build before core
// runtime must build before renderer-react
-const headPkgs = ['fes-runtime', 'fes-core', 'fes', 'fes-plugin-built-in', 'fes-plugin-request', 'fes-plugin-access', 'fes-plugin-state'];
+const headPkgs = ['fes-runtime', 'fes-core', 'fes', 'fes-plugin-built-in', 'fes-plugin-request', 'fes-plugin-access', 'fes-plugin-model'];
const tailPkgs = [];
// const otherPkgs = readdirSync(join(__dirname, 'packages')).filter(
// (pkg) =>
diff --git a/packages/fes-plugin-state/.fatherrc.js b/packages/fes-plugin-model/.fatherrc.js
similarity index 100%
rename from packages/fes-plugin-state/.fatherrc.js
rename to packages/fes-plugin-model/.fatherrc.js
diff --git a/packages/fes-plugin-state/package.json b/packages/fes-plugin-model/package.json
similarity index 90%
rename from packages/fes-plugin-state/package.json
rename to packages/fes-plugin-model/package.json
index 5f7d00e1..7d422cae 100644
--- a/packages/fes-plugin-state/package.json
+++ b/packages/fes-plugin-model/package.json
@@ -1,5 +1,5 @@
{
- "name": "@webank/fes-plugin-state",
+ "name": "@webank/fes-plugin-model",
"version": "1.0.0",
"description": "",
"main": "lib/index.js",
diff --git a/packages/fes-plugin-state/src/index.js b/packages/fes-plugin-model/src/index.js
similarity index 93%
rename from packages/fes-plugin-state/src/index.js
rename to packages/fes-plugin-model/src/index.js
index c731913a..0c6c03fe 100644
--- a/packages/fes-plugin-state/src/index.js
+++ b/packages/fes-plugin-model/src/index.js
@@ -5,7 +5,7 @@ import { lodash, winPath } from '@umijs/utils';
import { getModels } from './utils/getModels';
import { getTmpFile } from './utils/getTmpFile';
-const namespace = 'plugin-state';
+const namespace = 'plugin-model';
export default (api) => {
const {
@@ -55,7 +55,6 @@ export default (api) => {
});
const tmpFiles = getTmpFile(files, additionalModels, paths.absSrcPath);
- console.log(tmpFiles);
api.writeTmpFile({
path: absCoreFilePath,
@@ -77,6 +76,13 @@ export default (api) => {
});
});
+ api.addPluginExports(() => [
+ {
+ specifiers: ['useModel'],
+ source: absCoreFilePath
+ }
+ ]);
+
// 注册 getInitialState 方法
api.addRuntimePluginKey(() => 'getInitialState');
diff --git a/packages/fes-plugin-model/src/models/initialState.tpl b/packages/fes-plugin-model/src/models/initialState.tpl
new file mode 100644
index 00000000..53be94f6
--- /dev/null
+++ b/packages/fes-plugin-model/src/models/initialState.tpl
@@ -0,0 +1,34 @@
+import { reactive, toRefs, onMounted } from "vue";
+import { plugin, ApplyPluginsType } from "@@/core/coreExports";
+
+async function getInitialState() {
+ const appGetInitialState = plugin.applyPlugins({
+ key: "getInitialState",
+ type: ApplyPluginsType.modify,
+ initialValue: {},
+ });
+ return await appGetInitialState;
+}
+const initState = reactive({
+ initialState: undefined,
+ loading: true,
+ error: undefined,
+});
+
+export default function initalModel() {
+ const refresh = async () => {
+ initState.loading = true;
+ initState.error = undefined;
+ try {
+ const res = await getInitialState();
+ initState.initialState = res;
+ initState.loading = false;
+ } catch (e) {
+ console.error(e)
+ initState.loading = false;
+ initState.error = e;
+ }
+ };
+ onMounted(refresh);
+ return toRefs(initState);
+}
diff --git a/packages/fes-plugin-model/src/template/core.tpl b/packages/fes-plugin-model/src/template/core.tpl
new file mode 100644
index 00000000..2cae17a6
--- /dev/null
+++ b/packages/fes-plugin-model/src/template/core.tpl
@@ -0,0 +1,25 @@
+{{{userImports}}}
+{{{extraImports}}}
+
+export const models = {
+{{#extraModels}}
+ {{{extraModels}}},
+{{/extraModels}}
+{{#userModels}}
+ {{{userModels}}},
+{{/userModels}}
+}
+const cache = new Map();
+export const useModel = (name) => {
+ const model = models[name];
+ if(model === undefined){
+ throw new Error("[plugin-model]: useModel, name is undefined.");
+ }
+ if (typeof model !== "function") {
+ throw new Error("[plugin-model]: useModel is not a function.");
+ }
+ if(!cache.has(name)){
+ cache.set(name, model())
+ }
+ return cache.get(name)
+};
\ No newline at end of file
diff --git a/packages/fes-plugin-model/src/template/runtime.tpl b/packages/fes-plugin-model/src/template/runtime.tpl
new file mode 100644
index 00000000..242e1ea0
--- /dev/null
+++ b/packages/fes-plugin-model/src/template/runtime.tpl
@@ -0,0 +1,26 @@
+import { plugin, ApplyPluginsType } from "@@/core/coreExports";
+import { useModel } from "./core.js";
+
+export function rootContainer(childComponent, args) {
+ const useRuntimeConfig =
+ plugin.applyPlugins({
+ key: "initialStateConfig",
+ type: ApplyPluginsType.modify,
+ initialValue: {},
+ }) || {};
+ return {
+ setup() {
+ const { loading } = useModel("@@initialState") || {};
+ return () => {
+ if (loading.value) {
+ return useRuntimeConfig.loading ? (
+