Skip to content

Instantly share code, notes, and snippets.

View Wigny's full-sized avatar

Wígny Almeida Wigny

View GitHub Profile
import { useCallback, useEffect, useMemo, useState } from 'react';
import dayjs, { Dayjs, duration } from 'dayjs';
import { Duration } from 'dayjs/plugin/duration';
type Status = 'idle' | 'running' | 'paused';
type TimerState = {
status: Status;
startedAt: Dayjs | null;
elapsed: Duration;
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"}
@Wigny
Wigny / mysql.ex
Last active February 5, 2025 13:42
MySQL Ecto returning
defmodule MySQL do
defmacro __using__(_opts \\ []) do
quote do
defoverridable insert_all: 3
def insert_all(schema_or_source, entries, opts) when is_list(entries) do
{returning, opts} = Keyword.pop(opts, :returning)
if returning do
import Ecto.Query
@Wigny
Wigny / queue.ex
Last active October 31, 2024 20:07
Elixir Queue data type
defmodule Queue do
defstruct value: :queue.new()
def new do
%Queue{}
end
def new(enumerable) do
list = Enum.to_list(enumerable)
%Queue{value: :queue.from_list(list)}
defmodule Test do
def str_to_int(str) do
Enum.reduce(String.to_charlist(str), 0, &(&1 - ?0 + &2 * 10))
end
end
Mix.install([
{:ecto_sql, "~> 3.11"},
{:myxql, "~> 0.6.4"},
{:testcontainers, "~> 1.8"}
])
alias Testcontainers.MySqlContainer
{:ok, _} = Testcontainers.start_link()
{:ok, container} = Testcontainers.start_container(MySqlContainer.with_image(MySqlContainer.new(), "mysql:8.4"))
defmacro delegate_functions_for(module, functions) do
for {fun_name, fun_arity} <- functions do
fun_args = Macro.generate_arguments(fun_arity, Macro.expand_literals(module, __CALLER__))
quote do
defdelegate unquote(fun_name)(unquote_splicing(fun_args)), to: unquote(module)
end
end
end