You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

74 rivejä
1.7 KiB

  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.EventBus = exports.throttle = exports.formatSeconds = void 0;
  6. function formatSeconds(seconds) {
  7. var result = typeof seconds === "string" ? parseFloat(seconds) : seconds;
  8. if (isNaN(result)) return "";
  9. let h = Math.floor(result / 3600) < 10 ? "0" + Math.floor(result / 3600) : Math.floor(result / 3600);
  10. let m = Math.floor((result / 60) % 60) < 10 ? "0" + Math.floor((result / 60) % 60) : Math.floor((result / 60) % 60);
  11. let s = Math.floor(result % 60) < 10 ? "0" + Math.floor(result % 60) : Math.floor(result % 60);
  12. return `${h}:${m}:${s}`;
  13. //time为毫秒数
  14. }
  15. exports.formatSeconds = formatSeconds;
  16. function throttle(fn, wait) {
  17. let previous = 0;
  18. return function(...arg) {
  19. let context = this;
  20. let now = Date.now();
  21. //每隔一段时间执行一次;
  22. if (now - previous > wait) {
  23. fn.apply(context, arg);
  24. previous = now;
  25. }
  26. };
  27. }
  28. exports.throttle = throttle;
  29. class EventBus {
  30. constructor() {
  31. this._events = new Map();
  32. }
  33. on(event, action, fn) {
  34. let arr = this._events.get(event);
  35. let hasAction = arr ?
  36. arr.findIndex((i) => i.action == action) :
  37. -1;
  38. if (hasAction > -1) {
  39. return;
  40. }
  41. this._events.set(event, [
  42. ...(this._events.get(event) || []),
  43. {
  44. action,
  45. fn,
  46. },
  47. ]);
  48. }
  49. has(event) {
  50. return this._events.has(event);
  51. }
  52. emit(event, data) {
  53. if (!this.has(event)) {
  54. return;
  55. }
  56. let arr = this._events.get(event);
  57. arr.forEach((i) => {
  58. i.fn(data);
  59. });
  60. }
  61. off(event, action) {
  62. if (!this.has(event)) {
  63. return;
  64. }
  65. let arr = this._events.get(event);
  66. let newdata = arr.filter((i) => i.action !== action);
  67. this._events.set(event, [...newdata]);
  68. }
  69. }
  70. exports.EventBus = EventBus;