Skip to content

Instantly share code, notes, and snippets.

@bvaughn
bvaughn / updating-external-data-when-props-changes-using-promises.js
Last active July 20, 2023 16:00
Example for loading new external data in response to updated props
// This is an example of how to fetch external data in response to updated props,
// If you are using an async mechanism that does not support cancellation (e.g. a Promise).
class ExampleComponent extends React.Component {
_currentId = null;
state = {
externalData: null
};
@zerobias
zerobias / fs-extra.js
Last active April 30, 2018 14:38
Flow typings delaration for fs-extra
declare module 'fs-extra' {
import type {
createReadStream as createReadStreamType,
createWriteStream as createWriteStreamType,
} from 'fs'
declare export var createReadStream: $PropertyType<
$Exports<'fs'>,
'createReadStream',
>
@sebmarkbage
sebmarkbage / Infrastructure.js
Last active May 2, 2024 03:11
SynchronousAsync.js
let cache = new Map();
let pending = new Map();
function fetchTextSync(url) {
if (cache.has(url)) {
return cache.get(url);
}
if (pending.has(url)) {
throw pending.get(url);
}
@benadamstyles
benadamstyles / flow-utility-type-functions.js
Last active September 24, 2017 16:21
Flow Utility Type Functions
// Below are some useful Flow utility type functions, some I've written and some I've found on the web.
// All contributions welcome!
// Extracting a data type from a maybe type, e.g. getting `string` from `?string`
type _ExtractFromMaybe<T, M: ?T> = T
export type ExtractFromMaybe<M> = _ExtractFromMaybe<*, M>
// FROM https://hackernoon.com/redux-flow-type-getting-the-maximum-benefit-from-the-fewest-key-strokes-5c006c54ec87
// Extracting the type of a function's return value
type _ExtractReturn<R, F: (...args: any[]) => R> = R
@lackneets
lackneets / swipedirection.js
Created August 5, 2014 02:12
Detect left/right-swipe on touch-devices, but allow up/down-scrolling
// a modification from http://goo.gl/tLbLXr
var attachEvent = function(element, event, fn) {
if (element.addEventListener)
element.addEventListener(event, fn, false);
else if (element.attachEvent) // if IE
element.attachEvent('on' + event, fn);
}
var onReady = function(func) {
@staltz
staltz / introrx.md
Last active May 10, 2024 12:08
The introduction to Reactive Programming you've been missing
@bennadel
bennadel / code-1.htm
Created March 25, 2014 11:16
Using jQuery's Animate() Step Callback Function To Create Custom Animations
<!DOCTYPE HTML>
<html>
<head>
<title>jQuery Animate() Step Demo</title>
<script type="text/javascript" src="../jquery-1.4.1.js"></script>
<script type="text/javascript">
// When the DOM is ready, initialize.
jQuery(function( $ ){
@karlwhite
karlwhite / gist:6032693
Last active December 19, 2015 23:09
Sketch.js: Added screen buffer to improve performance on mobile (tested on iPad) Known issue: On iPad there is a slight, but noticeable, screen flash as it copies the canvas to the buffer. Usage: Simple pass {buffer:true} as an option when initializing, e.g. canvas.sketch({buffer:true});
var __slice = Array.prototype.slice;
(function($) {
var Sketch;
$.fn.sketch = function() {
var args, key, sketch;
key = arguments[0], args = 2 <= arguments.length ? __slice.call(arguments, 1) : [];
if (this.length > 1) {
$.error('Sketch.js can only be called on one element at a time.');
}
sketch = this.data('sketch');
@desandro
desandro / transition-scroll-to.js
Created December 4, 2012 16:50
Use CSS transitions to scroll to element
( function( window, undefined ) {
'use strict';
// helper function
function capitalize( str ) {
return str.charAt(0).toUpperCase() + str.slice(1);
}
// ========================= getStyleProperty by kangax ===============================
@leecrossley
leecrossley / shake.js
Last active December 28, 2021 10:56
Shake gesture detection in PhoneGap / Cordova
/*
THIS GIST IS OUT OF DATE AND NOT MONITORED
PLEASE SEE https://github.com/leecrossley/cordova-plugin-shake-detection
*/
var shake = (function () {
var shake = {},
watchId = null,
options = { frequency: 300 },
previousAcceleration = { x: null, y: null, z: null },