Skip to content

Instantly share code, notes, and snippets.

Josef Richter josefrichter

View GitHub Profile
View store.ts
import * as React from "react";
import { useState, useEffect } from "react";
A hook to simply use state between components
// You can put this in an central file and import it too
const useStore = createStore({ count: 0 })
View queue-agent.ex
defmodule QueueAgent do
def start_link(queue, name) do
Agent.start_link(fn -> queue end, name: name)
def add(queue, item) do
Agent.cast(queue, fn(state) -> state ++ [item] end)
def get(queue) do
View queue.ex
defmodule Queue do
use GenServer
def start_link(queue, name) do
GenServer.start(__MODULE__, queue, name: name)
# GenServer callbacks
def handle_call(:get, _from, [item | queue]) do
View tester.ex
defmodule Tester do
def start_run do
Agent.start(fn -> [total: 0, passed: 0, failed: 0] end, name: :test_stats)
def record_pass do
Agent.update(:test_stats, fn [total: t, passed: p, failed: f] ->
[total: t + 1, passed: p + 1, failed: f]
View kv-registry.ex
defmodule KV.Registry do
use GenServer
## Missing Client API - will add this later
## Defining GenServer Callbacks
@impl true
def init(:ok) do
{:ok, %{}}
View stack.ex
defmodule Stack do
use GenServer
# Client
def start_link(default) when is_list(default) do
GenServer.start_link(__MODULE__, default)
def push(pid, element) do
View counter.ex
defmodule Counter do
use Agent
def start_link(initial_value) do
Agent.start_link(fn -> initial_value end, name: __MODULE__)
def value do
Agent.get(__MODULE__, & &1)
View moviedata.ex
defmodule MovieData do
def start_link do
Agent.start_link(fn -> %{} end)
def add(pid, movie) do
Agent.update(pid, fn(state) ->
Map.put(state, movie, 1)
View blog.ex
def create_comment(attrs \\ %{}) do
|> Comment.changeset(attrs)
# TODO how to I get "post" here, to do the following step?
# |> Ecto.Changeset.put_assoc(:post, post)
|> Repo.insert()
josefrichter / Spacer.tsx
Created Apr 10, 2020
replicating SwitftUI Spacer() in React
View Spacer.tsx
// replicating SwitftUI Spacer() in React
import * as React from "react"
export function Spacer() {
return <div style={{ flex: "1 1 0" }}>&nbsp;</div>
You can’t perform that action at this time.