I am developing an app using electron and angular where I need to send locally stored information from my computer. I have successfully managed to send a message from the electron side to the angular side at the right time. However, I am facing issues accessing the message anywhere other than in the console log.
file.service.ts:
import { Injectable } from '@angular/core';
import { IpcRenderer } from 'electron';
@Injectable({
providedIn: 'root'
})
export class FileService {
private ipc: IpcRenderer | undefined;
private filePath: string[];
constructor() {
if (window.require) {
try {
this.ipc = window.require('electron').ipcRenderer;
} catch (error) {
throw error;
}
} else {
console.warn('Could not load electron ipc');
}
this.ipc.on('getFiles', (event, file) => {
this.filePath = file;
console.log('in service: ' + this.filePath); // correct
});
}
public getFilePaths(): string[] {
return this.filePath; // undefined
}
}
part of app.component.ts:
export class AppComponent implements OnInit{
constructor(private formBuilder: FormBuilder, private fileService: FileService) {
}
ngOnInit(): void {
const bob: string[] = this.fileService.getFilePaths();
console.log('in component: ' + bob); // undefined
}
I have discovered another method, but I'm unable to retrieve the message from the promise. file.service.ts:
import { Injectable } from '@angular/core';
import { IpcRenderer } from 'electron';
@Injectable({
providedIn: 'root'
})
export class FileService {
private ipc: IpcRenderer | undefined;
constructor() {
if (window.require) {
try {
this.ipc = window.require('electron').ipcRenderer;
} catch (error) {
throw error;
}
} else {
console.warn('Could not load electron ipc');
}
}
async getFiles(): Promise<string[]> {
return new Promise<string[]>((resolve) => {
this.ipc.on('getFiles', (event, info) => {
resolve(info);
});
});
}
}
part of app.component.ts:
export class AppComponent implements OnInit{
constructor(private formBuilder: FormBuilder, private fileService: FileService) {
}
ngOnInit(): void {
this.fileService.getFiles().then(console.log);
}
If there is a way to extract the message into a string[] variable using either of these methods or a different approach, any assistance would be greatly appreciated.