This file contains hidden or 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'; | |
| import { type Duration } from 'dayjs/plugin/duration'; | |
| import { throttle } from 'lodash-es'; | |
| const events: Array<keyof DocumentEventMap> = [ | |
| 'mousemove', | |
| 'keydown', | |
| 'wheel', | |
| 'mousedown', | |
| 'touchstart', |
This file contains hidden or 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 { useSyncExternalStore, useMemo } from 'react'; | |
| export const useDocumentVisibility = (): boolean => { | |
| const subscribe = useMemo(() => { | |
| return (callback: () => void) => { | |
| document.addEventListener('visibilitychange', callback); | |
| return () => document.removeEventListener('visibilitychange', callback); | |
| }; | |
| }, []); |
This file contains hidden or 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 { 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); |
This file contains hidden or 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 { 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) => { |
This file contains hidden or 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 { 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; | |
| } |
This file contains hidden or 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 { 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; | |
| }; |
This file contains hidden or 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
| 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"} | |
| ]) |
This file contains hidden or 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
| 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]) |
This file contains hidden or 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
| Mix.install([:dataloader, :absinthe]) | |
| defmodule User do | |
| defstruct [:id, :name] | |
| def get(id) do | |
| Enum.find(list(), &(&1.id == id)) | |
| end | |
| def list do |
This file contains hidden or 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
| 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"} |
NewerOlder