-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathschema.js
208 lines (199 loc) · 5.81 KB
/
schema.js
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/**
* These are all the shared data types used throughout. Transports receive these types and
* can choose how to deliver the message to their respective native platforms.
*
* - Notifications via {@link NotificationMessage}
* - Request -> Response via {@link RequestMessage} and {@link MessageResponse}
* - Subscriptions via {@link Subscription}
*
* Note: For backwards compatibility, some platforms may alter the data shape within the transport.
*
* @module Messaging Schema
*
*/
/**
* This is the format of an outgoing message.
*
* - See {@link MessageResponse} for what's expected in a response
*
* **NOTE**:
* - Windows will alter this before it's sent, see: {@link Messaging.WindowsRequestMessage}
*/
export class RequestMessage {
/**
* @param {object} params
* @param {string} params.context
* @param {string} params.featureName
* @param {string} params.method
* @param {string} params.id
* @param {Record<string, any>} [params.params]
* @internal
*/
constructor(params) {
/**
* The global context for this message. For example, something like `contentScopeScripts` or `specialPages`
* @type {string}
*/
this.context = params.context;
/**
* The name of the sub-feature, such as `duckPlayer` or `clickToLoad`
* @type {string}
*/
this.featureName = params.featureName;
/**
* The name of the handler to be executed on the native side
*/
this.method = params.method;
/**
* The `id` that native sides can use when sending back a response
*/
this.id = params.id;
/**
* Optional data payload - must be a plain key/value object
*/
this.params = params.params;
}
}
/**
* Native platforms should deliver responses in this format
*/
export class MessageResponse {
/**
* @param {object} params
* @param {string} params.context
* @param {string} params.featureName
* @param {string} params.id
* @param {Record<string, any>} [params.result]
* @param {MessageError} [params.error]
* @internal
*/
constructor(params) {
/**
* The global context for this message. For example, something like `contentScopeScripts` or `specialPages`
* @type {string}
*/
this.context = params.context;
/**
* The name of the sub-feature, such as `duckPlayer` or `clickToLoad`
* @type {string}
*/
this.featureName = params.featureName;
/**
* The resulting payload - must be a plain object
*/
this.result = params.result;
/**
* The `id` that is used to pair this response with its sender
*/
this.id = params.id;
/**
* An optional error
*/
this.error = params.error;
}
}
/**
* **NOTE**:
* - Windows will alter this before it's sent, see: {@link Messaging.WindowsNotification}
*/
export class NotificationMessage {
/**
* @param {object} params
* @param {string} params.context
* @param {string} params.featureName
* @param {string} params.method
* @param {Record<string, any>} [params.params]
* @internal
*/
constructor(params) {
/**
* The global context for this message. For example, something like `contentScopeScripts` or `specialPages`
*/
this.context = params.context;
/**
* The name of the sub-feature, such as `duckPlayer` or `clickToLoad`
*/
this.featureName = params.featureName;
/**
* The name of the handler to be executed on the native side
*/
this.method = params.method;
/**
* An optional payload
*/
this.params = params.params;
}
}
export class Subscription {
/**
* @param {object} params
* @param {string} params.context
* @param {string} params.featureName
* @param {string} params.subscriptionName
* @internal
*/
constructor(params) {
this.context = params.context;
this.featureName = params.featureName;
this.subscriptionName = params.subscriptionName;
}
}
/**
* This is the shape of payloads that can be delivered via subscriptions
*/
export class SubscriptionEvent {
/**
* @param {object} params
* @param {string} params.context
* @param {string} params.featureName
* @param {string} params.subscriptionName
* @param {Record<string, any>} [params.params]
* @internal
*/
constructor(params) {
this.context = params.context;
this.featureName = params.featureName;
this.subscriptionName = params.subscriptionName;
this.params = params.params;
}
}
/**
* Optionally received as part of {@link MessageResponse}
*/
export class MessageError {
/**
* @param {object} params
* @param {string} params.message
* @internal
*/
constructor(params) {
this.message = params.message;
}
}
/**
* @param {RequestMessage} request
* @param {Record<string, any>} data
* @return {data is MessageResponse}
*/
export function isResponseFor(request, data) {
if ('result' in data) {
return data.featureName === request.featureName && data.context === request.context && data.id === request.id;
}
if ('error' in data) {
if ('message' in data.error) {
return true;
}
}
return false;
}
/**
* @param {Subscription} sub
* @param {Record<string, any>} data
* @return {data is SubscriptionEvent}
*/
export function isSubscriptionEventFor(sub, data) {
if ('subscriptionName' in data) {
return data.featureName === sub.featureName && data.context === sub.context && data.subscriptionName === sub.subscriptionName;
}
return false;
}