Skip to content

Commit b163888

Browse files
committed
refactor(ama-mfe-ng-utils): simplify resize directive
1 parent 2863386 commit b163888

File tree

2 files changed

+19
-41
lines changed

2 files changed

+19
-41
lines changed

packages/@ama-mfe/ng-utils/src/resize/resize.consumer.service.ts

+10-6
Original file line numberDiff line numberDiff line change
@@ -22,22 +22,22 @@ import {
2222
/**
2323
* This service listens for resize messages and updates the height of elements based on the received messages.
2424
*/
25-
@Injectable({
26-
providedIn: 'root'
27-
})
25+
@Injectable()
2826
export class ResizeConsumerService implements MessageConsumer<ResizeMessage> {
29-
private readonly newHeight = signal<{ height: number; channelId: string } | undefined>(undefined);
27+
private readonly heightPxSignal = signal<number | undefined>(undefined);
3028

3129
/**
3230
* A readonly signal that provides the new height information from the channel.
3331
*/
34-
public readonly newHeightFromChannel = this.newHeight.asReadonly();
32+
public readonly heightPx = this.heightPxSignal.asReadonly();
3533

3634
/**
3735
* The type of messages this service handles ('resize').
3836
*/
3937
public readonly type = RESIZE_MESSAGE_TYPE;
4038

39+
public from = '';
40+
4141
/**
4242
* The supported versions of resize messages and their handlers.
4343
*/
@@ -46,7 +46,11 @@ export class ResizeConsumerService implements MessageConsumer<ResizeMessage> {
4646
* Use the message paylod to compute a new height and emit it via the public signal
4747
* @param message message to consume
4848
*/
49-
'1.0': (message: RoutedMessage<ResizeV1_0>) => this.newHeight.set({ height: message.payload.height, channelId: message.from })
49+
'1.0': (message: RoutedMessage<ResizeV1_0>) => {
50+
if (this.from === message.from) {
51+
this.heightPxSignal.set(message.payload.height);
52+
}
53+
}
5054
};
5155

5256
private readonly consumerManagerService = inject(ConsumerManagerService);
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
import {
2-
computed,
32
Directive,
43
effect,
5-
ElementRef,
64
inject,
75
input,
8-
Renderer2,
96
} from '@angular/core';
107
import {
118
ResizeConsumerService,
@@ -15,47 +12,24 @@ import {
1512
* A directive that adjusts the height of an element based on resize messages from a specified channel.
1613
*/
1714
@Directive({
18-
selector: '[scalable]',
19-
standalone: true
15+
selector: '[connect][scalable]',
16+
standalone: true,
17+
host: {
18+
'[style.height.px]': 'resizeConsumer.heightPx()'
19+
},
20+
providers: [ResizeConsumerService]
2021
})
2122
export class ScalableDirective {
2223
/**
23-
* The connection ID for the element, used as channel id backup
24+
* The connection ID for the element
2425
*/
2526
public connect = input<string>();
2627

27-
/**
28-
* The channel id
29-
*/
30-
public scalable = input<string>();
31-
32-
private readonly resizeHandler = inject(ResizeConsumerService);
33-
34-
/**
35-
* This signal checks if the current channel requesting the resize matches the channel ID from the resize handler.
36-
* If they match, it returns the new height information; otherwise, it returns undefined.
37-
*/
38-
private readonly newHeightFromChannel = computed(() => {
39-
const channelAskingResize = this.scalable() || this.connect();
40-
const newHeightFromChannel = this.resizeHandler.newHeightFromChannel();
41-
if (channelAskingResize && newHeightFromChannel?.channelId === channelAskingResize) {
42-
return newHeightFromChannel;
43-
}
44-
return undefined;
45-
});
28+
protected readonly resizeConsumer = inject(ResizeConsumerService);
4629

4730
constructor() {
48-
const elem = inject(ElementRef);
49-
const renderer = inject(Renderer2);
50-
51-
this.resizeHandler.start();
52-
53-
/** When a new height value is received set the height of the host element (in pixels) */
5431
effect(() => {
55-
const newHeightFromChannel = this.newHeightFromChannel();
56-
if (newHeightFromChannel) {
57-
renderer.setStyle(elem.nativeElement, 'height', `${newHeightFromChannel.height}px`);
58-
}
32+
this.resizeConsumer.from = this.connect() ?? '';
5933
});
6034
}
6135
}

0 commit comments

Comments
 (0)