cljfx / cljfx

Declarative, functional and extensible wrapper of JavaFX inspired by better parts of react and re-frame
MIT License
960 stars 48 forks source link

No implementation of method: :create of protocol: #'cljfx.lifecycle/Lifecycle found for class: clojure.lang.MultiFn #55

Open jelmerderonde opened 4 years ago

jelmerderonde commented 4 years ago

I'm getting the error "No implementation of method: :create of protocol: #'cljfx.lifecycle/Lifecycle found for class: clojure.lang.MultiFn" when trying to use a multimethod as a component. As in:

{:fx/type multi-method-here
 :other :params}

Is this something that could be implemented or are their good reasons not to use multimethods as components?

vlaaad commented 4 years ago

Hi!

There is no reason to not support multimethods by default, and adding support seems to be a non-breaking change. Are you interested to make a PR for that? You'll need to modify defaults.clj to make it accept MultiFn-s in addition to Fn-s. I can do it too, but first I'll need to find some time to do that.

Meanwhile, you can make your own type->lifecycle function that checks for multimethods as well:

(require '[cljfx.api :as fx] '[cljfx.lifecycle :as fx.lifecycle])

(defmulti component ::type)

(defmethod component ::label [{:keys [text]}]
  {:fx/type :label :text text})

(fx/on-fx-thread
  (fx/create-component
    {:fx/type :stage
     :showing true
     :scene {:fx/type :scene
             :root {:fx/type :v-box
                    :children [{:fx/type component
                                ::type ::label
                                :text "Hello multimethods!"}]}}}
    {:fx.opt/type->lifecycle (some-fn
                               #(when (instance? clojure.lang.MultiFn %) 
                                  fx.lifecycle/dynamic-fn->dynamic)
                               fx/keyword->lifecycle
                               fx/fn->lifecycle)}))
jelmerderonde commented 4 years ago

Hi, I would like to try and make a PR, but it will have to wait till the weekend I suspect ;-).

jelmerderonde commented 4 years ago

I created a PR: #60.

vlaaad commented 4 years ago

Great, thanks! I'll have a look — most probably tomorrow.