Back
when-let (clj)
(source)macro
(when-let bindings & body)
bindings => binding-form test
When test is true, evaluates body with binding-form bound to the value of test
Examples
PrecursorApp/precursor
(ns pc.http.routes.api
(:require [cemerick.url :as url]
[cheshire.core :as json]
[clojure.core.memoize :as memo]
[clojure.string :as str]
[clojure.tools.reader.edn :as edn]
[crypto.equality :as crypto]
[defpage.core :as defpage :refer (defpage)]
[pc.auth :as auth]
[pc.crm :as crm]
[pc.datomic :as pcd]
[pc.early-access]
[pc.http.doc :as doc-http]
[pc.http.team :as team-http]
[pc.http.handlers.custom-domain :as custom-domain]
[pc.models.chat-bot :as chat-bot-model]
[pc.models.doc :as doc-model]
[pc.models.flag :as flag-model]
[pc.models.team :as team-model]
[pc.profile :as profile]
[ring.middleware.anti-forgery :as csrf]
[slingshot.slingshot :refer (try+ throw+)]))
(defpage new [:post "/api/v1/document/new"] [req]
(let [params (some-> req :body slurp edn/read-string)
read-only? (:read-only params)
doc-name (:document/name params)]
(if-not (:subdomain req)
(let [cust-uuid (get-in req [:auth :cust :cust/uuid])
intro-layers? (:intro-layers? params)
doc (doc-model/create-public-doc!
(merge {:document/chat-bot (rand-nth chat-bot-model/chat-bots)}
(when cust-uuid {:document/creator cust-uuid})
(when read-only? {:document/privacy :document.privacy/read-only})
(when doc-name {:document/name doc-name})))]
(when intro-layers?
(doc-http/add-intro-layers doc))
{:status 200 :body (pr-str {:document (doc-model/read-api doc)})})
(if (and (:team req)
(auth/logged-in? req)
(auth/has-team-permission? (pcd/default-db) (:team req) (:auth req) :admin))
(let [doc (doc-model/create-team-doc!
(:team req)
(merge {:document/chat-bot (rand-nth chat-bot-model/chat-bots)}
(when-let [cust-uuid (get-in req [:cust :cust/uuid])]
{:document/creator cust-uuid})
(when read-only?
{:document/privacy :document.privacy/read-only})
(when doc-name
{:document/name doc-name})))]
{:status 200 :body (pr-str {:document (doc-model/read-api doc)})})
{:status 400 :body (pr-str {:error :unauthorized-to-team
:redirect-url (str (url/map->URL {:host (profile/hostname)
:protocol (if (profile/force-ssl?)
"https"
(name (:scheme req)))
:port (if (profile/force-ssl?)
(profile/https-port)
(profile/http-port))
:path "/new"
:query (:query-string req)}))
:msg "You're unauthorized to make documents in this subdomain. Please request access."})}))))
hraberg/deuce
(ns deuce.emacs.term
(:use [deuce.emacs-lisp :only (defun defvar)])
(:require [clojure.core :as c]
[deuce.emacs.data :as data]
[deuce.emacs.eval :as eval]
[deuce.emacs.terminal :as terminal])
(:import [com.googlecode.lanterna.screen Screen])
(:refer-clojure :exclude []))
TTY may be a terminal object, a frame, or nil (meaning the selected
frame's terminal)."
(when-let [terminal ^Screen (terminal/frame-terminal)]
(.startScreen terminal)
((ns-resolve 'deuce.main 'start-ui))
(eval/run-hook-with-args 'resume-tty-functions terminal)))
A suspended tty may be resumed by calling `resume-tty' on it."
(when-let [terminal ^Screen (terminal/frame-terminal)]
((ns-resolve 'deuce.main 'stop-ui))
(.stopScreen terminal)
(eval/run-hook-with-args 'suspend-tty-functions terminal)))
eponai/sulolive
(ns eponai.server.external.datomic
(:require [com.stuartsierra.component :as component]
[eponai.server.datomic-dev :as datomic-dev]
[eponai.common.database :as db]
[datomic.api :as datomic]
[suspendable.core :as suspendable]
[clojure.core.async :as async]
[taoensso.timbre :refer [debug error]])
(:import (java.util.concurrent BlockingQueue)))
(defrecord Datomic [db-url provided-conn add-mocked-data?]
IDatomic
(add-tx-listener [this tx-listener on-error]
(when-let [listeners (:tx-listeners this)]
(let [id (datomic/squuid)]
(swap! listeners assoc id {:on-tx-report tx-listener
:on-error on-error})
id)))
(remove-tx-listener [this listener-id]
(when-let [listeners (:tx-listeners this)]
(swap! listeners dissoc listener-id)))
component/Lifecycle
(start [this]
(if (:conn this)
this
(let [conn (or provided-conn
(datomic-dev/create-connection db-url
{::datomic-dev/add-data? add-mocked-data?}))
tx-listeners (atom {})
control-chan (async/chan)
tx-report-queue (datomic/tx-report-queue conn)]
(async/thread
(try
(loop []
(let [tx-report (.take ^BlockingQueue tx-report-queue)]
(when-not (= ::finished tx-report)
(->>
(deref tx-listeners)
(vals)
(run!
(fn [{:keys [on-tx-report on-error]}]
(try
(on-tx-report tx-report)
(catch Throwable e
(error "Error in DatomicChat thread reading tx-report-queue: " e)
(try
(on-error e)
(catch Throwable e
(error "on-error threw exception, really?: " e))))))))
(recur))))
(catch InterruptedException e
(error "Error in DatomicChat thread reading tx-report-queue: " e)))
(debug "Exiting Datomic async/thread..")
(async/close! control-chan))
(assoc this :conn conn
:control-chan control-chan
:tx-listeners tx-listeners
:tx-report-queue tx-report-queue))))
(stop [this]
(when-let [conn (:conn this)]
(datomic/remove-tx-report-queue conn)
;; Adding ::finished to will eventually close the control-chan
(.add ^BlockingQueue (:tx-report-queue this) ::finished)
(let [[v c] (async/alts!! [(:control-chan this) (async/timeout 1000)])]
(if (= c (:control-chan this))
(debug "Datomic report-queue successfully stopped.")
(debug "Datomic report-queue timed out when stopping.")))
(datomic/release conn))
(dissoc this :conn :control-chan :tx-listeners :tx-report-queue))
dvingo/dv.fulcro-template
(ns {{namespace}}.task.resolvers
(:require
[clojure.core.async :refer [go <! <!! >! >!! chan]]
[clojure.pprint :refer [pprint]]
[com.wsscode.pathom.connect :as pc]
[{{namespace}}.user.model :as user]
[{{namespace}}.user.db :as user.db]
[{{namespace}}.task.model :as task]
[{{namespace}}.task.db :as task.db]
[dv.fulcro-util :as fu]
[dv.xtdb-util :as xu]
[taoensso.timbre :as log]))
(defmacro validity-check
[& args]
`(when-let
[msg# (cond ~@args)]
(fu/server-error msg#)))
duanebester/clunk
(require '[com.clunk.core :as clunk]
'[clojure.core.async :as async]
'[clojure.core.match :as m])
;; Receive Handler
(async/go-loop []
(when-let [msg (async/<! (:in client))]
(println (str "Received Message: " msg))
(m/match msg
{:type :AuthenticationMD5}
(async/>! (:out client) (clunk/get-password username password (byte-array (:salt msg))))
:else nil)
(recur)))