mirror of
https://github.com/penpot/penpot.git
synced 2025-07-27 17:27:19 +02:00
♻️ Make the namespacing independent of the branding.
This commit is contained in:
parent
aaf8b71837
commit
6c67c3c71b
305 changed files with 2399 additions and 2580 deletions
53
frontend/src/app/util/worker.cljs
Normal file
53
frontend/src/app/util/worker.cljs
Normal file
|
@ -0,0 +1,53 @@
|
|||
;; This Source Code Form is subject to the terms of the Mozilla Public
|
||||
;; License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
;; file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||
;;
|
||||
;; This Source Code Form is "Incompatible With Secondary Licenses", as
|
||||
;; defined by the Mozilla Public License, v. 2.0.
|
||||
;;
|
||||
;; Copyright (c) 2020 UXBOX Labs SL
|
||||
|
||||
(ns app.util.worker
|
||||
"A lightweight layer on top of webworkers api."
|
||||
(:require
|
||||
[beicon.core :as rx]
|
||||
[app.common.uuid :as uuid]
|
||||
[app.util.transit :as t]))
|
||||
|
||||
(declare handle-response)
|
||||
(defrecord Worker [instance stream])
|
||||
|
||||
(defn ask!
|
||||
[w message]
|
||||
(let [sender-id (uuid/next)
|
||||
data (t/encode {:payload message :sender-id sender-id})
|
||||
instance (:instance w)]
|
||||
(.postMessage instance data)
|
||||
(->> (:stream w)
|
||||
(rx/filter #(= (:reply-to %) sender-id))
|
||||
(rx/map handle-response)
|
||||
(rx/first))))
|
||||
|
||||
(defn init
|
||||
"Return a initialized webworker instance."
|
||||
[path on-error]
|
||||
(let [ins (js/Worker. path)
|
||||
bus (rx/subject)
|
||||
wrk (Worker. ins bus)]
|
||||
(.addEventListener ins "message"
|
||||
(fn [event]
|
||||
(let [data (.-data event)
|
||||
data (t/decode data)]
|
||||
(rx/push! bus data))))
|
||||
(.addEventListener ins "error"
|
||||
(fn [error]
|
||||
(on-error wrk error)))
|
||||
|
||||
wrk))
|
||||
|
||||
(defn- handle-response
|
||||
[{:keys [payload error] :as response}]
|
||||
(if-let [{:keys [data message]} error]
|
||||
(throw (ex-info message data))
|
||||
payload))
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue