Skip to content

akiroz/re-frame-storage

Repository files navigation

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 custom reg-event-db
;; local storage key is :my-app
;; everything inside the db's :persistent key is automatically stored and retreived.
(defn my-reg-event-db
  [event-id handler]
  (reg-event-fx
    event-id
    [(persist-db :my-app :persistent)]
    (fn [{:keys [db]} event-vec]
      {:db (handler db event-vec)})))

;; ...

(my-reg-event-db
  :read-foo-store-bar
  (fn [db _]
    (print (get-in db [:persistent :foo]))
    (assoc-in db [:persistent :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)})))

;; May want an init helper to dispatch on app init

(my-reg-event-db :init (fn [db] db))

About

re-frame interceptors for browser local storage

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published