Skip to content

Instantly share code, notes, and snippets.

View craigmr's full-sized avatar

Craig Simpson craigmr

View GitHub Profile
@craigmr
craigmr / gist:5e83e12fb2e102c7b43f
Last active August 29, 2015 14:20
Example of Race Condition
/**
* calculateZ() is a simplified version of a race conidtion. We have two ansync tasks
* that modifiy the state before each call is complete.
*/
function calculateZ() {
let x = 0, y = 0, z;
//Async call to get the true value of y
setTimeout(() => y = 2, Math.random() * 1000);
@craigmr
craigmr / gist:c83320db1d230210b8b3
Last active August 29, 2015 14:20
Chained Async Calls to Prevent Race Conditions
/**
* calculateZChained() Chain the two aysnc calls together to fix the race condition.
*/
function calculateZChained() {
let x = 0, y = 0, z;
//Async call to get the true value of y
setTimeout(() => {
y = 2;
@craigmr
craigmr / gist:3cc34725cd82e81e3a4e
Last active August 29, 2015 14:20
Promise Async Calls to Prevent Race Conditions
/**
* calculatePromise() Use promises to get x and y values, now we can run our
* async tasks in parallel using Promise.all. Once we have both values
*/
function calculatePromise() {
var y = new Promise((resolve, reject) => {
setTimeout(() => resolve(2), Math.random() * 1000);
});
var x = new Promise((resolve, reject) => {
@craigmr
craigmr / breadcrumb.js
Last active August 29, 2015 14:21
How Functional Programing Can Avoid Broken State
(function nonFunctionalExample(){
this.breadCrumb = []; //<-- external state
this.node = {
name: "Child",
parent: {
name: "Parent"
}
};
@craigmr
craigmr / zip-poly-factory.js
Created May 28, 2015 18:24
How would you implement the following factory?
var zipCodeData = {
'30324': {
value: 10
//Etc.
}
};
var map = new google.maps.Map(document.getElementById('map-canvas'), {});
var zipPolys = createZipPolygons(map);
@craigmr
craigmr / VIBirst.js
Last active August 29, 2015 14:24
Sample way to configure product specific Birst api
angular.module('valueIntelligence.test', [])
.config(function(VIBirstProvider) {
VIBirstProvider.setConfig({
url: ''
});
});
angular.module('valueIntelligence.test').provider('VIBirst', function(){
var saConfig = {};
@craigmr
craigmr / using_concat.js
Created July 29, 2015 15:45
Reduce code to reduce bugs
//So writing out the arrays like this works but will lead to bugs b/c since allQueries is just conversionTotalsQueries and conversionTypeQueries
//combined you might forget to update one array. To prevent that just concat the two.
var allQueries = [conversionsQueryMonth, leadsQueryMonth, actionsQueryMonth, conversionsQueryDay, leadsQueryDay, actionsQueryDay];
var conversionTotalsQueries = [conversionsQueryMonth, conversionsQueryDay];
var conversionTypeQueries = [actionsQueryMonth, actionsQueryDay, leadsQueryMonth, leadsQueryDay];
//refactored to use less code and prevent bugs
var conversionTotalsQueries = [conversionsQueryMonth, conversionsQueryDay];
var conversionTypeQueries = [actionsQueryMonth, actionsQueryDay, leadsQueryMonth, leadsQueryDay];
var allQueries = conversionTotalsQueries.concat(conversionTypeQueries); //Returns new array so conversionTotalsQueries is not modified
@craigmr
craigmr / widget.html
Last active September 2, 2015 03:27
Captionable Widget Test
<!-- Start Captionable Widget Tag -->
<script>
var username = 'charliereid';
var width = 300;
var height = 250;
document.write('<iframe src="http://play.captionable.com/widget/'+ encodeURI(username) +'?w='+ width +'&h='+ height +'" width="'+ width +'" height="'+ height +'" marginwidth="0" marginheight="0" hspace="0" vspace="0" frameborder="0" scrolling="no" allowtransparency="true"></iframe>');
</script>
<!-- End Captionable Widget Tag -->
@craigmr
craigmr / widget-simple.html
Created September 8, 2015 00:51
Captionable Simple Widget
<div style="width:300px; height:250px; text-align: center;">
<a href="http://play.captionable.com/widget/charliereid/link" target="_blank" style="padding: 0;margin: 0;">
<img src="http://play.captionable.com/widget/charliereid/image" alt="Can you guess the caption?" height="250" width="250">
</a>
</div>
@craigmr
craigmr / angular-talk.md
Created October 21, 2015 01:48
Better Angular Apps using Directives as Components

Since the creation of advance framworks like React, Angular, Polymer, and others web development is evoluting by focusing on component based development for large SAPs. These components allow developers to create modular, single focused pieces of functionality that are easy to maintain and adapt. Browser vendors recongize the power of components and are working on supporting native web components. While support is growing for native componets, directives in Angular give developers the power to create web components today. Directives are the most misunderstood and mysterious feature of Angular, but contains the some of most power and functionality. This talk will show how moving your functionality away from ng-controller and into directives will provide the flexibilty and modularity needed for modern SAPs.

Craig Simpson is the lead frontend developer for PureCars in Atlanta and has been developing frontends for 10 years. He is a fan of both React and Angular, advocate for UI/UX, and believes TDD and continous