GithubHelp home page GithubHelp logo

kelsey-sorrels / storage-atom Goto Github PK

View Code? Open in Web Editor NEW

This project forked from alandipert/storage-atom

0.0 2.0 0.0 248 KB

ClojureScript atoms backed by HTML5 web storage.

Clojure 85.73% HTML 2.63% JavaScript 11.64%

storage-atom's Introduction

storage-atom

storage-atom is a ClojureScript library that provides an easy way to create atoms backed by HTML5 Web Storage.

Any change in the atom will be saved into the web storage.

The reverse is also true. This means that an atom modified in a tab or a window will also be modified in all of them.

Build Status

Usage

Dependency

[alandipert/storage-atom "1.2.4"]

Example

;; Require or use alandipert.storage-atom in your namespace.
;; The primary functions it provides are html-storage and local-storage.
;; It also provides the IStorageBackend protocol.

(ns your-ns
  (:require [alandipert.storage-atom :refer [local-storage]]))

;; Persist atom to HTML localStorage. The local-storage function takes an
;; atom and a key to store with, and returns the atom. If the key in storage
;; isn't set it will be initialized with the value obtained by dereferencing
;; the provided atom. Otherwise the atom's value will be reset! with the value
;; obtained from localStorage. All subsequent swap! and reset! operations on
;; the atom will cause the value in localStorage to be updated.

(def prefs (local-storage (atom {}) :prefs))

;; You can use the atom normally now - values are transparently persisted.

(add-watch prefs
           :new
           (fn [_ _ _ v]
             (.log js/console "new preference" v)))

(swap! prefs assoc :bg-color "red")

(:bg-color @prefs) ;=> "red"

;; Check that current value has been stored in localStorage.

(.getItem js/localStorage ":prefs") ;=> "{:bg-color \"red\"}"

;; To remove an item or clear all storage, use the provided methods instead
;; of calling the js method. This ensures that affected atoms are updated.

(alandipert.storage-atom/remove-local-storage! :prefs) ;; remove single value
(alandipert.storage-atom/clear-local-storage!)         ;; clear all values

;; Note: clearing a value will reset it to the initial value of the atom passed
;; to `local-storage`, not nil. This is probably what you want.

Notes

Because web storage keys and values are stored as strings, only values that can be printed readably may be used as storage keys or values.

I haven't done any performance testing, but this approach is much slower than using web storage directly because the entire atom contents are written on every swap!.

To prevent superfluous writes to the local storage, there is a 10 ms debounce (when a bunch of changes happen quickly, the last value is committed). It can be modified with the storage-delay atom or the *storage-delay* dynamic var. :

(reset! alandipert.storage-atom/storage-delay 100) ;; permanently
                                                   ;; increase
                                                   ;; debounce to 100
                                                   ;; ms

(binding [alandipert.storage-atom/*storage-delay* 500]
	 ... do some stuff ... ) ;; temporarily increase debounce to
                                 ;; 500 ms

enduro is a Clojure library that provides similar functionality by using files or a database for storage.

The cross-window propagation doesn't always work if browsing the .html directly instead of passing throught a webserver. (Yes Chrome, I'm looking at you...)

Testing

PhantomJS 1.7.0 is used for unit testing. With it installed, you can run the tests like so:

lein cljsbuild test

License

Copyright © 2013 Alan Dipert

Distributed under the Eclipse Public License, the same as Clojure.

storage-atom's People

Contributors

alandipert avatar frozenlock avatar ninjudd avatar micha avatar darkone23 avatar

Watchers

James Cloos avatar Kelsey Sorrels avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.