-
Notifications
You must be signed in to change notification settings - Fork 1
/
core.clj
40 lines (30 loc) · 931 Bytes
/
core.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
(ns websocket-layer.core
(:require [clojure.core.async :as async])
(:import (java.util UUID)))
(def ^:dynamic *state* nil)
(defonce sockets (atom {}))
(defn new-state [request]
{:id (UUID/randomUUID)
:socket nil
:outbound (async/chan 100)
:request request
:subscriptions {}
:state {}})
(defn get-socket-id []
(some-> *state* deref :id))
(defn get-socket []
(some-> *state* deref :socket))
(defn get-subscriptions []
(some-> *state* deref :subscriptions))
(defn get-outbound []
(some-> *state* deref :outbound))
(defn get-state []
(some-> *state* deref :state))
(defn swap-state! [f & args]
(get (swap! *state* update :state #(apply f % args)) :state))
(defn get-request []
(some-> *state* deref :request))
(def dispatch (comp keyword :kind))
(defmulti handle-push dispatch)
(defmulti handle-request dispatch)
(defmulti handle-subscription dispatch)