Skip to content

Commit 0221f67

Browse files
author
Dennis Labordus
committed
Refactor Websocket code to be reused.
Signed-off-by: Dennis Labordus <[email protected]>
1 parent e1e3053 commit 0221f67

File tree

5 files changed

+66
-34
lines changed

5 files changed

+66
-34
lines changed

public/js/plugins.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ export const officialPlugins = [
102102
},
103103
{
104104
name: 'Validate using OCL',
105-
src: '/src/validators/ValidateSchemaWithCompas.js',
105+
src: '/src/validators/CompasValidateSchema.js',
106106
icon: 'rule_folder',
107107
default: true,
108108
kind: 'validator',

src/compas-services/CompasValidatorService.ts

Lines changed: 8 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
handleResponse,
66
parseXml
77
} from "./foundation.js";
8+
import {Websockets} from "./Websockets";
89

910
export const SVS_NAMESPACE = 'https://www.lfenergy.org/compas/SclValidatorService/v1';
1011

@@ -22,17 +23,13 @@ export function CompasSclValidatorService() {
2223

2324
function getWebsocketUri(): string {
2425
const sclValidatorServiceUrl = getCompasSettings().sclValidatorServiceUrl;
25-
if (sclValidatorServiceUrl.startsWith("ws://") || sclValidatorServiceUrl.startsWith("wss://")) {
26-
return sclValidatorServiceUrl + (sclValidatorServiceUrl.endsWith("/") ? "" : "/");
27-
}
2826
if (sclValidatorServiceUrl.startsWith("http://") || sclValidatorServiceUrl.startsWith("https://")) {
29-
return sclValidatorServiceUrl.replace("http://", "ws://").replace("https://", "wss://")
30-
+ (sclValidatorServiceUrl.endsWith("/") ? "" : "/");
27+
return sclValidatorServiceUrl.replace("http://", "ws://").replace("https://", "wss://");
3128
}
3229

3330
return (document.location.protocol == "http:" ? "ws://" : "wss://")
3431
+ document.location.hostname + ":" + getWebsocketPort()
35-
+ sclValidatorServiceUrl + (sclValidatorServiceUrl.endsWith("/") ? "" : "/");
32+
+ sclValidatorServiceUrl;
3633
}
3734

3835
function createRequest(doc: Document): string {
@@ -61,30 +58,11 @@ export function CompasSclValidatorService() {
6158
},
6259

6360
validateSCLUsingWebsockets(type: string, doc: Document, callback: (doc: Document) => void) {
64-
const websocket = new WebSocket(getWebsocketUri() + 'validate-ws/v1/' + type);
65-
66-
websocket.onopen = () => {
67-
websocket.send(createRequest(doc));
68-
};
69-
70-
websocket.onmessage = (evt) => {
71-
parseXml(evt.data)
72-
.then(doc => {
73-
callback(doc);
74-
websocket.close();
75-
})
76-
.catch(reason => {
77-
createLogEvent(reason);
78-
websocket.close();
79-
});
80-
};
81-
82-
websocket.onerror = () => {
83-
createLogEvent(
84-
{ message: 'Websocket Error',
85-
type: 'Error'})
86-
websocket.close();
87-
};
61+
Websockets('CompasValidatorService')
62+
.execute(
63+
getWebsocketUri() + '/validate-ws/v1/' + type,
64+
createRequest(doc),
65+
callback);
8866
},
8967

9068
listNsdocFiles(): Promise<Document> {

src/compas-services/Websockets.ts

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import { newPendingStateEvent } from "../foundation.js";
2+
import { dispatchEventOnOpenScd } from "../compas/foundation.js";
3+
import { createLogEvent, parseXml } from "./foundation.js";
4+
5+
export function Websockets(serviceName: string) {
6+
let websocket: WebSocket | undefined;
7+
8+
function sleep(sleepTime: number): Promise<unknown> {
9+
return new Promise(resolve => setTimeout(resolve, sleepTime));
10+
}
11+
12+
async function waitUntilValidated(): Promise<void> {
13+
while (websocket !== undefined) {
14+
await sleep(250);
15+
}
16+
}
17+
18+
return {
19+
execute(url: string, request: string, callback: (doc: Document) => void) {
20+
websocket = new WebSocket(url);
21+
22+
websocket.onopen = () => {
23+
websocket?.send(request);
24+
};
25+
26+
websocket.onmessage = (evt) => {
27+
parseXml(evt.data)
28+
.then(doc => {
29+
callback(doc);
30+
websocket?.close();
31+
})
32+
.catch(reason => {
33+
createLogEvent(reason);
34+
websocket?.close();
35+
});
36+
};
37+
38+
websocket.onerror = () => {
39+
createLogEvent(
40+
{ message: `Websocket Error in service "${serviceName}"`,
41+
type: 'Error'})
42+
websocket?.close();
43+
};
44+
45+
websocket.onclose = () => {
46+
websocket = undefined;
47+
}
48+
49+
dispatchEventOnOpenScd(newPendingStateEvent(waitUntilValidated()))
50+
}
51+
}
52+
}

src/translations/en.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -565,7 +565,7 @@ export const en = {
565565
noSclVersions: 'No versions found for this project in CoMPAS',
566566
error: {
567567
type: 'Unable to determine type from document name!',
568-
server: 'Error communicating with CoMPAS Server',
568+
server: 'Error communicating with CoMPAS Ecosystem',
569569
serverDetails: '{{type}}: {{message}}',
570570
},
571571
changeset: {

src/validators/ValidateSchemaWithCompas.ts renamed to src/validators/CompasValidateSchema.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,11 @@ export default class ValidateTemplates extends LitElement {
2929
this.processValidationResponse(doc);
3030
});
3131
} else {
32-
service.validateSCLUsingRest(docType, this.doc)
33-
.then(response => this.processValidationResponse(response))
32+
const response = await service.validateSCLUsingRest(docType, this.doc)
3433
.catch(createLogEvent);
34+
if (response instanceof Document) {
35+
this.processValidationResponse(response);
36+
}
3537
}
3638
}
3739

0 commit comments

Comments
 (0)