Liking cljdoc? Tell your friends :D

re-frame-storage

Clojars Project GitHub license Build Status

A very simple re-frame wrapper around storage-atom for persisting app state.

Depends on re-frame >= 0.8.0.

Usage

There are 2 ways to use this:

1. As individual re-frame fx & cofx

(ns my-app
  (:require [akiroz.re-frame.storage :refer [reg-co-fx!]]))

;; both :fx and :cofx keys are optional, they will not be registered if unspecified.
(reg-co-fx! :my-app         ;; local storage key
            {:fx :store     ;; re-frame fx ID
             :cofx :store}) ;; re-frame cofx ID

;; ...

(reg-event-fx
  :read-foo-store-bar
  [(inject-cofx :store)]
  (fn [{:keys [store]} _]
    (print (:foo store))
    {:store (assoc store :bar "qux")}))

2. As a re-frame interceptor that automatically persists part of your app-db

(ns my-app
  (:require [akiroz.re-frame.storage :refer [persist-db]]))

;; define a custum reg-event-db
;; local storage key is :my-app
;; everything inside the db's :persistant key is automatically stored and retreived.
(defn my-reg-event-db
  [event-id handler]
  (reg-event-fx
    event-id
    [(persist-db :my-app :persistant)]
    (fn [{:keys [db]} event-vec]
      {:db (handler db event-vec)})))

;; ...

(my-reg-event-db
  :read-foo-store-bar
  (fn [db _]
    (print (get-in db [:persistant :foo]))
    (assoc-in db [:persistant :bar] "qux")))

;; Or, if you want to persist multiple DB keys:

(defn my-reg-event-db
  [event-id handler]
  (reg-event-fx
    event-id
    [(persist-db-keys :my-app [:persistent-1 :persistent-2])]
    (fn [{:keys [db]} event-vec]
      {:db (handler db event-vec)})))

Can you improve this documentation?Edit on GitHub

cljdoc is a website building & hosting documentation for Clojure/Script libraries

× close