event_bus.dart 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. //订阅者回调签名
  2. typedef EventCallback = void Function(dynamic arg);
  3. class EventBus {
  4. // 刷新门户事件
  5. static const refreshPortalMsg = "refreshPortalMsg";
  6. // im消息
  7. static const websocketCreateImMsg = "websocketCreateImMsg";
  8. static const websocketRevokeImMsg = "websocketRevokeImMsg";
  9. static const websocketImConversationUpdateMsg = "websocketImConversationUpdateMsg";
  10. static const websocketImConversationDeleteMsg = "websocketImConversationDeleteMsg";
  11. // 刷新未读消息数量的消息
  12. static const imUnReadNumberMsg = "imUnReadNumberMsg";
  13. /// 选择器数据同步用
  14. static const contactPickerAddPersonMsg = "contactPickerAddPersonMsg";
  15. static const contactPickerAddIdentityMsg = "contactPickerAddIdentityMsg";
  16. static const contactPickerAddUnitMsg = "contactPickerAddUnitMsg";
  17. static const contactPickerAddGroupMsg = "contactPickerAddGroupMsg";
  18. static const contactPickerRemovePersonMsg = "contactPickerRemovePersonMsg";
  19. static const contactPickerRemoveIdentityMsg = "contactPickerRemoveIdentityMsg";
  20. static const contactPickerRemoveUnitMsg = "contactPickerRemoveUnitMsg";
  21. static const contactPickerRemoveGroupMsg = "contactPickerRemoveGroupMsg";
  22. static const contactPickerChangeAllPersonMsg = "contactPickerChangeAllPersonMsg";
  23. static const contactPickerChangeAllIdentityMsg = "contactPickerChangeAllIdentityMsg";
  24. static const contactPickerChangeAllUnitMsg = "contactPickerChangeAllUnitMsg";
  25. static const personPickerRemovePersonMsg = "personPickerRemovePersonMsg";
  26. static const identityPickerRemoveIdentityMsg = "identityPickerRemoveIdentityMsg";
  27. static const unitPickerRemoveUnitMsg = "unitPickerRemoveUnitMsg";
  28. static const groupPickerRemoveGroupMsg = "groupPickerRemoveGroupMsg";
  29. /// 刷新头像
  30. static const avatarUpdateMsg = "avatarUpdateMsg";
  31. /// 签名修改刷新
  32. static const mySignatureUpdateMsg = "mySignatureUpdateMsg";
  33. /// work 工作文档关闭通知
  34. static const processWorkCloseMsg = "processWorkCloseMsg";
  35. /// cms文档关闭通知 刷新列表使用
  36. static const cmsDocumentCloseMsg = "cmsDocumentCloseMsg";
  37. /// portal 门户页面关闭通知
  38. static const portalCloseMsg = "portalCloseMsg";
  39. /// 网盘 上传文件成功刷新通知
  40. static const clouddiskFileUploadedMsg = "clouddiskFileUploadedMsg";
  41. /// 刷新黑白
  42. static const greyColorChangeGlobalMsg = 'greyColorChangeGlobalMsg';
  43. /// 我的应用编辑后通知
  44. static const myAppChangeMsg = 'myAppChangeMsg';
  45. //私有构造函数
  46. EventBus._internal();
  47. //保存单例
  48. static final EventBus _singleton = EventBus._internal();
  49. //工厂构造函数
  50. factory EventBus()=> _singleton;
  51. //保存事件订阅者队列,key:事件名(id),value: 对应事件的订阅者
  52. // final _emap = <String, EventCallback?>{};
  53. final _emap = <String, Map<String, EventCallback?>?>{};
  54. //添加订阅者
  55. void on(eventName, id, EventCallback? f) {
  56. if (eventName == null || id == null || f == null) return;
  57. final subscribers = _emap[eventName];
  58. if (subscribers == null) {
  59. _emap[eventName]= <String, EventCallback?>{id: f};
  60. } else {
  61. subscribers[id] = f;
  62. }
  63. }
  64. //移除订阅者
  65. void off(eventName, id) {
  66. // _emap[eventName] = null;
  67. final subscribers = _emap[eventName];
  68. if (subscribers != null) {
  69. subscribers[id] = null;
  70. }
  71. }
  72. //触发事件,事件触发后该事件所有订阅者会被调用
  73. void emit(eventName, [arg]) {
  74. var subscribers = _emap[eventName];
  75. if (subscribers != null) {
  76. for(var f in subscribers.values) {
  77. if (f != null) {
  78. f(arg);
  79. }
  80. }
  81. }
  82. }
  83. }