-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathactions.js
More file actions
369 lines (324 loc) · 10.1 KB
/
actions.js
File metadata and controls
369 lines (324 loc) · 10.1 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
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
import { LightningElement, api } from 'lwc';
import {
EVENT_TYPES,
EVT_PUSH_TOPIC,
EVT_GENERIC,
EVT_STD_PLATFORM_EVENT,
EVT_PLATFORM_EVENT,
EVT_CDC,
EVT_CUSTOM_CHANNEL_CDC,
EVT_CUSTOM_CHANNEL_PE,
EVT_MONITORING,
FILTER_ALL,
FILTER_CUSTOM,
getChannelPrefix
} from 'c/streamingUtility';
import getBlankPlatformEvent from '@salesforce/apex/StreamingMonitorController.getBlankPlatformEvent';
import subscribeAll from './subscribeAll.html';
import subscribe from './subscribe.html';
import publish from './publish.html';
import register from './register.html';
const ACTION_SUBSCRIBE_ALL = 'subscribeAll';
const ACTION_SUBSCRIBE = 'subscribe';
const ACTION_PUBLISH = 'publish';
const ACTION_REGISTER = 'register';
export default class Actions extends LightningElement {
@api action = 'subscribeAll';
@api channels = [];
subAllFilter = FILTER_ALL;
subAllReplay = '-1';
subEventType;
subEventName;
subChannel;
subReplayOption = '-1';
subReplayId;
pubEventType;
pubEventName;
pubChannel;
pubPayload;
regEventType;
render() {
switch (this.action) {
case ACTION_SUBSCRIBE_ALL:
return subscribeAll;
case ACTION_SUBSCRIBE:
return subscribe;
case ACTION_PUBLISH:
return publish;
case ACTION_REGISTER:
return register;
default:
throw new Error(`Unsupported action: ${this.action}`);
}
}
handleSubscribeAll() {
const subscribeEvent = new CustomEvent('subscribeall', {
detail: {
filter: this.subAllFilter,
replayId: this.subAllReplay
}
});
this.dispatchEvent(subscribeEvent);
}
handleSubscribe(event) {
event.preventDefault();
const replayId =
this.subReplayOption === 'custom'
? this.subReplayId
: this.subReplayOption;
const subscribeEvent = new CustomEvent('subscribe', {
detail: {
channel: this.subChannel,
replayId
}
});
this.dispatchEvent(subscribeEvent);
this.subEventName = undefined;
this.subChannel = '';
this.subReplayOption = '-1';
this.subReplayId = undefined;
}
handlePublish() {
const publishEvent = new CustomEvent('publish', {
detail: {
eventType: this.pubEventType,
eventName: this.pubEventName,
eventPayload: this.pubPayload
}
});
this.dispatchEvent(publishEvent);
}
handleValueChange(event) {
const { name } = event.target;
this[name] = event.detail.value;
}
handleSubEventTypeChange(event) {
this.subEventType = event.detail.value;
this.subEventName = undefined;
if (
this.subEventType === EVT_CUSTOM_CHANNEL_CDC ||
this.subEventType === EVT_CUSTOM_CHANNEL_PE
) {
this.subChannel = getChannelPrefix(this.subEventType);
} else {
this.subChannel = '';
}
}
handleSubEventNameChange(event) {
this.subEventName = event.detail.value;
this.subChannel =
getChannelPrefix(this.subEventType) + this.subEventName;
}
handlePubEventTypeChange(event) {
this.pubEventType = event.detail.value;
this.pubEventName = undefined;
this.pubChannel = undefined;
this.pubPayload = undefined;
}
async handlePubEventNameChange(event) {
this.pubEventName = event.detail.value;
this.pubChannel =
getChannelPrefix(this.pubEventType) + this.pubEventName;
// Load blank event for platform event
if (this.pubEventType === EVT_PLATFORM_EVENT) {
try {
const blankEvent = await getBlankPlatformEvent({
eventName: this.pubEventName
});
this.pubPayload = JSON.stringify(blankEvent, null, 2);
} catch (error) {
console.error(error);
}
}
}
handlePubPayloadChange(event) {
const payloadElement = event.target;
const { value } = event.detail;
this.pubPayload = value;
// Validate payload
if (this.pubEventType === EVT_GENERIC) {
payloadElement.setCustomValidity('');
} else {
try {
if (value) {
JSON.parse(value);
}
payloadElement.setCustomValidity('');
} catch (error /* eslint-disable-line no-unused-vars */) {
payloadElement.setCustomValidity('Invalid JSON');
}
}
}
get subEventTypes() {
return EVENT_TYPES;
}
get subEventNames() {
if (!this.subEventType) {
return [];
}
return this.channels[this.subEventType];
}
get subEventNamePlaceholder() {
if (this.subEventType && this.channels[this.subEventType].length > 0) {
return 'Select event';
}
if (!this.subEventType) {
return 'Waiting for event type';
}
if (this.subEventType === EVT_CUSTOM_CHANNEL_CDC) {
return 'The /data/ChangeEvents channel and custom channels require manual channel input';
} else if (this.subEventType === EVT_CUSTOM_CHANNEL_PE) {
return 'Custom channels require manual channel input';
}
const eventDefinition = EVENT_TYPES.find(
(e) => e.value === this.subEventType
);
if (!eventDefinition) {
throw new Error(`Unsupported event type ${this.subEventType}`);
}
return `No ${eventDefinition.label}s available`;
}
get isSubEventNameDisabled() {
return (
this.subEventType === undefined ||
this.channels[this.subEventType].length === 0
);
}
get isSubChannelDisabled() {
return (
this.subEventType !== EVT_CUSTOM_CHANNEL_CDC &&
this.subEventType !== EVT_CUSTOM_CHANNEL_PE
);
}
get isSubscribeDisabled() {
if (
this.subEventType === EVT_CUSTOM_CHANNEL_CDC ||
this.subEventType === EVT_CUSTOM_CHANNEL_PE
) {
const channel = this.subChannel.trim();
return (
channel === '' ||
channel === getChannelPrefix(this.subEventType)
);
}
return (
this.subEventType !== EVT_CUSTOM_CHANNEL_CDC &&
this.subEventName === undefined
);
}
get pubEventTypes() {
return EVENT_TYPES;
}
get pubEventNames() {
if (!this.pubEventType) {
return [];
}
return this.channels[this.pubEventType];
}
get pubEventNamePlaceholder() {
if (this.pubEventType && this.channels[this.pubEventType].length > 0) {
return 'Select event';
}
if (!this.pubEventType) {
return 'Waiting for event type';
}
const eventDefinition = EVENT_TYPES.find(
(e) => e.value === this.pubEventType
);
if (!eventDefinition) {
throw new Error(`Unsupported event type ${this.pubEventType}`);
}
return `No ${eventDefinition.label}s available`;
}
get isPubEventNameDisabled() {
return (
this.pubEventType === undefined ||
this.channels[this.pubEventType].length === 0
);
}
get isPublishDisabled() {
return (
this.pubEventType === undefined || this.pubEventName === undefined
);
}
get isManualPublishedAllowed() {
return (
this.pubEventType === EVT_GENERIC ||
this.pubEventType === EVT_PLATFORM_EVENT
);
}
get regEventTypes() {
return EVENT_TYPES;
}
get replayOptions() {
const options = [
{ label: 'No replay', value: '-1' },
{ label: 'Replay past events', value: '-2' }
];
if (this.action === ACTION_SUBSCRIBE) {
options.push({ label: 'Custom replay ID', value: 'custom' });
}
return options;
}
get isCustomReplayIdVisible() {
return (
this.action === ACTION_SUBSCRIBE &&
this.subReplayOption === 'custom'
);
}
get isPushTopicReg() {
return this.regEventType === EVT_PUSH_TOPIC;
}
get isGenericReg() {
return this.regEventType === EVT_GENERIC;
}
get isPlatformEventReg() {
return this.regEventType === EVT_PLATFORM_EVENT;
}
get isStandardPlatformEventReg() {
return this.regEventType === EVT_STD_PLATFORM_EVENT;
}
get isStandardCDCReg() {
return this.regEventType === EVT_CDC;
}
get isCustomChannelCDCReg() {
return this.regEventType === EVT_CUSTOM_CHANNEL_CDC;
}
get isCustomChannelPEReg() {
return this.regEventType === EVT_CUSTOM_CHANNEL_PE;
}
get isMonitoringReg() {
return this.regEventType === EVT_MONITORING;
}
get isCDCSub() {
return (
this.subEventType === EVT_CDC ||
this.subEventType === EVT_CUSTOM_CHANNEL_CDC
);
}
get isEventMonitoringSub() {
return this.subEventType === EVT_MONITORING;
}
get pubPayloadHelp() {
return this.pubEventType === EVT_GENERIC
? 'Plain string payload'
: 'JSON formatted payload with strings delimited by double quotes';
}
get subAllFilterOptions() {
const options = [
{ label: 'All events', value: FILTER_ALL },
{ label: 'All custom events', value: FILTER_CUSTOM }
];
EVENT_TYPES.forEach((type) => {
const { value } = type;
if (
value !== EVT_CUSTOM_CHANNEL_PE &&
value !== EVT_CUSTOM_CHANNEL_CDC
) {
const label = `Only ${type.label}s`;
options.push({ label, value });
}
});
return options;
}
}