Skip to content

Instantly share code, notes, and snippets.

View ccnokes's full-sized avatar

Cameron Nokes ccnokes

View GitHub Profile
@ccnokes
ccnokes / poller.js
Last active August 29, 2015 14:03
Poller object
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) {
@ccnokes
ccnokes / whenready.js
Last active August 29, 2015 14:03
WhenReady object. Fires callback when all conditions are satisfied.
//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') {
//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 September 17, 2016 21:49
Example "store" for user data in an Electron app
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 September 17, 2016 22:14
Sample electron app demonstrating how to save user data to a file.
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 October 6, 2016 03:36
Async series with reduce
function asyncThing() {
return new Promise(res => {
setTimeout(() => {
res(Math.random());
}, 1000);
});
}
function series(...promises) {
return promises.reduce((p, fn) => p.then(fn), Promise.resolve());
@ccnokes
ccnokes / stateful-module.js
Created October 25, 2016 03:31
stateful module
let count = 0;
module.exports = {
get count() {
return count;
}
increment() {
return count++;
}
};
@ccnokes
ccnokes / works-in-main-or-render.js
Last active October 27, 2016 03:41
Basic pattern for using an main process only API in either the main or renderer in Electron
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 works in the either the main or renderer processes.
const { requireTaskPool } = require('electron-remote');
const work = requireTaskPool(require.resolve('./work'));
console.log('start work');
// `work` will get executed concurrently in separate processes
work().then(result => {
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++;
}