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 Poller(interval, cb, limit) { | |
var _this = this; | |
this.interval = interval || 250; | |
this.poll = null; | |
this.limit = limit; | |
var count = 0; | |
this.poll = setInterval(function() { | |
if(typeof cb == 'function') { | |
if(_this.limit) { |
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
//executes a callback when all conditions are satisfied | |
function WhenReady(obj, cb) { | |
this.conditions = obj; | |
this.readyCallback = cb; | |
} | |
WhenReady.prototype = { | |
satisfyCondition: function(o) { | |
if(typeof o == 'object') { |
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
//custom string manipulation class | |
class MyString { | |
constructor(str) { | |
this.str = str; | |
} | |
replaceVowels(replacement) { | |
this.str = this.str.replace(/a|e|i|o|u/gi, replacement); | |
} | |
//The valueOf() method returns the primitive value of the specified object. | |
valueOf() { |
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 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 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 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 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 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
// 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 |
OlderNewer