<commit_before>### Question

How do I access the value of a subscription from within an event handler? 

### The Wrong Way

You should NOT do this:
```clj
(re-frame.core/reg-event-db
  :event-id 
  (fn [db v]
    (let [sub-val  @(subscribe [:something])]   ;; <--- Eeek
       ....)))  
```

If you do:
1. You might create a memory leak (the subscription might not be "freed")
2. You have just made you event handler impure (it reaches out and grabs a global value). 
 
### The Better Way

Instead, to make the value of a subscription available within an event handler, it has to 
be injected into the `coeffects` of that handler via an interceptor.

The overall sketch is:
```clj
(re-frame.core/reg-event-fx     ;; handler must access coeffects, so use -fx
  :event-id 
  (inject-sub  [:query-id :param])  ;; <-- will inject subscription value into coeffects
  (fn [coeffect event]
    (let [sub-val  (:something coeffect)]  ;; obtain subscription value 
       ....)))
```

### Solutions

re-frame doesn't yet have a built in interceptor to do this injection. 

So you'll need to create your own or you may want to use this 3rd party library: 
https://github.com/vimsical/re-frame-utils/blob/master/src/vimsical/re_frame/cofx/inject.cljc


<!-- START doctoc generated TOC please keep comment here to allow auto update -->
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
<!-- END doctoc generated TOC please keep comment here to allow auto update -->



<commit_msg>FAQ update<commit_after>### Question

How do I access the value of a subscription from within an event handler?

### The Wrong Way

You should NOT do this:
```clj
(re-frame.core/reg-event-db
  :event-id 
  (fn [db v]
    (let [sub-val  @(subscribe [:something])]   ;; <--- Eeek
       ....)))
```

because that `subscribe`:
1. might create a memory leak (the subscription might not be "freed")
2. makes the event handler impure (it grabs a global value)

### The Better Way

Instead, the value of a subscription should  
be injected into the `coeffects` of that handler via an interceptor.

A sketch:
```clj
(re-frame.core/reg-event-fx     ;; handler must access coeffects, so use -fx
  :event-id 
  (inject-sub  [:query-id :param])  ;; <-- will inject subscription value into coeffects
  (fn [coeffect event]
    (let [sub-val  (:something coeffect)]  ;; obtain subscription value 
       ....)))
```

Notes:
1. `inject-sub` is an interceptor which will get the subscription value and add it to coeffects (somehow)
2. The event handler obtains the value from coeffects

So how to write this interceptor?

### Solutions

re-frame doesn't yet have a builtin `inject-sub` interceptor to do this injection.

I'd suggest you use this 3rd party library: 
https://github.com/vimsical/re-frame-utils/blob/master/src/vimsical/re_frame/cofx/inject.cljc


<!-- START doctoc generated TOC please keep comment here to allow auto update -->
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
<!-- END doctoc generated TOC please keep comment here to allow auto update -->




{'fraction_non_alphanumeric': 0.11365120836054866, 'fraction_numerical': 0.002612671456564337, 'mean_word_length': 3.3632478632478633, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 9, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1206185', 'n_tokens_mistral': 989, 'n_tokens_neox': 908, 'n_words': 397}