Files
Nikolay Vitkov 12187c44f6 [cleanup] Types around ViewFunctionHelper
Bug: none
Change-Id: I55cd01185cd811cf57b14da941a7d8a3f5546b87
Reviewed-on: https://chromium-review.googlesource.com/c/devtools/devtools-frontend/+/6351856
Commit-Queue: Nikolay Vitkov <nvitkov@chromium.org>
Auto-Submit: Nikolay Vitkov <nvitkov@chromium.org>
Commit-Queue: Alex Rudenko <alexrudenko@chromium.org>
Reviewed-by: Alex Rudenko <alexrudenko@chromium.org>
2025-03-13 06:13:07 -07:00

63 lines
2.6 KiB
TypeScript

// Copyright 2025 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import type * as Platform from '../core/platform/platform.js';
import type * as UI from '../ui/legacy/legacy.js';
type WidgetConstructor = Platform.Constructor.AbstractConstructor<UI.Widget.Widget|HTMLElement>;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
type ViewFunctionLike = (input: any, output: any, target: HTMLElement) => void;
type FindViewFunction<ParametersT extends readonly unknown[]> = ParametersT extends [infer Head, ...infer Tail] ?
Head extends ViewFunctionLike ? Head : FindViewFunction<Tail>:
never;
type ViewFunction<WidgetConstructorT extends WidgetConstructor> =
FindViewFunction<Required<ConstructorParameters<WidgetConstructorT>>>;
type ViewFunctionParameters<WidgetConstructorT extends WidgetConstructor> =
Parameters<ViewFunction<WidgetConstructorT>>;
type ViewInput<WidgetConstructorT extends WidgetConstructor> = ViewFunctionParameters<WidgetConstructorT>[0];
type ViewOutput<WidgetConstructorT extends WidgetConstructor> = ViewFunctionParameters<WidgetConstructorT>[1];
interface ViewStubExtensions<WidgetConstructorT extends WidgetConstructor> {
input: ViewInput<WidgetConstructorT>;
nextInput: Promise<ViewInput<WidgetConstructorT>>;
callCount: number;
}
interface InternalViewStubExtensions<WidgetConstructorT extends WidgetConstructor> extends
ViewStubExtensions<WidgetConstructorT> {
invoked?: (input: ViewInput<WidgetConstructorT>) => void;
}
export type ViewFunctionStub<WidgetConstructorT extends WidgetConstructor> =
ViewFunction<WidgetConstructorT>&ViewStubExtensions<WidgetConstructorT>;
export function createViewFunctionStub<WidgetConstructorT extends WidgetConstructor>(
_constructor: WidgetConstructorT,
outputValues?: ViewOutput<WidgetConstructorT>,
): ViewFunctionStub<WidgetConstructorT> {
const result: InternalViewStubExtensions<WidgetConstructorT> =
((input: ViewInput<WidgetConstructorT>, output: ViewOutput<WidgetConstructorT>, _target: HTMLElement) => {
++result.callCount;
result.input = input;
if (output && outputValues) {
Object.assign((output as object), outputValues);
}
result.invoked?.(input);
}) as ViewFunctionStub<WidgetConstructorT>;
result.callCount = 0;
Object.defineProperty(result, 'nextInput', {
get() {
return new Promise<ViewInput<WidgetConstructorT>>(resolve => {
result.invoked = resolve;
});
}
});
return result as ViewFunctionStub<WidgetConstructorT>;
}