Skip to content

Instantly share code, notes, and snippets.

View jamsesso's full-sized avatar

Sam Jesso jamsesso

  • Replicant
  • Fredericton, Canada
View GitHub Profile
type Node<T> = {
next: Node<T>;
prev: Node<T>;
key: number;
value: T;
};
class LRUCache {
private capacity: number;
private size: number = 0;
import {useRef, useState, useCallback, useEffect} from 'react';
function useStateThen(initialValue) {
const p = useRef(null);
const [value, setValue] = useState(initialValue);
const wrappedSetValue = useCallback((...args) => {
return new Promise(resolve => {
p.current = resolve;
setValue(...args);
});
@jamsesso
jamsesso / ApiGatewayServer.java
Created February 26, 2020 13:31
Thin lambda dev server
import com.amazonaws.services.lambda.runtime.Context;
import io.javalin.Javalin;
import io.javalin.http.HandlerType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Map;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
@jamsesso
jamsesso / example.js
Created December 9, 2018 15:38
Managing component state in the URL with react-router
import React from 'react';
import withUrlState from './with-url-state';
function Example({urlState, setUrlState}) {
const {count} = urlState;
return <button onClick={() => setUrlState({count: count + 1})}>{count}</button>;
}
export default withUrlState({count: 0})(Example);
import React, { Component, PropTypes } from 'react'
import { render } from 'react-dom'
import withContext from './with-context'
import thread from './thread'
class Parent extends Component {
static childContextTypes = {
someContext1: PropTypes.object,
someContext2: PropTypes.object
}
import thread from './thread'
const addOne = x => x + 1;
const double = x => x * 2;
console.log(thread(10, double, addOne))
// -> 21 (same as addOne(double(10)))
function thread(arg, ...funcs) {
return funcs.reduce((prev, func) => func(prev), arg)
}
export default thread
import React, { Component, PropTypes } from 'react'
import { render } from 'react-dom'
import withContext from './with-context'
class Parent extends Component {
static childContextTypes = {
someContext1: PropTypes.object,
someContext2: PropTypes.object
}
import React, { Component } from 'react'
function withContext(key, type) {
return WrappedComponent => class ContextComponent extends Component {
static contextTypes = {
...WrappedComponent.contextTypes,
[key]: type
}
render() {
import React, { Component, PropTypes } from 'react'
import { render } from 'react-dom'
class Parent extends Component {
static childContextTypes = {
someContext1: PropTypes.object,
someContext2: PropTypes.object
}
render() {