uniBus.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. import devCache from "../libs/devCache";
  2. import devOptions from "../libs/devOptions";
  3. import jsonCompress from "../libs/jsonCompress";
  4. export default {
  5. logList: [],
  6. busCount: [],
  7. options: null,
  8. /**
  9. * 挂载打印拦截器
  10. */
  11. install() {
  12. try {
  13. let that = this;
  14. this.options = devOptions.getOptions()
  15. if (!this.options.uniBus.status) return;
  16. this.logList = devCache.get("uniBus");
  17. if (!this.logList) this.logList = [];
  18. this.busCount = devCache.get("busCount");
  19. if (!this.busCount) this.busCount = [];
  20. this.syncReqData(); //同步缓存
  21. let now = () => new Date().getTime();
  22. const _on = uni.$on;
  23. uni.$on = function () {
  24. try {
  25. let n = arguments[0];
  26. if (n && typeof n == "string" && n.length < 200 && n.indexOf("devTools_") == -1) {
  27. that.logList.unshift({
  28. t: now(),
  29. e: jsonCompress.compressObject(`on>${n}`, that.options.uniBus.cache.rowMax)
  30. })
  31. addCount(n, "on")
  32. }
  33. } catch (error) {
  34. console.error("uni.$on出错", error);
  35. }
  36. _on(...arguments)
  37. }
  38. const _once = uni.$once;
  39. uni.$once = function () {
  40. try {
  41. let n = arguments[0];
  42. if (n && typeof n == "string" && n.length < 200 && n.indexOf("devTools_") == -1) {
  43. that.logList.unshift({
  44. t: now(),
  45. e: jsonCompress.compressObject(`once>${n}`, that.options.uniBus.cache.rowMax)
  46. })
  47. addCount(n, "once")
  48. }
  49. } catch (error) {
  50. console.error("uni.$once出错", error);
  51. }
  52. _once(...arguments)
  53. }
  54. const _emit = uni.$emit;
  55. uni.$emit = function () {
  56. try {
  57. let n = arguments[0];
  58. let p = arguments[1];
  59. if (n && typeof n == "string" && n.length < 200 && n.indexOf("devTools_") == -1) {
  60. that.logList.unshift({
  61. t: now(),
  62. e: jsonCompress.compressObject(`emit>${n}` + (p ? ('>' + JSON.stringify(p)) : ''), that.options.uniBus.cache.rowMax)
  63. })
  64. addCount(n, "emit")
  65. }
  66. } catch (error) {
  67. console.error("uni.$emit出错", error);
  68. }
  69. _emit(...arguments)
  70. }
  71. const _off = uni.$off;
  72. uni.$off = function () {
  73. try {
  74. let n = arguments[0];
  75. if (n && typeof n == "string" && n.length < 200 && n.indexOf("devTools_") == -1) {
  76. that.logList.unshift({
  77. t: now(),
  78. e: jsonCompress.compressObject(`off>${n}` + arguments[0], that.options.uniBus.cache.rowMax)
  79. })
  80. addCount(n, "off")
  81. }
  82. } catch (error) {
  83. console.error("uni.$off出错", error);
  84. }
  85. _off(...arguments)
  86. }
  87. /**
  88. * 统计总次数
  89. */
  90. function addCount(name, type = "on") {
  91. let i = that.busCount.findIndex(x => x.e == name)
  92. if (i == -1) {
  93. let item = {
  94. e: name,
  95. on: 0,
  96. off: 0,
  97. emit: 0,
  98. once: 0,
  99. };
  100. item[type] = item[type] + 1;
  101. that.busCount.push(item)
  102. } else {
  103. that.busCount[i][type] = that.busCount[i][type] + 1;
  104. }
  105. }
  106. // ! 清空全部记录
  107. uni.$on("devTools_delUniBusAll", () => {
  108. that.logList = []
  109. that.busCount = []
  110. })
  111. } catch (error) {
  112. console.log("devTools uniBus.install error", error);
  113. }
  114. },
  115. /**
  116. * 同步请求信息到缓存数据中
  117. */
  118. syncReqData() {
  119. let that = this;
  120. setTimeout(() => {
  121. try {
  122. that.logList = jsonCompress.compressArray(that.logList, "end", that.options.uniBus.cache.rowMax)
  123. devCache.set("uniBus", that.logList)
  124. that.busCount = jsonCompress.compressArray(that.busCount, "end", that.options.uniBus.cache.countMaxSize)
  125. devCache.set("busCount", that.busCount)
  126. } catch (error) {
  127. console.log("devTools uniBus.syncReqData error", error);
  128. }
  129. that.syncReqData()
  130. }, 5000);
  131. },
  132. }