1
- // This file is copied from https://github.com/source-academy/sa-vscode/blob/main/src/utils/messages.ts
1
+ // This file is originally created in https://github.com/source-academy/sa-vscode/blob/main/src/utils/messages.ts
2
+ // It also needs to be copied to source-academy/frontend:src/features/vscode/messages.ts
3
+ // Ideally it is split into multiple files, but for ease of copying, it is kept as one file.
4
+
5
+ // ================================================================================
6
+ // Message type definitions
7
+ // ================================================================================
8
+ const Messages = createMessages ( {
9
+ /** Sent from the iframe to the extension */
10
+ ExtensionPing : ( ) => ( { } ) ,
11
+ /** Sent from the extension to the iframe */
12
+ ExtensionPong : ( token : string | null ) => ( { token } ) ,
13
+ IsVsc : ( ) => ( { } ) ,
14
+ NewEditor : ( assessmentName : string , questionId : number , code : string ) => ( {
15
+ assessmentName,
16
+ questionId,
17
+ code,
18
+ } ) ,
19
+ Text : ( code : string ) => ( { code } ) ,
20
+ } ) ;
21
+
22
+ export default Messages ;
23
+
24
+ // ================================================================================
25
+ // Code for type generation
26
+ // ================================================================================
27
+
28
+ // Define BaseMessage to be the base type for all messages, such that all messages have a type field
2
29
type BaseMessage < T extends string , P extends object > = {
3
30
type : T ;
4
31
} & P ;
5
32
33
+ // A helper function to create messages dynamically from schema (hoisted!)
6
34
function createMessages < T extends Record < string , ( ...args : any [ ] ) => object > > (
7
35
creators : T ,
8
36
) : {
@@ -21,27 +49,34 @@ function createMessages<T extends Record<string, (...args: any[]) => object>>(
21
49
) as any ;
22
50
}
23
51
24
- const Messages = createMessages ( {
25
- WebviewStarted : ( token : string | null ) => ( { token } ) ,
26
- IsVsc : ( ) => ( { } ) ,
27
- NewEditor : ( assessmentName : string , questionId : number , code : string ) => ( {
28
- assessmentName,
29
- questionId,
30
- code,
31
- } ) ,
32
- Text : ( code : string ) => ( { code } ) ,
33
- } ) ;
34
-
35
- export default Messages ;
36
-
37
- // Define MessageTypes to map each key in Messages to its specific message type
38
- export type MessageTypes = {
52
+ // Define MessageTypes as a map of each key in Messages to its specific message type
53
+ type MessageTypes = {
39
54
[ K in keyof typeof Messages ] : ReturnType < ( typeof Messages ) [ K ] > ;
40
55
} ;
41
56
42
57
// Define MessageType as a union of all message types
43
58
export type MessageType = MessageTypes [ keyof MessageTypes ] ;
44
59
60
+ // Also define MessageTypeNames as an "enum" to avoid hardcoding strings
61
+ export const MessageTypeNames = ( ( ) =>
62
+ ( {
63
+ ...Object . keys ( Messages )
64
+ . filter ( ( k ) => isNaN ( Number ( k ) ) )
65
+ . reduce (
66
+ ( acc , cur ) => ( {
67
+ ...acc ,
68
+ [ cur ] : cur ,
69
+ } ) ,
70
+ { } ,
71
+ ) ,
72
+ } ) as {
73
+ [ k in keyof typeof Messages ] : k ;
74
+ } ) ( ) ;
75
+
76
+ // ================================================================================
77
+ // Wrapper functions
78
+ // ================================================================================
79
+
45
80
export const FRONTEND_ELEMENT_ID = "frontend" ;
46
81
47
82
export function sendToWebview ( message : MessageType ) {
0 commit comments