Skip to content

Instantly share code, notes, and snippets.

View narrowtux's full-sized avatar
:shipit:
Shipping it

Moritz Schmale narrowtux

:shipit:
Shipping it
View GitHub Profile
@narrowtux
narrowtux / consumer_channel.ex
Created October 24, 2016 11:10
Phoenix.Channel + GenStage
defmodule MyApp.Channel do
alias Experimental.GenStage
use GenStage
use MyApp.Web, :channel
def init(socket) do
{:consumer, socket, subscribe_to: [MyApp.BroadcastStage]}
end
def handle_events(events, _from, socket) do
# get events pushed directly to each channel connection
Enum.each events, &Phoenix.Channel.push(socket, "message", &1)
@narrowtux
narrowtux / collection.ex
Last active November 15, 2016 08:04
remove and difference functions for Enum
defmodule Coll do
@doc """
Removes all values from the subject
iex> Coll.remove([1, 2, 3], [2, 3])
[1]
iex> Coll.remove([1, 2], [2, 3, 4])
[1]
"""
def descendants(organization_id, truncation, filter \\ from o in __MODULE__) do
sub = from os in filter, where: os.organization_id == ^organization_id and os.truncation == ^truncation, select: [os.organization_id, os.parent_organization_id, os.id, ^1, os.datetime, os.truncation]
query = from outer in __MODULE__,
join: t in fragment("""
WITH RECURSIVE search_orgs(organization_id, parent_organization_id, id, depth, datetime, truncation) AS (
?
UNION ALL
SELECT
o.organization_id,
def descendants(organization_id, truncation, filter \\ from o in __MODULE__) do
sub = from os in filter, where: os.organization_id == ^organization_id and os.truncation == ^truncation, select: [os.organization_id, os.parent_organization_id, os.id, ^1, os.datetime, os.truncation]
query = from outer in __MODULE__,
join: t in fragment("""
WITH RECURSIVE search_orgs(organization_id, parent_organization_id, id, depth, datetime, truncation) AS (
?
UNION ALL
SELECT
o.organization_id,
def descendants(organization_id, truncation, filter \\ from o in __MODULE__) do
sub = from os in filter, where: os.organization_id == ^organization_id and os.truncation == ^truncation, select: [os.organization_id, os.parent_organization_id, os.id, ^1, os.datetime, os.truncation]
query = from outer in __MODULE__,
join: t in fragment("""
WITH RECURSIVE search_orgs(organization_id, parent_organization_id, id, depth, datetime, truncation) AS (
?
UNION ALL
SELECT
o.organization_id,
@narrowtux
narrowtux / wifi_sleep.lua
Last active August 14, 2017 08:36
Remedy for a sleep problem my MacBook Pro late 2016 has.
displaySleep = false -- Need better way to determine initial state of this variable. But it should be the truth most of the time.
systemSleep = false
function handleCaffeinateUpdates()
wifiPower = hs.wifi.interfaceDetails().power
if displaySleep and systemSleep and wifiPower then
print("Disabling WiFi before going to sleep")
hs.wifi.setPower(false)
elseif not displaySleep and not systemSleep and not wifiPower then
print("Woke up - enabling wifi")
defmodule Swarm.DynamicSupervisor do
use GenServer
require Logger
defstruct [
:child_name,
:child_module,
:child_args,
:group,
:pid,
@narrowtux
narrowtux / libcluster_rabbit.ex
Created September 20, 2017 12:02
RabbitMQ strategy for libcluster
defmodule Cluster.RabbitStrategy do
use GenServer
use Cluster.Strategy
import Cluster.Logger
alias Cluster.Strategy.State
@routing_key "cluster.heartbeat"
def start_link(opts) do
@narrowtux
narrowtux / repo_stream.ex
Created October 9, 2017 09:18
Module for Ecto and GenStage to work together when you want to use Repo.stream in conjunction with a GenStage subscriber or a Flow.
defmodule RepoStream do
defmodule Producer do
use GenStage
defstruct [:demand, :pid]
def start_link() do
GenStage.start_link(__MODULE__, self())
end
*smoothScroll.js*
*smooth*.js*
*nicescroll*js*
*jquery.mousewheel.js*
*plugins-scroll.js*
https://d33wubrfki0l68.cloudfront.net/bundles/85ee573a175fbccec4ed26bf47ede7fd06c1d248.js