Skip to content

Instantly share code, notes, and snippets.

@emilong
emilong / promise-tree.js
Created May 14, 2017 17:17
Return promises from a tree growing algorithm to get all the endpoints
// render the given branch, which is specified by its startingPoint, length, and angle
// If there are more branches to grow, grow randomly to the left and right
function growBranch({ startingPoint, length, angle, remainingBranches }) {
endingPoint = computeEndpoint({ startingPoint, length, angle });
renderBranch({ startingPoint, endingPoint });
const newRemainingBranches = remainingBranches - 1;
if (newRemainingBranches <= 0) {
// Return the endingPoint immediately: we know the end of this branch.
// We return this as a promise that can be resolved immediately so
@emilong
emilong / tree.js
Last active April 14, 2021 23:40
High-level outline of an asynchronous tree drawing algorithm
// render the given branch, which is specified by its startingPoint, length, and angle
// If there are more branches to grow, grow randomly to the left and right
function growBranch({ startingPoint, length, angle, remainingBranches }) {
endingPoint = computeEndpoint({ startingPoint, length, angle });
renderBranch({ startingPoint, endingPoint });
const newRemainingBranches = remainingBranches - 1;
if (newRemainingBranches <= 0) {
return;
}
// Link.react-test.js
import React from 'react';
import Link from '../Link.react';
import renderer from 'react-test-renderer';
test('Link changes the class when hovered', () => {
const component = renderer.create(
<Link page="http://www.facebook.com">Facebook</Link>
);
let tree = component.toJSON();
const colors = require('colors/safe');
const path = require('path');
const fs = require('fs');
function captureLogs(filename) {
const output = path.join(BROWSER_LOGS_DIR, filename);
console.log(colors.red.bold('📜 capturing logs to'), output);
return browser.manage().logs().get('browser').then(browserLog => {
fs.writeFileSync(output, JSON.stringify(browserLog));
});
const sanitize = require('sanitize-filename');
// Declared outside any describe() or context() block so that it's called after every test.
// this is bound by Mocha to let us have access to the current test state, so we make sure
// to avoid a fat-arrow function declaration here.
afterEach(function(){
const filename = `${sanitize(this.currentTest.title.replace(/\s+/g, '-'))}-${+new Date()}`;
if (this.currentTest.state === 'failed') {
#!/bin/bash
set -ex
google-chrome --version
wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | sudo apt-key add -
sudo sh -c 'echo "deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google.list'
sudo apt-get update
sudo apt-get --only-upgrade install google-chrome-stable
google-chrome --version
#!/bin/bash
echo "Waiting for servers to start..."
while true; do
curl -f http://localhost:8000/health > /dev/null 2> /dev/null
if [ $? = 0 ]; then
echo "Frontend started"
curl -f http://localhost:3000/health > /dev/null 2> /dev/null
if [ $? = 0 ]; then
dependencies:
cache_directories:
- client-cache
- service-cache
post:
- bash scripts/get-latest-chrome.sh
- bash scripts/run-client.sh:
background: true
const original = [0,1,2,3];
const copy = Object.assign([], original, { 2: 42 }); // [0,1,42,3]
console.log(original);
// [ 0, 1, 2, 3 ]
console.log(copy);
// [ 0, 1, 42, 3 ]
function WhySoManyProps(props) {
const user = extractUser(props);
const fudge = calculateFudge();
const bits = computeBits();
// This is soooooo redundant.
return <SomeComponent user={user} fudge={fudge} bits={bits} />;
}
function Shorthand(props) {