Skip to content

Instantly share code, notes, and snippets.

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
View render-component-to-print-html.jsx
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' }) => (`
moodysalem / react-portal.jsx
Last active November 10, 2016 21:02
Render children to a div tag at the end of the body
View react-portal.jsx
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 / 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
View queuePromises.jsx
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 / promise-cancellable.js
Last active April 11, 2019 01:32
ES6 Cancellable Promise Wrapper
View promise-cancellable.js
* 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 / rAF.js
Last active May 7, 2023 13:52
requestAnimationFrame polyfill
View rAF.js
// 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'];