MCPcopy Create free account
hub / github.com/microsoft/vscode-cpptools / newEvent

Function newEvent

Extension/src/Utility/Eventing/emitter.ts:142–195  ·  view source on GitHub ↗
(eventName: string, options?: EventOptions<TOutput> & WithDefault<TOutput>)

Source from the content-addressed store, hash-verified

140 protected newEvent<TData, TOutput>(eventName: string, options?: EventOptions & WithDefault<TOutput>): (input: TData) => Promise<TOutput>;
141 protected newEvent<TText extends string, TData, TOutput>(eventName: string, options?: EventOptions<TOutput> & WithDefault<TOutput>): (text: TText, data: TData) => Promise<TOutput>;
142 protected newEvent<TText extends string, TData, TOutput>(eventName: string, options?: EventOptions<TOutput> & WithDefault<TOutput>): (text: TText, data: TData) => Promise<EventStatus | TOutput> {
143 eventName = smash(eventName);
144 this.#knownEvents.add(eventName);
145 const descriptors = options?.descriptors ? new Descriptors(this.descriptors, options.descriptors) : this.descriptors;
146 // is it an immediate event?
147 if (options?.now) {
148 return async (input?: TText, data?: TData): Promise<TOutput | EventStatus> => {
149 switch (options?.once) {
150 case false:
151 // already triggered this one time event.
152 return;
153
154 case true:
155 options.once = false;
156 }
157 // trigger the event
158 const result = await ((data !== undefined) ?
159 emitNow<TOutput>(eventName, descriptors, this, input || '', data as any) : // text and data
160 emitNow<TOutput>(eventName, descriptors, this, input || '', input as any)); // text or data (or neither)
161
162 return is.cancelled(result) ? // was the event cancelled?
163 options?.cancel?.() || Cancelled : // the event was cancelled - call the cancel function, or return Cancelled
164 is.continue(result) ? // was the event continued (handler returned nothing)?
165 is.function(options?.default) ? // the event is continued, is the default handler a function?
166 options?.default() : // the default is a function, call it.
167 options?.default || Continue : // the default is a value, call it (or just return Continue)
168 result as TOutput; // the event was not cancelled, and the handler returned a value.
169 };
170 }
171
172 // otherwise queue it
173 return async (input?: TText, data?: TData): Promise<TOutput | EventStatus> => {
174 switch (options?.once) {
175 case false:
176 // already triggered this one time event.
177 return;
178
179 case true:
180 options.once = false;
181 }
182 // trigger the event
183 const result = await ((data !== undefined) ?
184 emit<TOutput>(eventName, descriptors, this, input || '', data as any) : // text and data
185 emit<TOutput>(eventName, descriptors, this, input || '', input as any)); // text or data (or neither)
186
187 return is.cancelled(result) ? // was the event cancelled?
188 options?.cancel?.() || Cancelled : // the event was cancelled - call the cancel function, or return Cancelled
189 is.continue(result) ? // was the event continued (handler returned nothing)?
190 is.function(options?.default) ? // the event is continued, is the default handler a function?
191 options?.default() : // the default is a function, call it.
192 options?.default || Continue : // the default is a value, call it (or just return Continue)
193 result as TOutput; // the event was not cancelled, and the handler returned a value.
194 };
195 }
196
197 /** notification with event name, but no data in the event. */
198 protected newNotification(eventName: string, options?: EventOptions): () => void;

Callers

nothing calls this directly

Calls 9

smashFunction · 0.90
emitNowFunction · 0.90
emitFunction · 0.90
cancelledMethod · 0.80
continueMethod · 0.80
functionMethod · 0.80
defaultMethod · 0.80
cancelMethod · 0.65
addMethod · 0.45

Tested by

no test coverage detected