GithubHelp home page GithubHelp logo

athos / aintegrant Goto Github PK

View Code? Open in Web Editor NEW
9.0 4.0 0.0 36 KB

Aintegrant ain't Integrant, it's Async Integrant!

License: Eclipse Public License 1.0

Clojure 100.00%
clojure clojurescript integrant async

aintegrant's Introduction

Aintegrant

Clojars Project CircleCI codecov

Aintegrant ain't Integrant, it's Async Integrant!

In short, Aintegrant is the async version of Integrant. Particularly in ClojureScript, application initialization may involve a various kinds of asynchronous execution, such as Web API call, resource fetch or file I/O (on Node.js). Aintegrant will help in such cases.

Also, Aintegrant is completely compatible with Integrant: Initialization code for Integrant perfectly runs via Aintegrant, and migration for turning some of the initialization asynchronous usually takes just a little effort.

Note: Aintegrant focuses on the asynchronization of Integrant's facilities, and NOT on the parallelization of them (at least for now). So, each component's init-key/halt-key! etc. is always performed one by one.

Installation

Add the following to the :dependencies in your project.clj or build.boot:

Clojars Project

Usage

If you are not familiar with Integrant, you may want to read its document first.

Aintegrant provides the same set of APIs as Integrant's. That is, it has a custom version of init, halt!, etc. and allows users to extend them with init-key, halt-key!, etc. respectively. Unlike Integrant, Aintegrant’s vesion of init, halt! etc. and their multimethod cousins take a callback function as the last argument.

For example, let’s say you have an implementation of init-key for the :database key. You can invoke the callback to return the initialized database component and notify completing initialization to trigger the next component’s initialization (if any).

(require '[aintegrant.core :as ag]
         '[integrant.core :as ig])

(def config
  {:database {:uri "..."}})

(defmethod ag/init-key :database [_ {:keys [uri]} callback]
  (callback (connect-to-database uri)))

(defmethod ag/halt-key! :database [_ conn callback]
  (disconnect! conn)
  (callback))

(def system (atom nil))
;; for initializing
(ag/init config (fn [_ sys] (reset! system sys)))
;; for halting
(ag/halt! @system (fn [_] (reset! system nil)))

You can also use the callback to handle errors raised during the initialization:

(defmethod ag/init-key :database [_ {:keys [uri]} callback]
  (try
    (let [conn (connect-to-database uri)]
      (callback conn))
    (catch Exception e
      (callback e nil))))
      
(defmethod ag/halt-key! :database [_ conn callback]
  (try
    (disconnect! conn)
    (catch Exception e
      (callback e))))
      
(ag/init config
         (fn [e sys]
           (if e
             (println (ex-message e))
             (reset! system sys))))

(ag/halt! config
          (fn [e]
            (if e
              (println (ex-message e))
              (reset! system nil))))

Note that the arity of callback functions differs between ag/init-key and ag/halt-key! (ie. the callback for ag/init-key accepts one or two arguments while the one for ag/halt-key! accepts zero or one argument)

When you invoke the callback for ag/init-key with one argument, that will be taken as the return value. And when you call it with two argument, the first will be taken as the raised error. The same goes for the callback for ag/resume-key.

When the callback for ag/halt-key! is called with zero argument, it will just notify the completion of halting without errors. When it is called with one argument, that will be taken as the raised error. The same goes for the callback for ag/suspend-key!.

License

Copyright © 2018 Shogo Ohta

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

aintegrant's People

Contributors

athos avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  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.