Last active
October 27, 2017 14:47
-
-
Save hackjutsu/f68b2b101519a48596822db67e15d5a7 to your computer and use it in GitHub Desktop.
[How to store user data in Electron] (https://goo.gl/FPbs8z) #tags: electron, lepton
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // vim: syntax=javascript | |
| const { app, BrowserWindow } = require('electron'); | |
| const path = require('path'); | |
| const Store = require('./store.js'); | |
| let mainWindow; //do this so that the window object doesn't get GC'd | |
| // First instantiate the class | |
| const store = new Store({ | |
| // We'll call our data file 'user-preferences' | |
| configName: 'user-preferences', | |
| defaults: { | |
| // 800x600 is the default size of our window | |
| windowBounds: { width: 800, height: 600 } | |
| } | |
| }); | |
| // When our app is ready, we'll create our BrowserWindow | |
| app.on('ready', function() { | |
| // First we'll get our height and width. This will be the defaults if there wasn't anything saved | |
| let { width, height } = store.get('windowBounds'); | |
| // Pass those values in to the BrowserWindow options | |
| mainWindow = new BrowserWindow({ width, height }); | |
| // The BrowserWindow class extends the node.js core EventEmitter class, so we use that API | |
| // to listen to events on the BrowserWindow. The resize event is emitted when the window size changes. | |
| mainWindow.on('resize', () => { | |
| // The event doesn't pass us the window size, so we call the `getBounds` method which returns an object with | |
| // the height, width, and x and y coordinates. | |
| let { width, height } = mainWindow.getBounds(); | |
| // Now that we have them, save them using the `set` method. | |
| store.set('windowBounds', { width, height }); | |
| }); | |
| mainWindow.loadURL('file://' + path.join(__dirname, 'index.html')); | |
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const electron = require('electron'); | |
| const path = require('path'); | |
| const fs = require('fs'); | |
| class Store { | |
| constructor(opts) { | |
| // Renderer process has to get `app` module via `remote`, whereas the main process can get it directly | |
| // app.getPath('userData') will return a string of the user's app data directory path. | |
| const userDataPath = (electron.app || electron.remote.app).getPath('userData'); | |
| // We'll use the `configName` property to set the file name and path.join to bring it all together as a string | |
| this.path = path.join(userDataPath, opts.configName + '.json'); | |
| this.data = parseDataFile(this.path, opts.defaults); | |
| } | |
| // This will just return the property on the `data` object | |
| get(key) { | |
| return this.data[key]; | |
| } | |
| // ...and this will set it | |
| set(key, val) { | |
| this.data[key] = val; | |
| // Wait, I thought using the node.js' synchronous APIs was bad form? | |
| // We're not writing a server so there's not nearly the same IO demand on the process | |
| // Also if we used an async API and our app was quit before the asynchronous write had a chance to complete, | |
| // we might lose that data. Note that in a real app, we would try/catch this. | |
| fs.writeFileSync(this.path, JSON.stringify(this.data)); | |
| } | |
| } | |
| function parseDataFile(filePath, defaults) { | |
| // We'll try/catch it in case the file doesn't exist yet, which will be the case on the first application run. | |
| // `fs.readFileSync` will return a JSON string which we then parse into a Javascript object | |
| try { | |
| return JSON.parse(fs.readFileSync(filePath)); | |
| } catch(error) { | |
| // if there was some kind of error, return the passed in defaults instead. | |
| return defaults; | |
| } | |
| } | |
| // expose the class | |
| module.exports = Store; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment