forked from ChromeDevTools/devtools-frontend
-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathViewFunctionHelpers.ts
More file actions
62 lines (51 loc) · 2.61 KB
/
Copy pathViewFunctionHelpers.ts
File metadata and controls
62 lines (51 loc) · 2.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// 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>;
}