I have an application that is listening for incoming data from an IPC Renderer Channel. Here is my setup:
container that sends data to angular app (mainWindow):
mainWindow.loadURL('http://www.myangularapp.com') //where the angular app lives (example url).
mainWindow.webContents.on('did-finish-load', () => {
const data = { name: "John Doe", address: "123 Main St", city: "NY"}
mainWindow.webContents.send('data-from-container', data)
}
})
angular app:
constructor(
private store: Store<fromStore.AppState>,
private cd: ChangeDetectorRef,
private zone: NgZone,
) {}
ngOnInit() {
this.isLoading = true;
if (this.electronService.ipcRenderer) {
this.electronService.ipcRenderer.on('data-from-container', (event, data) => {
this.zone.run(() => {
if(data){
setTimeout(() => {
console.log(data) // verified data is always received
this.formData = data; // property that form uses to populate data in the dom
this.prepopulateForm(data) // method that places incoming data in ngrx store
}, 1000)
}
})
})
}
this.store.select('containerData').subscribe(data => {
setTimeout(()=> {
console.log(data) // data is always being set in ngrx
this.isLoading = false
}, 5000);
})
}
Everytime the IPC Channel emits the 'data-from-container' event, the data is always getting received from my OnInit call, but the data doesn't always get set in my form! The pattern that i've noticed is typically that the data does not prepopulate the form when the angular app first launches inside the container, after the initial launch, every subsequent time the app is launched, the data appears.
I've tried using ngZone, setTimeout, and detectChange methods to trigger Change Detection so the Angular App can detect the newly set formData, but its not consistently prepopulating the form. Any suggestions on how to fix this?
if (data) { setTimeout(() => { this.zone.run(() => { this.formData = data; ... }); }, 1000); }
. – DorellesetTimeout
insidezone.run
? – GielguddetectChanges
but there seems to be no mention here that theChangeDetectionStrategy
you have used isOnPush
. If you are usingOnPush
, you will need to trigger changeDetection manually or assignformData
immutably. I'm not sure why you are using ngZone when you don't seem to be running anything outside Angular. It would be easier to help if you provided the entire component and template or even better, a StackBlitz example. – TonemecombineLatest('data-from-container', viewInitialized)
– Sauls