|
1 | 1 | import { Events } from '../../common/Events';
|
2 | 2 | import { AttachmentEvents } from '../../events/AttachmentEvents';
|
3 | 3 | import { BacktraceAttachment } from '../../model/attachment';
|
| 4 | +import { BacktraceAttachmentProvider } from './BacktraceAttachmentProvider'; |
4 | 5 |
|
5 | 6 | export class AttachmentManager {
|
6 | 7 | public readonly attachmentEvents: Events<AttachmentEvents>;
|
7 | 8 |
|
8 |
| - private readonly _attachments: BacktraceAttachment[] = []; |
| 9 | + private readonly _attachmentProviders: BacktraceAttachmentProvider[] = []; |
9 | 10 |
|
10 | 11 | constructor() {
|
11 | 12 | this.attachmentEvents = new Events();
|
12 | 13 | }
|
13 | 14 |
|
14 |
| - public add(...attachments: BacktraceAttachment[]) { |
15 |
| - this._attachments.push(...attachments); |
16 |
| - this.attachmentEvents.emit('attachments-updated', this.get()); |
| 15 | + public add(...attachments: Array<BacktraceAttachment | (() => BacktraceAttachment)>) { |
| 16 | + this.addProviders( |
| 17 | + ...attachments.map<BacktraceAttachmentProvider>((a) => |
| 18 | + typeof a === 'function' |
| 19 | + ? { |
| 20 | + type: 'dynamic', |
| 21 | + get: a, |
| 22 | + } |
| 23 | + : { |
| 24 | + type: 'scoped', |
| 25 | + get: () => a, |
| 26 | + }, |
| 27 | + ), |
| 28 | + ); |
17 | 29 | }
|
18 | 30 |
|
19 |
| - public get(): BacktraceAttachment[] { |
20 |
| - return [...this._attachments]; |
| 31 | + public addProviders(...attachmentProviders: BacktraceAttachmentProvider[]) { |
| 32 | + let anyScoped = false; |
| 33 | + for (const provider of attachmentProviders) { |
| 34 | + if (provider.type === 'dynamic') { |
| 35 | + this._attachmentProviders.push(provider); |
| 36 | + } else { |
| 37 | + const attachment = provider.get(); |
| 38 | + this._attachmentProviders.push({ |
| 39 | + type: 'scoped', |
| 40 | + get: () => attachment, |
| 41 | + }); |
| 42 | + anyScoped = true; |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + if (anyScoped) { |
| 47 | + this.attachmentEvents.emit('scoped-attachments-updated', this.get('scoped')); |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + public get(type?: 'scoped' | 'dynamic'): BacktraceAttachment[] { |
| 52 | + const result: BacktraceAttachment[] = []; |
| 53 | + for (const provider of this._attachmentProviders) { |
| 54 | + if (type && provider.type !== type) { |
| 55 | + continue; |
| 56 | + } |
| 57 | + |
| 58 | + result.push(provider.get()); |
| 59 | + } |
| 60 | + return result; |
21 | 61 | }
|
22 | 62 | }
|
0 commit comments