Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Converts Compassing mixin into addon #320

Merged
merged 1 commit into from
Mar 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
import { html, query, state, property, TemplateResult } from 'lit-element';

import { Mixin } from 'open-scd/src/foundation.js';
import { EditingElement } from 'open-scd/src/Editing.js';
import { html, query, state, property, TemplateResult, customElement, LitElement } from 'lit-element';

import { CompasUserInfoService } from '../compas-services/CompasUserInfoService.js';

import './CompasSessionExpiringDialog.js';
import './CompasSessionExpiredDialog.js';
import '../compas/CompasSessionExpiringDialog.js';
import '../compas/CompasSessionExpiredDialog.js';

import { CompasSessionExpiringDialogElement } from './CompasSessionExpiringDialog.js';
import { CompasSessionExpiredDialogElement } from './CompasSessionExpiredDialog.js';
import { CompasSessionExpiringDialogElement } from '../compas/CompasSessionExpiringDialog.js';
import { CompasSessionExpiredDialogElement } from '../compas/CompasSessionExpiredDialog.js';

import { loadNsdocFiles } from './CompasNsdoc.js';
import { retrieveUserInfo } from './CompasUserInfo.js';
import { loadNsdocFiles } from '../compas/CompasNsdoc.js';
import { retrieveUserInfo } from '../compas/CompasUserInfo.js';

/** Event to handle the Session Expire Timers value */
export interface SetSessionTimeoutsDetail {
Expand All @@ -33,22 +30,19 @@ export function newSetSessionTimeoutsEvent(
});
}

/** Mixin that add CoMPAS Expire Dialogs and User Info to OpenSCD */
export type CompasingElement = Mixin<typeof Compasing>;

/**
* @typeParam TBase - a type extending `LitElement`
* @returns `Base` with Dialogs and User Info from CoMPAS.
*/
export function Compasing<TBase extends new (...args: any[]) => EditingElement>(
Base: TBase
) {
class CompasingElement extends Base {
@customElement('compas-session')
pascalwilbrink marked this conversation as resolved.
Show resolved Hide resolved
export class CompasSession extends LitElement {
@state()
private expiringSessionWarning: number = 10 * 60 * 1000;
@state()
private expiredSessionMessage: number = 15 * 60 * 1000;

/** The `XMLDocument` to be edited */
@property({ attribute: false })
doc: XMLDocument | null = null;
/** The name of the current [[`doc`]] */
@property({ type: String }) docName = '';
/** Index of the last [[`EditorAction`]] applied. */
@property({ type: Number })
editCount = -1;

Expand All @@ -63,103 +57,100 @@ export function Compasing<TBase extends new (...args: any[]) => EditingElement>(
private expiringSessionWarningTimer: NodeJS.Timeout | null = null;

private resetKeepAlivePing() {
// Set timer on null, so next time the ping will be executed again if the user interacts with OpenSCD.
this.pingTimer = null;
// Set timer on null, so next time the ping will be executed again if the user interacts with OpenSCD.
this.pingTimer = null;
}

private schedulePing() {
if (this.pingTimer === null) {
if (this.pingTimer === null) {
// Every minute we will send a Ping to the CoMPAS Services while the user is still active.
// This to keep the connection alive so long the user is working.
CompasUserInfoService()
.ping()
.finally(() => console.debug('Ping executed.'));
.ping()
.finally(() => console.debug('Ping executed.'));
this.pingTimer = setTimeout(this.resetKeepAlivePing, 60 * 1000);
}
}
}

private resetTimer(): void {
if (this.expiringSessionWarningTimer) {
if (this.expiringSessionWarningTimer) {
clearTimeout(this.expiringSessionWarningTimer);
}
this.expiringSessionWarningTimer = setTimeout(() => {
}
this.expiringSessionWarningTimer = setTimeout(() => {
this.expiringDialogElement.show();
}, this.expiringSessionWarning);
}, this.expiringSessionWarning);

if (this.expiredSessionMessageTimer) {
if (this.expiredSessionMessageTimer) {
clearTimeout(this.expiredSessionMessageTimer);
}
this.expiredSessionMessageTimer = setTimeout(() => {
}
this.expiredSessionMessageTimer = setTimeout(() => {
this.expiringDialogElement.close();
this.expiredDialogElement.show();
this.unregisterEvents();
}, this.expiredSessionMessage);
}, this.expiredSessionMessage);

this.schedulePing();
this.schedulePing();
}

private setSessionTimeouts(event: SetSessionTimeoutsEvent) {
this.expiringSessionWarning = event.detail.sessionWarning * 60 * 1000;
this.expiredSessionMessage = event.detail.sessionExpires * 60 * 1000;
this.expiringSessionWarning = event.detail.sessionWarning * 60 * 1000;
this.expiredSessionMessage = event.detail.sessionExpires * 60 * 1000;

this.resetTimer();
this.registerEvents();
this.resetTimer();
this.registerEvents();
}

private registerEvents() {
this.addEventListener('click', this.resetTimer);
this.addEventListener('keydown', this.resetTimer);
this.addEventListener('click', this.resetTimer);
this.addEventListener('keydown', this.resetTimer);
}

private unregisterEvents() {
this.removeEventListener('click', this.resetTimer);
this.removeEventListener('keydown', this.resetTimer);
this.removeEventListener('click', this.resetTimer);
this.removeEventListener('keydown', this.resetTimer);

if (this.pingTimer) {
if (this.pingTimer) {
clearTimeout(this.pingTimer);
}
}
}

constructor(...args: any[]) {
super(...args);

this.addEventListener('set-session-timeouts', this.setSessionTimeouts);
connectedCallback() {
super.connectedCallback();
this.addEventListener('set-session-timeouts', this.setSessionTimeouts);

// Just wait a small moment before starting to fetch all kind of info.
new Promise(resolve => setTimeout(resolve, 100)).then(() => {
// Just wait a small moment before starting to fetch all kind of info.
new Promise(resolve => setTimeout(resolve, 100)).then(() => {
// When the plugin is loaded we will also start retrieving the User Information and prepare the Timeout Panels.
retrieveUserInfo(this).finally(() =>
console.info('User info retrieved from CoMPAS')
console.info('User info retrieved from CoMPAS')
);
// And we will start loading the Nsdoc Files from the Compas Backend Service.
loadNsdocFiles(this).finally(() =>
console.info('Nsdoc Files loaded from CoMPAS')
console.info('Nsdoc Files loaded from CoMPAS')
);
});
});
}

render(): TemplateResult {
return html`
return html`<slot></slot>
<compas-session-expiring-dialog
.expiringSessionWarning="${this.expiringSessionWarning}"
.expiredSessionMessage="${this.expiredSessionMessage}"
.expiringSessionWarning="${this.expiringSessionWarning}"
.expiredSessionMessage="${this.expiredSessionMessage}"
>
</compas-session-expiring-dialog>
<compas-session-expired-dialog
.expiredSessionMessage="${this.expiredSessionMessage}"
.doc="${this.doc}"
.editCount=${this.editCount}
.docName="${this.docName}"
.expiredSessionMessage="${this.expiredSessionMessage}"
.doc="${this.doc}"
.editCount=${this.editCount}
.docName="${this.docName}"
>
</compas-session-expired-dialog>
${super.render()}
`;
`;
}
}

return CompasingElement;
}



declare global {
interface ElementEventMap {
['set-session-timeouts']: SetSessionTimeoutsEvent;
Expand Down
2 changes: 1 addition & 1 deletion packages/compas-open-scd/src/compas/CompasUserInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { newUserInfoEvent } from './foundation.js';
import { CompasUserInfoService } from '../compas-services/CompasUserInfoService.js';
import { createLogEvent } from '../compas-services/foundation.js';

import { newSetSessionTimeoutsEvent } from './Compasing.js';
import { newSetSessionTimeoutsEvent } from '../addons/CompasSession.js';

export async function retrieveUserInfo(element: Element): Promise<void> {
await CompasUserInfoService()
Expand Down
8 changes: 4 additions & 4 deletions packages/compas-open-scd/src/open-scd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ import { Plugging } from './Plugging.js';
import { Setting } from './Setting.js';
import { Waiting } from 'open-scd/src/Waiting.js';
import { Wizarding } from 'open-scd/src/Wizarding.js';
import { Compasing } from './compas/Compasing.js';
import './addons/CompasSession.js';

/** The `<open-scd>` custom element is the main entry point of the
* Open Substation Configuration Designer. */
@customElement('open-scd')
export class OpenSCD extends Compasing(
export class OpenSCD extends
Waiting(Hosting(Setting(Wizarding(Plugging(Editing(Historing(LitElement)))))))
) {
{
private currentSrc = '';
/** The current file's URL. `blob:` URLs are *revoked after parsing*! */
@property({ type: String })
Expand Down Expand Up @@ -92,7 +92,7 @@ export class OpenSCD extends Compasing(
}

render(): TemplateResult {
return html` ${super.render()} ${getTheme(this.settings.theme)} `;
return html`<compas-session> ${super.render()} ${getTheme(this.settings.theme)} </compas-session>`;
}

static styles = css`
Expand Down