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 { Observable } = require('rxjs/Observable'); | |
| require('rxjs/add/observable/fromEvent'); | |
| require('rxjs/add/operator/map'); | |
| require('rxjs/add/observable/merge'); | |
| function createOnline$() { | |
| //merge several events into one | |
| return Observable.merge( | |
| //use .map() to transform the returned Event type into a true/false value | |
| Observable.fromEvent(window, 'offline').map(() => false), |
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 $webview = document.querySelector('webview'); | |
| const $loader = document.querySelector('.loader'); | |
| let isInitialLoad = true; | |
| $webview.addEventListener('did-start-loading', () => { | |
| // we use client side rendering in the web app, so the loader is only needed on the first page load | |
| if(isInitialLoad) { | |
| $webview.classList.add('hide'); | |
| $loader.classList.remove('loader-hide'); | |
| isInitialLoad = false; |
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
| // in preload scripts, we have access to node.js and electron APIs | |
| // the remote web app will not have access, so this is safe | |
| const { ipcRenderer: ipc, remote } = require('electron'); | |
| init(); | |
| function init() { | |
| // Expose a bridging API to by setting an global on `window`. | |
| // We'll add methods to it here first, and when the remote web app loads, | |
| // it'll add some additional methods as well. |
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 crypto = require('crypto'); | |
| // this usually takes a few seconds | |
| function work(limit = 100000) { | |
| let start = Date.now(); | |
| n = 0; | |
| while(n < limit) { | |
| crypto.randomBytes(2048); | |
| n++; | |
| } |
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
| // This works in the either the main or renderer processes. | |
| const { requireTaskPool } = require('electron-remote'); | |
| const work = requireTaskPool(require.resolve('./work')); | |
| console.log('start work'); | |
| // `work` will get executed concurrently in separate processes | |
| work().then(result => { |
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 Menu = electron.Menu || electron.remote.Menu; | |
| //now you can use it seamlessly in either main or renderer | |
| console.log(Menu); |
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
| let count = 0; | |
| module.exports = { | |
| get count() { | |
| return count; | |
| } | |
| increment() { | |
| return count++; | |
| } | |
| }; |
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
| function asyncThing() { | |
| return new Promise(res => { | |
| setTimeout(() => { | |
| res(Math.random()); | |
| }, 1000); | |
| }); | |
| } | |
| function series(...promises) { | |
| return promises.reduce((p, fn) => p.then(fn), Promise.resolve()); |
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 { 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: { |
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 |