I've got a fresh experience of refactoring my project to use web workers. Here is the story:
I'm building a code editor using web technologies[1]. The initial design was pretty simple and sweet. I decided to go with re-frame (thanks Mike!), which means UI driven by reagent and react with nice&clean event processing/subscription system. The editor is doing basically three things: 1) UI rendering and responding to user actions 2) analyzing source code (for syntax highlighting, etc.) 3) layouting source code for renderer to generate markup I observed that analyzing and layouting is blocking my rendering tasks, so I decided to offload them to another thread using webworkers. What seemed like an easy change at first, introduced a lot of complexity later. Problems encountered so far: 1) cljs-devtools[2] does not work from web workers, this is a major problem for my development workflow 2) data must be marshalled as JSON => wasn't that hard, started using transmit 3) instead of one app-db state and one queue of events, now I have it split in two, one in the worker and one in the main thread. Keeping them in sync can be tricky due to async worker nature. imagine undo/redo operations - those have to be executed on both databases "at once". To solve problem #1. I had to implement special development version of the app, which does not run a worker thread, but emulates it on the main thread. It does not sound that hard, except re-frame wasn't ready to provide me with two independent re-frame event queues in single javascript context. I had to rewrite it: https://github.com/Day8/re-frame/pull/107. To make the long story short. One month later and wish I'd chosen regexs instead ;) happy web-working! :) Antonin [1] https://github.com/darwin/plastic [2] https://github.com/binaryage/cljs-devtools On Wednesday, August 26, 2015 at 10:27:34 AM UTC+2, Atamert Ölçgen wrote: > On Wed, Aug 26, 2015 at 2:46 AM, Mike Thompson <m.l.tho...@gmail.com> wrote: > On Tuesday, August 25, 2015 at 7:45:28 PM UTC+10, Thomas Heller wrote: > > > On Tuesday, August 25, 2015 at 10:59:53 AM UTC+2, Daniel Kersten wrote: > > > > "The browser does not support threads so neither can core.async." > > > > > > > > > > > > To expand on that, core.async uses cooperative multitasking, which means > > > you have to give control back every so often so it can schedule other go > > > blocks to be run. Calls like <! will do this while they block (which is > > > why timeout works). > > > > > > > > > > While that is correct let me emphasize that timeout is not a solution! > > > > > > Do you always know how long task X will run or whether you are going to > > need to chunk it? Is it even possible to split up? A "task" that may > > complete in 10ms on your machine might take 100ms on another one or even > > 500ms on yours if the computer is doing something else. > > > > > > If you need to do CPU intensive work in the browser use a WebWorker. It is > > their purpose. While not perfect it is far better than trying to be > > "cooperative" in your code. > > > > > > My 2 cents, > > > /thomas > > > > > > You have a problem, and you decide to solve it via webworkers. > > > > Later you'll WISH you'd chosen regexs instead :-) > > > > Why the cynicism? Isn't it true that the only way to make use of multiple > cores in browser is to use WebWorkers? > > > > > > -- > > Mike > > > > > > -- > > Note that posts from new members are moderated - please be patient with your > first post. > > --- > > You received this message because you are subscribed to the Google Groups > "ClojureScript" group. > > To unsubscribe from this group and stop receiving emails from it, send an > email to clojurescrip...@googlegroups.com. > > To post to this group, send email to clojur...@googlegroups.com. > > Visit this group at http://groups.google.com/group/clojurescript. > > > > > > -- > > > > > > > Kind Regards, > Atamert Ölçgen > > > ◻◼◻ > ◻◻◼ > ◼◼◼ > www.muhuk.com -- Note that posts from new members are moderated - please be patient with your first post. --- You received this message because you are subscribed to the Google Groups "ClojureScript" group. To unsubscribe from this group and stop receiving emails from it, send an email to clojurescript+unsubscr...@googlegroups.com. To post to this group, send email to clojurescript@googlegroups.com. Visit this group at http://groups.google.com/group/clojurescript.