How to implement MVC using pure Javascript?

Clojure (programming language): Is ClojureScript capable of real multithreading (maybe using the new HTML5 web workers)? If it isn't it will be in the future, or there are other ways to implement real multithreading in JavaScript?

  • Answer:

    As stated by Robert, ClojureScript compiles to JavaScript via Google's Closure Compiler. You have access to everything in the browser that vanilla JavaScript has access to, including HTML5 APIs like web workers and the File API. You could use the BlobBuilder to build embedded web workers (https://developer.mozilla.org/En/Using_web_workers#Embedded_workers) and then launch them. The process could be polished up with the use of compile-time macros. At the time of writing this, there is no set goal to introduce this into ClojureScript's core language.  Some community projects are actively trying to incorporate this functionality. I'd also like to point out https://github.com/clojure/core.async might give you what you're ultimately looking for (perhaps not backed by web-workers yet, but isn't out of the realm).

Paul deGrandis at Quora Visit the source

Was this solution helpful to you?

Other answers

As ClojureScript compiles down to JavaScript it's not capable of anything that the host language isn't.  If ClojureScript targeted a JS runtime capable of such things it would be theoretically possible, however you're not likely to find that on an in-browser JS, and if you're using CS on the server you should likely just use vanilla Clojure :)

Robert Pitts

Check out the library Parallel.js.  It provides a very simple API over web workers to provide a level of concurrency.  It works both with node.js and in the browser.  I don't have time to spin up leiningen to test, but clojurescript could easily call into the library: (defn spawn "Wraps Parallel.js spawn function. Takes a function to run in parallel, p-func. This function must take one argument, the data which is also passed. Optionally, a callback function (cb-func) of one argument, the data returned, can be passed to handle any return data from the web worker" ([p-func data] (let [p (new js/Parallel (clj->js data))] (.spawn p p-func))) ([p-func data cb-func] (let [p (new js/Parallel (clj->js data))] (.then (.spawn p p-func) cb-func)))) ;; example use (defn process-data [data] (map #(* % %) data)) (defn handle-data [data] (doseq [d] (.log js/console d))) ;; should run in web worker, but I haven't tested (spawn process-data (range 1 100000) handle-data) (.log js/console "This should print first") ;;if everything worked, numbers will print last Again - totally untested.  But you get the general idea.  I'm not convinced this is a very robust solution for a lot of cases, however.  I believe that web workers use more memory than other solutions.  I think they're fine for the odd one off job, but if your solution requires large amounts of concurrency you might want to look into something like Erlang/Elixir instead, Pulsar/Quasar on Clojure, etc.

Dillon Spencer

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.