| // Copyright 2022 The Fuchsia Authors. All rights reserved. |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| |
| // Defines analytics-only VS Code events. |
| // We define these events instead of sending analytics directly at the place of firing these events |
| // because: |
| // - Some analytics depends on other resources (e.g. an ffx instance to get ffxVersion) that might |
| // not be available at the place of event firing place. Those resources could be easier made |
| // available at the place of registering event listeners. |
| // - These events could also be listened by other analytics event consumers such as UX research |
| // tools. |
| |
| import * as vscode from 'vscode'; |
| |
| /** Event for VS code command execution */ |
| export interface CommandExecuteEvent { |
| readonly command: string; |
| } |
| const onWillExecuteCommandEmitter = new vscode.EventEmitter<CommandExecuteEvent>(); |
| /** An Event which fires when a VS Code command will execute */ |
| export const onWillExecuteCommand = onWillExecuteCommandEmitter.event; |
| |
| /** |
| * Register a command such that an onDidExecuteCommand event will be fired when the command is |
| * executed. |
| * |
| * @param command same as in vscode.commands.registerCommand |
| * @param callback same as in vscode.commands.registerCommand |
| * @param thisArg same as in vscode.commands.registerCommand |
| * @returns the disposable returned by vscode.commands.registerCommand |
| */ |
| export function registerCommandWithAnalyticsEvent<T extends any[], S>( |
| command: string, |
| callback: (...args: T) => S, |
| thisArg?: any |
| ): vscode.Disposable { |
| return vscode.commands.registerCommand( |
| command, |
| (...args: T): S => { |
| onWillExecuteCommandEmitter.fire({ command }); |
| return callback(...args); |
| }, |
| thisArg |
| ); |
| } |