-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogrocket-initializer.ts
More file actions
80 lines (70 loc) · 2.66 KB
/
Copy pathlogrocket-initializer.ts
File metadata and controls
80 lines (70 loc) · 2.66 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
import { Injectable, PLATFORM_ID, inject } from '@angular/core';
import { isLogRocketBrowser } from './browser-env';
import { LOGROCKET_CONFIG_SOURCE } from './logrocket-config.token';
import { buildLogRocketSdkInitOptions } from './logrocket-merge-init-options';
import { LogRocketRuntimeState } from './logrocket-runtime.state';
import { LogRocketSdkAdapter } from './logrocket-sdk.adapter';
import { LogRocketService } from './logrocket.service';
import { shouldRecordLogRocketSession } from './logrocket-sampling';
function isAngularDevMode(): boolean {
return typeof (globalThis as { ngDevMode?: boolean }).ngDevMode !== 'undefined' &&
!!(globalThis as { ngDevMode?: boolean }).ngDevMode;
}
@Injectable()
export class LogRocketInitializer {
private readonly platformId = inject(PLATFORM_ID);
private readonly configSource = inject(LOGROCKET_CONFIG_SOURCE);
private readonly runtime = inject(LogRocketRuntimeState);
private readonly adapter = inject(LogRocketSdkAdapter);
private readonly logRocket = inject(LogRocketService);
private runOnce = false;
/**
* Safe to call multiple times; only the first invocation runs setup.
* Intended to be fired from `ENVIRONMENT_INITIALIZER` without blocking bootstrap.
*/
kickoff(): void {
if (this.runOnce) {
return;
}
this.runOnce = true;
void this.run();
}
private async run(): Promise<void> {
try {
if (!isLogRocketBrowser(this.platformId)) {
this.runtime.configReady.set(true);
return;
}
const config = typeof this.configSource === 'function' ? await this.configSource() : this.configSource;
this.runtime.resolvedConfig.set(config);
const enabled = config.enabled !== false;
this.runtime.libraryEnabled.set(enabled);
if (!enabled) {
this.runtime.recording.set(false);
this.runtime.configReady.set(true);
this.logRocket.discardPendingOps();
return;
}
const recording = shouldRecordLogRocketSession(config);
this.runtime.recording.set(recording);
if (!recording) {
this.runtime.configReady.set(true);
this.logRocket.discardPendingOps();
return;
}
const initMode = config.initMode ?? 'auto';
this.runtime.configReady.set(true);
if (initMode === 'manual') {
return;
}
await this.adapter.init(config.appId, buildLogRocketSdkInitOptions(config));
this.runtime.initialized.set(true);
} catch (err) {
this.runtime.configReady.set(true);
this.logRocket.discardPendingOps();
if (isAngularDevMode()) {
console.warn('@grandgular/logrocket-angular: initializer failed', err);
}
}
}
}