Skip to content

Commit 118b769

Browse files
committed
Added data events
1 parent 674eb22 commit 118b769

14 files changed

+222
-19
lines changed

docs/demo/embeded-nav-bar.html

+5
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@
6464
manager.initialize();
6565
var contentMethods = new ufe.ContentCommunicationHandlerMethods();
6666
contentMethods.dispose = function() { dispose(); };
67+
contentMethods.handleDataEvent = function(data) {
68+
console.log('Iframe recevied data: \n\n' + JSON.stringify(data));
69+
};
6770
communicationHandler = new ufe.CrossWindowContentCommunicationHandler(
6871
manager,
6972
contentMethods
@@ -104,6 +107,7 @@
104107
} else {
105108
if (communicationHandler) {
106109
communicationHandler.dispatchBeforeUpdate();
110+
107111
setTimeout(() => {
108112
communicationHandler.dispatchUpdated();
109113
}, 1000);
@@ -123,6 +127,7 @@
123127
// Simulate a delay to consider exts processing
124128
window.setTimeout(() => {
125129
communicationHandler.dispatchMounted();
130+
communicationHandler.sendData(['Data from content!']);
126131
}, 1000);
127132
}
128133
}

docs/demo/js/app.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,10 @@
2828
case 'error':
2929
console.error(evt.error);
3030
break;
31-
default:
31+
case 'data':
32+
console.log('Data from child:\n' + JSON.stringify(evt.data));
33+
break;
34+
default:
3235
alert('Unknown event: ' + evt.type);
3336
}
3437
}
@@ -43,6 +46,7 @@
4346
globalHandlers['beforeDestroy'] = mfeEventHadler;
4447
globalHandlers['destroyed'] = mfeEventHadler;
4548
globalHandlers['error'] = mfeEventHadler;
49+
globalHandlers['data'] = mfeEventHadler;
4650

4751

4852
var configuration = new ufe.RootComponentOptions();
@@ -206,6 +210,10 @@
206210
if (!navbarIframeId)
207211
return;
208212

213+
mfe.getChild(navbarIframeId).sendData({
214+
'foo': 'bar'
215+
})
216+
209217
await mfe.removeChild(navbarIframeId);
210218
navbarIframeId = '';
211219
}

docs/demo/lib/bundle/index.js

+54-10
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/demo/lib/bundle/index.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/core/children/childComponent.ts

+9
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ export abstract class ChildComponent extends Component {
4242
methods.mounted = () => this.callHandler(ComponentEventType.Mounted);
4343
methods.beforeUpdate = () => this.callHandler(ComponentEventType.BeforeUpdate);
4444
methods.updated = () => this.callHandler(ComponentEventType.Updated);
45+
methods.data = (data: any) => this.callHandler(ComponentEventType.Data, data);
4546
methods.beforeDispose = () => this.contentBeginDisposed();
4647
methods.disposed = () => this.contentDisposed();
4748
return this.getCommunicationHandlerCore(methods);
@@ -139,4 +140,12 @@ export abstract class ChildComponent extends Component {
139140
this.contentDisposePromise = null;
140141
await super.disposeCore();
141142
}
143+
144+
/**
145+
* Send data.
146+
* @param data The data to send.
147+
*/
148+
public sendData(data: any): void{
149+
this.communicationHandler?.sendData(data);
150+
}
142151
}

src/core/children/communications/containerHandler.ts

+15-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ export class ContainerCommunicationHandlerMethods {
2626
* Call the container to signal the component has disposed(almost).
2727
*/
2828
public [CommunicationsEventKind.Disposed]: () => void = noop;
29+
/**
30+
* Call the container to signal the component has disposed(almost).
31+
*/
32+
public [CommunicationsEventKind.Data]: (data: any) => void = noop;
2933
}
3034

3135
/**
@@ -65,7 +69,7 @@ export abstract class ContainerCommunicationHandler {
6569
if (!method)
6670
return;
6771

68-
method();
72+
method(e.data);
6973
}
7074

7175
/**
@@ -97,6 +101,16 @@ export abstract class ContainerCommunicationHandler {
97101
this.communicationsManager?.send(event);
98102
}
99103

104+
/**
105+
* Send data.
106+
* @param data The data to send.
107+
*/
108+
public sendData(data: any): void{
109+
const event = new CommunicationsEvent(CommunicationsEventKind.Data);
110+
event.data = data;
111+
this.communicationsManager?.send(event);
112+
}
113+
100114
/**
101115
* Reuest that the content begins disposing.
102116
*/

src/core/children/communications/contentHandler.ts

+18
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ export class ContentCommunicationHandlerMethods {
1010
* Method to dispose the content.
1111
*/
1212
public dispose: () => void = noop;
13+
/**
14+
* Method to dispose the content.
15+
*/
16+
public handleDataEvent: (data: any) => void = noop;
1317
}
1418

1519
/**
@@ -49,6 +53,11 @@ export abstract class ContentCommunicationHandler {
4953
this.methods.dispose();
5054
}
5155
break;
56+
case CommunicationsEventKind.Data:
57+
if (this.methods) {
58+
this.methods.handleDataEvent(e.data);
59+
}
60+
break;
5261
default:
5362
throw new Error(`The "${e.kind}" event is not configured.`);
5463
}
@@ -76,6 +85,15 @@ export abstract class ContentCommunicationHandler {
7685
this.communicationsManager?.send(event);
7786
}
7887

88+
/**
89+
* Dispatch event to signal mounting finished.
90+
*/
91+
public sendData(data: any): void {
92+
const evt = new CommunicationsEvent(CommunicationsEventKind.Data);
93+
evt.data = data;
94+
this.send(evt);
95+
}
96+
7997
/**
8098
* Dispatch event to signal mounting finished.
8199
*/

src/core/children/communications/event.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ export enum CommunicationsEventKind {
77
BeforeUpdate = 'beforeUpdate',
88
Updated = 'updated',
99
BeforeDispose = 'beforeDispose',
10-
Disposed = 'disposed'
10+
Disposed = 'disposed',
11+
Data = 'data'
1112
}
1213

1314
/**

src/core/component.ts

+5-3
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ export abstract class Component {
160160
* Call a specific event handler.
161161
* @param type The type of handler to call.
162162
*/
163-
protected callHandler(type: ComponentEventType): void {
163+
protected callHandler(type: ComponentEventType, data?: any): void {
164164
if (type === ComponentEventType.Error)
165165
throw new Error(`For calling the "${ComponentEventType.Error}" handler use the "callErrorHandler" method.`);
166166

@@ -170,13 +170,15 @@ export abstract class Component {
170170

171171
if (handler) {
172172
try {
173-
handler(new ComponentEvent(
173+
const event = new ComponentEvent(
174174
this.id,
175175
type,
176176
this.rootElement,
177177
this.getParentElement(),
178178
null
179-
));
179+
);
180+
event.data = data;
181+
handler(event);
180182
} catch (error) {
181183
this.callErrorHandler(error);
182184
}

src/core/componentEvent.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ export enum ComponentEventType {
1010
Updated = 'updated',
1111
BeforeDestroy = 'beforeDestroy',
1212
Destroyed = 'destroyed',
13-
Error = 'error'
13+
Error = 'error',
14+
Data = 'data'
1415
}
1516

1617
/**
@@ -28,6 +29,7 @@ export class ComponentEvent {
2829
public parentEl: HTMLElement;
2930
public error: Error | null;
3031
public timestamp: Date;
32+
public data: any;
3133

3234
/**
3335
* COnstructor.

src/core/componentOptions.ts

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export class ComponentEventHandlers {
1414
[ComponentEventType.BeforeDestroy]?: ComponentEventHandler;
1515
[ComponentEventType.Destroyed]?: ComponentEventHandler;
1616
[ComponentEventType.Error]?: ComponentEventHandler;
17+
[ComponentEventType.Data]?: ComponentEventHandler;
1718
}
1819

1920
/**

0 commit comments

Comments
 (0)