Skip to content

A collection of components built using reagent and beicon and bulma

License

Notifications You must be signed in to change notification settings

noxecane/irinse

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

42 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Irinse

Introduction

irinse is a collection of simple reagent components.

Install

The simplest way to use irinse in a clojurescript project, is by including it in the dependency vector on your project.clj file:

Clojars Project

Events

Creating event streams

To listen to events from a reagent component, you'll need to create a subject. The subject acts like a function so you can call it's next method.

(require '[beicon.core :as rx]
         '[irinse.beiconx :as rxt]) ;; import beiconx as subjects can be used as functions

(let [v (rx/subject)
      _ (rxt/log "Value" v)]
    (v 1) ;; ==> Value: 1
    (v [1 2]) ;; ==> Value: [1 2]
    (v)) ;; end

Converting streams to state

We could use reagent atoms to store the values from streams mainly because reagent itself does not work with reactive streams.

(def name-stream (rx/subject))

(def name (rxt/to-ratom "" name-stream))

@name 
;; ==> ""

(name-stream "noxecane")
(name-stream "Obasanjo")
 
@name
;; ==> "Obasanjo"

One could also reduce over the state

(def fill-list (rx/subject))
(def ilist (rxt/to-ratom [] conj fill-list))

(fill-list 12)
(fill-list 13)
(fill-list 14)

@ilist
;; ==> [12 13 14]

Converting a channel to an observable

There is an equivalent function to from-promise for core.async channels. Bear in mind that buffers don't matter as this function eagerly consume new events from the channel and dispatches them regardless of availability of subscribers. Hence, more like subjects, only events received after subscription matter.

(require '[cljs.core.async :as a])

(def b (a/chan 1))

(def c (rxt/from-chan a))
(rxt/log "From Channel" c)

(a/put! b 10)
;; ==> From Channel: 10

Creating Simple forms

For instance to create a login form component

(require '[irinse.forms :as form])

(defn model [{:keys [login-service]}]
  (let [write  (rx/subject)
        submit (rx/subject)
        state  (form/writes write) ;; reduce write events over a map [:name :value]
        login  (rx/flat-map  submit)] ;
    {:write  write
     :submit submit
     :state  state}))

(defn view [{:keys [write submit state]}]
  (fn []
    [:form
      [:input.input {:type      "text"
                     :on-change (form/input write [:username])}]
      [:input.input {:type      "password"
                     :on-change (form/input write [:password])}]
      [:button.button {:type     "button"
                       :on-click #(submit @state)}]]))

License

irinse is licensed under GPLv3 license: