Skip to content

Forbid imperative calls of effector units from reducers #541

@AlexandrHoroshih

Description

@AlexandrHoroshih

Proposal

Throw error or show a console warning, if event or effect have been manually called from inside of store reducer or other pure functions.

Currently effector does not forbid or warn about event or effect calls inside reducers or other supposed-to-be-pure functions like fn of sample or combine rule

But, as it can be seen at effector telegram chat, many people do call events from reducers sometimes (and sometimes for a really long time) and have no clue, that it is not how it supposed to work.

Even though effector, apparently, still can handle that, using events this way goes way too much aganist declared rules and some projects may found themselves absolutely incompatible with some future release of effector

I think, explicit warning (or even exception) like

effector events must not be called from pure functions like store reducers or fn of sample.
Use effector operators instead.
Or, if you really need imperative control flow, use watch or effect for that

will help a lot here

Use case

Today an effector user shared the way he has been using .on for a long time - it was something like this:

// this code must be either rewritten to operators or written in the same way, but inside `.watch`
$store.on(someEvent, (state, payload) => { 
 // store reducer meant to update stored value
 if (payload.some) {
   // but there is some control flow happening instead, which is wrong
   someEffect(state)
 } else {
  someOtherEvent(payload)
 }
 // next state is never returned - plain wrong usage of `.on`
})

From his words, he was using effector like that from the very beginning and have not seen any problems yet.

Obviously, all of it mentioned in the documentation, but it looks like that sometimes people do not read it carefully enough and little push is needed

Metadata

Metadata

Assignees

Labels

RFCRequest for Commentscoreeffector package

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions