Skip to content

Commit 767313e

Browse files
committed
Remove factory function, use class constructor directly
1 parent 171f112 commit 767313e

File tree

16 files changed

+94
-74
lines changed

16 files changed

+94
-74
lines changed

packages/component/src/ActivityDebug/ActivityDebugContext.ts

Lines changed: 0 additions & 3 deletions
This file was deleted.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { RestrictedDebugAPI } from 'botframework-webchat-core/internal';
2+
import type { ArrayElement } from 'type-fest';
3+
4+
const BREAKPOINT_NAMES = ['render'] as const;
5+
6+
type ActivityBreakpointName = ArrayElement<typeof BREAKPOINT_NAMES>;
7+
type ActivityDebugContext = { readonly activity: any;
8+
};
9+
10+
class RestrictedActivityDebugAPI extends RestrictedDebugAPI<ActivityBreakpointName, ActivityDebugContext> {
11+
constructor(getActivity: () => any) {
12+
super(BREAKPOINT_NAMES, Object.freeze({ activity: getActivity }));
13+
}
14+
}
15+
16+
export default RestrictedActivityDebugAPI;

packages/component/src/ActivityDebug/createActivityPrivateDebugAPI.ts

Lines changed: 0 additions & 7 deletions
This file was deleted.
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
export { type ActivityDebugContext } from './ActivityDebugContext';
2-
export { default as createActivityPrivateDebugAPI } from './createActivityPrivateDebugAPI';
1+
export { default as RestrictedActivityDebugAPI } from './RestrictedActivityDebugAPI';

packages/component/src/Composer.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,12 +111,12 @@ const ComposerCoreUI = memo(({ children, nonce }: ComposerCoreUIProps) => {
111111

112112
if (current) {
113113
current['webChat'] = rootDebugAPI;
114+
115+
return () => delete current['webChat'];
114116
}
115117

116118
return () => {
117-
if (current) {
118-
delete current['webChat'];
119-
}
119+
// Intentionally left blank.
120120
};
121121
}, [rootDebugAPI]);
122122

packages/component/src/Transcript/ActivityRow.tsx

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,27 @@
11
import { hooks } from 'botframework-webchat-api';
2-
import { createRestrictedDebugAPI } from 'botframework-webchat-core/internal';
32
import classNames from 'classnames';
43
import PropTypes from 'prop-types';
54
import React, { forwardRef, memo, useCallback, useMemo, useRef } from 'react';
5+
import { useRefFrom } from 'use-ref-from';
66

77
import SpeakActivity from '../Activity/Speak';
8+
import { RestrictedActivityDebugAPI } from '../ActivityDebug';
89
import useActiveDescendantId from '../providers/TranscriptFocus/useActiveDescendantId';
910
import useFocusByActivityKey from '../providers/TranscriptFocus/useFocusByActivityKey';
1011
import useGetDescendantIdByActivityKey from '../providers/TranscriptFocus/useGetDescendantIdByActivityKey';
1112
import ScreenReaderText from '../ScreenReaderText';
1213
import { android } from '../Utils/detectBrowser';
1314
import FocusTrap from './FocusTrap';
14-
import useActivityAccessibleName from './useActivityAccessibleName';
15-
16-
import type { WebChatActivity } from 'botframework-webchat-core';
17-
import type { MouseEventHandler, PropsWithChildren } from 'react';
18-
import { useRefFrom } from 'use-ref-from';
1915
import {
2016
TranscriptFocusContent,
2117
TranscriptFocusContentActiveDescendant,
2218
TranscriptFocusContentOverlay,
2319
TranscriptFocusIndicator
2420
} from './TranscriptFocus';
21+
import useActivityAccessibleName from './useActivityAccessibleName';
22+
23+
import type { WebChatActivity } from 'botframework-webchat-core';
24+
import type { MouseEventHandler, PropsWithChildren } from 'react';
2525

2626
const { useActivityKeysByRead, useGetHasAcknowledgedByActivityKey, useGetKeyByActivity } = hooks;
2727

@@ -91,10 +91,7 @@ const ActivityRow = forwardRef<HTMLElement, ActivityRowProps>(({ activity, child
9191

9292
const prevArticleRef = useRef<HTMLElement>(null);
9393

94-
const restrictedDebugAPI = useMemo(
95-
() => createRestrictedDebugAPI(['render'], { activity: () => activityRef.current }),
96-
[activityRef]
97-
);
94+
const restrictedDebugAPI = useMemo(() => new RestrictedActivityDebugAPI(() => activityRef.current), [activityRef]);
9895

9996
const debugAPI = useMemo(() => restrictedDebugAPI.toPublic(), [restrictedDebugAPI]);
10097

packages/core-debug-api/src/private/RestrictedDebugAPI.ts renamed to packages/core-debug-api/src/RestrictedDebugAPI.ts

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
1-
import type { BaseContextGetters, BreakpointObject, ContextOfGetters, RestrictedDebugAPIType } from '../types';
2-
import { SHOULD_LOCKDOWN } from './constants';
3-
import DebugAPI from './DebugAPI';
1+
import { SHOULD_LOCKDOWN } from './private/constants';
2+
import DebugAPI from './private/DebugAPI';
3+
import type { BaseContext, BreakpointObject, RestrictedDebugAPIType } from './types';
44

55
// 🔒 This function must be left empty.
66
// eslint-disable-next-line @typescript-eslint/no-empty-function, @typescript-eslint/no-unused-vars
77
const BREAKPOINT_FUNCTION = <T>(__DEBUG_CONTEXT__: T) => {};
88

9-
class RestrictedDebugAPI<
10-
TBreakpointName extends string,
11-
TContextGetters extends BaseContextGetters
12-
> implements RestrictedDebugAPIType<TBreakpointName, TContextGetters> {
13-
constructor(breakpointNames: readonly TBreakpointName[], contextGetters: TContextGetters) {
14-
type TContext = { [K in keyof TContextGetters]: ReturnType<TContextGetters[K]> };
9+
type AsGetters<T> = {
10+
readonly [K in keyof T]: () => T[K];
11+
};
1512

13+
abstract class RestrictedDebugAPI<
14+
TBreakpointName extends string,
15+
TContext extends BaseContext
16+
> implements RestrictedDebugAPIType<TBreakpointName, TContext> {
17+
constructor(breakpointNames: readonly TBreakpointName[], contextGetters: AsGetters<TContext>) {
1618
this.#context = Object.create(null) satisfies Partial<TContext> as TContext;
1719

1820
for (const [name, getter] of Object.entries(contextGetters)) {
@@ -67,14 +69,19 @@ class RestrictedDebugAPI<
6769
}
6870
}
6971

70-
#breakpoint: BreakpointObject<TBreakpointName, ContextOfGetters<TContextGetters>>;
71-
#context: ContextOfGetters<TContextGetters>;
72+
#breakpoint: BreakpointObject<TBreakpointName, TContext>;
73+
#context: TContext;
7274

7375
toPublic() {
7476
return new DebugAPI(this.#breakpoint, this.#context);
7577
}
7678

7779
UNSAFE_callBreakpoint: Readonly<Record<TBreakpointName, (...args: any[]) => void>>;
80+
81+
// eslint-disable-next-line class-methods-use-this
82+
get '~types'() {
83+
return Object.freeze({ public: undefined as any });
84+
}
7885
}
7986

8087
export default RestrictedDebugAPI;

packages/core-debug-api/src/createRestrictedDebugAPI.ts

Lines changed: 0 additions & 8 deletions
This file was deleted.
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
export { default as createRestrictedDebugAPI } from './createRestrictedDebugAPI';
2-
export { type BreakpointObject, type DebugAPIType, type RestrictedDebugAPIType } from './types';
1+
export { default as RestrictedDebugAPI } from './RestrictedDebugAPI';
2+
export { type BreakpointObject, type DebugAPIType, type InferPublic, type RestrictedDebugAPIType } from './types';

packages/core-debug-api/src/types.ts

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
type BaseContextGetters = { readonly [key: string]: () => unknown };
2+
type BaseContext = { readonly [key: string]: unknown };
23
type ContextOfGetters<T extends BaseContextGetters> = { [K in keyof T]: ReturnType<T[K]> };
34

45
type BreakpointObject<TName extends string, TContext extends object> = Readonly<
@@ -32,16 +33,30 @@ interface DebugAPIType<TBreakpointName extends string, TContext extends object>
3233
*
3334
* For public consumption, call `toPublic()` to create an object for receiving side.
3435
*/
35-
interface RestrictedDebugAPIType<TBreakpointName extends string, TContextGetters extends BaseContextGetters> {
36+
interface RestrictedDebugAPIType<TBreakpointName extends string, TContext extends BaseContext> {
3637
/**
3738
* Creates a public version of Debug API for external consumption.
3839
*/
39-
toPublic(): DebugAPIType<TBreakpointName, ContextOfGetters<TContextGetters>>;
40+
toPublic(): DebugAPIType<TBreakpointName, TContext>;
4041

4142
/**
4243
* Triggers a breakpoint function.
4344
*/
4445
get UNSAFE_callBreakpoint(): Readonly<Record<TBreakpointName, (...args: any[]) => void>>;
46+
47+
readonly '~types': {
48+
readonly public: DebugAPIType<TBreakpointName, TContext>;
49+
};
4550
}
4651

47-
export type { BaseContextGetters, BreakpointObject, ContextOfGetters, DebugAPIType, RestrictedDebugAPIType };
52+
type InferPublic<T extends RestrictedDebugAPIType<any, any>> = T['~types']['public'];
53+
54+
export type {
55+
BaseContext,
56+
BaseContextGetters,
57+
BreakpointObject,
58+
ContextOfGetters,
59+
DebugAPIType,
60+
InferPublic,
61+
RestrictedDebugAPIType
62+
};

0 commit comments

Comments
 (0)