eventBus.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /**
  2. * author: Di (微信小程序开发工程师)
  3. * organization: WeAppDev(微信小程序开发论坛)(http://weappdev.com)
  4. * 垂直微信小程序开发交流社区
  5. *
  6. * github地址: https://github.com/icindy/WxNotificationCenter
  7. *
  8. * for: 微信小程序通知广播模式类,降低小程序之间的耦合度
  9. * detail : http://weappdev.com/t/wxnotificationcenter/233
  10. */
  11. // 存放
  12. var __notices = [];
  13. var isDebug = true;
  14. /**
  15. * addNotification
  16. * 注册通知对象方法
  17. *
  18. * 参数:
  19. * name: 注册名,一般let在公共类中
  20. * selector: 对应的通知方法,接受到通知后进行的动作
  21. * observer: 注册对象,指Page对象
  22. */
  23. function addNotification(name, selector, observer) {
  24. if (name && selector) {
  25. if (!observer) {
  26. // console.log("addNotification Warning: no observer will can't remove notice");
  27. }
  28. // console.log("addNotification:" + name);
  29. var newNotice = {
  30. name: name,
  31. selector: selector,
  32. observer: observer
  33. };
  34. addNotices(newNotice);
  35. } else {
  36. // console.log("addNotification error: no selector or name");
  37. }
  38. }
  39. /**
  40. * 仅添加一次监听
  41. *
  42. * 参数:
  43. * name: 注册名,一般let在公共类中
  44. * selector: 对应的通知方法,接受到通知后进行的动作
  45. * observer: 注册对象,指Page对象
  46. */
  47. function addOnceNotification(name, selector, observer) {
  48. if (__notices.length > 0) {
  49. for (var i = 0; i < __notices.length; i++) {
  50. var notice = __notices[i];
  51. if (notice.name === name) {
  52. if (notice.observer === observer) {
  53. return;
  54. }
  55. }
  56. }
  57. }
  58. this.addNotification(name, selector, observer)
  59. }
  60. function addNotices(newNotice) {
  61. // if (__notices.length > 0) {
  62. // for (var i = 0; i < __notices.length; i++) {
  63. // var hisNotice = __notices[i];
  64. // //当名称一样时进行对比,如果不是同一个 则放入数组,否则跳出
  65. // if (newNotice.name === hisNotice.name) {
  66. // if (!cmp(hisNotice, newNotice)) {
  67. // __notices.push(newNotice);
  68. // }
  69. // return;
  70. // }else{
  71. // __notices.push(newNotice);
  72. // }
  73. // }
  74. // } else {
  75. // }
  76. __notices.push(newNotice);
  77. }
  78. /**
  79. * removeNotification
  80. * 移除通知方法
  81. *
  82. * 参数:
  83. * name: 已经注册了的通知
  84. * observer: 移除的通知所在的Page对象
  85. */
  86. function removeNotification(name, observer) {
  87. // console.log("removeNotification:" + name);
  88. for (var i = 0; i < __notices.length; i++) {
  89. var notice = __notices[i];
  90. if (notice.name === name) {
  91. if (notice.observer === observer) {
  92. __notices.splice(i, 1);
  93. return;
  94. }
  95. }
  96. }
  97. }
  98. /**
  99. * fire
  100. * 发送通知方法
  101. *
  102. * 参数:
  103. * name: 已经注册了的通知
  104. * info: 携带的参数
  105. */
  106. function fire(cmdEvent) {
  107. let name = "cmdEventNotification";
  108. let info = cmdEvent;
  109. // console.log("fire:" + name);
  110. if (__notices.length == 0) {
  111. console.log("fire error: u hadn't add any notice.");
  112. return;
  113. }
  114. for (var i = 0; i < __notices.length; i++) {
  115. var notice = __notices[i];
  116. if (notice.name === name) {
  117. notice.selector(info);
  118. }
  119. }
  120. }
  121. function fireName(name, cmdEvent) {
  122. let info = cmdEvent;
  123. // console.log("fire:" + name);
  124. if (__notices.length == 0) {
  125. console.log("fire error: u hadn't add any notice.");
  126. return;
  127. }
  128. for (var i = 0; i < __notices.length; i++) {
  129. var notice = __notices[i];
  130. if (notice.name === name) {
  131. notice.selector(info);
  132. }
  133. }
  134. }
  135. // 用于对比两个对象是否相等
  136. function cmp(x, y) {
  137. // If both x and y are null or undefined and exactly the same
  138. if (x === y) {
  139. return true;
  140. }
  141. // If they are not strictly equal, they both need to be Objects
  142. if (!(x instanceof Object) || !(y instanceof Object)) {
  143. return false;
  144. }
  145. // They must have the exact same prototype chain, the closest we can do is
  146. // test the constructor.
  147. if (x.constructor !== y.constructor) {
  148. return false;
  149. }
  150. for (var p in x) {
  151. // Inherited properties were tested using x.constructor === y.constructor
  152. if (x.hasOwnProperty(p)) {
  153. // Allows comparing x[ p ] and y[ p ] when set to undefined
  154. if (!y.hasOwnProperty(p)) {
  155. return false;
  156. }
  157. // If they have the same strict value or identity then they are equal
  158. if (x[p] === y[p]) {
  159. continue;
  160. }
  161. // Numbers, Strings, Functions, Booleans must be strictly equal
  162. if (typeof (x[p]) !== "object") {
  163. return false;
  164. }
  165. // Objects and Arrays must be tested recursively
  166. if (!Object.equals(x[p], y[p])) {
  167. return false;
  168. }
  169. }
  170. }
  171. for (p in y) {
  172. // allows x[ p ] to be set to undefined
  173. if (y.hasOwnProperty(p) && !x.hasOwnProperty(p)) {
  174. return false;
  175. }
  176. }
  177. return true;
  178. };
  179. module.exports = {
  180. addNotification: addNotification,
  181. removeNotification: removeNotification,
  182. fire: fire,
  183. fireName: fireName,
  184. addOnceNotification: addOnceNotification
  185. }