Git Product home page Git Product logo

electron-app-settings's Introduction

electron-app-settings

An easy-to-use, efficient, and multi-process safe settings framework for Electron that presumes a single application configuration file.

Written to allow settings to be shared between the main process and renderer processes through ipcMain and ipcRenderer communication. The main process acts as the master to all renderer processes, loading the initial configuration file, managing synchronization, and the saving of the configuration on application quit.

Compatible with electron-config generated configuration files.

  • Advantages
    • Simple syntax allowing for escaped dot notation
      • e.g., "my\.property" => {"my\.property": ...}
    • Automatic merging of default values
    • IPC-based with no-config settings synchronization
    • Simple to use -- require wherever needed and begin usage!
  • Disadvantages
    • No key=>value monitoring (yet)
    • Only save-on-quit (to be further expanded)
    • Single settings file (could be changed, but might be beyond the module's scope)

Installation

$ npm install electron-app-settings --save

Usage

electron-app-settings can simply be required wherever it is needed, regardless of if it has been loaded in the main process or not, as the module will automatically handle setting itself up in the main process. If using a packer, it may be necessary to disable the use of remote.require via the NO_REMOTE_REQUIRE environment variable. If NO_REMOTE_REQUIRE is set, then you must ensure that the main process requires electron-app-settings as well as the renderer process.

The configuration file is presumed to be located at "userData/Settings" and will be saved on application quit.

// MAIN PROCESS
const settings = require('electron-app-settings');

settings.set('cat', {
  name: 'Cat',
  limbs: 4,
  fuzzy: true
});

settings.get('cat.name');
// => "Cat"

settings.has('cat.bark');
// => false

/* Object-only merge-style defaults */
settings.set({
  { dog: {
    name: "Dog",
    tail: true
  }
}, true);

settings.get('cat');
// => {name: "Cat", limbs: 4, fuzzy: true, tail: true}
settings.get('dog');
// => {name: "Dog", tail: true}

// RENDERER PROCESS
const settings = require('electron-app-settings');

// ... on app-ready
  settings.get('cat');
  // => {name: "Cat", limbs: 4, fuzzy: true, tail: true}

Promises API

electron-app-settings also has a Promise-style API accessible through the promises property of the settings object returned via require('electron-app-settings'). It functions exactly the same as the normal API with any return values being returned through the Promise's resolve callback. Access through the promises interface ensures that full renderer to main process IPC communication has already been completed.

// RENDERER PROCESS
const settings = require('electron-app-settings');

// ... somewhere in an async function
  await settings.promises.get('cat');
  // => {name: "Cat", limbs: 4, fuzzy: true, tail: true}

electron-app-settings's People

Contributors

kettek avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

electron-app-settings's Issues

bundled code fails on renderer initialization

hey there,

i'm bundling my electron app which contains this package to handle settings with vue-cli-plugin-electron-builder which uses webpack to bundle js code.

i've noticed that my bundled/packaged code doesn't work and throws an exception like this:
image

since this is an atypical error message, i've managed to trace the cause of this down to this line:

require('electron').remote.require('electron-app-settings');

if i comment it out and just make sure that the main process has an instance of settings initialized, this error goes away and behavior is as expected.

does this qualify as a bug, or should i be trying to mess with webpack in order for this to be dynamically requirable?

Renderer doesn't "unregister" on close

When a renderer closes it doesn't unregister for events, this causes a "Object is distroyed" error in the main process if another renderer then tries to get or set settings.

I have worked around this by setting listeners as array and then creating a remove event. e.g
Main.js
let listeners = []; // ---- Register any listeners that request ipcMain.on('configurator-add-listener', (event) => { let listener = event.sender listeners.push(listener) // -- Send our settings over to the listener listener.send('configurator-master-set', {key: main_settings._storage}); }); // ---- Remove any listeners that request ipcMain.on('configurator-remove-listener', (event) => { let listener = event.sender listeners = listeners.filter(item => item !== listener) }); // -- Set our *set master events to listeners main_settings.on('set', (arg) => { listeners.forEach((listener)=>{ listener.send('configurator-master-set', arg); }) }); main_settings.on('unset', (arg) => { listeners.forEach((listener)=>{ listener.send('configurator-master-unset', arg); }) });

and then in Renderer,js
window.addEventListener('beforeunload', ()=>{ ipcRenderer.send('configurator-remove-listener'); return })

settings.get(key) return null when called to quick in render

My render calls settings.get(key) on window.DOMContentLoaded but it appears at this point the the render has not received the storage item via ipc channel 'configurator-master-set' yet and hence returns null even though the key exists in settings.

I solved this by waiting for ipcRenderer.on('configurator-master-set',()=>{ }); before calling settings.get(key) but perhaps settings.get() could wait for the initial set before it returns any values?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.