60 lines
No EOL
1.2 KiB
TypeScript
60 lines
No EOL
1.2 KiB
TypeScript
import fs from 'fs';
|
|
import { backgroundMitt } from "@/modules/emitter";
|
|
import { SessionState, WindowState } from "@/types";
|
|
|
|
|
|
export const ipcEmit = (channel: string, payload: any) => {
|
|
backgroundMitt.emit('ipc-renderer', {
|
|
endpoint: channel,
|
|
message: payload
|
|
});
|
|
}
|
|
|
|
export const loadState = (fileName: string): SessionState => {
|
|
let state: SessionState;
|
|
|
|
try {
|
|
state = JSON.parse(fs.readFileSync(fileName).toString());
|
|
}
|
|
|
|
catch (error) {
|
|
state = {
|
|
key: false,
|
|
newMessages: []
|
|
}
|
|
}
|
|
|
|
return state
|
|
|
|
}
|
|
|
|
export const loadWinState = (fileName: string): WindowState => {
|
|
let state: WindowState;
|
|
|
|
try {
|
|
state = JSON.parse(fs.readFileSync(fileName).toString());
|
|
}
|
|
|
|
catch (error) {
|
|
state = {
|
|
width: 600,
|
|
height: 500,
|
|
x: null,
|
|
y: null,
|
|
}
|
|
}
|
|
|
|
return state
|
|
|
|
}
|
|
|
|
// Save session or window state.
|
|
export const saveToJson = (fileName: string, data: any) => {
|
|
|
|
fs.writeFile(fileName, JSON.stringify(data), (err) => {
|
|
if (err) {
|
|
console.log("Error when saving to json.")
|
|
}
|
|
})
|
|
|
|
} |