2021-04-07 19:42:06 +02:00
|
|
|
import { ipcMain, ipcRenderer } from 'electron';
|
2019-03-12 03:36:29 +01:00
|
|
|
import * as fs from 'fs';
|
|
|
|
|
2021-06-03 18:58:57 +02:00
|
|
|
import { StorageService } from 'jslib-common/abstractions/storage.service';
|
2019-03-12 03:36:29 +01:00
|
|
|
|
2021-06-03 18:58:57 +02:00
|
|
|
import { NodeUtils } from 'jslib-common/misc/nodeUtils';
|
2019-03-12 03:36:29 +01:00
|
|
|
|
|
|
|
// tslint:disable-next-line
|
|
|
|
const Store = require('electron-store');
|
|
|
|
|
|
|
|
export class ElectronStorageService implements StorageService {
|
|
|
|
private store: any;
|
|
|
|
|
|
|
|
constructor(dir: string, defaults = {}) {
|
|
|
|
if (!fs.existsSync(dir)) {
|
|
|
|
NodeUtils.mkdirpSync(dir, '700');
|
|
|
|
}
|
|
|
|
const storeConfig: any = {
|
|
|
|
defaults: defaults,
|
|
|
|
name: 'data',
|
|
|
|
};
|
|
|
|
this.store = new Store(storeConfig);
|
2021-04-07 19:42:06 +02:00
|
|
|
|
|
|
|
ipcMain.handle('storageService', (event, options) => {
|
|
|
|
switch (options.action) {
|
|
|
|
case 'get':
|
|
|
|
return this.get(options.key);
|
|
|
|
case 'save':
|
|
|
|
return this.save(options.key, options.obj);
|
|
|
|
case 'remove':
|
|
|
|
return this.remove(options.key);
|
|
|
|
}
|
|
|
|
});
|
2019-03-12 03:36:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
get<T>(key: string): Promise<T> {
|
|
|
|
const val = this.store.get(key) as T;
|
|
|
|
return Promise.resolve(val != null ? val : null);
|
|
|
|
}
|
|
|
|
|
|
|
|
save(key: string, obj: any): Promise<any> {
|
2021-05-19 02:10:20 +02:00
|
|
|
if (obj instanceof Set) {
|
|
|
|
obj = Array.from(obj);
|
|
|
|
}
|
2019-03-12 03:36:29 +01:00
|
|
|
this.store.set(key, obj);
|
|
|
|
return Promise.resolve();
|
|
|
|
}
|
|
|
|
|
|
|
|
remove(key: string): Promise<any> {
|
|
|
|
this.store.delete(key);
|
|
|
|
return Promise.resolve();
|
|
|
|
}
|
|
|
|
}
|