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

Is it valid to start actions in onActionMiddleware's onStart/onFinish? #519

Open
PEZO19 opened this issue Jun 12, 2023 · 2 comments
Open
Labels
❔ question General question

Comments

@PEZO19
Copy link

PEZO19 commented Jun 12, 2023

The rationale is to keep multiple (huge) collections consistent / in sync, without the need to recalculate the derived from scratch.

Eg. if I set an ObjectMap's key to 10, I'd like to subscribe to this set modelAction via onActionMiddleware and automatically set (with fast mutation, instead mobx-ish "pure recalculation") another ObjectMap's same key based on a mapper function (x=>2*x) to 20. I want them to happen in same transaction.

I assume it is not a good idea to inject such "business logic"(?) in the middleware layer, but I can not think of any other way to do it in a reactive (and not passive) way in keystone. (Mobx has observe, but it does not respect transactions and keystone onPatches only run at the end of the transactions if I am right, which is "too late" to keep data consistent that way.)

@xaviergonz
Copy link
Owner

another alternative would be to mark the mapped object as "dirty" and make the mapped object recalculate on "get" if dirty is true

@xaviergonz xaviergonz added the ❔ question General question label Jun 12, 2023
@PEZO19
Copy link
Author

PEZO19 commented Jun 13, 2023

@xaviergonz Regarding #519 (comment):

My point is about having the freedom to eliminate "recalculation" (from scratch). I am totally fine if 95% of my app works with recalculation (it's easier to reason about + simpler code), but that 5% must rely on deep observation and listening to deep changes and propagating these deep changes solely. Also, this part must be reactive and can not be passive. (Explicit mutation of mapped is not good enough, does not scale regarding DX.)

As onPatches does not help inside of a transaction, my attempts was to treat actions like these changes events which "needs to be propagated". For that, I'd need the freedom to nest "onAction"s and as part of that, to emit new actions.

Just and idea: it would be nice if we could insert a callback (eg used with standaloneAction) before and especially after an action. So, beyond onStart and onFinish there could be a beforeStartStandaloneAction and afterFinishStandaloneAction - as sibling actions, respecting the boundaries.

However it would be also interesting something like afterStartStandaloneAction and beforeFinishStandaloneAction which could be ("pseudo")children actions of the target action, called before/after anything else in the action.

Is my assumption good, that currently it is not valid to start actions in onActionMiddleware hooks?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
❔ question General question
Projects
None yet
Development

No branches or pull requests

2 participants