From c060ba2a99b29c87fa832a993a0812b7acf7e0f3 Mon Sep 17 00:00:00 2001
From: Christoph Reiter <reiter.christoph@gmail.com>
Date: Tue, 23 Jun 2020 14:24:09 +0200
Subject: [PATCH] Port to EventBus

---
 packages/auth/src/auth.js | 25 +++++++++++--------------
 1 file changed, 11 insertions(+), 14 deletions(-)

diff --git a/packages/auth/src/auth.js b/packages/auth/src/auth.js
index 3f53fcd2..f33a6c7c 100644
--- a/packages/auth/src/auth.js
+++ b/packages/auth/src/auth.js
@@ -5,8 +5,7 @@ import {ScopedElementsMixin} from '@open-wc/scoped-elements';
 import JSONLD from 'vpu-common/jsonld';
 import * as commonUtils from 'vpu-common/utils';
 import * as commonStyles from 'vpu-common/styles';
-import * as events from 'vpu-common/events.js';
-import {Icon} from 'vpu-common';
+import {Icon, EventBus} from 'vpu-common';
 import VPULitElement from 'vpu-common/vpu-lit-element';
 import  {KeycloakWrapper} from './keycloak.js';
 
@@ -49,18 +48,7 @@ export class Auth extends ScopedElementsMixin(VPULitElement) {
         this.person = null;
         this.entryPointUrl = commonUtils.getAPiUrl();
         this.keycloakConfig = null;
-
-        const _getLoginData = () => {
-            const message = {
-                status: this._loginStatus,
-                token: this.token,
-            };
-            return message;
-        };
-
         this._loginStatus = LoginStatus.UNKNOWN;
-        this._emitter = new events.EventEmitter('vpu-auth-update', 'vpu-auth-update-request');
-        this._emitter.registerCallback(_getLoginData);
 
         // Create the events
         this.initEvent = new CustomEvent("vpu-auth-init", { "detail": "KeyCloak init event", bubbles: true, composed: true });
@@ -162,7 +150,13 @@ export class Auth extends ScopedElementsMixin(VPULitElement) {
             return;
 
         this._loginStatus = status;
-        this._emitter.emit();
+
+        this._bus.publish('auth-update', {
+            status: this._loginStatus,
+            token: this.token,
+        }, {
+            retain: true,
+        });
     }
 
     /**
@@ -197,6 +191,8 @@ export class Auth extends ScopedElementsMixin(VPULitElement) {
     connectedCallback() {
         super.connectedCallback();
 
+        this._bus = new EventBus();
+
         // Keycloak config
         let baseURL = '';
         let realm = '';
@@ -257,6 +253,7 @@ export class Auth extends ScopedElementsMixin(VPULitElement) {
     }
 
     disconnectedCallback() {
+        this._bus.close();
         this._kcwrapper.removeEventListener('changed', this._onKCChanged);
         document.removeEventListener('click', this.closeDropdown);
         super.disconnectedCallback();
-- 
GitLab