Skip to content

Instantly share code, notes, and snippets.

@moodysalem
moodysalem / render-component-to-print-html.jsx
Created November 22, 2016 16:09
Render a react component to an html document that shares all the links and styles for printing, and convert it to a data URI for window.open
import React, { PureComponent, PropTypes } from 'react';
import { render, unmountComponentAtNode } from 'react-dom';
import { Base64 } from 'js-base64';
import Promise from 'bluebird';
const htmlDocTemplate = ({ links, pageStyles, body, title = 'Print' }) => (`
<html>
<head>
<title>${title}</title>
${links}
@moodysalem
moodysalem / react-portal.jsx
Last active November 10, 2016 21:02
Render children to a div tag at the end of the body
import React, { Children, PureComponent, PropTypes } from 'react';
import { unmountComponentAtNode, unstable_renderSubtreeIntoContainer } from 'react-dom';
// renders children at the end of the body
export default class Portal extends PureComponent {
static propTypes = {
children: PropTypes.node.isRequired
};
componentDidMount() {
@moodysalem
moodysalem / queuePromises.jsx
Created November 7, 2016 21:11
JS function that calls a set of functions that generate promises (called generators) in batches and resolves when they have all completed
import _ from 'underscore';
/**
* Takes an array of generators, i.e. functions that return promises, and calls them such that there are only ever
* 5 requests that are waiting on responses. Returns a promise that resolves to all the resulting promises only after
* they have all been executed
*
* @param generators array of functions that
* @param batchSize max number of requests to execute at a time
* @param throttle how often the queue is checked for more requests to process
@moodysalem
moodysalem / promise-cancellable.js
Last active April 9, 2024 18:06
ES6 Cancellable Promise Wrapper
/**
* Returns a promise that has a cancelled method that will cause the callbacks not to fire when it resolves or rejects
* @param promise to wrap
* @returns new promise that will only resolve or reject if cancel is not called
*/
export default function cancellable(promise) {
var cancelled = false;
const toReturn = new Promise((resolve, reject) => {
promise.then(() => {
@paulirish
paulirish / rAF.js
Last active November 11, 2024 03:20
requestAnimationFrame polyfill
// http://paulirish.com/2011/requestanimationframe-for-smart-animating/
// http://my.opera.com/emoller/blog/2011/12/20/requestanimationframe-for-smart-er-animating
// requestAnimationFrame polyfill by Erik Möller. fixes from Paul Irish and Tino Zijdel
// MIT license
(function() {
var lastTime = 0;
var vendors = ['ms', 'moz', 'webkit', 'o'];