Skip to content

Instantly share code, notes, and snippets.

View aackerman's full-sized avatar
💻
Working

Aaron Ackerman aackerman

💻
Working
View GitHub Profile
@aackerman
aackerman / reset_webpack.js
Last active August 29, 2015 14:27
Resetting the webpack module cache
// Necessary for the karma-webpack alternative use-case https://github.com/webpack/karma-webpack#alternative-usage
//
// With the alternative usage setup your tests will be bundled with all other modules and tests.
// Each test will not have its own bundle of modules and tests have to handle cleaning up module state in between tests.
//
// This ends up being much better in the long run because each test can have a large number of deps and a single test
// may end up being very large and using a lot of memory and eventually cause OOM exceptions in the default usage setup.
//
// This solves that problem
var net = require('net');
var fs = require('fs');
var os = require('os');
var path = require('path');
var osExtras = require('./os_extras');
let defaultOptions = {
name: 'Application',
socketPath: 'application'
};
@aackerman
aackerman / application.js
Last active January 26, 2017 18:09
Preventing multiple electron application instances
var net = require('net');
var fs = require('fs');
var os = require('os');
var path = require('path');
// Create server to listen for additional application launches
function listenForNewProcesses(socketPath) {
// create a server for listening on the socket path
var server = net.createServer(function(connection) {
connection.on('data', function(data) {
@aackerman
aackerman / Usage.md
Last active August 29, 2015 14:23
Async timeout promise

For use in testing instead of a setTimeout callback.

it('will be called later', async function(done) {
  let now = Date.now();
  await delay(500);
  // timing will probably be off by a few milliseconds
  expect(Date.now() - now).toBeAbout(500);
  done();
});
@aackerman
aackerman / generate_random_string.js
Last active August 29, 2015 14:23
Generate random string in JavaScript
let defaultWhitelist = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
let generateRandomString = (len, whitelist = defaultWhitelist) => {
if (len <= 0) throw new Error('Must use a positive length');
let text = '';
while ( len-- ) {
text += whitelist.charAt(Math.floor(Math.random() * whitelist.length));
}
return text;
}
@aackerman
aackerman / jasmine_helpers.js
Last active August 29, 2015 14:23
Auto-unmounting React components in Jasmine tests
import React from 'react';
import {mountNodes} from 'testing/render_test_utils';
// setup afterEach method to unmount all mounted nodes
jasmine.getEnv().topSuite().afterEach({
fn: () => {
while ( mountNodes.length ) {
let mountNode = mountNodes.pop();
React.unmountComponentAtNode(mountNode);
}
@aackerman
aackerman / generate_backoff_time.js
Created June 18, 2015 00:52
Exponential backoff
const MAX_TIME = 30000;
export default (iteration, maxTime = MAX_TIME) => {
let time = Math.pow(2, iteration - 1) * 1000;
if (time > maxTime) { time = maxTime; }
return time;
};
@aackerman
aackerman / file.md
Last active October 19, 2015 14:30
Front-end application parts
  • Application specific components
  • Application specific business logic modules
  • Declarative Router
  • Global application events - logout, deauthorization, theme change, etc.
  • Stores/Collections/Caches - Objects in memory that are global application state
  • Backend/Storage/Persistence Abstraction - Rest adapter, Models, JSON serializers
  • Actions - socket events, taps, clicks, keyboard events all need to be able to perform the same action
  • Feature compilation abstraction - Dead code elimination, minification
  • Module compilation abstraction - Webpack, Browserify, Native ES6 modules
  • DOM and event binding abstraction - Ember, Angular, React
@aackerman
aackerman / inject.js
Last active April 3, 2018 08:37
Webpack dependency injection loader
var regex = /require\(([^\)]+)\)/g;
module.exports = function inject(src) {
this.cacheable();
return [
'module.exports = function inject(__injections__) {',
' var module = {exports: {}};',
' ' + src.replace(regex, '(__injections__[$1] || $&)'),
' return module.exports;',
'}'
@aackerman
aackerman / gotchas.txt
Last active August 29, 2015 14:15
Flux gotchas
* Dispatching undefined actions types
* FIX: Add a method on the dispatcher to check for undefined action types and throw errors when this happens
* Attempting to receive a dispatch with an undefined action type
* ex. switch on an undefined action type key
* FIX: Check action type for undefined and throw an error
* Fallthrough switch case in dispatcher callback
* FIX: Linting
* FIX: Use a method instead of a switch statement
* Naming actions
* FIX: Attempt to use a record type, singular and plural, and FIND, DESTROY, CREATE, UPDATE VERBS