Skip to content

Commit 7ebbd0b

Browse files
committed
some cljc
1 parent 2f86cdd commit 7ebbd0b

5 files changed

Lines changed: 99 additions & 67 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,4 @@
2424
/tags
2525
/target
2626
\#*\#
27+
/.cljs_node_repl

project.clj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@
2727

2828
:dependencies
2929
[[pretty "1.0.0"]
30-
[potemkin "0.4.5"]]
30+
[potemkin "0.4.5"]
31+
[org.clojure/clojurescript "1.10.520" :scope "provided"]]
3132

3233
:aot [methodical.interface methodical.impl.standard]
3334

src/methodical/impl/dispatcher/common.clj renamed to src/methodical/impl/dispatcher/common.cljc

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
(ns methodical.impl.dispatcher.common
2-
"Utility functions for implementing Dispatchers.")
2+
"Utility functions for implementing Dispatchers."
3+
#?(:cljs
4+
(:require
5+
[goog.string :refer [format]])))
6+
7+
#?(:cljs (def ^:private IllegalStateException js/Error))
38

49
(defn add-preference
510
"Add a method preference to `prefs` for dispatch value `x` over `y`. Used to implement `prefer-method`."

src/methodical/impl/dispatcher/standard.clj renamed to src/methodical/impl/dispatcher/standard.cljc

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,12 @@
44
(:refer-clojure :exclude [prefers prefer-method])
55
(:require [methodical.impl.dispatcher.common :as dispatcher.common]
66
[methodical.interface :as i]
7-
[pretty.core :refer [PrettyPrintable]])
8-
(:import methodical.interface.Dispatcher))
7+
#?(:clj [pretty.core :refer [PrettyPrintable]])
8+
#?(:cljs [methodical.interface :refer [Dispatcher]])
9+
#?(:cljs [goog.string :refer [format]]))
10+
#?(:clj (:import methodical.interface.Dispatcher)))
11+
12+
#?(:cljs (def ^:private IllegalArgumentException js/Error))
913

1014
(defn- matching-primary-pairs-excluding-default
1115
"Return a sequence of pairs of `[dispatch-value method]` for all applicable dispatch values, excluding the default
@@ -86,20 +90,23 @@
8690
:when (seq pairs)]
8791
[qualifier (map second pairs)])))
8892

89-
9093
(deftype StandardDispatcher [dispatch-fn hierarchy-var default-value prefs]
91-
PrettyPrintable
92-
(pretty [_]
93-
(concat ['standard-dispatcher dispatch-fn]
94-
(when (not= hierarchy-var #'clojure.core/global-hierarchy)
95-
[:hierarchy hierarchy-var])
96-
(when (not= default-value :default)
97-
[:default-value default-value])
98-
(when (seq prefs)
99-
[:prefers prefs])))
100-
101-
Object
102-
(equals [_ another]
94+
#?@(:clj
95+
[PrettyPrintable
96+
(pretty [_]
97+
(concat ['standard-dispatcher dispatch-fn]
98+
(when (not= hierarchy-var #'clojure.core/global-hierarchy)
99+
[:hierarchy hierarchy-var])
100+
(when (not= default-value :default)
101+
[:default-value default-value])
102+
(when (seq prefs)
103+
[:prefers prefs])))])
104+
105+
#?(:clj Object
106+
:cljs IEquiv)
107+
;; todo: hashcode
108+
109+
(#?(:clj equals, :cljs -equiv) [_ another]
103110
(and
104111
(instance? StandardDispatcher another)
105112
(let [^StandardDispatcher another another]
@@ -109,7 +116,7 @@
109116
(= default-value (.default-value another))
110117
(= prefs (.prefs another))))))
111118

112-
Dispatcher
119+
#?(:clj Dispatcher :cljs Object)
113120
(dispatchValue [_] (dispatch-fn))
114121
(dispatchValue [_ a] (dispatch-fn a))
115122
(dispatchValue [_ a b] (dispatch-fn a b))
@@ -130,6 +137,8 @@
130137
prefs)
131138

132139
(preferMethod [this x y]
140+
;; var-get is not implemented in cljs
141+
;; https://github.com/camsaul/methodical/issues/29
133142
(let [new-prefs (dispatcher.common/add-preference (partial isa? (var-get hierarchy-var)) prefs x y)]
134143
(if (= prefs new-prefs)
135144
this
Lines changed: 65 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,27 @@
11
(ns methodical.interface
22
(:refer-clojure :exclude [isa? prefers prefer-method]))
33

4-
(defmacro ^:private defonceinterface [interface-name & body]
5-
(let [class-name (clojure.string/replace (str *ns* "." interface-name) #"\-" "_")
6-
exists (try
7-
(Class/forName class-name)
8-
true
9-
(catch Exception _
10-
false))]
11-
(if exists
12-
`(do
13-
(import ~(symbol class-name))
14-
nil)
15-
`(definterface ~interface-name ~@body))))
16-
17-
(defonceinterface MethodCombination
18-
(allowedQualifiers [])
19-
(combineMethods [primary-methods aux-methods])
20-
(transformFnTail [qualifier fn-tail]))
4+
#?(:clj
5+
(defmacro ^:private defonceinterface [interface-name & body]
6+
(let [class-name (clojure.string/replace (str *ns* "." interface-name) #"\-" "_")
7+
exists (try
8+
(Class/forName class-name)
9+
true
10+
(catch Exception _
11+
false))]
12+
(if exists
13+
`(do
14+
(import ~(symbol class-name))
15+
nil)
16+
`(definterface ~interface-name ~@body)))))
17+
18+
#?(:clj
19+
(defonceinterface MethodCombination
20+
(allowedQualifiers [])
21+
(combineMethods [primary-methods aux-methods])
22+
(transformFnTail [qualifier fn-tail]))
23+
:cljs
24+
(def MethodCombination))
2125

2226
(defn allowed-qualifiers
2327
"The set containg all qualifiers supported by this method combination. `nil` in the set means the method
@@ -44,13 +48,16 @@
4448
[^MethodCombination method-combination qualifier fn-tail]
4549
(.transformFnTail method-combination qualifier fn-tail))
4650

47-
(defonceinterface MethodTable
48-
(primaryMethods [])
49-
(auxMethods [])
50-
(addPrimaryMethod [dispatch-value f])
51-
(removePrimaryMethod [dispatch-value])
52-
(addAuxMethod [qualifier dispatch-value f])
53-
(removeAuxMethod [qualifier dispatch-val method]))
51+
#?(:clj
52+
(defonceinterface MethodTable
53+
(primaryMethods [])
54+
(auxMethods [])
55+
(addPrimaryMethod [dispatch-value f])
56+
(removePrimaryMethod [dispatch-value])
57+
(addAuxMethod [qualifier dispatch-value f])
58+
(removeAuxMethod [qualifier dispatch-val method]))
59+
:cljs
60+
(def MethodTable))
5461

5562
(defn primary-methods
5663
"Get a `dispatch-value -> fn` map of all primary methods assoicated with this method table."
@@ -88,19 +95,22 @@
8895
[^MethodTable method-table qualifier dispatch-val method]
8996
(.removeAuxMethod method-table qualifier dispatch-val method))
9097

91-
(defonceinterface Dispatcher
92-
(dispatchValue [])
93-
(dispatchValue [a])
94-
(dispatchValue [a b])
95-
(dispatchValue [a b c])
96-
(dispatchValue [a b c d])
97-
(dispatchValue [a b c d more])
98-
99-
(matchingPrimaryMethods [method-table dispatch-value])
100-
(matchingAuxMethods [method-table dispatch-value])
101-
(defaultDispatchValue [])
102-
(prefers [])
103-
(preferMethod [dispatch-val-x dispatch-val-y]))
98+
#?(:clj
99+
(defonceinterface Dispatcher
100+
(dispatchValue [])
101+
(dispatchValue [a])
102+
(dispatchValue [a b])
103+
(dispatchValue [a b c])
104+
(dispatchValue [a b c d])
105+
(dispatchValue [a b c d more])
106+
107+
(matchingPrimaryMethods [method-table dispatch-value])
108+
(matchingAuxMethods [method-table dispatch-value])
109+
(defaultDispatchValue [])
110+
(prefers [])
111+
(preferMethod [dispatch-val-x dispatch-val-y]))
112+
:cljs
113+
(def Dispatcher))
104114

105115
(defn dispatch-value
106116
"Return an appropriate dispatch value for args passed to a multimethod. (This method is equivalent in purpose to
@@ -140,13 +150,16 @@
140150
[^Dispatcher dispatcher dispatch-val-x dispatch-val-y]
141151
(.preferMethod dispatcher dispatch-val-x dispatch-val-y))
142152

143-
(defonceinterface MultiFnImpl
144-
(^methodical.interface.MethodCombination methodCombination [])
145-
(^methodical.interface.Dispatcher dispatcher [])
146-
(^methodical.interface.MultiFnImpl withDispatcher [new-dispatcher])
147-
(^methodical.interface.MethodTable methodTable [])
148-
(^methodical.interface.MultiFnImpl withMethodTable [new-method-table])
149-
(effectiveMethod [dispatch-value]))
153+
#?(:clj
154+
(defonceinterface MultiFnImpl
155+
(^methodical.interface.MethodCombination methodCombination [])
156+
(^methodical.interface.Dispatcher dispatcher [])
157+
(^methodical.interface.MultiFnImpl withDispatcher [new-dispatcher])
158+
(^methodical.interface.MethodTable methodTable [])
159+
(^methodical.interface.MultiFnImpl withMethodTable [new-method-table])
160+
(effectiveMethod [dispatch-value]))
161+
:cljs
162+
(def MultiFnImpl))
150163

151164
(defn ^methodical.interface.MethodCombination method-combination
152165
"Get the method combination associated with this multifn."
@@ -181,11 +194,14 @@
181194
[^MultiFnImpl multifn dispatch-value]
182195
(.effectiveMethod multifn dispatch-value))
183196

184-
(defonceinterface Cache
185-
(cachedMethod [dispatch-value])
186-
(cacheMethodBang [dispatch-value method])
187-
(clearCacheBang [])
188-
(^methodical.interface.Cache emptyCopy []))
197+
#?(:clj
198+
(defonceinterface Cache
199+
(cachedMethod [dispatch-value])
200+
(cacheMethodBang [dispatch-value method])
201+
(clearCacheBang [])
202+
(^methodical.interface.Cache emptyCopy []))
203+
:cljs
204+
(def Cache))
189205

190206
(defn cached-method
191207
"Return cached effective method for `dispatch-value`, if it exists in the cache."

0 commit comments

Comments
 (0)