macro woes

Skip to first unread message

Dimitrios Jim Piliouras

Feb 24, 2022, 11:28:36 AMFeb 24
to ClojureScript
Hello everyone,

I'm trying to implement a rudimentary feature-flagging system in CLJS, and as you might expect, I want to emit different code when a particular feature is enabled VS disabled. So this is how I started:
(ns app.features
(:require [clojure.string :as str]))

(goog-define features "")

(def CURRENT (into #{} (map keyword) (str/split features #",")))
I now want to define a simple macro like the following:
(defmacro with-feature 
   [id enabled disabled]
   (if (contains? app.features/CURRENT id)
     (when (some? enabled) `~enabled)
     (when (some? disabled) `~disabled)
Where do I put this macro so that the compiler doesn't complain that `app.features/CURRENT` cannot be resolved? I know it has to be a .clj file, but I am unable to require cljs namespaces from clj. I can make the whole thing work, if I emit the entire `if` expression from the macro (i.e. backtick the `if` expression), but if possible I'd like to avoid doing that check at runtime, simply because everything I need to make the decision is a compile-time constant. Here are two examples of how i want to call it (from various namespaces):
;; in this case enabling the feature adds code, so there is no `disabled` expression
(features/with-feature :keycloak
   (rf/dispatch-sync [  app.config/keycloak])

;; in this case disabling the feature adds code so there is no `enabled` expression
(features/with-feature :keycloak
   (rf/reg-event-fx ::sign-out-button-clicked
      (fn [{:keys [db]} _]
         (-> (auth-db/get-session db)

Is that at all possible? Any help whatsoever is greatly appreciated - many thanks in advance...

Reply all
Reply to author
0 new messages