Skip to content

Commit a69f02e

Browse files
committedJun 22, 2021
Configuration improvements: proxy subscribers and version property
1 parent 2a57090 commit a69f02e

File tree

6 files changed

+61
-66
lines changed

6 files changed

+61
-66
lines changed
 

‎packages/browser/src/configuration/BrowserConfiguration.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ export class BrowserConfiguration extends Configuration {
77
if (BrowserLocalStorage.isAvailable()) {
88
this.services.storage = new BrowserLocalStorageProvider();
99
SettingsManager.applySavedServerSettings(this);
10-
super.changed();
1110
}
1211
}
1312
}

‎packages/core/src/Utils.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,6 @@ export function merge<T>(defaultValues: T, values: T): T {
5454
return <T>result;
5555
}
5656

57-
export const nameof = <T>(name: keyof T) => name;
58-
5957
export function parseVersion(source: string): string {
6058
if (!source) {
6159
return null;

‎packages/core/src/configuration/Configuration.ts

Lines changed: 59 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -21,21 +21,20 @@ 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-
3224
constructor() {
25+
// TODO: Can we make this seamless via setters.
26+
this.services = new Proxy({
27+
lastReferenceIdManager: new DefaultLastReferenceIdManager(),
28+
log: new NullLog(),
29+
storage: new InMemoryStorageProvider(),
30+
queue: new DefaultEventQueue(this)
31+
}, this.subscriberHandler);
32+
3333
// TODO: Verify this works in derived classes.
34-
return new Proxy(this, this.handler);
34+
return new Proxy(this, this.subscriberHandler);
3535
}
3636

3737
// TODO: add flag if your suspended.
38-
// TODO: change version to be a string.
3938
/**
4039
* A default list of tags that will automatically be added to every
4140
* report submitted to the server.
@@ -61,21 +60,16 @@ export class Configuration {
6160
public enabled: boolean = true;
6261

6362
public services: {
64-
environmentInfoCollector?: IEnvironmentInfoCollector,
65-
errorParser?: IErrorParser,
66-
lastReferenceIdManager: ILastReferenceIdManager,
67-
log: ILog,
68-
moduleCollector?: IModuleCollector,
69-
requestInfoCollector?: IRequestInfoCollector,
70-
submissionClient?: ISubmissionClient,
71-
storage: IStorageProvider,
72-
queue: IEventQueue
73-
} = new Proxy({
74-
lastReferenceIdManager: new DefaultLastReferenceIdManager(),
75-
log: new NullLog(),
76-
storage: new InMemoryStorageProvider(),
77-
queue: new DefaultEventQueue(this)
78-
}, this.handler);
63+
environmentInfoCollector?: IEnvironmentInfoCollector,
64+
errorParser?: IErrorParser,
65+
lastReferenceIdManager: ILastReferenceIdManager,
66+
log: ILog,
67+
moduleCollector?: IModuleCollector,
68+
requestInfoCollector?: IRequestInfoCollector,
69+
submissionClient?: ISubmissionClient,
70+
storage: IStorageProvider,
71+
queue: IEventQueue
72+
};
7973

8074
/**
8175
* Maximum number of events that should be sent to the server together in a batch. (Defaults to 50)
@@ -174,7 +168,7 @@ export class Configuration {
174168
public set apiKey(value: string) {
175169
this._apiKey = value || null;
176170
this.services.log.info(`apiKey: ${this._apiKey}`);
177-
this.changed();
171+
this.notifySubscribers();
178172
}
179173

180174
/**
@@ -203,7 +197,7 @@ export class Configuration {
203197
this._configServerUrl = value;
204198
this._heartbeatServerUrl = value;
205199
this.services.log.info(`serverUrl: ${value}`);
206-
this.changed();
200+
this.notifySubscribers();
207201
}
208202
}
209203

@@ -223,7 +217,7 @@ export class Configuration {
223217
if (value) {
224218
this._configServerUrl = value;
225219
this.services.log.info(`configServerUrl: ${value}`);
226-
this.changed();
220+
this.notifySubscribers();
227221
}
228222
}
229223

@@ -243,7 +237,7 @@ export class Configuration {
243237
if (value) {
244238
this._heartbeatServerUrl = value;
245239
this.services.log.info(`heartbeatServerUrl: ${value}`);
246-
this.changed();
240+
this.notifySubscribers();
247241
}
248242
}
249243

@@ -272,7 +266,7 @@ export class Configuration {
272266

273267
this._updateSettingsWhenIdleInterval = value;
274268
this.services.log.info(`updateSettingsWhenIdleInterval: ${value}`);
275-
this.changed();
269+
this.notifySubscribers();
276270
}
277271

278272
/**
@@ -327,7 +321,7 @@ export class Configuration {
327321
this._includePostData = val;
328322
this._includeQueryString = val;
329323
this.services.log.info(`includePrivateInformation: ${val}`);
330-
this.changed();
324+
this.notifySubscribers();
331325
}
332326

333327
/**
@@ -344,7 +338,7 @@ export class Configuration {
344338
*/
345339
public set includeUserName(value: boolean) {
346340
this._includeUserName = value === true;
347-
this.changed();
341+
this.notifySubscribers();
348342
}
349343

350344
/**
@@ -361,7 +355,7 @@ export class Configuration {
361355
*/
362356
public set includeMachineName(value: boolean) {
363357
this._includeMachineName = value === true;
364-
this.changed();
358+
this.notifySubscribers();
365359
}
366360

367361
/**
@@ -378,7 +372,7 @@ export class Configuration {
378372
*/
379373
public set includeIpAddress(value: boolean) {
380374
this._includeIpAddress = value === true;
381-
this.changed();
375+
this.notifySubscribers();
382376
}
383377

384378
/**
@@ -397,7 +391,7 @@ export class Configuration {
397391
*/
398392
public set includeCookies(value: boolean) {
399393
this._includeCookies = value === true;
400-
this.changed();
394+
this.notifySubscribers();
401395
}
402396

403397
/**
@@ -416,7 +410,7 @@ export class Configuration {
416410
*/
417411
public set includePostData(value: boolean) {
418412
this._includePostData = value === true;
419-
this.changed();
413+
this.notifySubscribers();
420414
}
421415

422416
/**
@@ -435,7 +429,7 @@ export class Configuration {
435429
*/
436430
public set includeQueryString(value: boolean) {
437431
this._includeQueryString = value === true;
438-
this.changed();
432+
this.notifySubscribers();
439433
}
440434

441435
/**
@@ -490,16 +484,8 @@ export class Configuration {
490484
* @param priority Used to determine plugins priority.
491485
* @param pluginAction A function that is run.
492486
*/
493-
public addPlugin(
494-
name: string,
495-
priority: number,
496-
pluginAction: (context: EventPluginContext) => Promise<void>,
497-
): void;
498-
public addPlugin(
499-
pluginOrName: IEventPlugin | string,
500-
priority?: number,
501-
pluginAction?: (context: EventPluginContext) => Promise<void>,
502-
): void {
487+
public addPlugin(name: string, priority: number, pluginAction: (context: EventPluginContext) => Promise<void>): void;
488+
public addPlugin(pluginOrName: IEventPlugin | string, priority?: number, pluginAction?: (context: EventPluginContext) => Promise<void>): void {
503489
const plugin: IEventPlugin = pluginAction
504490
? { name: pluginOrName as string, priority, run: pluginAction }
505491
: pluginOrName as IEventPlugin;
@@ -559,12 +545,21 @@ export class Configuration {
559545
}
560546

561547
/**
562-
* Automatically set the application version for events.
548+
* The application version for events.
549+
*/
550+
public get version(): string {
551+
return <string>this.defaultData[KnownEventDataKeys.Version];
552+
}
553+
554+
/**
555+
* Set the application version for events.
563556
* @param version
564557
*/
565-
public setVersion(version: string): void {
558+
public set version(version: string) {
566559
if (version) {
567560
this.defaultData[KnownEventDataKeys.Version] = version;
561+
} else {
562+
delete this.defaultData[KnownEventDataKeys.Version];
568563
}
569564
}
570565

@@ -587,9 +582,7 @@ export class Configuration {
587582
this.defaultData[KnownEventDataKeys.UserInfo] = userInfo;
588583
}
589584

590-
this.services.log.info(
591-
`user identity: ${shouldRemove ? "null" : userInfo.identity}`,
592-
);
585+
this.services.log.info(`user identity: ${shouldRemove ? "null" : userInfo.identity}`);
593586
}
594587

595588
/**
@@ -603,10 +596,7 @@ export class Configuration {
603596
/**
604597
* Automatically send a heartbeat to keep the session alive.
605598
*/
606-
public useSessions(
607-
sendHeartbeats: boolean = true,
608-
heartbeatInterval: number = 30000,
609-
): void {
599+
public useSessions(sendHeartbeats: boolean = true, heartbeatInterval: number = 30000): void {
610600
if (sendHeartbeats) {
611601
this.addPlugin(new HeartbeatPlugin(heartbeatInterval));
612602
}
@@ -628,14 +618,24 @@ export class Configuration {
628618
handler && this._subscribers.push(handler);
629619
}
630620

631-
protected changed() {
632-
const handlers = this._subscribers; // optimization for minifier.
633-
for (const handler of handlers) {
621+
protected notifySubscribers() {
622+
for (const handler of this._subscribers) {
634623
try {
635624
handler(this);
636625
} catch (ex) {
637626
this.services.log.error(`Error calling subscribe handler: ${ex}`);
638627
}
639628
}
640629
}
630+
631+
// TODO: Rework handlers as it will set property values with the friendly key as well as private members.
632+
private subscriberHandler = {
633+
set: (target, key: string | symbol, value: unknown): boolean => {
634+
this.services.log.trace(`${typeof key === "symbol" ? key.toString() : key} set to ${value}`);
635+
target[key] = value;
636+
this.notifySubscribers();
637+
return true;
638+
}
639+
};
640+
641641
}

‎packages/core/src/index.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,5 @@ export {
8181
randomNumber,
8282
startsWith,
8383
stringify,
84-
toBoolean,
85-
nameof
84+
toBoolean
8685
} from "./Utils.js";

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { Configuration } from "../../src/configuration/Configuration.js";
22
import { SettingsManager } from "../../src/configuration/SettingsManager.js";
33

44
describe("SettingsManager", () => {
5-
test("should call changed handler", (done) => {
5+
test("should call subscribe handler", (done) => {
66
const config = new Configuration();
77
config.apiKey = "UNIT_TEST_API_KEY";
88

‎packages/node/src/configuration/NodeConfiguration.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,5 @@ export class NodeConfiguration extends Configuration {
55
public useLocalStorage(folder?: string): void {
66
this.services.storage = new NodeFileStorageProvider(folder);
77
SettingsManager.applySavedServerSettings(this);
8-
this.changed();
98
}
109
}

0 commit comments

Comments
 (0)
Please sign in to comment.