mirror of
https://github.com/jwetzell/showbridge-webui.git
synced 2026-09-07 14:29:01 +00:00
fix schema loading race
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
import { computed, effect, inject, Injectable, signal } from '@angular/core';
|
import { computed, effect, inject, Injectable, signal } from '@angular/core';
|
||||||
import { cloneDeep, isEqual } from 'lodash-es';
|
import { cloneDeep, isEqual } from 'lodash-es';
|
||||||
import { Config, ConfigError, ModuleError, RouteError } from '../models/config';
|
import { Config, ConfigError, ModuleConfig, ModuleError, RouteConfig, RouteError } from '../models/config';
|
||||||
import { SchemaService } from './schema';
|
import { SchemaService } from './schema';
|
||||||
import { HttpClient } from '@angular/common/http';
|
import { HttpClient } from '@angular/common/http';
|
||||||
import { SettingsService } from './settings';
|
import { SettingsService } from './settings';
|
||||||
@@ -14,7 +14,7 @@ export class ConfigService {
|
|||||||
if (config === undefined) {
|
if (config === undefined) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return this.schemaService.validate(config);
|
return this.schemaService.validate(this.schemaService.configSchemaId, config);
|
||||||
});
|
});
|
||||||
currentlyShownConfig = signal<Config | undefined>(undefined);
|
currentlyShownConfig = signal<Config | undefined>(undefined);
|
||||||
runningConfig = signal<Config | undefined>(undefined);
|
runningConfig = signal<Config | undefined>(undefined);
|
||||||
@@ -59,7 +59,7 @@ export class ConfigService {
|
|||||||
}
|
}
|
||||||
this.http.get<Config>(configUrl.toString()).subscribe({
|
this.http.get<Config>(configUrl.toString()).subscribe({
|
||||||
next: (config) => {
|
next: (config) => {
|
||||||
if (this.schemaService.validate(config)) {
|
if (this.schemaService.validate(this.schemaService.configSchemaId, config)) {
|
||||||
this.updateCurrentlyShownConfig(config);
|
this.updateCurrentlyShownConfig(config);
|
||||||
this.runningConfig.set(cloneDeep(config));
|
this.runningConfig.set(cloneDeep(config));
|
||||||
} else {
|
} else {
|
||||||
@@ -75,7 +75,7 @@ export class ConfigService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
uploadConfig(config: Config) {
|
uploadConfig(config: Config) {
|
||||||
if (this.schemaService.validate(config)) {
|
if (this.schemaService.validate(this.schemaService.configSchemaId,config)) {
|
||||||
this.updateCurrentlyShownConfig(config);
|
this.updateCurrentlyShownConfig(config);
|
||||||
const configUrl = this.settingsService.configUrl();
|
const configUrl = this.settingsService.configUrl();
|
||||||
if (!configUrl) {
|
if (!configUrl) {
|
||||||
|
|||||||
+26
-14
@@ -25,6 +25,12 @@ export class SchemaService {
|
|||||||
routesSchema = signal<JSONSchemaType<RouteConfig[]> | undefined>(undefined);
|
routesSchema = signal<JSONSchemaType<RouteConfig[]> | undefined>(undefined);
|
||||||
processorsSchema = signal<JSONSchemaType<ProcessorConfig[]> | undefined>(undefined);
|
processorsSchema = signal<JSONSchemaType<ProcessorConfig[]> | undefined>(undefined);
|
||||||
|
|
||||||
|
// TODO(jwetzell): populate from loaded schemas
|
||||||
|
configSchemaId = 'https://showbridge.io/config.schema.json';
|
||||||
|
modulesSchemaId = 'https://showbridge.io/modules.schema.json';
|
||||||
|
routesSchemaId = 'https://showbridge.io/routes.schema.json';
|
||||||
|
processorsSchemaId = 'https://showbridge.io/processors.schema.json';
|
||||||
|
|
||||||
schemasLoaded = computed(() => {
|
schemasLoaded = computed(() => {
|
||||||
return (
|
return (
|
||||||
this.configSchema() !== undefined &&
|
this.configSchema() !== undefined &&
|
||||||
@@ -63,10 +69,7 @@ export class SchemaService {
|
|||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case 'closed':
|
case 'closed':
|
||||||
this.ajv.removeSchema(this.configSchema()!);
|
this.resetAjv();
|
||||||
this.ajv.removeSchema(this.modulesSchema()!);
|
|
||||||
this.ajv.removeSchema(this.routesSchema()!);
|
|
||||||
this.ajv.removeSchema(this.processorsSchema()!);
|
|
||||||
this.configSchema.set(undefined);
|
this.configSchema.set(undefined);
|
||||||
this.modulesSchema.set(undefined);
|
this.modulesSchema.set(undefined);
|
||||||
this.routesSchema.set(undefined);
|
this.routesSchema.set(undefined);
|
||||||
@@ -74,15 +77,13 @@ export class SchemaService {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
|
||||||
effect(() => {
|
resetAjv() {
|
||||||
if (this.schemasLoaded()) {
|
this.ajv.removeSchema(this.configSchema()!);
|
||||||
this.ajv.addSchema(this.modulesSchema()!);
|
this.ajv.removeSchema(this.modulesSchema()!);
|
||||||
this.ajv.addSchema(this.routesSchema()!);
|
this.ajv.removeSchema(this.routesSchema()!);
|
||||||
this.ajv.addSchema(this.processorsSchema()!);
|
this.ajv.removeSchema(this.processorsSchema()!);
|
||||||
this.ajv.compile(this.configSchema()!);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
loadConfigSchema() {
|
loadConfigSchema() {
|
||||||
@@ -117,9 +118,9 @@ export class SchemaService {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
validate(data: any): boolean {
|
validate(schemaId: string, data: any): boolean {
|
||||||
if (this.schemasLoaded() && this.ajv) {
|
if (this.schemasLoaded() && this.ajv) {
|
||||||
this.ajv.validate('https://showbridge.io/config.schema.json', data);
|
this.ajv.validate(schemaId, data);
|
||||||
if (this.ajv.errors) {
|
if (this.ajv.errors) {
|
||||||
console.error('validation errors', this.ajv.errors);
|
console.error('validation errors', this.ajv.errors);
|
||||||
const errorPaths = new Set(
|
const errorPaths = new Set(
|
||||||
@@ -196,21 +197,32 @@ export class SchemaService {
|
|||||||
return paramsTemplate;
|
return paramsTemplate;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
addSchemaToAjv(schema: SomeJSONSchema) {
|
||||||
|
if (this.ajv.getSchema(schema.$id!)) {
|
||||||
|
this.ajv.removeSchema(schema.$id!);
|
||||||
|
}
|
||||||
|
this.ajv.addSchema(schema);
|
||||||
|
}
|
||||||
|
|
||||||
setConfigSchema(schema: JSONSchemaType<Config>) {
|
setConfigSchema(schema: JSONSchemaType<Config>) {
|
||||||
|
this.addSchemaToAjv(schema);
|
||||||
this.configSchema.set(schema);
|
this.configSchema.set(schema);
|
||||||
}
|
}
|
||||||
|
|
||||||
setModulesSchema(schema: JSONSchemaType<ModuleConfig[]>) {
|
setModulesSchema(schema: JSONSchemaType<ModuleConfig[]>) {
|
||||||
|
this.addSchemaToAjv(schema);
|
||||||
this.modulesSchema.set(schema);
|
this.modulesSchema.set(schema);
|
||||||
this.moduleTypes = [];
|
this.moduleTypes = [];
|
||||||
this.populateModuleTypes();
|
this.populateModuleTypes();
|
||||||
}
|
}
|
||||||
|
|
||||||
setRoutesSchema(schema: JSONSchemaType<RouteConfig[]>) {
|
setRoutesSchema(schema: JSONSchemaType<RouteConfig[]>) {
|
||||||
|
this.addSchemaToAjv(schema);
|
||||||
this.routesSchema.set(schema);
|
this.routesSchema.set(schema);
|
||||||
}
|
}
|
||||||
|
|
||||||
setProcessorsSchema(schema: JSONSchemaType<ProcessorConfig[]>) {
|
setProcessorsSchema(schema: JSONSchemaType<ProcessorConfig[]>) {
|
||||||
|
this.addSchemaToAjv(schema);
|
||||||
this.processorsSchema.set(schema);
|
this.processorsSchema.set(schema);
|
||||||
this.processorTypes = [];
|
this.processorTypes = [];
|
||||||
this.populateProcessorTypes();
|
this.populateProcessorTypes();
|
||||||
|
|||||||
Reference in New Issue
Block a user