Skip to content

Commit a19da50

Browse files
committed
feat: implement luigi config communication settings api
1 parent 768d4a8 commit a19da50

9 files changed

+155
-65
lines changed

src/lib/injection-tokens.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
export const LUIGI_STATIC_SETTINGS_CONFIG_SERVICE_INJECTION_TOKEN =
22
'OPENMFP_LUIGI_STATIC_SETTINGS_CONFIG_SERVICE';
3-
export const LUIGI_COMMUNICATION_CONFIG_SERVICE_INJECTION_TOKEN =
4-
'OPENMFP_LUIGI_COMMUNICATION_CONFIG_SERVICE';
3+
export const LUIGI_CUSTOM_MESSAGE_LISTENERS_INJECTION_TOKEN =
4+
'OPENMFP_LUIGI_CUSTOM_MESSAGE_LISTENERS_INJECTION_TOKEN';

src/lib/portal.module.ts

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { HttpClientModule } from '@angular/common/http';
33
import { BrowserModule } from '@angular/platform-browser';
44
import { RouterOutlet } from '@angular/router';
55
import {
6-
LUIGI_COMMUNICATION_CONFIG_SERVICE_INJECTION_TOKEN,
6+
LUIGI_CUSTOM_MESSAGE_LISTENERS_INJECTION_TOKEN,
77
LUIGI_STATIC_SETTINGS_CONFIG_SERVICE_INJECTION_TOKEN,
88
} from './injection-tokens';
99
import { LogoutComponent } from './logout/logout.component';
@@ -12,18 +12,17 @@ import { CallbackComponent } from './callback/callback.component';
1212
import { PortalRoutingModule } from './portal-routing.module';
1313
import { PortalComponent } from './portal.component';
1414
import {
15+
CustomMessageListener,
1516
StaticSettingsConfigService,
1617
StaticSettingsConfigServiceImpl,
17-
CommunicationConfigService,
18-
CommunicationConfigServiceImpl,
1918
} from './services';
2019

2120
export interface PortalModuleOptions {
2221
/** Service containing and providing the luigi settings configuration **/
2322
staticSettingsConfigService?: Type<StaticSettingsConfigService>;
2423

25-
/** Service containing and providing the luigi communication configuration **/
26-
communicationConfigService?: Type<CommunicationConfigService>;
24+
/** A set of class representing custom listeners **/
25+
customMessageListeners?: Type<CustomMessageListener>[];
2726
}
2827

2928
@NgModule({
@@ -38,17 +37,21 @@ export interface PortalModuleOptions {
3837
provide: LUIGI_STATIC_SETTINGS_CONFIG_SERVICE_INJECTION_TOKEN,
3938
useClass: StaticSettingsConfigServiceImpl,
4039
},
41-
{
42-
provide: LUIGI_COMMUNICATION_CONFIG_SERVICE_INJECTION_TOKEN,
43-
useClass: CommunicationConfigServiceImpl,
44-
},
4540
],
4641
imports: [PortalRoutingModule, BrowserModule, RouterOutlet, HttpClientModule],
4742
exports: [PortalComponent],
4843
bootstrap: [PortalComponent],
4944
})
5045
export class PortalModule {
5146
static create(options: PortalModuleOptions): NgModule {
47+
const customMessageListeners = (options.customMessageListeners || []).map(
48+
(customMessageListenerClass) => ({
49+
provide: LUIGI_CUSTOM_MESSAGE_LISTENERS_INJECTION_TOKEN,
50+
multi: true,
51+
useClass: customMessageListenerClass,
52+
})
53+
);
54+
5255
return {
5356
declarations: [
5457
LuigiComponent,
@@ -57,18 +60,13 @@ export class PortalModule {
5760
LogoutComponent,
5861
],
5962
providers: [
63+
...customMessageListeners,
6064
{
6165
provide: LUIGI_STATIC_SETTINGS_CONFIG_SERVICE_INJECTION_TOKEN,
6266
useClass:
6367
options.staticSettingsConfigService ||
6468
StaticSettingsConfigServiceImpl,
6569
},
66-
{
67-
provide: LUIGI_COMMUNICATION_CONFIG_SERVICE_INJECTION_TOKEN,
68-
useClass:
69-
options.communicationConfigService ||
70-
CommunicationConfigServiceImpl,
71-
},
7270
],
7371
imports: [
7472
PortalRoutingModule,

src/lib/services/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ export * from './auth.service';
22
export * from './luigi-core.service';
33
export * from './luigi-config/luigi-config.service';
44
export * from './luigi-config/static-settings-config.service';
5-
export * from './luigi-config/communication-config.service';
5+
export * from './luigi-config/custom-message-listener';

src/lib/services/luigi-config/communication-config.service.spec.ts

Lines changed: 0 additions & 23 deletions
This file was deleted.

src/lib/services/luigi-config/communication-config.service.ts

Lines changed: 0 additions & 15 deletions
This file was deleted.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* A representation of a custom message listener and the custom message the listener listens on.
3+
* See also {@link https://docs.luigi-project.io/docs/communication/?section=luigi-client-to-luigi-core}
4+
*/
5+
import { Subject } from 'rxjs';
6+
7+
export interface CustomMessageListener {
8+
/**
9+
* The custom message id the listener is registered for.
10+
*/
11+
messageId(): string;
12+
changed?: Subject<void>;
13+
14+
/**
15+
* The callback to be executed when the custom message is send by Luigi.
16+
*
17+
* @param customMessage The message object, see also {@link https://docs.luigi-project.io/docs/luigi-client-api?section=sendcustommessage}
18+
* @param mfObject The micro frontend object, see also {@link https://docs.luigi-project.io/docs/luigi-core-api?section=getmicrofrontends}
19+
* @param mfNodesObject The nodes object of the micro frontend, see also {@link https://docs.luigi-project.io/docs/navigation-parameters-reference?section=node-parameters}
20+
*/
21+
onCustomMessageReceived(
22+
customMessage: string,
23+
mfObject: any,
24+
mfNodesObject: any
25+
): void;
26+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import { TestBed } from '@angular/core/testing';
2+
import { LUIGI_CUSTOM_MESSAGE_LISTENERS_INJECTION_TOKEN } from '../../injection-tokens';
3+
import { Subject } from 'rxjs';
4+
import { CustomMessageListener } from './custom-message-listener';
5+
import { CustomMessageListeners } from './custom-message-listeners.service';
6+
7+
class ProjectCreatedListener implements CustomMessageListener {
8+
messageId(): string {
9+
return 'ProjectCreatedListener';
10+
}
11+
changed?: Subject<void>;
12+
onCustomMessageReceived(): void {}
13+
}
14+
15+
class EntityChangedListener implements CustomMessageListener {
16+
messageId(): string {
17+
return 'EntityChangedListener';
18+
}
19+
changed?: Subject<void>;
20+
onCustomMessageReceived(): void {}
21+
}
22+
23+
describe('CustomMessageListeners', () => {
24+
let customMessageListeners: CustomMessageListeners;
25+
26+
beforeEach(() => {
27+
TestBed.configureTestingModule({
28+
providers: [
29+
{
30+
provide: LUIGI_CUSTOM_MESSAGE_LISTENERS_INJECTION_TOKEN,
31+
multi: true,
32+
useClass: ProjectCreatedListener,
33+
},
34+
{
35+
provide: LUIGI_CUSTOM_MESSAGE_LISTENERS_INJECTION_TOKEN,
36+
multi: true,
37+
useClass: EntityChangedListener,
38+
},
39+
],
40+
}).compileComponents();
41+
42+
customMessageListeners = TestBed.inject(CustomMessageListeners);
43+
});
44+
45+
it('should be defined', () => {
46+
expect(customMessageListeners).toBeDefined();
47+
});
48+
49+
it('should provide a customMessageListeners object', () => {
50+
expect(Object.keys(customMessageListeners.messageListeners)).toContain(
51+
'customMessagesListeners'
52+
);
53+
});
54+
55+
it('should provide custom message listeners', () => {
56+
expect(listeners()).toBeDefined();
57+
expect(listeners()).toContain('ProjectCreatedListener');
58+
expect(listeners()).toContain('EntityChangedListener');
59+
expect(listeners().length).toBe(2);
60+
});
61+
62+
function listeners(): string[] {
63+
return Object.keys(
64+
customMessageListeners.messageListeners.customMessagesListeners
65+
);
66+
}
67+
});
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { Inject, Injectable } from '@angular/core';
2+
import { Subject } from 'rxjs';
3+
import { LUIGI_CUSTOM_MESSAGE_LISTENERS_INJECTION_TOKEN } from '../../injection-tokens';
4+
import { CustomMessageListener } from './custom-message-listener';
5+
6+
/**
7+
* This class holds the listeners for custom messages used by the application portal.
8+
* See also {@link https://docs.luigi-project.io/docs/communication/?section=luigi-core-to-luigi-client}
9+
* See also {@link https://docs.luigi-project.io/docs/luigi-core-api/?section=custommessages}
10+
*/
11+
@Injectable({
12+
providedIn: 'root',
13+
})
14+
export class CustomMessageListeners {
15+
public changed: Subject<void> = new Subject<void>();
16+
17+
constructor(
18+
@Inject(LUIGI_CUSTOM_MESSAGE_LISTENERS_INJECTION_TOKEN)
19+
private listeners: CustomMessageListener[]
20+
) {}
21+
22+
/**
23+
* An object containing the property 'customMessageListeners' in which the message-id and listener assignments are made.
24+
* See also {@link https://docs.luigi-project.io/docs/communication/?section=luigi-core-to-luigi-client}
25+
*/
26+
public get messageListeners(): any {
27+
const result = { customMessagesListeners: {} };
28+
for (const listener of this.listeners) {
29+
const obj = {
30+
[listener.messageId()]: (msg, mf, mfNodes) => {
31+
listener.onCustomMessageReceived(msg, mf, mfNodes);
32+
listener.changed?.subscribe(() => {
33+
this.changed.next();
34+
});
35+
},
36+
};
37+
Object.assign(result.customMessagesListeners, obj);
38+
}
39+
return result;
40+
}
41+
}

src/lib/services/luigi-config/luigi-config.service.ts

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
import { Inject, Injectable } from '@angular/core';
2-
import {
3-
LUIGI_COMMUNICATION_CONFIG_SERVICE_INJECTION_TOKEN,
4-
LUIGI_STATIC_SETTINGS_CONFIG_SERVICE_INJECTION_TOKEN,
5-
} from '../../injection-tokens';
2+
import { LUIGI_STATIC_SETTINGS_CONFIG_SERVICE_INJECTION_TOKEN } from '../../injection-tokens';
63
import { ClientEnvironment } from '../../model/env';
74
import { AuthConfigService } from './auth-config.service';
85
import { EnvConfigService } from '../env-config.service';
9-
import { CommunicationConfigService } from './communication-config.service';
106
import { StaticSettingsConfigService } from './static-settings-config.service';
7+
import { CustomMessageListeners } from './custom-message-listeners.service';
118

129
@Injectable({
1310
providedIn: 'root',
@@ -16,10 +13,9 @@ export class LuigiConfigService {
1613
constructor(
1714
private envConfigService: EnvConfigService,
1815
private authConfigService: AuthConfigService,
16+
private customMessageListeners: CustomMessageListeners,
1917
@Inject(LUIGI_STATIC_SETTINGS_CONFIG_SERVICE_INJECTION_TOKEN)
20-
private staticSettingsConfigService: StaticSettingsConfigService,
21-
@Inject(LUIGI_COMMUNICATION_CONFIG_SERVICE_INJECTION_TOKEN)
22-
private communicationConfigService: CommunicationConfigService
18+
private staticSettingsConfigService: StaticSettingsConfigService
2319
) {}
2420

2521
public async getLuigiConfiguration() {
@@ -31,7 +27,7 @@ export class LuigiConfigService {
3127
envConfig.clientId
3228
),
3329
routing: this.getInitialRoutingConfig(),
34-
communication: this.communicationConfigService.getCommunicationConfig(),
30+
communication: this.customMessageListeners.messageListeners,
3531
settings:
3632
this.staticSettingsConfigService.getInitialStaticSettingsConfig(),
3733
};

0 commit comments

Comments
 (0)