-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathFileAttributeManager.ts
More file actions
73 lines (59 loc) · 2.29 KB
/
FileAttributeManager.ts
File metadata and controls
73 lines (59 loc) · 2.29 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
import { jsonEscaper } from '../../common/jsonEscaper.js';
import { AttributeType } from '../../model/data/index.js';
import { BacktraceModule, BacktraceModuleBindData } from '../BacktraceModule.js';
import { BacktraceStorage, BacktraceSyncStorage } from '../storage/BacktraceStorage.js';
import { SessionFiles } from '../storage/index.js';
import { AttributeManager } from './AttributeManager.js';
const ATTRIBUTE_FILE_NAME = 'bt-attributes';
export class FileAttributeManager implements BacktraceModule {
private _attributeManager?: AttributeManager;
constructor(
private readonly _storage: BacktraceStorage,
private _fileName?: string,
) {}
public static create(storage: BacktraceStorage) {
return new FileAttributeManager(storage);
}
public static createFromSession(sessionFiles: SessionFiles, storage: BacktraceStorage & BacktraceSyncStorage) {
const fileName = sessionFiles.getFileName(ATTRIBUTE_FILE_NAME);
return new FileAttributeManager(storage, fileName);
}
public initialize(): void {
this.saveAttributes();
}
public bind({ attributeManager, sessionFiles }: BacktraceModuleBindData): void {
if (this._fileName) {
throw new Error('This instance is already bound.');
}
if (!sessionFiles) {
return;
}
this._fileName = sessionFiles.getFileName(ATTRIBUTE_FILE_NAME);
this._attributeManager = attributeManager;
attributeManager.attributeEvents.on('scoped-attributes-updated', () => this.saveAttributes());
}
public dispose(): void {
this._fileName = undefined;
}
public async get(): Promise<Record<string, AttributeType>> {
if (!this._fileName) {
return {};
}
try {
const content = await this._storage.get(this._fileName);
if (!content) {
return {};
}
return JSON.parse(content);
} catch {
return {};
}
}
private async saveAttributes() {
if (!this._fileName || !this._attributeManager) {
return;
}
const reportData = this._attributeManager.get('scoped');
await this._storage.set(this._fileName, JSON.stringify(reportData.attributes, jsonEscaper()));
}
}