eventSource: Add autofire on emit for APP_READY
| @@ -24,10 +24,22 @@ if (typeof Array.prototype.indexOf === 'function') { | ||
| 24 | 24 | |
| 25 | 25 | |
| 26 | 26 | /* Polyfill EventEmitter. */ |
| 27 | -var EventEmitter = function () { | |
| 27 | +/** | |
| 28 | + * Creates an event emitter. | |
| 29 | + * @param {string[]} autoFireAfterEmit Auto-fire event names | |
| 30 | + */ | |
| 31 | +var EventEmitter = function (autoFireAfterEmit = []) { | |
| 28 | 32 | this.events = {}; |
| 33 | + this.autoFireLastArgs = new Map(); | |
| 34 | + this.autoFireAfterEmit = new Set(autoFireAfterEmit); | |
| 29 | 35 | }; |
| 30 | 36 | |
| 37 | +/** | |
| 38 | + * Adds a listener to an event. | |
| 39 | + * @param {string} event Event name | |
| 40 | + * @param {function} listener Event listener | |
| 41 | + * @returns | |
| 42 | + */ | |
| 31 | 43 | EventEmitter.prototype.on = function (event, listener) { |
| 32 | 44 | // Unknown event used by external libraries? |
| 33 | 45 | if (event === undefined) { |
| @@ -40,6 +52,10 @@ EventEmitter.prototype.on = function (event, listener) { | ||
| 40 | 52 | } |
| 41 | 53 | |
| 42 | 54 | this.events[event].push(listener); |
| 55 | + | |
| 56 | + if (this.autoFireAfterEmit.has(event) && this.autoFireLastArgs.has(event)) { | |
| 57 | + listener.apply(this, this.autoFireLastArgs.get(event)); | |
| 58 | + } | |
| 43 | 59 | }; |
| 44 | 60 | |
| 45 | 61 | /** |
| @@ -60,6 +76,10 @@ EventEmitter.prototype.makeLast = function (event, listener) { | ||
| 60 | 76 | } |
| 61 | 77 | |
| 62 | 78 | events.push(listener); |
| 79 | + | |
| 80 | + if (this.autoFireAfterEmit.has(event) && this.autoFireLastArgs.has(event)) { | |
| 81 | + listener.apply(this, this.autoFireLastArgs.get(event)); | |
| 82 | + } | |
| 63 | 83 | } |
| 64 | 84 | |
| 65 | 85 | /** |
| @@ -80,8 +100,17 @@ EventEmitter.prototype.makeFirst = function (event, listener) { | ||
| 80 | 100 | } |
| 81 | 101 | |
| 82 | 102 | events.unshift(listener); |
| 103 | + | |
| 104 | + if (this.autoFireAfterEmit.has(event) && this.autoFireLastArgs.has(event)) { | |
| 105 | + listener.apply(this, this.autoFireLastArgs.get(event)); | |
| 106 | + } | |
| 83 | 107 | } |
| 84 | 108 | |
| 109 | +/** | |
| 110 | + * Removes a listener from an event. | |
| 111 | + * @param {string} event Event name | |
| 112 | + * @param {function} listener Event listener | |
| 113 | + */ | |
| 85 | 114 | EventEmitter.prototype.removeListener = function (event, listener) { |
| 86 | 115 | var idx; |
| 87 | 116 | |
| @@ -94,6 +123,10 @@ EventEmitter.prototype.removeListener = function (event, listener) { | ||
| 94 | 123 | } |
| 95 | 124 | }; |
| 96 | 125 | |
| 126 | +/** | |
| 127 | + * Emits an event with optional arguments. | |
| 128 | + * @param {string} event Event name | |
| 129 | + */ | |
| 97 | 130 | EventEmitter.prototype.emit = async function (event) { |
| 98 | 131 | let args = [].slice.call(arguments, 1); |
| 99 | 132 | if (localStorage.getItem('eventTracing') === 'true') { |
| @@ -118,6 +151,10 @@ EventEmitter.prototype.emit = async function (event) { | ||
| 118 | 151 | } |
| 119 | 152 | } |
| 120 | 153 | } |
| 154 | + | |
| 155 | + if (this.autoFireAfterEmit.has(event)) { | |
| 156 | + this.autoFireLastArgs.set(event, args); | |
| 157 | + } | |
| 121 | 158 | }; |
| 122 | 159 | |
| 123 | 160 | EventEmitter.prototype.emitAndWait = function (event) { |
| @@ -144,10 +181,14 @@ EventEmitter.prototype.emitAndWait = function (event) { | ||
| 144 | 181 | } |
| 145 | 182 | } |
| 146 | 183 | } |
| 184 | + | |
| 185 | + if (this.autoFireAfterEmit.has(event)) { | |
| 186 | + this.autoFireLastArgs.set(event, args); | |
| 187 | + } | |
| 147 | 188 | }; |
| 148 | 189 | |
| 149 | 190 | EventEmitter.prototype.once = function (event, listener) { |
| 150 | 191 | this.on(event, function g () { |
| 151 | 192 | this.removeListener(event, g); |
| 152 | 193 | listener.apply(this, arguments); |
| 153 | 194 | }); |
| @@ -512,7 +512,7 @@ export const event_types = { | ||
| 512 | 512 | TOOL_CALLS_RENDERED: 'tool_calls_rendered', |
| 513 | 513 | }; |
| 514 | 514 | |
| 515 | 515 | export const eventSource = new EventEmitter([event_types.APP_READY]); |
| 516 | 516 | |
| 517 | 517 | eventSource.on(event_types.CHAT_CHANGED, processChatSlashCommands); |
| 518 | 518 | |