Skip to content

Instantly share code, notes, and snippets.

View ccnokes's full-sized avatar

Cameron Nokes ccnokes

View GitHub Profile
@ccnokes
ccnokes / preload-example.js
Created February 1, 2017 03:03
Electron preload script
// 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.
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;
@ccnokes
ccnokes / rx-online-offline.js
Last active October 11, 2022 14:18
Online/offline event observable with RxJS (see comments below for a better, more up-to-date way of doing this)
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),
@ccnokes
ccnokes / vanilla-online-offline.js
Last active September 22, 2020 02:35
Online/offline emitter in vanilla JS
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));
};
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
@ccnokes
ccnokes / sum-objs.js
Created February 22, 2017 22:22
Function that sums objects of the same shape
// 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
@ccnokes
ccnokes / pipe.js
Last active May 21, 2017 02:06
Pipe function, taken from twitter
// 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
@ccnokes
ccnokes / lazy-container.js
Last active December 15, 2017 14:11
A map that lazily gets and caches values on access
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();
@ccnokes
ccnokes / make-lodash-decorator.js
Last active July 5, 2018 09:17
Wrap a lodash method in a TS decorator
//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;
@ccnokes
ccnokes / detect-reflow.js
Created May 27, 2017 05:34
script for detecting DOM method calls that are know to cause a reflow
// 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',