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
Usage:
// 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)
end
def add(queue, item) do
Agent.cast(queue, fn(state) -> state ++ [item] end)
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)
end
# 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)
end
def record_pass do
Agent.update(:test_stats, fn [total: t, passed: p, failed: f] ->
[total: t + 1, passed: p + 1, failed: f]
end)
end
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)
end
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__)
end
def value do
Agent.get(__MODULE__, & &1)
end
View moviedata.ex
defmodule MovieData do
def start_link do
Agent.start_link(fn -> %{} end)
end
def add(pid, movie) do
Agent.update(pid, fn(state) ->
Map.put(state, movie, 1)
end)
View blog.ex
def create_comment(attrs \\ %{}) do
%Comment{}
|> Comment.changeset(attrs)
# TODO how to I get "post" here, to do the following step?
# |> Ecto.Changeset.put_assoc(:post, post)
|> Repo.insert()
end
@josefrichter
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.