From 8f79a59e1f9851dbeed3afda4d8d361f9e59c73d Mon Sep 17 00:00:00 2001 From: Marcus Andersson Date: Mon, 13 Jan 2025 09:23:14 +0100 Subject: [PATCH] PluginExtensions: Persisting log messages accross the browser session (#95836) * making sure to keep the messages across a session. * moved values into consts. --- .../features/plugins/extensions/logs/log.ts | 20 ++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/public/app/features/plugins/extensions/logs/log.ts b/public/app/features/plugins/extensions/logs/log.ts index e66b7be4d15..252171c2ffd 100644 --- a/public/app/features/plugins/extensions/logs/log.ts +++ b/public/app/features/plugins/extensions/logs/log.ts @@ -16,16 +16,22 @@ export type ExtensionsLogItem = { }; const channelName = 'ui-extension-logs'; +const logsNumberLimit = 1000; +const logsRetentionTime = 1000 * 60 * 10; export class ExtensionsLog { private baseLabels: Labels | undefined; - private subject: ReplaySubject | undefined; + private subject: ReplaySubject; private channel: BroadcastChannel; constructor(baseLabels?: Labels, subject?: ReplaySubject, channel?: BroadcastChannel) { this.baseLabels = baseLabels; this.channel = channel ?? new BroadcastChannel(channelName); - this.subject = subject; + this.subject = subject ?? new ReplaySubject(logsNumberLimit, logsRetentionTime); + + if (!channel) { + this.channel.onmessage = (msg: MessageEvent) => this.subject.next(msg.data); + } } info(message: string, labels?: Labels): void { @@ -68,17 +74,13 @@ export class ExtensionsLog { extensionPointId: isString(extensionPointId) ? extensionPointId : undefined, }; + // We only receive messages from different contexts so adding the ones + // pushed by this log to the local subject. + this.subject.next(item); this.channel.postMessage(item); } asObservable(): Observable { - if (!this.subject) { - // Lazily create the subject on first subscription to prevent - // to create buffers when no subscribers exists - this.subject = new ReplaySubject(1000, 1000 * 60 * 10); - this.channel.onmessage = (msg: MessageEvent) => this.subject?.next(msg.data); - } - return this.subject.asObservable(); }