Skip to content

Instantly share code, notes, and snippets.

View tokafish's full-sized avatar

Thomas Fisher tokafish

  • Plaid
  • San Francisco
View GitHub Profile
@tokafish
tokafish / local-peer.js
Created September 9, 2014 00:33
Simple Peer
var peer1 = new SimplePeer({ initiator: true });
var peer2 = new SimplePeer();
peer1.on('signal', function (data) {
// when peer1 has signaling data, give it to peer2
peer2.signal(data);
});
peer2.on('signal', function (data) {
// same as above, but in reverse
var peer = new SimplePeer({ initiator: true });
peer.on('signal', function (data) {
channel.trigger('client-signal-' + peerUserId, { userId: currentUser.id, data: data });
});
peer.on('ready', function () {
peer.send('hey peer, how is it going?')
});
@tokafish
tokafish / video.js
Last active August 29, 2015 14:06
Adding video
navigator.getUserMedia({ video: true, audio: true }, function(stream) {
currentUser.stream = stream;
}, function() {});
// ...
peer = new SimplePeer({ stream: currentUser.stream });
peer.on('stream', function(stream) {
var video = document.querySelector("video");
defmodule CombinationsBenchmark do
def combinations(collection, k) do
List.last(do_combinations(collection, k))
end
defp do_combinations(list, k) do
combinations_by_length = [[[]]|List.duplicate([], k)]
List.foldr list, combinations_by_length, fn x, next ->
sub = :lists.droplast(next)
@tokafish
tokafish / Connector.ex
Last active February 3, 2016 18:26 — forked from neektza/Connector.ex
Confused?
defmodule MessageSpammer.Connector do
use Supervisor
def start_link do
Supervisor.start_link(__MODULE__, [])
end
def init(_) do
IO.puts "Starting Connector"
defmodule ConcurrentAcceptance.AcceptanceCase do
use ExUnit.CaseTemplate
using do
quote do
use Hound.Helpers
import Ecto.Model
import Ecto.Query, only: [from: 2]
import ConcurrentAcceptance.Router.Helpers
config :concurrent_acceptance, sql_sandbox: true
defmodule ConcurrentAcceptance.Endpoint do
# rest of endpoint omitted
if Application.get_env(:concurrent_acceptance, :sql_sandbox) do
plug Phoenix.Ecto.SQL.Sandbox
end
plug ConcurrentAcceptance.Router
end
defmodule Phoenix.Ecto.SQL.Sandbox do
import Plug.Conn
@cookie_name "phoenix.ecto.sql.sandbox"
def init(opts), do: opts
def call(%{request_path: @config_path <> configuration} = conn, _opts) do
conn |> put_resp_cookie(@cookie_name, configuration) |> send_resp(200, "OK") |> halt
end
defmodule Phoenix.Ecto.SQL.Sandbox do
# ...
def call(conn, sandbox) do
conn |> fetch_cookies |> allow_sandbox_access(sandbox)
end
defp allow_sandbox_access(%{req_cookies: %{@cookie_name => configuration}} = conn, sandbox) do
[pid_string,repo_string] = configuration |> URI.decode |> String.split("|")
owner = to_pid(pid_string)