Skip to content

Instantly share code, notes, and snippets.

View slmyers's full-sized avatar
💯
crushing code

Steven Myers slmyers

💯
crushing code
View GitHub Profile
@slmyers
slmyers / example.js
Last active May 8, 2020 19:19
common promise mishaps
// bad you're waiting for the first result but the second and third request don't need anything from the NAME query
// sequential async actions :do_not_want:
async fucntion waterfall(db) {
const name = await db.query(NAME)
const open = await db.query(IS_OPEN)
const welcomeMessage = await db.query(WELCOME_MESSAGE)
return { name, open, welcomeMessage }
}
// better there are no depenecies so start them all off at the same time
@slmyers
slmyers / game_1.js
Last active May 8, 2020 14:58
exchange js gists 5/7/2020, for a game
// adds a function to the microtask queue, will not print anything this tick
Promise.resolve().then(() => console.log(2))
// this will execute on the current phase of the event loop
console.log(1)
@slmyers
slmyers / gist:fc3ae96093719c2f66779f0574cc6555
Created February 23, 2020 01:32 — forked from andlima/gist:1774060
Median of medians selection algorithm
int find_kth(int *v, int n, int k) {
if (n == 1 && k == 0) return v[0];
int m = (n + 4)/5;
int *medians = new int[m];
for (int i=0; i<m; i++) {
if (5*i + 4 < n) {
int *w = v + 5*i;
for (int j0=0; j0<3; j0++) {
int jmin = j0;
@slmyers
slmyers / README.md
Created January 30, 2020 15:33 — forked from nikcub/README.md
Facebook PHP Source Code from August 2007
@slmyers
slmyers / schema.spec.js
Created January 22, 2020 18:05
how to mock graphql context in a test
// in src/schema.spec.js
const { graphql } = require('graphql');
const schema = require('./schema');
it('responds to the Tweets query', () => {
// stubs
const queryStub = q => {
if (q == 'SELECT * from tweets') {
return Promise.resolve({ rows: [
{ id: 1, body: 'Lorem Ipsum', date: new Date(), author_id: 10 },
@slmyers
slmyers / DateRangePicker.jsx
Created January 22, 2020 16:47 — forked from Skitionek/DateRangePicker.jsx
Range select for @mui-org/material-ui-pickers
import React, { useContext, useRef, useState } from 'react'
import { DatePicker, MuiPickersContext } from '@material-ui/pickers'
import withStyles from '@material-ui/core/styles/withStyles'
import clsx from "clsx";
import { useStyles as dayStyles } from '@material-ui/pickers/views/Calendar/Day';
function DateRangePicker({
classes,
value,
onChange,
@slmyers
slmyers / schema.graphql
Last active July 27, 2019 19:04
lesson0 graphql schema
# this file was autogenerated by updateSchema.js
# yarn update:schema
type Mutation {
dummy: Boolean
}
"""An object with an ID"""
interface Node {
@slmyers
slmyers / index.md
Created April 1, 2019 14:59
State tracking for conditional rendering in form anti-pattern

A common situation is rendering a form input iff some condition has been met. As an example, a user selects other from a gender dropdown and a text input is rendered so they can specify.

// how we commonly address this use-case
class Example extends React.Component {
  state = { gender: null } //tracking form state in component
  onGenderChange = e => this.setState({ gender: e.target.value }) //updating form state outside of form
  render() {
    const { gender } = this.state
@slmyers
slmyers / server.js
Created August 30, 2018 01:16
Example of running https server with node.
const http = require('http');
const https = require('https');
const fs = require('fs');
const App = require('./App');
const httpsOpts = {
key: fs.readFileSync("../certs/key.pem", "utf8"),
cert: fs.readFileSync("../certs/server.crt", "utf8")
};
http.createServer(App).listen(3000, () => console.log("The http server is listening on port 3000."));
@slmyers
slmyers / IncomingMessages.js
Last active August 12, 2018 03:11
Apollo client link example
import React, { Component } from "react"
import logo from '../logo.svg';
import ListItem from '@material-ui/core/ListItem';
import ListItemText from '@material-ui/core/ListItemText';
import Avatar from '@material-ui/core/Avatar';
import { GET_MESSAGES } from "../GraphQL"
import { Query } from 'react-apollo';
export default class extends Component {
render(){