This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
async fn time_async<F, O>(f: F) -> (O, Duration) | |
where | |
F: std::future::Future<Output = O>, | |
{ | |
let start = std::time::Instant::now(); | |
let out = f.await; | |
let duration = start.elapsed(); | |
(out, duration) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { useState, useRef, useCallback } from "react"; | |
const useCycle = <T>(...values: T[]): [T, (idx?: number) => void] => { | |
const index = useRef<number>(0); | |
const [value, setValue] = useState<T>(values[index.current]); | |
const cycle = useCallback((idx?: number) => { | |
index.current = typeof idx !== "number" | |
? (index.current + 1) % values.length | |
: idx; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { useState, useRef, useCallback } from "react"; | |
/** | |
* Like `useCycle`, but more performant and guarantees a stable function reference | |
* for the lifetime of the hook. Only safe when the cycle options do not change. | |
*/ | |
const useCycle = <T>(values: T[], initialState?: T): [T, () => void] => { | |
const valuesRef = useRef<T[]>(values); | |
const [index, setIndex] = useState<number>(() => { | |
const idx = initialState ? values.indexOf(initialState) : 0; | |
if (idx === -1) return 0; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { useEffect, useState } from "react"; | |
function useElementSize(ref: React.RefObject<HTMLElement>) { | |
const [size, setSize] = useState<DOMRectReadOnly>(new DOMRectReadOnly()); | |
useEffect(() => { | |
if (ref.current && !!window?.ResizeObserver) { | |
const observer = new ResizeObserver(([entry]) => { | |
setSize(entry.contentRect); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from "react"; | |
export type ElementVisibility = { | |
isVisible: boolean; | |
wasVisible: boolean; | |
}; | |
const useElementVisibility = ( | |
ref?: React.RefObject<HTMLElement>, | |
options?: IntersectionObserverInit, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { useEffect, EffectCallback } from "react"; | |
const useEffectOnce = (effect: EffectCallback | (() => Promise<void>)) => { | |
useEffect(() => { | |
const res = effect(); | |
if (!(res instanceof Promise)) { | |
return res; | |
} | |
// eslint-disable-next-line react-hooks/exhaustive-deps | |
}, []); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { useRef, useEffect, useCallback } from "react"; | |
const useDragScroll = <E extends HTMLElement = HTMLElement>() => { | |
const ref = useRef<E | null>(null); | |
const startX = useRef<number>(0); | |
const startY = useRef<number>(0); | |
const scrollLeft = useRef<number>(0); | |
const scrollTop = useRef<number>(0); | |
const onMouseMove = useCallback((e: MouseEvent) => { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Interview question of the week - 2021-5-3 | |
* Given a positive or negative real number, round it to the next whole integer closer to zero. | |
* This means if it’s positive, round down, and if it’s negative, round up. | |
* Try to do this in as few characters possible! | |
* | |
* Test cases: | |
* | |
* 1.7 => 1 | |
* -2.1 => -2 | |
* 500.4 => 500 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[ | |
{ "key": "ctrl+k a", "command": "macros.minimalMode" } | |
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type DocumentState = { stale: string; position: number; errors: string[] }; | |
type ValidationState = { valid: boolean; errors?: string[] }; | |
type Operation = "insert" | "delete" | "skip"; | |
interface OperationalTransform { | |
op: Operation; | |
count?: number; | |
chars?: string; | |
} | |
type TransformApplication = ( |
NewerOlder