Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve exception handling associated with subs #633

Open
olivergeorge opened this issue Jul 10, 2020 · 5 comments
Open

Improve exception handling associated with subs #633

olivergeorge opened this issue Jul 10, 2020 · 5 comments

Comments

@olivergeorge
Copy link
Contributor

olivergeorge commented Jul 10, 2020

This code will throw errors due to the registered sub handler throwing an exception. The exception gets cached so that the same error appears after fixing and re-registering the sub at the repl.

In order to repeat remove the assert or register a new sub which doesn't throw then re-render the app.

The workaround is calling clear-subscription-cache!.

Ideas to improve this behaviour and make it less surprising

  • clear the cache when subs are re-registered to avoid data getting out of sync
  • don't cache exceptions
(ns hello-world.case1
  (:require [re-frame.core :as rf]
            [reagent.dom :as rdom]))

(rf/reg-sub ::throwing-sub (fn [_ _] (assert (= 2 4)) "fn sub"))

(defn app []
  [:p @(rf/subscribe [::throwing-sub])])

(rdom/render [app] (js/document.getElementById "app"))
@olivergeorge olivergeorge changed the title Clear sub cache when a sub is re-registered Improve exception handing associated with subs Jul 10, 2020
@mike-thompson-day8
Copy link
Contributor

@olivergeorge
In your reload function, are you calling re-frame.core/clear-subscription-cache!, like this:
https://github.com/day8/re-frame/blob/master/examples/todomvc/src/todomvc/core.cljs#L58

@olivergeorge
Copy link
Contributor Author

This repro was done using vanilla clojurescript REPL (as per the getting started guide).

I think it's good to consider the REPL experience rather than relying on hooks associated with hot-reloading. From the REPL I can call clear-subscription-cache! manually - but it's another thing to remember.

@olivergeorge
Copy link
Contributor Author

olivergeorge commented Jul 10, 2020

Here's a concrete suggestion. What if the cache invalidated when a subscription was re-registered.

The cache should definitely not be trusted in this case since the subscriptions would be out of sync.

The old "warning: re-registering handler" message is gone but how about in it's place we invalidate the cache when it's a subscription re-register.

If this was implemented there would be one less "setup step" for effective re-frame development and the REPL experience would be more seamless.

@olivergeorge
Copy link
Contributor Author

Thinking about possible complexities / side effects...

Clearing the subscription cache doesn't appear to trigger a re-render of the app. We can clear the cache without other code running (good) but that seems to imply we'd still need a hot-reload hook to tell the app to refresh itself.

@olivergeorge
Copy link
Contributor Author

Here's a possible approach which modifies reg-sub
57959c2

@kimo-k kimo-k changed the title Improve exception handing associated with subs Improve exception handling associated with subs Nov 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants