#13: Beginning work on outbound queue
This commit is contained in:
parent
92ad75da92
commit
36d8fa1273
115
src/cljc/youyesyet/outqueue.cljc
Normal file
115
src/cljc/youyesyet/outqueue.cljc
Normal 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)))
|
||||||
|
))
|
|
@ -58,6 +58,8 @@
|
||||||
:motd "This is a test version only. There is no real data."
|
:motd "This is a test version only. There is no real data."
|
||||||
;;; the options from among which electors can select.
|
;;; the options from among which electors can select.
|
||||||
:options [{:id :yes :description "Yes"} {:id :no :description "No"}]
|
: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.
|
;;; the currently displayed 'page' within the app.
|
||||||
:page :home
|
:page :home
|
||||||
})
|
})
|
||||||
|
|
|
@ -39,6 +39,11 @@
|
||||||
(fn [db _]
|
(fn [db _]
|
||||||
(:addresses db)))
|
(:addresses db)))
|
||||||
|
|
||||||
|
(reg-sub
|
||||||
|
:changes
|
||||||
|
(fn [db _]
|
||||||
|
(:changes db)))
|
||||||
|
|
||||||
(reg-sub
|
(reg-sub
|
||||||
:elector
|
:elector
|
||||||
(fn [db _]
|
(fn [db _]
|
||||||
|
|
|
@ -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
|
;;;; This program is free software; you can redistribute it and/or
|
||||||
;;;; modify it under the terms of the GNU General Public License
|
;;;; modify it under the terms of the GNU General Public License
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
(ns youyesyet.views.electors
|
(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]))
|
[youyesyet.ui-utils :as ui]))
|
||||||
|
|
||||||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||||
|
@ -47,12 +48,14 @@
|
||||||
image (if gender (name gender) "unknown")]
|
image (if gender (name gender) "unknown")]
|
||||||
[:td {:key (:id elector)} [:img {:src (str "img/gender/" image ".png") :alt image}]]))
|
[:td {:key (:id elector)} [:img {:src (str "img/gender/" image ".png") :alt image}]]))
|
||||||
|
|
||||||
|
|
||||||
(defn genders-row
|
(defn genders-row
|
||||||
[electors]
|
[electors]
|
||||||
[:tr
|
[:tr
|
||||||
(map
|
(map
|
||||||
#(gender-cell %) electors)])
|
#(gender-cell %) electors)])
|
||||||
|
|
||||||
|
|
||||||
(defn name-cell
|
(defn name-cell
|
||||||
[elector]
|
[elector]
|
||||||
[:td {:key (str "name-" (:id elector))} (:name elector)])
|
[:td {:key (str "name-" (:id elector))} (:name elector)])
|
||||||
|
@ -63,6 +66,7 @@
|
||||||
(map
|
(map
|
||||||
#(name-cell %) electors)])
|
#(name-cell %) electors)])
|
||||||
|
|
||||||
|
|
||||||
(defn options-row
|
(defn options-row
|
||||||
[electors option]
|
[electors option]
|
||||||
(let [optid (:id option)
|
(let [optid (:id option)
|
||||||
|
@ -77,6 +81,7 @@
|
||||||
[:img {:src image :alt optname}]]])
|
[:img {:src image :alt optname}]]])
|
||||||
electors)]))
|
electors)]))
|
||||||
|
|
||||||
|
|
||||||
(defn issue-cell
|
(defn issue-cell
|
||||||
"Create an issue cell for a particular elector"
|
"Create an issue cell for a particular elector"
|
||||||
[elector]
|
[elector]
|
||||||
|
@ -98,7 +103,8 @@
|
||||||
(let [address @(subscribe [:address])
|
(let [address @(subscribe [:address])
|
||||||
addresses @(subscribe [:addresses])
|
addresses @(subscribe [:addresses])
|
||||||
electors (:electors address)
|
electors (:electors address)
|
||||||
options @(subscribe [:options])]
|
options @(subscribe [:options])
|
||||||
|
changes @(subscribe [:changes])]
|
||||||
(if address
|
(if address
|
||||||
[:div
|
[:div
|
||||||
[:h1 (:address address)]
|
[:h1 (:address address)]
|
||||||
|
|
Loading…
Reference in a new issue