keechma

hadils 2024-03-24T16:21:55.348199Z

Hi @mihaelkonjevic. I have this controller:

(ns yardwerkz.controllers.authentication
  (:require
   [keechma.next.core :as core]
   [keechma.next.controller :as ctrl]
   [keechma.next.helix.core :as khc]
   [keechma.pipelines.core :as pp :refer-macros [pipeline!]]
   [promesa.core :as p]
   [taoensso.timbre :as log]
   [yardwerkz.rn.async-storage :as async-store]
   [yardwerkz.rn.authentication :as auth]
   [cljs.core.async :as async :refer-macros [go go-loop]]
   [cljs.core.async.interop :refer [p->c] :refer-macros [<p!]]
   [yardwerkz.api :as api :refer-macros [<response!]]
   [yardwerkz.lib.util :as util]
   [yardwerkz.rn.push-notifications :as notifications]
   [yardwerkz.navigation :as navigation]
   [clojure.edn :as edn]
   [yardwerkz.websocket :as ws]))

(defn get-auth-state
  [{:keys [state*] :as ctrl}]
  (go
    (try
      (let [auth-state (<p! (async-store/get-local-store "auth-state"))
            auth-state' (edn/read-string (or auth-state "true"))]
        (swap! state* assoc :signed-out? auth-state'))
      (catch js/Error ex
        (log/error "Can't get auth-state" ex)))))

(defn save-auth-state
  [{:keys [state*] :as ctrl}]
  (go
    (try
      (<p! (async-store/set-local-store "auth-state" (str (:signed-out? @state*))))
      (catch js/Error ex
        (log/error "Can't save auth-state" ex)))))

(defn sign-in
  [{:keys [state*] :as ctrl} {:keys [uid]}]
  (go
    (swap! state* assoc :signed-out? false)
    (<p! (save-auth-state ctrl))
    (ws/start! uid)))

(defn sign-out
  [{:keys [state*] :as ctrl}]
  (go
    (swap! state* assoc :signed-out? true)
    (<p! (save-auth-state ctrl))
    (ws/stop!)))


(derive :authentication :keechma/controller)

(defmethod ctrl/start ::authentication [ctrl]
  {})

(defmethod ctrl/handle :authentication  [ctrl event payload]
  (log/debug "ctrl/handle" :authentication ctrl event payload)
  (case event
    :user/sign-in
    (ctrl/transact ctrl (fn [] (sign-in ctrl payload)))

    :user/sign-out
    (ctrl/transact ctrl (fn [] (sign-out ctrl)))

    :get-auth-state
    (ctrl/transact ctrl (fn [] (get-auth-state ctrl)))

    nil))
I am getting this warning:
Controller state updated outside transact block. Controller: :authentication

mihaelkonjevic 2024-03-24T19:58:47.751369Z

@hadilsabbagh18 try moving ctrl/transact inside the method, so it can “see” the change:

(defn get-auth-state
  [{:keys [state*] :as ctrl}]
  (go
    (try
      (let [auth-state (