Skip to content

Instantly share code, notes, and snippets.

View tannerlinsley's full-sized avatar

Tanner Linsley tannerlinsley

View GitHub Profile

createCachedFn function wraps any function fn in a cache that is scoped to the current “event” (via getEvent()), ensuring repeat calls with the same arguments during that event return cached results instead of recomputing.

Summary: • Caches function results per event. • Uses fn.toString() and JSON.stringify(args) as cache keys. • Stores cache in a per-event __cachedStorage map. • Avoids recomputation for repeated calls with the same arguments during the same event.

It’s ideal for optimizing repeated pure function calls during a single request or lifecycle event.

This middleware does a few interesting things:

  • Ensures a url shape in the zustand store, where we'll store URL information.
  • Assumes we will be storing our url state slice in the ?state search parameter after it has been stringified and base 64 encoded.
  • On creation, decodes stores state from the ?state search parameter into the url slice of our store.
  • After each state update, updates the ?state search parameter with the new url state slice.
  • Sets up an event listener that listens for popstate and re-decodes the state from the URL into our store.
import * as React from 'react'
import * as Plot from '@observablehq/plot'
import useRect from '../hooks/useRect'
import { twMerge } from 'tailwind-merge'
import { useThemeMode } from '../utils/Theme'
import { data } from '../utils/DataColors'
export function ObservablePlot({
legend,
legendType = 'color',
// my-core
interface FrameworkGenerics<TData = unknown> {
// Options: unknown
}
type GetFrameworkGeneric<
U,
TData = unknown,
> = U extends keyof FrameworkGenerics<TData>
export function deepDiff<TObj extends object>(a: TObj, b: TObj) {
if (Array.isArray(a)) {
const changes = a
.map((item, index) => deepDiff(item, b[index]))
.filter(Boolean)
return changes.length ? changes : undefined
}
if (isObject(a)) {
import * as React from 'react'
let activeTimeout = null
let reactivateFn = null
export function useOnIdle(fn, time) {
React.useEffect(() => {
const onUsage = () => {
if (reactivateFn) {
reactivateFn()
@tannerlinsley
tannerlinsley / Table.tsx
Last active November 15, 2023 19:53
A quick snippet of an early ReactTable v8 table that renders!
import React from 'react'
import ReactDOM from 'react-dom'
import './index.css'
import { createTable } from 'react-table'
type Row = {
firstName: string
lastName: string
import { multiSortBy } from 'src/shared/utils'
export type PathNode<T> = {
id: string
path: string
parentId: string
meta: T
children?: PathNode<T>[]
}
{
"message": [
"Requesting backblaze auth..."
],
"level": "log",
"timestamp": 1638577536200
},
{
"message": [
"btoa error",
@tannerlinsley
tannerlinsley / useBroadcastLeader.ts
Created June 4, 2021 14:37
A React Hook to determine if a tab of your application is the "leader" using BroadcastChannel and leader election
import { BroadcastChannel, createLeaderElection } from 'broadcast-channel'
import React from 'react'
const channels = {}
export function useBroadcastLeader(id = 'default') {
const [isBroadcastLeader, setIsBroadcastLeader] = React.useState(false)
React.useEffect(() => {
if (!channels[id]) {