-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
messages.ts
143 lines (119 loc) · 4.49 KB
/
messages.ts
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/******************************************************************************
* Copyright 2022 TypeFox GmbH
* This program and the accompanying materials are made available under the
* terms of the MIT License, which is available in the project root.
******************************************************************************/
/**
* Identifies an endpoint able to send and receive messages.
*/
export type MessageParticipant = ExtensionMessageParticipant | WebviewMessageParticipant | BroadcastMessageParticipant
/**
* Specifies the host extension (if `extensionId` is undefined) or another extension.
*/
export interface ExtensionMessageParticipant {
type: 'extension'
/** Identifier in the form `publisher.name`. _This property is not supported yet._ */
extensionId?: string
}
export const HOST_EXTENSION: Readonly<ExtensionMessageParticipant> = { type: 'extension' };
/**
* A webview must be identified either with an ID (`webviewId`) or a type (`webviewType`).
*/
export interface WebviewMessageParticipant {
type: 'webview'
/** Identifier of a specific webview instance. */
webviewId?: string
/** Webview panel type or webview view type. */
webviewType?: string
}
/**
* This participant type is only valid for notifications and distributes a message
* to all participants that have registered for it.
*/
export interface BroadcastMessageParticipant {
type: 'broadcast'
}
export const BROADCAST: Readonly<BroadcastMessageParticipant> = { type: 'broadcast' };
export interface Message {
/** The receiver of this message. */
receiver: MessageParticipant
/**
* The sender of this message. Webviews can omit the sender so the property will be added
* by the host extension.
*/
sender?: MessageParticipant
}
export function isMessage(obj: unknown): obj is Message {
return typeof obj === 'object' && obj !== null && typeof (obj as Message).receiver === 'object';
}
export interface RequestMessage extends Message {
/** The request id. */
id: string
/** The method to be invoked. */
method: string
/** The parameters to be passed. */
params?: JsonAny;
}
export function isRequestMessage(msg: Message): msg is RequestMessage {
return !!(msg as RequestMessage).id && !!(msg as RequestMessage).method;
}
export interface ResponseMessage extends Message {
/** The request id. */
id: string
/** The result of a request in case the request was successful. */
result?: JsonAny
/** The error object in case the request failed. */
error?: ResponseError
}
export function isResponseMessage(msg: Message): msg is ResponseMessage {
return !!(msg as ResponseMessage).id && !(msg as RequestMessage).method;
}
export interface ResponseError {
/** The error message. */
message: string
/** Additional information about the error. */
data?: JsonAny
}
export interface NotificationMessage extends Message {
/** The method to be invoked. */
method: string
/** The parameters to be passed. */
params?: JsonAny
}
export function isNotificationMessage(msg: Message): msg is NotificationMessage {
return !(msg as RequestMessage).id && !!(msg as NotificationMessage).method;
}
export type JsonAny = JsonPrimitive | JsonMap | JsonArray | null;
export type JsonPrimitive = string | number | boolean;
export interface JsonMap {
[key: string]: JsonAny
}
export type JsonArray = JsonAny[];
/**
* Data structure for defining a request type.
*/
// eslint-disable-next-line @typescript-eslint/no-unused-vars
export type RequestType<P, R> = { method: string };
/**
* Function for handling incoming requests.
*/
export type RequestHandler<P, R> = (params: P, sender: MessageParticipant) => HandlerResult<R>;
export type HandlerResult<R> = R | Promise<R>;
/**
* Data structure for defining a notification type.
*/
// eslint-disable-next-line @typescript-eslint/no-unused-vars
export type NotificationType<P> = { method: string };
/**
* Function for handling incoming notifications.
*/
export type NotificationHandler<P> = (params: P) => void | Promise<void>;
/**
* Base API for Messenger implementations.
*/
export interface MessengerAPI {
sendRequest<P, R>(type: RequestType<P, R>, receiver: MessageParticipant, params: P): Promise<R>
onRequest<P, R>(type: RequestType<P, R>, handler: RequestHandler<P, R>): void
sendNotification<P>(type: NotificationType<P>, receiver: MessageParticipant, params: P): void
onNotification<P>(type: NotificationType<P>, handler: NotificationHandler<P>): void
}