Skip to content

Instantly share code, notes, and snippets.

@ohpauleez
Created June 28, 2012 23:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ohpauleez/3014746 to your computer and use it in GitHub Desktop.
Save ohpauleez/3014746 to your computer and use it in GitHub Desktop.
Quick pubsub example
(ns barker.client.main
(:require [barker.client.render :as render]
[barker.client.search :as search]
[shoreleave.client.common :as common]
[shoreleave.client.history :as history]
[shoreleave.client.pubsubs.simple :as pbus]
[shoreleave.client.pubsubs.protocols :as pubsub]
[shoreleave.client.worker :as swk]
)
(:use [jayq.core :only [$ attr]])
(:require-macros [shoreleave.client.remotes.macros :as srm]))
(def query-args (common/query-args-map))
(def hash-args (common/hash-args-map))
(def bus (pbus/bus))
(defn ^:export test-fn [] "This is from the test-fn")
(def ps-test-fn (pubsub/publishize test-fn bus))
(pubsub/subscribe bus ps-test-fn #(js/console.log (str "This is from the bus --FN--: " %)))
; this has a side-effect -> it publishes its result to the bus
(ps-test-fn)
; Build the "identity" worker
(defn echo-workerfn [data] data)
(def nw (swk/worker echo-workerfn))
; This works, but the pubsub is uniform across everything
;(add-watch nw :watcher #(js/console.log (last %4)))
;(js/console.log cljs.core)
;(js/console.log (js/JSON.stringify cljs.core (fn [n, v]
; (if (fn? v)
; (str v)
; v))))
;(js/console.log (js/JSON.stringify cljs.core))
(pubsub/publishize nw bus)
(pubsub/subscribe bus nw #(js/console.log (str "This is from the bus --worker--: " %)))
(nw "HELLO")
(nw "THREADS")
(def test-atom (atom {}))
(pubsub/publishize test-atom bus)
(pubsub/subscribe bus test-atom #(js/console.log (str "This is ALSO from the bus --atom--: " %)))
; This is next
;(pubsub/subscribe-> bus test-atom ps-test-fn #(js/console.log "this is the end"))
(swap! test-atom assoc :new-key 1)
(swap! test-atom assoc :another-key 2)
; You can do this, but it doesn't block and it's a terrible idea
;(js/console.log (apply str @nw))
;; ### Browser REPL
;; If you add a `repl` as a query-string arg, even on the live Barker,
;; You can remotely interact with the site from the local REPL
;; Visit: `http://127.0.0.1:8080/search?repl=yes#q=sat`
(common/toggle-brepl query-args :repl)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment