in src/client/src/analytics_client/context_service.ts [30:63]
public registerContextProvider<Context>({ name, context$, schema }: ContextProviderOpts<Context>) {
if (this.contextProvidersSubscriptions.has(name)) {
throw new Error(`Context provider with name '${name}' already registered`);
}
// Declare the validator only in dev-mode
const validator = this.isDevMode ? schemaToIoTs(schema) : undefined;
const subscription = context$
.pipe(
filter((context) => {
if (validator) {
try {
validateSchema(`Context Provider '${name}'`, validator, context as Record<string, unknown>);
} catch (validationError) {
this.logger.error(validationError);
return false;
}
}
return true;
})
)
.subscribe((context) => {
// We store each context linked to the context provider, so they can increase and reduce
// the number of fields they report without having left-overs in the global context.
this.contextProvidersRegistry.set(name, context);
// For every context change, we rebuild the global context.
// It's better to do it here than to rebuild it for every reportEvent.
this.updateGlobalContext();
});
this.contextProvidersSubscriptions.set(name, subscription);
}