Skip to content

Commit a40672e

Browse files
author
Dennis Labordus
authored
Merge pull request #204 from com-pas/data-service-ws
Added support for Websocket to Data Service, including error handling
2 parents 34db746 + fc59805 commit a40672e

File tree

9 files changed

+356
-147
lines changed

9 files changed

+356
-147
lines changed

src/compas-editors/CompasVersions.ts

Lines changed: 77 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -130,25 +130,40 @@ export default class CompasVersionsPlugin extends LitElement {
130130

131131
private confirmRestoreVersionWizard(version: string): Wizard {
132132
function openScl(plugin: CompasVersionsPlugin) {
133-
return function () {
134-
const type = getTypeFromDocName(plugin.docName);
135-
136-
CompasSclDataService()
137-
.getSclDocumentVersion(type, plugin.docId, version)
138-
.then(sclDocument => {
139-
updateDocumentInOpenSCD(plugin, sclDocument);
133+
function updateDocument(sclDocument: Document): void {
134+
updateDocumentInOpenSCD(plugin, sclDocument);
140135

141-
plugin.dispatchEvent(
142-
newLogEvent({
143-
kind: 'info',
144-
title: get('compas.versions.restoreVersionSuccess', {
145-
version: version,
146-
}),
147-
})
148-
);
136+
plugin.dispatchEvent(
137+
newLogEvent({
138+
kind: 'info',
139+
title: get('compas.versions.restoreVersionSuccess', {
140+
version: version,
141+
}),
149142
})
150-
.catch(reason => createLogEvent(plugin, reason));
143+
);
144+
}
151145

146+
return function () {
147+
const type = getTypeFromDocName(plugin.docName);
148+
149+
const service = CompasSclDataService();
150+
if (service.useWebsocket()) {
151+
service.getSclDocumentVersionUsingWebsockets(
152+
plugin,
153+
type,
154+
plugin.docId,
155+
version,
156+
(sclDocument) => {
157+
updateDocument(sclDocument);
158+
})
159+
} else {
160+
service
161+
.getSclDocumentVersionUsingRest(type, plugin.docId, version)
162+
.then(sclDocument => {
163+
updateDocument(sclDocument);
164+
})
165+
.catch(reason => createLogEvent(plugin, reason));
166+
}
152167
// Close the Restore Dialog.
153168
plugin.dispatchEvent(newWizardEvent());
154169

@@ -285,15 +300,17 @@ export default class CompasVersionsPlugin extends LitElement {
285300
const oldScl = await this.getVersion(oldVersion);
286301
const newScl = this.doc.documentElement;
287302

288-
this.dispatchEvent(
289-
newWizardEvent(
290-
compareWizard(this, oldScl, newScl, {
291-
title: get('compas.compare.titleCurrent', {
292-
oldVersion: oldVersion,
293-
}),
294-
})
295-
)
296-
);
303+
if (oldScl && newScl) {
304+
this.dispatchEvent(
305+
newWizardEvent(
306+
compareWizard(this, oldScl, newScl, {
307+
title: get('compas.compare.titleCurrent', {
308+
oldVersion: oldVersion,
309+
}),
310+
})
311+
)
312+
);
313+
}
297314
} else {
298315
this.dispatchEvent(
299316
newWizardEvent(
@@ -318,16 +335,18 @@ export default class CompasVersionsPlugin extends LitElement {
318335
const oldScl = await this.getVersion(oldVersion);
319336
const newScl = await this.getVersion(newVersion);
320337

321-
this.dispatchEvent(
322-
newWizardEvent(
323-
compareWizard(this, oldScl, newScl, {
324-
title: get('compas.compare.title', {
325-
oldVersion: oldVersion,
326-
newVersion: newVersion,
327-
}),
328-
})
329-
)
330-
);
338+
if (oldScl && newScl) {
339+
this.dispatchEvent(
340+
newWizardEvent(
341+
compareWizard(this, oldScl, newScl, {
342+
title: get('compas.compare.title', {
343+
oldVersion: oldVersion,
344+
newVersion: newVersion,
345+
}),
346+
})
347+
)
348+
);
349+
}
331350
} else {
332351
this.dispatchEvent(
333352
newWizardEvent(
@@ -351,13 +370,31 @@ export default class CompasVersionsPlugin extends LitElement {
351370
];
352371
}
353372

354-
private async getVersion(version: string) {
373+
private async getVersion(version: string): Promise<void | Element> {
355374
const type = getTypeFromDocName(this.docName);
356-
return CompasSclDataService()
357-
.getSclDocumentVersion(type, this.docId, version)
358-
.then(sclDocument => {
359-
return Promise.resolve(sclDocument.documentElement);
375+
const service = CompasSclDataService();
376+
377+
if (service.useWebsocket()) {
378+
return new Promise((resolve) => {
379+
service.getSclDocumentVersionUsingWebsockets(
380+
this,
381+
type,
382+
this.docId,
383+
version,
384+
(sclDocument) => {
385+
resolve(sclDocument.documentElement);
386+
})
360387
});
388+
} else {
389+
return service
390+
.getSclDocumentVersionUsingRest(type, this.docId, version)
391+
.then(sclDocument => {
392+
return Promise.resolve(<Element>sclDocument.documentElement);
393+
})
394+
.catch(reason => {
395+
createLogEvent(this, reason)
396+
});
397+
}
361398
}
362399

363400
private openEditWizard(): void {

0 commit comments

Comments
 (0)