Skip to content

Commit

Permalink
Fix compatibility with Electron 12 (#157)
Browse files Browse the repository at this point in the history
Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
  • Loading branch information
MehediH and sindresorhus authored Jan 22, 2021
1 parent 11e9cfa commit 191ae04
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 6 deletions.
5 changes: 5 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ declare class ElectronStore<T extends Record<string, any> = Record<string, unkno
Open the storage file in the user's editor.
*/
openInEditor(): void;

/**
Initializer to set up the required `ipc` communication channels for the module when a `Store` instance is not created in the main process and you are creating a `Store` instance in the Electron renderer process only.
*/
initRenderer(): void;
}

export = ElectronStore;
39 changes: 36 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,46 @@ const path = require('path');
const electron = require('electron');
const Conf = require('conf');

const {app, ipcMain, ipcRenderer} = electron;

// Set up the `ipcMain` handler for communication between renderer and main process.
const initDataListener = () => {
if (!ipcMain || !app) {
throw new Error('Electron Store: You need to call `.initRenderer()` from the main process.');
}

const appData = {
defaultCwd: app.getPath('userData'),
appVersion: app.getVersion()
};

ipcMain.on('electron-store-get-data', event => {
event.returnValue = appData;
});

return appData;
};

class ElectronStore extends Conf {
constructor(options) {
const app = (electron.app || electron.remote.app);
const defaultCwd = app.getPath('userData');
let defaultCwd;
let appVersion;

// If we are in the renderer process, we communicate with the main process
// to get the required data for the module otherwise, we pull from the main process.
if (ipcRenderer) {
({defaultCwd, appVersion} = ipcRenderer.sendSync('electron-store-get-data'));
} else if (ipcMain && app) {
({defaultCwd, appVersion} = initDataListener());
}

options = {
name: 'config',
...options
};

if (!options.projectVersion) {
options.projectVersion = app.getVersion();
options.projectVersion = appVersion;
}

if (options.cwd) {
Expand All @@ -25,9 +53,14 @@ class ElectronStore extends Conf {

options.configName = options.name;
delete options.name;

super(options);
}

static initRenderer() {
initDataListener();
}

openInEditor() {
// TODO: Remove `electron.shell.openItem` when targeting Electron 9.`
const open = electron.shell.openItem || electron.shell.openPath;
Expand Down
28 changes: 25 additions & 3 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@
Electron doesn't have a built-in way to persist user preferences and other data. This module handles that for you, so you can focus on building your app. The data is saved in a JSON file named config.json in [`app.getPath('userData')`](https://electronjs.org/docs/api/app#appgetpathname).

You can use this module directly in both the main and renderer process.

# When on Electron 10 or later, you need to enable the [`enableRemoteModule`](https://www.electronjs.org/docs/api/browser-window#new-browserwindowoptions) option to be able to use it directly in the renderer process.
You can use this module directly in both the main and renderer process. For use in the renderer process only, you need to call `Store.initRenderer()` in the main process, or create a new Store instance (`new Store()`) in the main process.

## Install

Expand Down Expand Up @@ -335,6 +333,30 @@ Get the path to the storage file.

Open the storage file in the user's editor.

### initRenderer()

Initializer to set up the required `ipc` communication channels for the module when a `Store` instance is not created in the main process and you are creating a `Store` instance in the Electron renderer process only.

In the main process:

```js
const Store = require('electron-store');

Store.initRenderer();
```

And in the renderer process:

```js
const Store = require('electron-store');

const store = new Store();

store.set('unicorn', '🦄');
console.log(store.get('unicorn'));
//=> '🦄'
```

## FAQ

#### [Advantages over `window.localStorage`](https://github.com/sindresorhus/electron-store/issues/17)
Expand Down

0 comments on commit 191ae04

Please sign in to comment.