Skip to content

Instantly share code, notes, and snippets.

View Wigny's full-sized avatar

Wígny Almeida Wigny

View GitHub Profile
import { useEffect, useState } from 'react';
import { type Duration } from 'dayjs/plugin/duration';
import { throttle } from 'lodash-es';
const events: Array<keyof DocumentEventMap> = [
'mousemove',
'keydown',
'wheel',
'mousedown',
'touchstart',
import { useSyncExternalStore, useMemo } from 'react';
export const useDocumentVisibility = (): boolean => {
const subscribe = useMemo(() => {
return (callback: () => void) => {
document.addEventListener('visibilitychange', callback);
return () => document.removeEventListener('visibilitychange', callback);
};
}, []);
import { useCallback, useSyncExternalStore } from 'react';
export function useLocalStorage<T>(key: string): [T | null, (value: T | null) => void] {
const onValueChange = useCallback(
(callback: () => void) => {
const handler = (event: StorageEvent) => {
if (event.storageArea === window.localStorage && event.key === key) callback();
};
window.addEventListener('storage', handler);
import { useLayoutEffect } from 'react';
import { duration } from 'dayjs';
import { type Duration } from 'dayjs/plugin/duration';
export function useAnimationFrame(callback: (deltaTime: Duration) => void): void {
useLayoutEffect(() => {
let requestID: number;
let previousTime = 0;
const tick = (time: number) => {
import { useSyncExternalStore } from 'react';
function onHashChange(callback: () => void) {
window.addEventListener('hashchange', callback);
return () => window.removeEventListener('hashchange', callback);
}
function getHash() {
return window.location.hash.slice(1) || null;
}
import { useCallback, useRef } from 'react';
import { duration } from 'dayjs';
import { type Duration } from 'dayjs/plugin/duration';
type Timer = {
start: () => void;
pause: () => Duration;
resume: () => void;
reset: () => void;
};
Mix.install([
{:bandit, "~> 1.6"},
{:phoenix, "~> 1.7.0"},
{:phoenix_live_view, "~> 1.0.10"},
{:phoenix_ecto, "~> 4.6"},
{:ecto_sql, "~> 3.12"},
{:postgrex, ">= 0.0.0"},
{:testcontainers, "~> 1.12"}
])
defmodule TermStorage do
use Task
def start_link(args \\ []) do
Task.start_link(fn ->
table_name = Keyword.get(args, :table_name, __MODULE__)
{:ok, _table} = :dets.open_file(table_name, auto_save: to_timeout(second: 1))
Process.hibernate(Function, :identity, [nil])
@Wigny
Wigny / example.exs
Created February 8, 2025 10:56
Absinthe dataloader usage example
Mix.install([:dataloader, :absinthe])
defmodule User do
defstruct [:id, :name]
def get(id) do
Enum.find(list(), &(&1.id == id))
end
def list do
@Wigny
Wigny / example.exs
Created February 8, 2025 08:38
Dataloader usage example
Mix.install([:dataloader])
defmodule User do
defstruct [:id, :name]
def list do
[
%__MODULE__{id: 1, name: "John"},
%__MODULE__{id: 2, name: "Alice"},
%__MODULE__{id: 3, name: "Bob"}