• Stars
    star
    168
  • Rank 225,507 (Top 5 %)
  • Language
    Clojure
  • License
    Eclipse Public Li...
  • Created almost 11 years ago
  • Updated over 9 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A reusable Om component for keeping local application state in sync with server application state

om-sync

A reusable synchronization component for Om.

Example

om-sync leverages the new :tx-listen option allowed by om.core/root. For example you can imagine setting up your application like so:

(let [tx-chan (chan)
      tx-pub-chan (async/pub tx-chan (fn [_] :txs))]
  (om-sync.util/edn-xhr
    {:method :get
     :url "/init"
     :on-complete
     (fn [res]
       (reset! app-state res)
       (om/root app-view app-state
         {:target (gdom/getElement "app")
          :shared {:tx-chan tx-pub-chan}
          :tx-listen
          (fn [tx-data root-cursor]
            (put! tx-chan [tx-data root-cursor]))}))}))

We publish a transaction queue channel :tx-chan as a global service via :shared so that om-sync instances can listen in.

We can now take any application data and wrap it in an om-sync instance. Whenever application data changes om-sync will synchronize those changes via EDN requests to a server.

Notice in the following that om-sync can take an :on-error handler. This error handler will be given tx-data which contains enough information to roll back the entire application state if something goes wrong.

(defn app-view [app owner]
  (reify
    om/IWillUpdate
    (will-update [_ next-props next-state]
      (when (:err-msg next-state)
        (js/setTimeout #(om/set-state! owner :err-msg nil) 5000)))
    om/IRenderState
    (render-state [_ {:keys [err-msg]}]
      (dom/div nil
        (om/build om-sync (:items app)
          {:opts {:view items-view
                  :filter (comp #{:create :update :delete} tx-tag)
                  :id-key :some/id
                  :on-success (fn [res tx-data] (println res))
                  :on-error
                  (fn [err tx-data]
                    (reset! app-state (:old-state tx-data))
                    (om/set-state! owner :err-msg
                      "Ooops! Sorry something went wrong try again later."))}})
         (when err-msg
           (dom/div nil err-msg))))))

om.core/transact! and om.core/update! now support tagging transactions with a keyword or a vector that starts with a keyword. om-sync listens in on transactions labeled :create, :update, and :delete.

(defn foo [some-data owner]
  (om.core/transact! some-data :foo (fn [_] :bar) :update))

If you are given some component that does not tag its transactions or the tags do not correspond to :create, :update, and :delete you can provide a :tag-fn via :opts to om-sync so that you can classify the transaction yourself.

Contributions

Pull requests welcome.

License

Copyright ยฉ 2014 David Nolen

Distributed under the Eclipse Public License either version 1.0 or (at your option) any later version.

More Repositories

1

mori

ClojureScript's persistent data structures and supporting API from the comfort of vanilla JavaScript
Clojure
3,385
star
2

logic-tutorial

A Very Gentle Introduction to Relational Programming
Clojure
936
star
3

lt-cljs-tutorial

A ClojureScript Programming Language Tutorial for Light Table Users
Clojure
864
star
4

enlive-tutorial

An Easy Introduction to Enlive
Clojure
612
star
5

mies

Minimal ClojureScript project template
Clojure
369
star
6

delimc

Delimited continuations for Clojure
Clojure
213
star
7

hello-cljsc

Hello ClojureScript Compiler
Clojure
190
star
8

cljs-bootstrap

ClojureScript compiling ClojureScript
Clojure
163
star
9

om-next-demo

TodoMVC with Om Next
Clojure
137
star
10

mies-node-template

A minimal ClojureScript Node.js template
Clojure
115
star
11

async-tests

Having fun with core.async
Clojure
103
star
12

swannodette.github.com

Clojure
80
star
13

clojure-snippets

Yasnippet's for Clojure
79
star
14

chambered

Port of Notch's Minecraft JavaScript demo to ClojureScript
Clojure
70
star
15

mies-om

Less is More template for Om
Clojure
53
star
16

jsx-fun

Experiments in transforming from React Native JSX/Flow into Closure Modules
JavaScript
30
star
17

clj-nehe

Nehe Tutorials in Clojure using Penumbra
Clojure
29
star
18

ejecta-cljs-template

ClojureScript + Ambly + Ejecta
Objective-C
29
star
19

native-deps

Native dependencies plugin for Leiningen
Clojure
29
star
20

react-cljs

Facebook React package for deployment to Clojars
JavaScript
28
star
21

om-datascript

Om Next w/ DataScript
Clojure
26
star
22

transit-example

A basic Transit example for Clojure/ClojureScript users
Clojure
21
star
23

om-async-tut

Template for Om Asyn Application State tutorial
Clojure
21
star
24

fun.coffee

Putting the funk into your coffee
CoffeeScript
19
star
25

cljs-master

ClojureScript Master Class
Clojure
17
star
26

transit-js-example

A transit-js example
JavaScript
17
star
27

cljs-demo

Demo repo for BK.js and NYC.js
JavaScript
16
star
28

hs-async

Examples for Tuesday
Clojure
15
star
29

om-nashorn

Om example that you can run with Nashorn
Clojure
14
star
30

GistPad

Gists for the iPad
Objective-C
12
star
31

cljs-ess

ClojureScript Essentials
Clojure
12
star
32

sicp

Chapters 4 and 5 of SICP
Scheme
11
star
33

ob-sml

org-babel support for Standard ML
Emacs Lisp
10
star
34

Vector2D

Basic fast 2D vector math for Objective-C
Objective-C
9
star
35

paip

Paradigms of Artificial Intelligence
Common Lisp
9
star
36

littlecomputers

Source code files for the ITP Little Computers class
Objective-C
8
star
37

es6-demo

ES6 support demo
Clojure
8
star
38

worlds

Worlds management for Om
Clojure
8
star
39

flocking

Dan Shiffman's flocking code ported to Clojure
Clojure
7
star
40

hs-compilers

Compilers Made Easy talk
Clojure
6
star
41

minikanren

My study of miniKanren in Racket
Scheme
6
star
42

sol

Python based library for talking to HPGL pen plotters
Python
5
star
43

underoop

Classes and Modules for Underscore.js
JavaScript
5
star
44

ui-check

Playground for testing transaction event generation for automated UI testing
Clojure
5
star
45

baduk

My Go/Weiqi/Baduk Blog
SCSS
5
star
46

ArtAndCode

Repository for Art && Code mobile iPhone workshop
Objective-C
4
star
47

fairKanren

miniKanren with fair conjunction
Scheme
4
star
48

cocoahelpers

some helpers for Cocoa programming
4
star
49

nyt-gen-talk

Code for the NYT ES6 Generators talk tomorrow
JavaScript
3
star
50

django-bbcode

a git fork of http://bitbucket.org/ojii/django-bbcode/
Python
3
star
51

alphaKanren

Understanding the alphaKanren implementation
Scheme
3
star
52

lt-settings

My Light Table Settings
2
star
53

cljs-material

Using NPM Material from CLJS
Clojure
2
star
54

js-snippets

JavaScript yasnippets
1
star
55

promisejs

Future and promises for server and client-side JavaScript
1
star
56

cljs-stl

Code for Strange Loop
Clojure
1
star
57

cljs-no-ns-test

Clojure
1
star
58

jampack

Useful instruments for Om
1
star
59

om-weasel-repro

Clojure
1
star
60

convex-hull

Optimized version of Uncle Bob's code
Clojure
1
star
61

techmesh-logic

TechMesh Conference Logic Demo Code
Clojure
1
star
62

techmesh-cljs

TechMesh Conference ClojureScript Demo Code
Clojure
1
star
63

zrch-logic

Logic Programming examples from Zurich Clojure Meetup
Clojure
1
star