This file contains 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 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 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 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 createOnlineEmitter() { | |
let cbs = []; //array of registered callbacks for the event | |
let unsub; //function for removing the main event listener | |
//this is the main event listener that gets registered with window.online/offline event | |
const mainListener = (isOnline) => { | |
//call all the subscribed callbacks | |
cbs.forEach(cb => cb(isOnline)); | |
}; |
This file contains 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/operator/filter'); | |
require('rxjs/add/operator/switchMap'); | |
require('rxjs/add/operator/take'); | |
require('rxjs/add/operator/toPromise'); | |
const axios = require('axios'); | |
const online$ = createOnline$(); | |
//only make the network request when we're online |
This file contains 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
// objs[] *must* be the same shape | |
// returns new object with summation of all properties | |
function sumObjs(objs) { | |
const keys = Object.keys(objs[0]); | |
// initialize return object with 0s | |
const ret = keys.reduce((aggr, k) => { | |
aggr[k] = 0; | |
return aggr; | |
}, {}); | |
// sum each property |
This file contains 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
// sync version | |
const pipe = (...fns) => x => fns.reduce((v, f) => f(v), x); | |
// example | |
const newFunc = pipe(fn1, fn2, fn3); | |
const result = newFunc(arg); | |
// async version | |
// take a series of promise producing functions and return a single promise |
This file contains 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 makeLazy(name, fn, obj = {}) { | |
const nullSym = Symbol('nil'); //could change this to be more es5 friendly | |
let val = nullSym; //I'm not sure if holding the value in this closure or right on the object is better | |
Object.defineProperty(obj, name, { | |
enumerable: true, | |
get() { | |
if(val === nullSym) { | |
val = fn(); |
This file contains 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
//wrap a fn that returns a function into a decorator | |
function makeFnWrapDecorator(fnWrapper: Function) { | |
return function decoratorWrapper(...args) { | |
return function decorator(target, propertyKey: string, descriptor: PropertyDescriptor) { | |
const fn = descriptor.value; | |
let wrappedFn = fnWrapper.apply(null, [fn, ...args]); | |
return { | |
configurable: true, | |
get() { | |
return wrappedFn; |
This file contains 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
// These methods and getter/setters force layout/reflow in Chrome/WebKit | |
// From https://gist.github.com/paulirish/5d52fb081b3570c81e3a | |
const getterSetters = [ | |
'offsetLeft', | |
'offsetTop', | |
'offsetWidth', | |
'offsetHeight', | |
'offsetParent', | |
'clientLeft', |