Skip to content

Instantly share code, notes, and snippets.

💭
Copying and pasting from StackOverflow

Cameron Nokes ccnokes

💭
Copying and pasting from StackOverflow
Block or report user

Report or block ccnokes

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@ccnokes
ccnokes / preload-example.js
Created Feb 1, 2017
Electron preload script
View preload-example.js
// 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.
View hybrid-renderer.js
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 Sep 13, 2019
Online/offline event observable with RxJS
View rx-online-offline.js
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 Feb 20, 2017
Online/offline emitter in vanilla JS
View vanilla-online-offline.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));
};
View rxjs-online-offline-composed.js
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 Feb 22, 2017
Function that sums objects of the same shape
View sum-objs.js
// 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
Pipe function, taken from twitter
View pipe.js
// 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 Dec 15, 2017
A map that lazily gets and caches values on access
View lazy-container.js
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 Jul 5, 2018
Wrap a lodash method in a TS decorator
View make-lodash-decorator.js
//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
script for detecting DOM method calls that are know to cause a reflow
View detect-reflow.js
// 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',
You can’t perform that action at this time.