Skip to content

Instantly share code, notes, and snippets.

(ns pie-a-la-mode
(:import [java.util.concurrent Executors]))
;; Concurrency example from The Pragmatic Programmers. Waiters check if there's
;; enough pie and ice cream before they sell an order, but if they don't
;; coordinate it's still possible to sell more than we have.
;;
;; The below code should return either :ok or :not-available for a given order.
;; If we oversell, then it will throw an exception.
;;
@thheller
thheller / fulcro.cljs
Created April 5, 2021 11:22
fulcro with shadow-grove
(ns dummy.fulcro
(:require
[clojure.pprint :refer [pprint]]
[cljs.core.async :as async]
[taoensso.timbre :as log]
[com.fulcrologic.fulcro.application :as app]
[com.fulcrologic.fulcro.components :as fc]
[com.fulcrologic.fulcro.networking.mock-server-remote :refer [mock-http-server]]
[com.fulcrologic.fulcro.alpha.raw :as raw]
[com.fulcrologic.fulcro.mutations :as m]
(ns example.common
(:require
[shadow.experiments.grove :as sg :refer (defc <<)]))
;; created once
(def foo
(<< [:div "foo"]))
(defn nested [x]
(<< [:div "nested: " x]))
(ns example.counter
(:require
[shadow.experiments.grove :as sg :refer (defc <<)]))
(defc ui-counter []
;; create a local state atom once on mount
(bind state-ref
(atom 0))
;; watch state-ref and bind deref result to val
module.exports = function (config) {
config.set({
singleRun: true,
browsers: ['ChromeHeadlessNoSandbox'],
files: ['target/karma-test.js'].concat(require("./target/files.json")),
preprocessors: {
'target/files/coverage.*.js': ['coverage']
},
frameworks: ['cljs-test'],
plugins: [
/**
* @constructor
* @implements {cljs.core.IMeta}
* @implements {cljs.core.IWithMeta}
* @implements {cljs.core.ILookup}
*/
demo.browser.t_demo$browser113375 = function(meta113376) {
this.meta113376 = meta113376;
this.cljs$lang$protocol_mask$partition0$ = 393472;
this.cljs$lang$protocol_mask$partition1$ = 0;
(ns demo.util
(:require [clojure.java.io :as io]
[clojure.string :as str]))
(defn generate-index-html [html manifest]
(reduce
(fn [html {:keys [module-id output-name]}]
(let [js-name (str "/" (name module-id) ".js")]
(str/replace html js-name (str "/" output-name))))
html

Dealing with platform specific code

Sometimes you need to modifiy code depending on which target it is running in. Say you are writing a :browser app but parts of the code should also work in :react-native. The way I deal with this by abstracting out the relevant bits and either providing separate implementations of a protocol or just plain functions.

Interface code

;; shared ns that defines the "interface"
;; with actual implementation added later
(ns my.app.env)
@thheller
thheller / benchmark.cljs
Last active August 6, 2019 20:00
dummy node benchmark for creating react elements in cljs
(ns shadow.grove.react.benchmark
(:require
["benchmark" :as b]
["react" :as react :rename {createElement rce}]
["react-dom/server" :as rdom]
[hx.react :as hx]
[shadow.grove.react :as shadow]
[reagent.core :as reagent]
[rum.core :as rum]
[fulcro.client.dom :as fulcro-dom]))
(ns codox.reader.clojurescript
"Read raw documentation information from ClojureScript source directory."
(:use [codox.utils :only [assoc-some update-some correct-indent]])
(:require [clojure.java.io :as io]
[cljs.analyzer :as an]
[cljs.analyzer.api :as ana]
[cljs.closure]
[cljs.env]
[clojure.string :as str]
[cljs.env :as env]))