-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcore.cljs
160 lines (141 loc) · 5.32 KB
/
core.cljs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
;; This is re-frame's hook environment .
; import { createRoot } from 'react-dom/client
; import { createRoot } from 'react-dom/client';
(ns cube-test.core
(:require
;; try calling rules early so re-frame doesnt hook it
; [cube-test.twizzlers.rules]
;;
[reagent.core :as reagent]
[reagent.dom :as rdom]
; [reagent.dom.server :as rdom-server]
; ["react-dom/client" :as rdom-client]
;; note how react is separated with "-" and not "."
;; note: there is some conflict (eg. function overridding)
;; between react native and reagent, so you should be careful
;; about mixing them. Thus, these are commented out for now
;; except for router-dom which is orthoganal to reagent.
; ["react-dom" :as react-dom]
; ["react-dom/client" :as react-dom-client]
["react-router-dom" :refer (Route Link) :rename {BrowserRouter Router}]
[re-frame.core :as re-frame]
[cube-test.base :as base]
[cube-test.events :as events]
;; need a global reference so it gets compiled
[cube-test.utils.choice-carousel.events :as cc-events]
[cube-test.views :as views]
[cube-test.subs :as subs]
[cube-test.config :as config]
[cube-test.game :as game]
;; game-level
;; Note: if you are going to use global ns (e.g for events)
;; like :cube-test.utils.events, you need to make sure at
;; least one place in the code requires the ns, and that place
;; is here.
[cube-test.twizzlers.events]
[cube-test.utils.events]
; [cube-test.top-scene.events]
; [cube-test.tmp-scene.events]
;; workers
[cljs-workers.core :as main]
[cljs-workers.worker :as worker]
[re-frame-worker-fx.core]))
;; worker support start
(def worker-pool)
(defn worker-setup []
(prn "core.worker-setup: entered")
(re-frame/reg-event-fx
:on-worker-fx-success
(fn [_ [_ result]]
(prn "worker success" result)))
(re-frame/reg-event-fx
:on-worker-fx-error
(fn [_ [_ result]]
(.debug js/console "worker error" result)))
(re-frame/reg-event-fx
:test-worker-fx
(fn [coeffects [_ task]]
(let [worker-pool (-> coeffects :db :worker-pool)
task-with-pool (assoc task :pool worker-pool)]
(set! cube-test.core/worker-pool worker-pool)
{:worker task-with-pool})))
(re-frame/reg-event-fx
:db-hook
(fn [cofx [_ val]]
{
:db (:db cofx)}))
(re-frame/reg-event-fx
:initialize
(fn [_ _]
{:db {:worker-pool (main/create-pool 2 "libs/cljs_workers/worker/worker.js")}
:dispatch-n [[:test-worker-fx {:handler :mirror, :arguments {:a "Hallo" :b "Welt" :c 10} :on-success [:on-worker-fx-success] :on-error [:on-worker-fx-error]}]
[:test-worker-fx {:handler :mirror, :arguments {:a "Hallo" :b "Welt" :c 10 :d (js/ArrayBuffer. 10) :transfer [:d]} :transfer [:d] :on-success [:on-worker-fx-success] :on-error [:on-worker-fx-error]}]]}))
(re-frame/reg-event-fx
:initialize-2
(fn [_ _]
(let [db {:db {:worker-pool (main/create-pool 2 "libs/cljs_workers/worker/worker.js")}
:dispatch-n [[:test-worker-fx {:handler :mirror, :arguments {:a "Hello" :b "World" :c 10} :on-success [:on-worker-fx-success] :on-error [:on-worker-fx-error]}]
[:test-worker-fx {:handler :mirror, :arguments {:a "Hallo" :b "Welt" :c 10 :d (js/ArrayBuffer. 10) :transfer [:d]} :transfer [:d] :on-success [:on-worker-fx-success] :on-error [:on-worker-fx-error]}]
[:test-worker-fx {:handler :count-worker, :arguments {:a "Bye" :b "Moon" :c 10} :on-success [:on-worker-fx-success] :on-error [:on-worker-fx-error]}]]}]
(set! worker-pool (-> db :db :worker-pool))
db)))
(set! worker-pool (re-frame/dispatch-sync [:initialize-2])))
;; worker support end
(defn dev-setup []
(when config/debug?
(println "dev mode")))
;; worker-support
; (worker-setup))
; (defn ^:dev/after-load mount-root [])
(defn ^:dev/after-load mount-root [top-level-scene]
(re-frame/clear-subscription-cache!)
(let [root-el (.getElementById js/document "app")]
(rdom/unmount-component-at-node root-el)
(rdom/render [views/main-panel top-level-scene] root-el)))
; (defn ^:dev/after-load mount-root []
; (re-frame/clear-subscription-cache!)
; (let [container (.getElementById js/document "app")
; root (react-dom-client/createRoot container)]
; ; root (rdom-client/createRoot container)]
; ; root (react.dom.client.createRoot container)]
; ; (rdom/unmount-component-at-node container)
; (.render root [views/main-panel])))
; ; (.render root (views/main-panel))))
; ; (.render root views/main-panel)))
; ; (react.dom/render root [views/main-panel])))
;; worker support
(defn worker
[]
(worker/register
:mirror
(fn [arguments]
arguments))
(worker/register
:count-worker
(fn [arguments]
arguments))
(worker/bootstrap))
(defn count-worker
[]
(worker/register
:count-worker
(fn [arguments]
(count arguments)))
(worker/bootstrap))
;; end worker support
(defn init
([] (init base/top-level-scene))
([top-level-scene]
(re-frame/dispatch-sync [::events/initialize-db])
(dev-setup)
;; worker
; (worker-setup)
(mount-root top-level-scene)
(game/init top-level-scene)))
; (defn re-init [top-level-scene]
; (re-frame/dispatch-sync [::events/initialize-db])
; (dev-setup)
; ;; worker
; ; (worker-setup)
; (mount-root)
; (game/init))