提交 8f2846b9 编写于 作者: tony001's avatar tony001

视图消息服务优化

上级 c637ba6d
...@@ -264,10 +264,11 @@ export default class ViewMessageService { ...@@ -264,10 +264,11 @@ export default class ViewMessageService {
// 启用缓存 // 启用缓存
if(isEnableCache){ if(isEnableCache){
const callback:Function = (context:any ={},data:any ={},tag:string,promise:Promise<any>) =>{ const callback:Function = (context:any ={},data:any ={},tag:string,promise:Promise<any>) =>{
const callbackKey:string = `${JSON.stringify(context)}-${JSON.stringify(data)}-${tag}`;
promise.then((result:any) =>{ promise.then((result:any) =>{
if(result.length > 0){ if(result.length > 0){
ViewMessageService.messageCached.set(`${JSON.stringify(context)}-${JSON.stringify(data)}-${tag}`,{items:result}); ViewMessageService.messageCached.set(callbackKey,{items:result});
ViewMessageService.messageCache.delete(`${JSON.stringify(context)}-${JSON.stringify(data)}-${tag}`); ViewMessageService.messageCache.delete(callbackKey);
return resolve(result); return resolve(result);
}else{ }else{
return resolve([]); return resolve([]);
...@@ -276,9 +277,10 @@ export default class ViewMessageService { ...@@ -276,9 +277,10 @@ export default class ViewMessageService {
return reject(result); return reject(result);
}) })
} }
const key:string = `${JSON.stringify(context)}-${JSON.stringify(data)}-${tag}`;
// 加载完成,从本地缓存获取 // 加载完成,从本地缓存获取
if(ViewMessageService.messageCached.get(`${JSON.stringify(context)}-${JSON.stringify(data)}-${tag}`)){ if(ViewMessageService.messageCached.get(key)){
let items:any = ViewMessageService.messageCached.get(`${JSON.stringify(context)}-${JSON.stringify(data)}-${tag}`).items; let items:any = ViewMessageService.messageCached.get(key).items;
if(items.length >0){ if(items.length >0){
if(new Date().getTime() <= messageService.getExpirationTime()){ if(new Date().getTime() <= messageService.getExpirationTime()){
return resolve(items); return resolve(items);
...@@ -287,11 +289,11 @@ export default class ViewMessageService { ...@@ -287,11 +289,11 @@ export default class ViewMessageService {
} }
if (messageService) { if (messageService) {
// 加载中,UI又需要数据,解决连续加载同一代码表问题 // 加载中,UI又需要数据,解决连续加载同一代码表问题
if(ViewMessageService.messageCache.get(`${JSON.stringify(context)}-${JSON.stringify(data)}-${tag}`)){ if(ViewMessageService.messageCache.get(key)){
callback(context,data,tag,ViewMessageService.messageCache.get(`${JSON.stringify(context)}-${JSON.stringify(data)}-${tag}`)); callback(context,data,tag,ViewMessageService.messageCache.get(key));
}else{ }else{
let result:Promise<any> = messageService.getItems(context,data,isloading); let result:Promise<any> = messageService.getItems(context,data,isloading);
ViewMessageService.messageCache.set(`${JSON.stringify(context)}-${JSON.stringify(data)}-${tag}`,result); ViewMessageService.messageCache.set(key,result);
messageService.setExpirationTime(new Date().getTime() + cacheTimeout); messageService.setExpirationTime(new Date().getTime() + cacheTimeout);
callback(context,data,tag,result); callback(context,data,tag,result);
} }
......
Markdown 格式
0% or
您添加了 0 到此讨论。请谨慎行事。
先完成此消息的编辑!
想要评论请 注册