Merge branch 'feature/13' into feature/36

This commit is contained in:
Simon Brooke 2017-03-30 13:22:00 +01:00
commit 4be0b9ce08
5 changed files with 131 additions and 3 deletions

View file

@ -0,0 +1,115 @@
(ns youyesyet.outqueue
(:require
#?(:clj [clojure.core]
:cljs [reagent.core :refer [atom]])))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;
;;;; youyesyet.outqueue: queue of messages waiting to be sent to the server.
;;;;
;;;; This program is free software; you can redistribute it and/or
;;;; modify it under the terms of the GNU General Public License
;;;; as published by the Free Software Foundation; either version 2
;;;; of the License, or (at your option) any later version.
;;;;
;;;; This program is distributed in the hope that it will be useful,
;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;;;; GNU General Public License for more details.
;;;;
;;;; You should have received a copy of the GNU General Public License
;;;; along with this program; if not, write to the Free Software
;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
;;;; USA.
;;;;
;;;; Copyright (C) 2016 Simon Brooke for Radical Independence Campaign
;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; The items are (obviously) the actual items in the queue;
;;; the queue is locked if an attempt is currently being made to transmit
;;; an item.
(defn new-queue
"Create a new queue"
([]
(new-queue '()))
([items]
(atom {:locked false
:items (if
(seq? items)
(reverse items)
(list items))})))
(defn add!
"Add this item to the queue."
[q item]
(swap! q
(fn [a]
(assoc a :items
(cons item (:items a))))))
(defn q?
[x]
(try
(let [q (deref x)
locked (:locked q)]
(and
(seq? (:items q))
(or (true? locked) (false? locked))))
(catch #?(:clj Exception :cljs js/Object) any
#?(:clj (print (.getMessage any))
:cljs (js/console.log (str any))))))
(defn peek
"Look at the next item which could be removed from the queue."
[q]
(last (:items (deref q))))
(defn locked?
[q]
(:locked (deref q)))
(defn unlock!
([q ]
(unlock! q true))
([q value]
(swap! q (fn [a] (assoc a :locked (not (true? value)))))))
(defn lock!
[q]
(unlock! q false))
(defn count
"Return the count of items currently in the queue."
[q]
(count (deref q)))
(defn take!
"Return the first item from the queue, rebind the queue to the remaining
items. If the queue is empty return nil."
[q]
(swap! q (fn [a]
(let [items (reverse (:items a))
item (first items)
new-queue (reverse (rest items))]
(assoc (assoc a :items new-queue) :v item))))
(:v (deref q)))
(defn maybe-process-next
"Apply this process, assumed to be a function of one argument, to the next
item in the queue, if the queue is not currently locked; return the value
returned by process."
[q process]
(if (and (q? q)(not (locked? q)))
(try
(lock! q)
(let [v (apply process (list (peek q)))]
(take! q)
v)
(catch #?(:clj Exception :cljs js/Object) any
#?(:clj (print (.getMessage any))
:cljs (js/console.log (str any))))
(finally (unlock! q)))
))

View file

@ -67,6 +67,8 @@
:motd "This is a test version only. There is no real data."
;;; the options from among which electors can select.
:options [{:id :yes :description "Yes"} {:id :no :description "No"}]
;;; the number of requests created by the user in the current session
:requests 0
;;; the currently displayed 'page' within the app.
:page :home
})

View file

@ -39,6 +39,11 @@
(fn [db _]
(:addresses db)))
(reg-sub
:changes
(fn [db _]
(:changes db)))
(reg-sub
:elector
(fn [db _]

View file

@ -5,7 +5,7 @@
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;
;;;; youyesyet.views.electors: about/credits view for youyesyet.
;;;; youyesyet.views.about: about/credits view for youyesyet.
;;;;
;;;; This program is free software; you can redistribute it and/or
;;;; modify it under the terms of the GNU General Public License

View file

@ -1,5 +1,6 @@
(ns youyesyet.views.electors
(:require [re-frame.core :refer [reg-sub subscribe]]
(:require [reagent.core :refer [atom]]
[re-frame.core :refer [reg-sub subscribe]]
[youyesyet.ui-utils :as ui]))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
@ -47,12 +48,14 @@
image (if gender (name gender) "unknown")]
[:td {:key (:id elector)} [:img {:src (str "img/gender/" image ".png") :alt image}]]))
(defn genders-row
[electors]
[:tr
(map
#(gender-cell %) electors)])
(defn name-cell
[elector]
[:td {:key (str "name-" (:id elector))} (:name elector)])
@ -63,6 +66,7 @@
(map
#(name-cell %) electors)])
(defn options-row
[electors option]
(let [optid (:id option)
@ -77,6 +81,7 @@
[:img {:src image :alt optname}]]])
electors)]))
(defn issue-cell
"Create an issue cell for a particular elector"
[elector]
@ -98,7 +103,8 @@
(let [address @(subscribe [:address])
addresses @(subscribe [:addresses])
electors (:electors address)
options @(subscribe [:options])]
options @(subscribe [:options])
changes @(subscribe [:changes])]
(if address
[:div
[:h1 (:address address)]