Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/akiroz/re-frame-storage
re-frame interceptors for browser local storage
https://github.com/akiroz/re-frame-storage
clojurescript localstorage re-frame
Last synced: about 1 month ago
JSON representation
re-frame interceptors for browser local storage
- Host: GitHub
- URL: https://github.com/akiroz/re-frame-storage
- Owner: akiroz
- License: mit
- Created: 2016-09-28T15:51:28.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2023-01-27T15:20:01.000Z (almost 2 years ago)
- Last Synced: 2024-11-05T18:57:51.453Z (about 2 months ago)
- Topics: clojurescript, localstorage, re-frame
- Language: Clojure
- Homepage:
- Size: 167 KB
- Stars: 60
- Watchers: 4
- Forks: 7
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-re-frame - re-frame-storage - frame interceptors for browser local storage (Tools / Utils)
README
# re-frame-storage
[![Clojars Project](https://img.shields.io/clojars/v/akiroz.re-frame/storage.svg)](https://clojars.org/akiroz.re-frame/storage)
[![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg)](https://raw.githubusercontent.com/akiroz/re-frame-storage/master/LICENSE)
[![Build Status](https://travis-ci.org/akiroz/re-frame-storage.svg?branch=master)](https://travis-ci.org/akiroz/re-frame-storage)A very simple re-frame wrapper around [storage-atom][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
```clojure
(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
```clojure
(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))
```
[storage-atom]: https://github.com/alandipert/storage-atom