Skip to content

Commit 60eea5c

Browse files
committed
feat: extra filters for batch subscribe
1 parent 0500d84 commit 60eea5c

File tree

3 files changed

+92
-57
lines changed

3 files changed

+92
-57
lines changed

src/main/default/lwc/actions/actions.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -338,9 +338,20 @@ export default class Actions extends LightningElement {
338338
}
339339

340340
get subAllFilterOptions() {
341-
return [
341+
const options = [
342342
{ label: 'All events', value: FILTER_ALL },
343-
{ label: 'Custom events only', value: FILTER_CUSTOM }
343+
{ label: 'All custom events', value: FILTER_CUSTOM }
344344
];
345+
EVENT_TYPES.forEach((type) => {
346+
const { value } = type;
347+
if (
348+
value !== EVT_CUSTOM_CHANNEL_PE &&
349+
value !== EVT_CUSTOM_CHANNEL_CDC
350+
) {
351+
const label = `Only ${type.label}s`;
352+
options.push({ label, value });
353+
}
354+
});
355+
return options;
345356
}
346357
}

src/main/default/lwc/streamingMonitor/streamingMonitor.js

Lines changed: 78 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -143,72 +143,96 @@ export default class StreamingMonitor extends LightningElement {
143143
`Subscribing to multiple streaming channels with filter ${filter} and replay ID ${replayId}`
144144
);
145145

146-
// Build list of channels
147-
let channels = [];
148-
EVENT_TYPES.forEach((eventType) => {
149-
const eventTypeName = eventType.value;
150-
if (filter === FILTER_ALL) {
151-
if (eventTypeName === EVT_CDC) {
146+
try {
147+
// Build list of channels
148+
let channels = [];
149+
switch (filter) {
150+
case FILTER_ALL:
151+
// Get all event channels
152+
EVENT_TYPES.forEach((type) => {
153+
const typeName = type.value;
154+
if (typeName === EVT_CDC) {
155+
// Use global channel for all CDC events
156+
channels.push(CHANNEL_ALL_CDC);
157+
} else {
158+
// Get all channels for the other event types
159+
const channelPrefix = getChannelPrefix(typeName);
160+
this.channels[typeName].forEach((channelData) => {
161+
channels.push(
162+
channelPrefix + channelData.value
163+
);
164+
});
165+
}
166+
});
167+
break;
168+
case FILTER_CUSTOM:
169+
// Get custom channels for all event types
170+
EVENT_TYPES.forEach((type) => {
171+
const typeName = type.value;
172+
const channelPrefix = getChannelPrefix(typeName);
173+
this.channels[typeName].forEach((channelData) => {
174+
if (isCustomChannel(typeName, channelData.value)) {
175+
channels.push(
176+
channelPrefix + channelData.value
177+
);
178+
}
179+
});
180+
});
181+
break;
182+
case EVT_CDC:
152183
// Use global channel for all CDC events
153184
channels.push(CHANNEL_ALL_CDC);
154-
} else {
155-
// Get all channels for the other event types
156-
const channelPrefix = getChannelPrefix(eventTypeName);
157-
this.channels[eventTypeName].forEach((channelData) => {
185+
break;
186+
default: {
187+
// Add channels of given type
188+
const channelPrefix = getChannelPrefix(filter);
189+
this.channels[filter].forEach((channelData) => {
158190
channels.push(channelPrefix + channelData.value);
159191
});
160192
}
161-
} else if (filter === FILTER_CUSTOM) {
162-
// Get custom channels for all event types
163-
const channelPrefix = getChannelPrefix(eventTypeName);
164-
this.channels[eventTypeName].forEach((channelData) => {
165-
if (isCustomChannel(eventTypeName, channelData.value)) {
166-
channels.push(channelPrefix + channelData.value);
167-
}
168-
});
169-
} else {
170-
throw new Error(`Unsupported filter value: ${filter}`);
171193
}
172-
});
173194

174-
// Remove already subscribed channels
175-
channels = channels.filter(
176-
(channel) =>
177-
!this.subscriptions.some((sub) => sub.channel === channel)
178-
);
179-
180-
// Abort if there are no remaining channels
181-
if (channels.length === 0) {
182-
this.notify(
183-
'warn',
184-
'There are no channels to subscribe to with the specified filter and current subscriptions'
195+
// Remove already subscribed channels
196+
channels = channels.filter(
197+
(channel) =>
198+
!this.subscriptions.some((sub) => sub.channel === channel)
185199
);
186-
return;
187-
}
188200

189-
// Temporarily ignore subscribe errors while subscribing to events
190-
this.ignoreSubscribeErrors = true;
191-
setTimeout(() => {
192-
this.ignoreSubscribeErrors = false;
193-
}, IGNORE_SUBCRIBE_ERRORS_DELAY);
201+
// Abort if there are no remaining channels
202+
if (channels.length === 0) {
203+
this.notify(
204+
'warn',
205+
'There are no channels to subscribe to with the specified filter and current subscriptions'
206+
);
207+
return;
208+
}
194209

195-
// Queue subscriptions
196-
const subscribePromises = channels.map((channel) => {
197-
return subscribe(channel, replayId, (streamingEvent) => {
198-
this.handleStreamingEvent(streamingEvent);
210+
// Temporarily ignore subscribe errors while subscribing to events
211+
this.ignoreSubscribeErrors = true;
212+
setTimeout(() => {
213+
this.ignoreSubscribeErrors = false;
214+
}, IGNORE_SUBCRIBE_ERRORS_DELAY);
215+
216+
// Queue subscriptions
217+
const subscribePromises = channels.map((channel) => {
218+
return subscribe(channel, replayId, (streamingEvent) => {
219+
this.handleStreamingEvent(streamingEvent);
220+
});
199221
});
200-
});
201222

202-
// Save susbcriptions and notify success once done
203-
const subscriptions = await Promise.all(subscribePromises);
204-
subscriptions.forEach((subscription) => {
205-
this.saveSubscription(subscription);
206-
});
207-
this.notify(
208-
'success',
209-
'Successfully subscribed to the specified channels'
210-
);
211-
this.view = VIEW_MONITOR;
223+
// Save susbcriptions and notify success once done
224+
const subscriptions = await Promise.all(subscribePromises);
225+
subscriptions.forEach((subscription) => {
226+
this.saveSubscription(subscription);
227+
});
228+
this.notify(
229+
'success',
230+
'Successfully subscribed to the specified channels'
231+
);
232+
this.view = VIEW_MONITOR;
233+
} catch (error) {
234+
console.error(error.message);
235+
}
212236
}
213237

214238
async handleSubscribe(event) {

src/main/default/lwc/streamingUtility/streamingUtility.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ export const EVENT_TYPES = [
4949
channelPrefix: '/data/'
5050
},
5151
{
52-
label: 'Monitoring events',
52+
label: 'Monitoring event',
5353
value: EVT_MONITORING,
5454
channelPrefix: '/event/'
5555
}

0 commit comments

Comments
 (0)