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 / whenready.js
Last active Aug 29, 2015
WhenReady object. Fires callback when all conditions are satisfied.
View whenready.js
//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') {
@ccnokes
ccnokes / poller.js
Last active Aug 29, 2015
Poller object
View poller.js
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) {
View valueOf.js
//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() {
@ccnokes
ccnokes / store.js
Created Sep 17, 2016
Example "store" for user data in an Electron app
View store.js
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
@ccnokes
ccnokes / app-with-storage.js
Created Sep 17, 2016
Sample electron app demonstrating how to save user data to a file.
View app-with-storage.js
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: {
@ccnokes
ccnokes / async-series.js
Last active Oct 6, 2016
Async series with reduce
View async-series.js
function asyncThing() {
return new Promise(res => {
setTimeout(() => {
res(Math.random());
}, 1000);
});
}
function series(...promises) {
return promises.reduce((p, fn) => p.then(fn), Promise.resolve());
View stateful-module.js
let count = 0;
module.exports = {
get count() {
return count;
}
increment() {
return count++;
}
};
@ccnokes
ccnokes / works-in-main-or-render.js
Last active Oct 27, 2016
Basic pattern for using an main process only API in either the main or renderer in Electron
View works-in-main-or-render.js
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);
View work.js
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++;
}
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;
You can’t perform that action at this time.