NotificationManager.ts 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /**
  2. * 消息管理类
  3. */
  4. import { ViewManager } from "./ViewManager";
  5. import BaseMediator from "../base/BaseMediator";
  6. export default class NotificationManager {
  7. // 实例
  8. private static _instance: NotificationManager = new NotificationManager();
  9. /**
  10. * @constructor
  11. * @private
  12. */
  13. private constructor() {
  14. }
  15. /**
  16. * 单例获取类
  17. */
  18. public static getInstance(): NotificationManager {
  19. return this._instance;
  20. }
  21. /**
  22. * 发送消息通知, 框架使用,外部不得调用。
  23. * @param {string} noti 通知key值
  24. * @param {Object} body 消息传递的参数
  25. * @private
  26. */
  27. public __sendNotification__(noti: string, body?: any): void {
  28. // pop view
  29. let popViewList: BaseMediator[] = ViewManager.getInstance().popViewList;
  30. this.loopMap(popViewList, noti, body);
  31. // add layer view
  32. let layerViewList: BaseMediator[] = ViewManager.getInstance().layerViewList;
  33. this.loopMap(layerViewList, noti, body);
  34. // scene
  35. let curScene: BaseMediator = ViewManager.getInstance().curScene;
  36. if (curScene)
  37. this.loopMap([curScene], noti, body);
  38. }
  39. /**
  40. * 循环遍历map检索通知对象
  41. * @param {BaseMediator[]} list view mediator list
  42. * @param {string} noti 消息名称
  43. * @param {Object} body 消息传递的参数
  44. */
  45. private loopMap(list: BaseMediator[], noti: string, body?: any): void {
  46. for (let med of list) {
  47. let notiMap: Map<string, { key: string, cb: (data: any) => void, target: any }> = med["_notiMap"];
  48. notiMap.forEach((value: { key: string, cb: (data: any) => void, target: any }, key: string) => {
  49. if (key === noti) {
  50. value.cb.call(value.target, body);
  51. }
  52. }, this);
  53. }
  54. }
  55. }