import { Module } from "./module"; /** * NCache模块-存储在内存中 */ export class NCache{ private cacheData:any; /** * 订阅map,格式为 {key:[{module:订阅模块,handler:},...]} */ private subscribeMap = new Map(); constructor(){ this.cacheData = {}; } /** * 通过提供的键名从内存中拿到对应的值 * @param key 键,支持"."(多级数据分割) * @reutrns 值或undefined */ public get(key:string){ let p = this.cacheData; if(key.indexOf('.') !== -1){ let arr = key.split('.'); if(arr.length>1){ for(let i=0;i1){ for(let i=0;i1){ for(let i=0;iitem.module === module && item.handler === handler)){ arr.push({module:module,handler:handler}); } } //如果存在值,则执行订阅回调 let v = this.get(key); if(v){ this.invokeSubscribe(module,handler,v); } } /** * 调用订阅方法 * @param module 模块 * @param foo 方法或方法名 * @param v 值 */ private invokeSubscribe(module:Module,foo:Function|string,v:any){ if(typeof foo === 'string'){ module.invokeMethod(foo,v); }else{ foo.call(module,v); } } }