Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Separate out the business logic from the stuff that depends on it

  • Loading branch information...
commit befd48c621651c41512600ddfbb64982e12b75c4 1 parent aa6be05
@paul-lshift paul-lshift authored
View
2  src/eventframework/app.clj
@@ -1,7 +1,7 @@
(ns eventframework.app
(:use
[eventframework.commands :only [valid-position? put-command! get-all-commands set-commands!]]
- [eventframework.business :only [listen-events!]]
+ [eventframework.apply-business :only [listen-events!]]
[compojure.core :only [defroutes context GET PUT POST]])
(:require
[clojure.tools.logging :as log]
View
45 src/eventframework/apply_business.clj
@@ -0,0 +1,45 @@
+(ns eventframework.apply-business
+ (:use [eventframework.business :only [initial-state update-state gen-events]])
+ (:require eventframework.commands
+ [clojure.tools.logging :as log]))
+
+(defrecord CommandResult
+ [new-state events])
+
+(defn reduce-non-nil
+ "Like reduce, but skipping over values for which f yields nil.
+
+ (nil results are logged as errors)"
+ [f & args]
+ (apply reduce
+ (fn [x y] (if-let [x' (f x y)]
+ x'
+ (do (log/errorf "Fn ~s returned nil on ~s, keeping ~s"
+ f y x)
+ x)))
+ args))
+
+(defn state-before [position]
+ (reduce-non-nil update-state initial-state
+ (eventframework.commands/get-commands-before position)))
+
+(defn apply-commands [user state commands]
+ (let [apply-command
+ (fn [{state :new-state events :events} command]
+ (when-let [new-state (update-state state command)]
+ (CommandResult. new-state (into events (gen-events user state command)))))]
+ (reduce-non-nil apply-command
+ (CommandResult. state [])
+ commands)))
+
+(defn listen-events!
+ ([user position callback]
+ (listen-events! user position callback (state-before position)))
+ ([user position callback state]
+ (eventframework.commands/apply-or-enqueue-listener!
+ position
+ (fn [new-pos commands]
+ (let [{:keys [new-state events]} (apply-commands user state commands)]
+ (if (not-empty events)
+ (callback new-pos events)
+ (listen-events! user new-pos callback new-state)))))))
View
48 src/eventframework/business.clj
@@ -1,15 +1,9 @@
(ns eventframework.business
- (:require eventframework.commands
- [clojure.tools.logging :as log]))
-
-;; --------- Business logic
+ (:require [clojure.tools.logging :as log]))
(defrecord ThreadsAndSubscriptions
[threads subscriptions])
-(defrecord CommandResult
- [new-state events])
-
(def initial-state (ThreadsAndSubscriptions. {} {}))
(defn update-state [state command]
@@ -47,43 +41,3 @@
[]))
(do (log/warn "Ignoring unknown command type" type "while gen. events")
[]))))
-
-(defn reduce-non-nil
- "Like reduce, but skipping over values for which f yields nil.
-
- (nil results are logged as errors)"
- [f & args]
- (apply reduce
- (fn [x y] (if-let [x' (f x y)]
- x'
- (do (log/errorf "Fn ~s returned nil on ~s, keeping ~s"
- f y x)
- x)))
- args))
-
-;; ----------- Generic stuff not dependent on exact business logic
-
-(defn state-before [position]
- (reduce-non-nil update-state initial-state
- (eventframework.commands/get-commands-before position)))
-
-(defn apply-commands [user state commands]
- (let [apply-command
- (fn [{state :new-state events :events} command]
- (when-let [new-state (update-state state command)]
- (CommandResult. new-state (into events (gen-events user state command)))))]
- (reduce-non-nil apply-command
- (CommandResult. state [])
- commands)))
-
-(defn listen-events!
- ([user position callback]
- (listen-events! user position callback (state-before position)))
- ([user position callback state]
- (eventframework.commands/apply-or-enqueue-listener!
- position
- (fn [new-pos commands]
- (let [{:keys [new-state events]} (apply-commands user state commands)]
- (if (not-empty events)
- (callback new-pos events)
- (listen-events! user new-pos callback new-state)))))))
View
1  test/eventframework/business_test.clj
@@ -1,6 +1,7 @@
(ns eventframework.business-test
(:use
eventframework.business
+ eventframework.apply-business
clojure.test
midje.sweet))
Please sign in to comment.
Something went wrong with that request. Please try again.