1
這是我最小的失敗案例。ClojureScript,Figwheel,Devcards,Race Condition
(ns hello
(:require-macros [devcards.core :as dc])
(:require [reagent.core :as r]
[devcards.core :as dc]
[gamma.api :as g]
[gamma.program :as p]
[goog.dom :as gdom]
[goog.webgl :as ggl]))
(defn main []
(let [canvas (.getElementById js/document "webgl")
gl (.getContext canvas "webgl")] ;; *** THIS LINE ***
(.clearColor gl 0.0 0.0 0.0 1.0)
(.clear gl gl.COLOR_BUFFER_BIT)))
(dc/defcard-rg canvas-example
[:div
[:canvas {:width 600
:height 600
:id "webgl"}]])
(main)
下面是當我在figwheel/devcard中加載它時發生的情況。
第一次加載頁面:上*此行* 「無法讀取屬性 '的getContext' 空的」。這是因爲devcard畫布尚未設置。
如果我做了一個毫無意義的更改並保存文件,代碼重新加載並正常工作。這是因爲devcard canvas已經設置好了。
很明顯,這是(a)當(主)運行時和(b)當devcard's:canvas被設置時的競態條件。
我該如何解決這個問題?理想情況下,我想在畫布上標註「在此之後運行主要功能...」