Skip to content

Commit 129d4b2

Browse files
author
Dennis Labordus
committed
Prevent running multiple OCL Validation at the same time.
Signed-off-by: Dennis Labordus <[email protected]>
1 parent df455ba commit 129d4b2

File tree

4 files changed

+1473
-9
lines changed

4 files changed

+1473
-9
lines changed

src/compas-services/CompasValidatorService.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,15 @@ export function CompasSclValidatorService() {
3939
.then(parseXml);
4040
},
4141

42-
validateSCLUsingWebsockets(type: string, doc: Document, callback: (doc: Document) => void) {
42+
validateSCLUsingWebsockets(type: string, doc: Document,
43+
callback: (doc: Document) => void,
44+
onCloseCallback: () => void) {
4345
Websockets('CompasValidatorService')
4446
.execute(
4547
getWebsocketUri(getSclValidatorServiceUrl()) + '/validate-ws/v1/' + type,
4648
createRequest(doc),
47-
callback);
49+
callback,
50+
onCloseCallback);
4851
},
4952

5053
listNsdocFiles(): Promise<Document> {

src/compas-services/Websockets.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ export function Websockets(serviceName: string) {
1616
}
1717

1818
return {
19-
execute(url: string, request: string, callback: (doc: Document) => void) {
19+
execute(url: string, request: string,
20+
onMessageCallback: (doc: Document) => void,
21+
onCloseCallback?: () => void) {
2022
websocket = new WebSocket(url);
2123

2224
websocket.onopen = () => {
@@ -26,7 +28,7 @@ export function Websockets(serviceName: string) {
2628
websocket.onmessage = (evt) => {
2729
parseXml(evt.data)
2830
.then(doc => {
29-
callback(doc);
31+
onMessageCallback(doc);
3032
websocket?.close();
3133
})
3234
.catch(reason => {
@@ -44,6 +46,9 @@ export function Websockets(serviceName: string) {
4446

4547
websocket.onclose = () => {
4648
websocket = undefined;
49+
if (onCloseCallback) {
50+
onCloseCallback();
51+
}
4752
}
4853

4954
dispatchEventOnOpenScd(newPendingStateEvent(waitUntilValidated()))

src/validators/CompasValidateSchema.ts

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@ import { CompasSclValidatorService, SVS_NAMESPACE } from "../compas-services/Com
66
import { createLogEvent } from "../compas-services/foundation.js";
77
import { dispatchEventOnOpenScd, getTypeFromDocName } from "../compas/foundation.js";
88

9-
export default class ValidateTemplates extends LitElement {
9+
// Boolean to prevent running the validation multiple times at the same time.
10+
let compasValidationSchemaRunning = false;
11+
12+
export default class CompasValidateSchema extends LitElement {
1013
@property({ attribute: false })
1114
doc!: XMLDocument;
1215

@@ -18,21 +21,31 @@ export default class ValidateTemplates extends LitElement {
1821

1922
async validate(manual: boolean): Promise<void> {
2023
// We don't want to externally validate every time a save is done. So only start the validation when manually triggered.
21-
if (!manual) {
24+
// And also if one is already running we don't want to start another one, wait until it's finished.
25+
if (!manual || compasValidationSchemaRunning) {
2226
return;
2327
}
2428

29+
// Block running another validation until this one is finished.
30+
compasValidationSchemaRunning = true;
31+
2532
const docType = getTypeFromDocName(this.docName);
2633
const service = CompasSclValidatorService();
2734
if (service.useWebsocket()) {
28-
service.validateSCLUsingWebsockets(docType, this.doc, (doc) => {
29-
this.processValidationResponse(doc);
30-
});
35+
service.validateSCLUsingWebsockets(docType, this.doc,
36+
(doc) => {
37+
this.processValidationResponse(doc);
38+
},
39+
() => {
40+
compasValidationSchemaRunning = false;
41+
});
3142
} else {
3243
const response = await service.validateSCLUsingRest(docType, this.doc)
3344
.catch(createLogEvent);
3445
if (response instanceof Document) {
3546
this.processValidationResponse(response);
47+
} else {
48+
compasValidationSchemaRunning = false;
3649
}
3750
}
3851
}
@@ -51,5 +64,7 @@ export default class ValidateTemplates extends LitElement {
5164
);
5265
})
5366
}
67+
68+
compasValidationSchemaRunning = false;
5469
}
5570
}

0 commit comments

Comments
 (0)