gitea源码

eventsource.sharedworker.ts 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. class Source {
  2. url: string;
  3. eventSource: EventSource;
  4. listening: Record<string, boolean>;
  5. clients: Array<MessagePort>;
  6. constructor(url: string) {
  7. this.url = url;
  8. this.eventSource = new EventSource(url);
  9. this.listening = {};
  10. this.clients = [];
  11. this.listen('open');
  12. this.listen('close');
  13. this.listen('logout');
  14. this.listen('notification-count');
  15. this.listen('stopwatches');
  16. this.listen('error');
  17. }
  18. register(port: MessagePort) {
  19. if (this.clients.includes(port)) return;
  20. this.clients.push(port);
  21. port.postMessage({
  22. type: 'status',
  23. message: `registered to ${this.url}`,
  24. });
  25. }
  26. deregister(port: MessagePort) {
  27. const portIdx = this.clients.indexOf(port);
  28. if (portIdx < 0) {
  29. return this.clients.length;
  30. }
  31. this.clients.splice(portIdx, 1);
  32. return this.clients.length;
  33. }
  34. close() {
  35. if (!this.eventSource) return;
  36. this.eventSource.close();
  37. this.eventSource = null;
  38. }
  39. listen(eventType: string) {
  40. if (this.listening[eventType]) return;
  41. this.listening[eventType] = true;
  42. this.eventSource.addEventListener(eventType, (event) => {
  43. this.notifyClients({
  44. type: eventType,
  45. data: event.data,
  46. });
  47. });
  48. }
  49. notifyClients(event: {type: string, data: any}) {
  50. for (const client of this.clients) {
  51. client.postMessage(event);
  52. }
  53. }
  54. status(port: MessagePort) {
  55. port.postMessage({
  56. type: 'status',
  57. message: `url: ${this.url} readyState: ${this.eventSource.readyState}`,
  58. });
  59. }
  60. }
  61. const sourcesByUrl: Map<string, Source | null> = new Map();
  62. const sourcesByPort: Map<MessagePort, Source | null> = new Map();
  63. // @ts-expect-error: typescript bug?
  64. self.addEventListener('connect', (e: MessageEvent) => {
  65. for (const port of e.ports) {
  66. port.addEventListener('message', (event) => {
  67. if (!self.EventSource) {
  68. // some browsers (like PaleMoon, Firefox<53) don't support EventSource in SharedWorkerGlobalScope.
  69. // this event handler needs EventSource when doing "new Source(url)", so just post a message back to the caller,
  70. // in case the caller would like to use a fallback method to do its work.
  71. port.postMessage({type: 'no-event-source'});
  72. return;
  73. }
  74. if (event.data.type === 'start') {
  75. const url = event.data.url;
  76. if (sourcesByUrl.get(url)) {
  77. // we have a Source registered to this url
  78. const source = sourcesByUrl.get(url);
  79. source.register(port);
  80. sourcesByPort.set(port, source);
  81. return;
  82. }
  83. let source = sourcesByPort.get(port);
  84. if (source) {
  85. if (source.eventSource && source.url === url) return;
  86. // How this has happened I don't understand...
  87. // deregister from that source
  88. const count = source.deregister(port);
  89. // Clean-up
  90. if (count === 0) {
  91. source.close();
  92. sourcesByUrl.set(source.url, null);
  93. }
  94. }
  95. // Create a new Source
  96. source = new Source(url);
  97. source.register(port);
  98. sourcesByUrl.set(url, source);
  99. sourcesByPort.set(port, source);
  100. } else if (event.data.type === 'listen') {
  101. const source = sourcesByPort.get(port);
  102. source.listen(event.data.eventType);
  103. } else if (event.data.type === 'close') {
  104. const source = sourcesByPort.get(port);
  105. if (!source) return;
  106. const count = source.deregister(port);
  107. if (count === 0) {
  108. source.close();
  109. sourcesByUrl.set(source.url, null);
  110. sourcesByPort.set(port, null);
  111. }
  112. } else if (event.data.type === 'status') {
  113. const source = sourcesByPort.get(port);
  114. if (!source) {
  115. port.postMessage({
  116. type: 'status',
  117. message: 'not connected',
  118. });
  119. return;
  120. }
  121. source.status(port);
  122. } else {
  123. // just send it back
  124. port.postMessage({
  125. type: 'error',
  126. message: `received but don't know how to handle: ${event.data}`,
  127. });
  128. }
  129. });
  130. port.start();
  131. }
  132. });