Skip to content

Instantly share code, notes, and snippets.

View slashdotdash's full-sized avatar

Ben Smith slashdotdash

View GitHub Profile
@slashdotdash
slashdotdash / wait_until.ex
Last active April 11, 2024 14:01
ExUnit wait until helper function
def wait_until(timeout \\ 1_000, fun)
def wait_until(0, fun), do: fun.()
def wait_until(timeout, fun) do
fun.()
rescue
ExUnit.AssertionError ->
:timer.sleep(10)
@slashdotdash
slashdotdash / app.js
Created November 16, 2023 17:01 — forked from cblavier/app.js
Responsive Phoenix LiveView
const Hooks = { ViewportResizeHooks}
const connectLiveSocket = () => {
const csrfToken = document.querySelector("meta[name='csrf-token']").getAttribute('content')
const liveSocket = new LiveSocket('/my_app/live', Socket, {
params: {
_csrf_token: csrfToken,
viewport: {
width: window.innerWidth,
height: window.innerHeight
@slashdotdash
slashdotdash / architecture.md
Created March 26, 2019 16:29
Architecture principles from "Design It!" by Michael Keeling

Architecture principles

From Design It! From Programmer to Software Architect by Michael Keeling.

  • Partition system and assign responsibilities.
  • Decide trade-offs among quality attributes:
    • Performance (response time)
    • Scalability (annual growth)
    • Availability
    • Security
@slashdotdash
slashdotdash / create_projection_versions.ex
Last active June 5, 2023 12:46
Building projections with Ecto using Commanded event handlers
defmodule Projections.Repo.Migrations.CreateProjectionVersions do
use Ecto.Migration
def change do
create table(:projection_versions, primary_key: false) do
add :projection_name, :text, primary_key: true
add :last_seen_event_id, :bigint
timestamps
end
@slashdotdash
slashdotdash / unique_username.ex
Last active November 9, 2022 13:26
Unique username command dispatch middleware for Commanded
defmodule UniqueUsername do
@behaviour Commanded.Middleware
alias Commanded.Middleware.Pipeline
def before_dispatch(%Pipeline{command: %RegisterUser{} = command} = pipeline) do
%RegisterUser{username: username} = command
case Repo.insert(%Username{username: username}) do
{:ok, _} ->
@slashdotdash
slashdotdash / elixir-tips.md
Last active September 19, 2022 23:02
Elixir tips
@slashdotdash
slashdotdash / README.md
Last active September 2, 2022 12:51
Commanded middleware to enrich commands during dispatch, such as calling an external API.

Commanded middleware for command enrichment

Usage

Add the EnrichCommand middleware to your command router:

defmodule MyApp.Router do
  use Commanded.Commands.Router
 
@slashdotdash
slashdotdash / big-o.md
Created January 11, 2022 09:46 — forked from PJUllrich/big-o.md
Big-O Time Complexities for Elixir Data Structures

Big-O Time Complexities for Elixir data structures

Map [1]

Operation Time Complexity
Access O(log n)
Search O(log n)
Insertion O(n) for < 32 elements, O(log n) for >= 32 elements [2]
Deletion O(n) for < 32 elements, O(log n) for >= 32 elements
@slashdotdash
slashdotdash / docker-postgres.sh
Created November 18, 2021 19:39
Run Postgres in-memory with Docker
docker pull postgres:12-alpine
docker run --rm \
--name postgres10 \
--tmpfs=/pgtmpfs \
-e PGDATA=/pgtmpfs \
-e POSTGRES_PASSWORD=postgres \
-e POSTGRES_USER=postgres \
-p 5432:5432 \
postgres:10-alpine
defmodule EventStore.CategoryStreamLinker do
@moduledoc """
Links streams from aggregate instances to their respective category streams.
example: events from stream_uuid of `contractors_contract-07c52787-da0c-444f-9783-5d380f7093f9` will be
linked to stream_uuid of `contractors_contract`.
"""
use Commanded.Event.Handler,
application: My.App,