Skip to content

Instantly share code, notes, and snippets.

View ededejr's full-sized avatar

Edede Oiwoh ededejr

View GitHub Profile
@ededejr
ededejr / useWaveform.tsx
Created January 25, 2022 02:06
Generate a waveform on a Canvas element, by passing in a url. Extracted from an early draft of de's personal site.
import React, { useEffect, useRef, useState } from "react";
import useAudioContext from "./useAudioContext";
import { AppColor, de } from "../../stylesheet";
import { useStylesheet } from "../../stylesheet/hooks";
const Canvas = de.createMotion.canvas`
width: 100%;
height: 100%;
`;
@ededejr
ededejr / print-interface.go
Last active February 6, 2024 05:22
Pretty print an interface in golang
package main
import (
"encoding/json"
"fmt"
)
func PrintInterface(i interface{}) error {
b, err := json.MarshalIndent(i, "", " ")
if err != nil {
@ededejr
ededejr / ticker.ts
Last active February 6, 2024 05:22
a simple non-blocking wrapper around setTimeout
interface TickerConfig {
fn: () => Promise<void>;
intervalMs?: number;
firstRunDelay?: number;
}
class Ticker {
private fn: () => Promise<void>;
private timer?: NodeJS.Timeout;
@ededejr
ededejr / on-process-exit.ts
Last active February 6, 2024 05:21
a utility function for executing callbacks on process exit in nodejs
function onProcessExit(callback: () => void): void {
[
"SIGINT",
"SIGTERM",
"SIGHUP",
"SIGBREAK",
"unhandledRejection",
"uncaughtException",
].forEach((signal: string) => {
process.on(signal, () => {
@ededejr
ededejr / debug-with-context.ts
Created June 13, 2023 21:18
simple debug function which logs its caller
const debug = (...messages: unknown[]) => {
  const { stack } = new Error("");
  const caller = stack?.split("\n")?.[2].trim().split(" ")[1];
  console.log(`[ededejr|${caller}]: `, ...messages);
};
@ededejr
ededejr / use-relative-timestamp.tsx
Created December 1, 2023 17:19
a react hook for formatting timestamps relative to now
function useRelativeTimestamp(ts: number) {
const now = Date.now();
let displayTime = "";
const diff = now - ts;
const timestamp = new Date(ts);
if (diff <= 1000 * 60 * 60) {
const formatter = new Intl.RelativeTimeFormat("en", { numeric: "auto" });
@ededejr
ededejr / responsive-dialog.tsx
Last active December 22, 2023 21:15
quick hack for a responsive dialog-drawer built on components from ui.shadcn.com
import * as React from 'react';
import { useMediaQuery } from '@/lib/hooks';
import * as DialogParts from '@/ui/dialog';
import * as DrawerParts from '@/ui/drawer';
type ResponsiveDialogPartsKey =
| ''
| 'Portal'
| 'Overlay'
@ededejr
ededejr / cacheable.ts
Created March 25, 2024 03:32
an extensible class which provides caching methods
import TTLCache, { Options } from '@isaacs/ttlcache';
export type CacheOptions<K = string, V = unknown> = Options<K, V>;
export class Cacheable<K = string, V = unknown> {
protected cache: TTLCache<K, V>;
constructor(options: Options<K, V> = { ttl: 1000 * 60 * 10 }) {
this.cache = new TTLCache(options);
}
@ededejr
ededejr / waitgroup.py
Last active April 10, 2024 18:52
a simple wait group written before discovering asyncio.TaskGroup
import asyncio
import logging
from typing import Any, Callable, Coroutine, List, Optional
logger = logging.getLogger(__name__)
class Waitgroup:
def __init__(self, timeout: Optional[float] = None):
self.timeout = timeout
@ededejr
ededejr / sh.ts
Created May 19, 2024 23:45
small nodejs sh wrapper
import { ExecSyncOptions, execSync } from 'child_process';
export function sh(command: TemplateStringsArray, ...substitutions: any[]) {
const fullCommand = command.reduce((acc, part, i) => acc + part + (substitutions[i] || ''), '');
return (options?: ExecSyncOptions) => execSync(fullCommand, { encoding: 'utf-8', ...(options || {}) });
}