Skip to content

Commit 51daf05

Browse files
committed
WIP - Simplified configuration handlers via proxies, start of integration handlers.
1 parent 2c37590 commit 51daf05

File tree

9 files changed

+70
-41
lines changed

9 files changed

+70
-41
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import {
2+
ExceptionlessClient,
3+
IEventPlugin,
4+
PluginContext
5+
} from "@exceptionless/core";
6+
7+
export class GlobalHandlerPlugin implements IEventPlugin {
8+
public priority: number = 100;
9+
public name: string = "GlobalHandlerPlugin";
10+
11+
private client: ExceptionlessClient = null;
12+
13+
public startup(context: PluginContext): Promise<void> {
14+
if (this.client) {
15+
return;
16+
}
17+
18+
this.client = context.client;
19+
Error.stackTraceLimit = Infinity;
20+
21+
// TODO: Discus if we want to unwire this handler in suspend?
22+
const originalOnError: OnErrorEventHandlerNonNull = globalThis.onerror;
23+
globalThis.onerror = (event: Event | string, source?: string, lineno?: number, colno?: number, error?: Error): any => {
24+
/*
25+
const builder = this.client.createUnhandledException(new Error(stackTrace.message || (options || {}).status || "Script error"), "onerror");
26+
builder.pluginContextData["@@_TraceKit.StackTrace"] = stackTrace;
27+
void builder.submit(); // TODO: Handle async?
28+
*/
29+
30+
// eslint-disable-next-line prefer-rest-params
31+
return originalOnError ? originalOnError.apply(this, ...arguments) : false;
32+
};
33+
34+
return Promise.resolve();
35+
}
36+
}

packages/core/src/ExceptionlessClient.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export class ExceptionlessClient {
2626
}
2727

2828
SettingsManager.applySavedServerSettings(this.config);
29-
this.config.onChanged(() => this.updateSettingsTimer(this._timeoutId > 0 ? 5000 : 0));
29+
this.config.subscribe(() => this.updateSettingsTimer(this._timeoutId > 0 ? 5000 : 0));
3030
this.config.services.queue.onEventsPosted(() => this.updateSettingsTimer());
3131
}
3232

packages/core/src/configuration/Configuration.ts

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,21 @@ import { guid } from "../Utils.js";
2121
import { KnownEventDataKeys } from "../models/Event.js";
2222

2323
export class Configuration {
24+
private handler = {
25+
set: (target, key, value) => {
26+
console.log(key, ` set to ${value}`);
27+
target[key] = value;
28+
return true;
29+
}
30+
};
31+
32+
constructor() {
33+
// TODO: Verify this works in derived classes.
34+
return new Proxy(this, this.handler);
35+
}
36+
2437
// TODO: add flag if your suspended.
25-
// change version to be a string.
38+
// TODO: change version to be a string.
2639
/**
2740
* A default list of tags that will automatically be added to every
2841
* report submitted to the server.
@@ -57,12 +70,12 @@ export class Configuration {
5770
submissionClient?: ISubmissionClient,
5871
storage: IStorageProvider,
5972
queue: IEventQueue
60-
} = {
73+
} = new Proxy({
6174
lastReferenceIdManager: new DefaultLastReferenceIdManager(),
6275
log: new NullLog(),
6376
storage: new InMemoryStorageProvider(),
6477
queue: new DefaultEventQueue(this)
65-
};
78+
}, this.handler);
6679

6780
/**
6881
* Maximum number of events that should be sent to the server together in a batch. (Defaults to 50)
@@ -140,11 +153,11 @@ export class Configuration {
140153
private _plugins: IEventPlugin[] = [];
141154

142155
/**
143-
* A list of handlers that will be fired when configuration changes.
156+
* A list of subscribers that will be fired when configuration changes.
144157
* @type {Array}
145158
* @private
146159
*/
147-
private _handlers: Array<(config: Configuration) => void> = [];
160+
private _subscribers: Array<(config: Configuration) => void> = [];
148161

149162
/**
150163
* The API key that will be used when sending events to the server.
@@ -611,17 +624,17 @@ export class Configuration {
611624
this.services.log = new ConsoleLog();
612625
}
613626

614-
public onChanged(handler: (config: Configuration) => void): void {
615-
handler && this._handlers.push(handler);
627+
public subscribe(handler: (config: Configuration) => void): void {
628+
handler && this._subscribers.push(handler);
616629
}
617630

618631
protected changed() {
619-
const handlers = this._handlers; // optimization for minifier.
632+
const handlers = this._subscribers; // optimization for minifier.
620633
for (const handler of handlers) {
621634
try {
622635
handler(this);
623636
} catch (ex) {
624-
this.services.log.error(`Error calling onChanged handler: ${ex}`);
637+
this.services.log.error(`Error calling subscribe handler: ${ex}`);
625638
}
626639
}
627640
}

packages/core/src/configuration/SettingsManager.ts

Lines changed: 3 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,6 @@ export class ClientSettings {
1212
export class SettingsManager {
1313
private static _isUpdatingSettings: boolean = false;
1414

15-
/**
16-
* A list of handlers that will be fired when the settings change.
17-
* @type {Array}
18-
* @private
19-
*/
20-
private static _handlers: Array<(config: Configuration) => void> = [];
21-
22-
// TODO: see if this is still needed.
23-
public static onChanged(handler: (config: Configuration) => void): void {
24-
handler && this._handlers.push(handler);
25-
}
26-
2715
public static applySavedServerSettings(config: Configuration): void {
2816
if (!config || !config.isValid) {
2917
return;
@@ -32,7 +20,6 @@ export class SettingsManager {
3220
const savedSettings = this.getSavedServerSettings(config);
3321
config.services.log.info(`Applying saved settings: v${savedSettings.version}`);
3422
config.settings = merge(config.settings, savedSettings.settings);
35-
this.changed(config);
3623
}
3724

3825
public static getVersion(config: Configuration): number {
@@ -79,7 +66,7 @@ export class SettingsManager {
7966
return;
8067
}
8168

82-
config.settings = merge(config.settings, response.data.settings);
69+
const settings = merge(config.settings, response.data.settings);
8370

8471
// TODO: Store snapshot of settings after reading from config and attributes and use that to revert to defaults.
8572
// Remove any existing server settings that are not in the new server settings.
@@ -89,28 +76,17 @@ export class SettingsManager {
8976
continue;
9077
}
9178

92-
delete config.settings[key];
79+
delete settings[key];
9380
}
9481

82+
config.settings = settings;
9583
config.services.storage.settings.save(response.data);
9684
log.info(`Updated settings: v${response.data.version}`);
97-
this.changed(config);
9885
} finally {
9986
this._isUpdatingSettings = false;
10087
}
10188
}
10289

103-
private static changed(config: Configuration) {
104-
const handlers = this._handlers; // optimization for minifier.
105-
for (const handler of handlers) {
106-
try {
107-
handler(config);
108-
} catch (ex) {
109-
config.services.log.error(`Error calling onChanged handler: ${ex}`);
110-
}
111-
}
112-
}
113-
11490
private static getSavedServerSettings(config: Configuration): ClientSettings {
11591
const item = config.services.storage.settings.get()[0];
11692
if (item && item.value && item.value.version && item.value.settings) {

packages/core/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ export { ReferenceIdPlugin } from "./plugins/default/ReferenceIdPlugin.js";
3838
export { RequestInfoPlugin } from "./plugins/default/RequestInfoPlugin.js";
3939
export { SubmissionMethodPlugin } from "./plugins/default/SubmissionMethodPlugin.js";
4040
export { ContextData } from "./plugins/ContextData.js";
41+
export { PluginContext } from "./plugins/PluginContext.js";
4142
export { EventPluginContext } from "./plugins/EventPluginContext.js";
4243
export { EventPluginManager } from "./plugins/EventPluginManager.js";
4344
export { IEventPlugin } from "./plugins/IEventPlugin.js";

packages/core/src/plugins/EventPluginManager.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ export class EventPluginManager {
4545
break;
4646
}
4747

48+
if (!plugin.run) {
49+
continue;
50+
}
51+
4852
try {
4953
await plugin.run(context);
5054
} catch (ex) {

packages/core/src/plugins/IEventPlugin.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@ export interface IEventPlugin {
66
name?: string;
77
startup?(context: PluginContext): Promise<void>;
88
suspend?(context: PluginContext): Promise<void>;
9-
run(context: EventPluginContext): Promise<void>;
9+
run?(context: EventPluginContext): Promise<void>;
1010
}

packages/core/test/configuration/SettingsManager.test.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,11 @@ describe("SettingsManager", () => {
66
const config = new Configuration();
77
config.apiKey = "UNIT_TEST_API_KEY";
88

9-
SettingsManager.onChanged((configuration: Configuration) => {
9+
config.subscribe((configuration: Configuration) => {
1010
expect(configuration.settings).not.toBeUndefined();
1111
done();
1212
});
1313

1414
SettingsManager.applySavedServerSettings(config);
15-
(SettingsManager as any)._handlers = [];
1615
});
1716
});

packages/core/test/plugins/default/EventPluginTestFixture.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export function createFixture(): { contextData: ContextData, context: EventPlugi
99
parse: (c: EventPluginContext, exception: Error) => Promise.resolve({
1010
type: exception.name,
1111
message: exception.message,
12-
stack_trace: (exception as any).testStack || null
12+
stack_trace: null
1313
})
1414
};
1515
const client: ExceptionlessClient = new ExceptionlessClient();

0 commit comments

Comments
 (0)