clojurescript - Using defprotocol to create javascript object -
given i've defined protocol
(defprotocol subscriptionlistener (onconnection [cid] "") (onupdate [cid data] ""))
and interacting library in javascript object interface passed in follows
(js/somelib.connect url listener)
is there easy way create javascript object using defined protocol?
i have tried reify
protocol:
(js/somelib.connection "localhost" (reify subscriptionlistener (onconnection [cid] (println cid)) (onupdate [cid data] (println data))))
however not give object compatible external libraries.
thanks
there conceptual mismatch here. js library expects defined behavior want define cljs. should listener js object 2 methods, onconnection
, onupdate
? need translator between subscriptionlistener
in cljs , regular object in js:
(defprotocol subscriptionlistener (on-connection [o cid]) (on-update [o cid data])) (defn translator "translates cljs object follows subscriptionlistener js object has right mehods" [o] #js {:onconnection (fn [cid] (on-connection o cid)) :onupdate (fn [cid data] (on-update o cid data))}) (js/somelib.connection "localhost" (translator (reify subscriptionlistener (on-connection [_ cid] (println cid)) (on-update [_ cid data] (println data))))
notice functions in subscriptionlistener
take object complies protocol first argument. if cid
id given server , tried call (on-connection cid)
method on-connection not defined integers
.
Comments
Post a Comment