Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add unwrapArgs util #1246

Merged
merged 1 commit into from
Oct 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions utils/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1765,3 +1765,5 @@ export declare type FindableByIdTreeNode = FindableByIdTreeNodeV2 | AzExtTreeIte
* NOTE: If the environment variable `DEBUGTELEMETRY` is set to a non-empty, non-zero value, then telemetry will not be sent. If the value is 'verbose' or 'v', telemetry will be displayed in the console window.
*/
export declare function registerCommandWithTreeNodeUnwrapping<T>(commandId: string, callback: TreeNodeCommandCallback<T>, debounce?: number, telemetryId?: string): void;

export declare function unwrapArgs<T>(treeNodeCallback: TreeNodeCommandCallback<T>): TreeNodeCommandCallback<T>;
12 changes: 7 additions & 5 deletions utils/src/registerCommandWithTreeNodeUnwrapping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

import type { CommandCallback, IActionContext, Wrapper } from '../index';
import type { IActionContext, Wrapper } from '../index';
import type { TreeNodeCommandCallback } from '../hostapi.v2';
import { registerCommand } from './registerCommand';

export function registerCommandWithTreeNodeUnwrapping<T>(commandId: string, treeNodeCallback: TreeNodeCommandCallback<T>, debounce?: number, telemetryId?: string): void {
const unwrappingCallback: CommandCallback = async (context: IActionContext, ...args: unknown[]) => {
registerCommand(commandId, unwrapArgs(treeNodeCallback), debounce, telemetryId);
}

export function unwrapArgs<T>(treeNodeCallback: TreeNodeCommandCallback<T>): TreeNodeCommandCallback<T> {
return async (context: IActionContext, ...args: unknown[]) => {
const maybeNodeWrapper = args?.[0];
const maybeNodeWrapperArray = args?.[1];
const remainingArgs = args.slice(2);
Expand All @@ -35,11 +39,9 @@ export function registerCommandWithTreeNodeUnwrapping<T>(commandId: string, tree
nodes = maybeNodeWrapperArray as T[];
}

// eslint-disable-next-line @typescript-eslint/no-unsafe-return, @typescript-eslint/no-unsafe-call
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
return treeNodeCallback(context, node, nodes, ...remainingArgs);
};

registerCommand(commandId, unwrappingCallback, debounce, telemetryId);
}

export function isWrapper(maybeWrapper: unknown): maybeWrapper is Wrapper {
Expand Down