优化 eps 不刷新本地 service 问题

This commit is contained in:
神仙都没用 2023-10-26 17:15:08 +08:00
parent 7bfd30373b
commit 2ccd1febdd
5 changed files with 59 additions and 3 deletions

View File

@ -33,6 +33,12 @@ export function virtual(): Plugin {
// 代码保存时触发 eps 刷新
if (!file.includes("build/cool/dist")) {
buildEps(server);
// 通知客户端刷新
server.ws.send({
type: "custom",
event: "eps-update"
});
}
},
resolveId(id) {

View File

@ -43,7 +43,7 @@ export function createEps(modules: Module[]) {
list.push({
api,
module: s.namespace.split("/")[0],
name: s.constructor.name,
name: s.constructor.name + "Entity",
prefix: `/admin/${s.namespace}`
});
} else {

View File

@ -3,11 +3,13 @@ import { hmr } from "../hook";
import { eps } from "virtual:eps";
import { merge } from "lodash-es";
// service 数据集合
export const service: Eps.Service = hmr.getData("service", {
request: new BaseService().request
});
function main() {
// 同步 service 数据
function update() {
function deep(d: any) {
if (d.namespace) {
const a = new BaseService(d.namespace);
@ -46,8 +48,17 @@ function main() {
// 缓存
hmr.setData("service", service);
// tips
console.log("[eps] update");
}
main();
update();
if (import.meta.hot) {
import.meta.hot.on("eps-update", () => {
update();
});
}
export * from "./base";

View File

@ -0,0 +1,6 @@
import { BaseService, Service } from "/@/cool";
@Service("demo/user/follow")
class DemoUserFollow extends BaseService {}
export default DemoUserFollow;

View File

@ -0,0 +1,33 @@
import axios from "axios";
import { BaseService, Service } from "/@/cool";
import dayjs from "dayjs";
@Service("demo/user/info")
class DemoUserInfo extends BaseService {
// 测试方法,使用 request 请求数据
t1() {
return this.request({
url: "/t1" // 测试地址,实际项目中请更换为真实接口地址
});
}
// 自定义请求,通过 axios 返回数据
t2() {
return axios({
url: "https://"
});
}
// 自定义请求,通过 Promise 返回数据
t3() {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve({
date: dayjs().format("YYYY-MM-DD HH:mm:ss")
});
}, 1500);
});
}
}
export default DemoUserInfo;