Skip to content

Instantly share code, notes, and snippets.

View slashdotdash's full-sized avatar

Ben Smith slashdotdash

View GitHub Profile
slashdotdash / app.js
Created November 16, 2023 17:01 — forked from cblavier/app.js
Responsive Phoenix LiveView
View app.js
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 / unique_username.ex
Last active November 9, 2022 13:26
Unique username command dispatch middleware for Commanded
View unique_username.ex
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 /
Created January 11, 2022 09:46 — forked from PJUllrich/
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 /
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_USER=postgres \
-p 5432:5432 \
slashdotdash / wait_until.ex
Last active September 19, 2021 14:41
ExUnit wait until helper function
View wait_until.ex
def wait_until(timeout \\ 1_000, fun)
def wait_until(0, fun), do: fun.()
def wait_until(timeout, fun) do
ExUnit.AssertionError ->
View category_stream_linker.ex
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,
View invalid_event.ex
defmodule InvalidEvent do
@doc """
Module to be used when an event cannot be deserialized from the event store.
The payload field will be populated with the source event data if
deserialization is possible.
Receiving this event usually indicates an event has been removed or renamed in
the source code between releases. Deserializing to this invalid event module
allows the application to continue running, otherwise it would terminate as


What I expect from some system managing storage of streams of events, intended to be used in an event-sourced system.

  • ability to create streams
  • ability to delete streams
  • ability to use optimistic locking on stream level
  • ability to write a batch of events in one stream atomically and durably
  • ability to read events from one stream
  • ability to read all events in the store ( the so called "$all" stream)
slashdotdash / default_behaviour.ex
Created September 8, 2020 10:40 — forked from christhekeele/default_behaviour.ex
Behaviours with Defaults for Elixir
View default_behaviour.ex
defmodule Default.Behaviour do
@moduledoc """
Creates a behaviour that carries its own default implementation.
When used into a behaviour module, when that module in turn is used, all functions
defined on it are given to the using module.
This allows you to have concrete implementations of the behaviour's default functionality
for testing, unlike cramming them all into a __using__ macro.
slashdotdash /
Created May 28, 2020 14:53
Elixir GenServer with a `handle_info/2` callback will crash when receiving an unexpected message

Elixir GenServer with a handle_info/2 callback will crash when receiving an unexpected message

Define a GenServer with a handle_info callback function:

defmodule Echo do
  use GenServer

  def start_link(reply_to) do
    GenServer.start_link(__MODULE__, reply_to)