Socket
Socket
Sign inDemoInstall

electron-store

Package Overview
Dependencies
Maintainers
1
Versions
27
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

electron-store - npm Package Compare versions

Comparing version 6.0.1 to 7.0.0

5

index.d.ts

@@ -60,4 +60,9 @@ /// <reference types="node"/>

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;

43

index.js
'use strict';
const path = require('path');
const electron = require('electron');
const {app, ipcMain, ipcRenderer, shell} = require('electron');
const Conf = require('conf');
// 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 = {

@@ -17,3 +43,3 @@ name: 'config',

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

@@ -29,9 +55,12 @@

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;
open(this.path);
shell.openPath(this.path);
}

@@ -38,0 +67,0 @@ }

16

package.json
{
"name": "electron-store",
"version": "6.0.1",
"version": "7.0.0",
"description": "Simple data persistence for your Electron app or module - Save and load user preferences, app state, cache, etc",

@@ -37,11 +37,11 @@ "license": "MIT",

"dependencies": {
"conf": "^7.1.2",
"type-fest": "^0.16.0"
"conf": "^9.0.0",
"type-fest": "^0.20.2"
},
"devDependencies": {
"ava": "^2.1.0",
"electron": "^9.1.0",
"execa": "^4.0.3",
"tsd": "^0.13.1",
"xo": "^0.32.1"
"ava": "^2.4.0",
"electron": "^11.2.0",
"execa": "^5.0.0",
"tsd": "^0.14.0",
"xo": "^0.37.1"
},

@@ -48,0 +48,0 @@ "xo": {

@@ -1,2 +0,2 @@

# electron-store [![Build Status](https://travis-ci.com/sindresorhus/electron-store.svg?branch=master)](https://travis-ci.com/github/sindresorhus/electron-store)
# electron-store

@@ -7,6 +7,4 @@ > Simple data persistence for your [Electron](https://electronjs.org) app or module - Save and load user preferences, app state, cache, etc

You can use this module directly in both the main and 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.
# 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.
## Install

@@ -18,3 +16,3 @@

*Requires Electron 7 or later.*
*Requires Electron 11 or later.*

@@ -133,4 +131,2 @@ ## Usage

> Note: The version the migrations use refers to the **project version** by default. If you want to change this behavior, specify the [`projectVersion`](#projectVersion) option.
#### name

@@ -179,5 +175,5 @@

Type: `boolean`\
Default: `true`
Default: `false`
The config is cleared if reading the config file causes a `SyntaxError`. This is a good default, as the config file is not intended to be hand-edited, so it usually means the config is corrupt and there's nothing the user can do about it anyway. However, if you let the user edit the config file directly, mistakes might happen and it could be more useful to throw an error when the config is invalid instead of clearing. Disabling this option will make it throw a `SyntaxError` on invalid config instead of clearing.
The config is cleared if reading the config file causes a `SyntaxError`. This is a good behavior for unimportant data, as the config file is not intended to be hand-edited, so it usually means the config is corrupt and there's nothing the user can do about it anyway. However, if you let the user edit the config file directly, mistakes might happen and it could be more useful to throw an error when the config is invalid instead of clearing.

@@ -244,6 +240,4 @@ #### serialize

Watch for any changes in the config file and call the callback for `onDidChange` if set. This is useful if there are multiple processes changing the same config file.
Watch for any changes in the config file and call the callback for `onDidChange` or `onDidAnyChange` if set. This is useful if there are multiple processes changing the same config file, for example, if you want changes done in the main process to be reflected in a renderer process.
**Currently this option doesn't work on Node.js 8 on macOS.**
### Instance

@@ -273,2 +267,4 @@

Use `.clear()` to reset all items.
#### .has(key)

@@ -286,2 +282,4 @@

This resets known items to their default values, if defined by the `defaults` or `schema` option.
#### .onDidChange(key, callback)

@@ -295,4 +293,2 @@

Events are only triggered in the same process. So you won't get events in the main process if you trigger an event in a renderer process. See [#39](https://github.com/sindresorhus/electron-store/issues/39).
Returns a function which you can use to unsubscribe:

@@ -348,2 +344,26 @@

### 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

@@ -370,5 +390,5 @@

#### How do I get store values in the renderer process, when my store was initialized in the main process?
#### How do I get store values in the renderer process when my store was initialized in the main process?
The store is not a singleton, so you will have to pass the values back and forth as messages. Electron provides a handy [`invoke/handle` API](https://www.electronjs.org/docs/api/ipc-main#ipcmainhandlechannel-listener) that works well for accessing these values.
The store is not a singleton, so you will need to either [initialize the store in a file that is imported in both the main and renderer process](https://github.com/sindresorhus/electron-store/issues/15), or you have to pass the values back and forth as messages. Electron provides a handy [`invoke/handle` API](https://www.electronjs.org/docs/api/ipc-main#ipcmainhandlechannel-listener) that works well for accessing these values.

@@ -385,4 +405,2 @@ ```js

It is recommended to adopt this pattern even if `electron-store` currently works directly in the renderer process as Electron plans to remove the `remote` module in the future. An alternative is to create your own singleton, which is described in [#15](https://github.com/sindresorhus/electron-store/issues/15).
## Related

@@ -389,0 +407,0 @@

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚑️ by Socket Inc