{"id":26347210,"url":"https://github.com/jtkdvlp/cljs-workers","last_synced_at":"2025-07-19T15:02:55.930Z","repository":{"id":57713755,"uuid":"83063669","full_name":"jtkDvlp/cljs-workers","owner":"jtkDvlp","description":"A clojurescript lib for performing async tasks via web workers","archived":false,"fork":false,"pushed_at":"2024-04-25T20:42:20.000Z","size":25,"stargazers_count":44,"open_issues_count":1,"forks_count":7,"subscribers_count":5,"default_branch":"master","last_synced_at":"2025-01-09T17:12:16.741Z","etag":null,"topics":["async","clojurescript","thread","web-worker"],"latest_commit_sha":null,"homepage":"","language":"Clojure","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jtkDvlp.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null},"funding":{"github":null,"patreon":null,"open_collective":null,"ko_fi":null,"tidelift":null,"community_bridge":null,"liberapay":null,"issuehunt":null,"otechie":null,"lfx_crowdfunding":null,"custom":["https://www.paypal.com/donate?hosted_button_id=2PDXQMHX56T6U"]}},"created_at":"2017-02-24T16:58:31.000Z","updated_at":"2024-10-24T10:35:01.000Z","dependencies_parsed_at":"2023-02-15T18:46:24.357Z","dependency_job_id":null,"html_url":"https://github.com/jtkDvlp/cljs-workers","commit_stats":null,"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jtkDvlp%2Fcljs-workers","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jtkDvlp%2Fcljs-workers/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jtkDvlp%2Fcljs-workers/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jtkDvlp%2Fcljs-workers/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jtkDvlp","download_url":"https://codeload.github.com/jtkDvlp/cljs-workers/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243835926,"owners_count":20355616,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["async","clojurescript","thread","web-worker"],"created_at":"2025-03-16T07:16:05.739Z","updated_at":"2025-03-16T07:16:06.319Z","avatar_url":"https://github.com/jtkDvlp.png","language":"Clojure","funding_links":["https://www.paypal.com/donate?hosted_button_id=2PDXQMHX56T6U"],"categories":[],"sub_categories":[],"readme":"[![Clojars Project](https://img.shields.io/clojars/v/jtk-dvlp/cljs-workers.svg)](https://clojars.org/jtk-dvlp/cljs-workers)\n[![cljdoc badge](https://cljdoc.org/badge/jtk-dvlp/cljs-workers)](https://cljdoc.org/d/jtk-dvlp/cljs-workers/CURRENT)\n[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://github.com/jtkDvlp/cljs-workers/blob/master/LICENSE)\n[![paypal](https://www.paypalobjects.com/en_US/i/btn/btn_donate_SM.gif)](https://www.paypal.com/donate?hosted_button_id=2PDXQMHX56T6U)\n\n# Web workers for clojurescript\n\nThis [clojurescript](https://clojurescript.org/) library wraps the [web worker api](https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Using_web_workers) and provides an simple way for multithreading within browsers with cljs.\n\n## Getting started\n\n### Get it / add dependency\n\nAdd the following dependency to your `project.cljs`:\u003cbr\u003e\n[![Clojars Project](https://img.shields.io/clojars/v/jtk-dvlp/cljs-workers.svg)](https://clojars.org/jtk-dvlp/cljs-workers)\n\n### Usage\n\nTo understand web workers itself see the [web worker api](https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Using_web_workers). Or see the [quick guide](#quick-guide).\n\nThe following example handling both the browser and the worker within one script. The script provides an worker mirroring its inputs as outputs and testing four worker calls. Two of them will success and two of them will fail.\n\n```clojure\n(ns cljs-workers.test\n  (:require [cljs.core.async :refer [\u003c!]]\n            [cljs-workers.core :as main]\n            [cljs-workers.worker :as worker])\n  (:require-macros [cljs.core.async.macros :refer [go]]))\n\n;; Setup the browser path (handling both in one file)\n(defn app\n  []\n  (let [;; you can create one worker or a pool (async channel of workers)\n        worker-pool\n        (main/create-pool 2 \"worker.js\")\n\n        ;; a \"do-with-pool\" or \"-worker\" (see below) will return immediately and give you a result channel. So to print the result you have to handle the channel\n        print-result\n        (fn [message result-chan]\n          (go\n            (let [result (\u003c! result-chan)]\n              (.debug js/console\n                      message\n                      (str (:state result))\n                      (clj-\u003ejs result)))))]\n\n    (print-result \"Copy all simple values\" (main/do-with-pool! worker-pool {:handler :mirror, :arguments {:a \"Hallo\" :b \"Welt\" :c 10}}))\n    (print-result \"Copy the simple values and transfer the ArrayBuffer\" (main/do-with-pool! worker-pool {:handler :mirror, :arguments {:a \"Hallo\" :b \"Welt\" :c 10 :d (js/ArrayBuffer. 10) :transfer [:d]} :transfer [:d]}))\n    (print-result \"Copy the simple values and transfer the ArrayBuffer, but transfer (browser thread) will fail cause the wrong value and the wrong type is marked to do so\" (main/do-with-pool! worker-pool {:handler :mirror, :arguments {:a \"Hallo\" :b \"Welt\" :c 10 :d (js/ArrayBuffer. 10) :transfer [:d]} :transfer [:c]}))\n    (print-result \"Copy the simple values and transfer the ArrayBuffer, but transfer mirroring (worker thread) will fail cause the wrong value and the wrong type is marked to do so\" (main/do-with-pool! worker-pool {:handler :mirror, :arguments {:a \"Hallo\" :b \"Welt\" :c 10 :d (js/ArrayBuffer. 10) :transfer [:c]} :transfer [:d]}))\n    (print-result \"Copy values but do it with every worker of the pool\" (main/do-for-pool! worker-pool {:handler :mirror, :arguments {:a \"Hallo\" :b \"Welt\" :c 10}}))))\n\n;; Setup the worker path (handling both in one file)\n(defn worker\n  []\n  (worker/register\n    :mirror\n    (fn [arguments]\n      arguments))\n\n  (worker/bootstrap))\n\n;; Decide which path to setup\n(if (main/main?)\n  (app)\n  (worker))\n```\n\n#### Quick guide\n\nWorkers have their own context, not the global window context. So there is no document / DOM and some other things are also not present.\n\n[To handle data](https://developer.mozilla.org/de/docs/Web/API/Worker/postMessage) between these two contexts you have to copy or [transfer](https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Using_web_workers#Transferring_data_to_and_from_workers_further_details) your values / objects. Consider, you can copy values / objects handled by the [structured clone algorithm](https://developer.mozilla.org/de/docs/Web/API/Web_Workers_API/Structured_clone_algorithm) and transfer [transferables](https://developer.mozilla.org/en-US/docs/Web/API/Transferable), everything else will cause an error. But don´t be worried, most the time that´s no problem.\n\nSince there are two contexts, you have to provide two script threads / procedures. You can handle this by providing two script files or you can also provide one file handling both. When using the second way pay attention on what you get with the current context (workers have no DOM).\n\nFor full documentation see the [web worker api](https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Using_web_workers).\n\n## Appendix\n\nI´d be thankful to receive patches, comments and constructive criticism.\n\nHope the package is useful :-)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjtkdvlp%2Fcljs-workers","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjtkdvlp%2Fcljs-workers","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjtkdvlp%2Fcljs-workers/lists"}