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

Generic promise-based passthough actions (convenient way to access ChangeableDocument in a thread-safe way) #609

Open
Equbuxu opened this issue Feb 4, 2024 · 1 comment
Labels
enhancement New feature or request

Comments

@Equbuxu
Copy link
Member

Equbuxu commented Feb 4, 2024

Passthough actions (AddSoftSelectedMember_PassthroughAction, RemoveViewport_PassthroughAction) are used as a way to run code as part of ActionAccumulator action processing pipeline. The pipeline generally operates in a 1. Pass accumulated actions to ChangeableDocument -> 2. Wait for them to be processed -> 3. Update UI stuff based on the results of processing cycle. Passthrough actions are "dummy" actions, they don't do anything in ChangeableDocument, and instead that pass straight though and trigger some code to be executed in DocumentUpdater. Since this happens in the stage 3 of the pipeline, there is a guarantee that no code is currently being executed in a separate thread in ChangeableDocument (it only happens during stage 2).

Right now passthough actions are inconvenient to use, as you have to create a whole new class, then add a condition to DocumentUpdater, and then somehow pass the "return" value to whereever you need it.

My initialy idea is to make a single generic passthough action that would contain a lambda to be executed. Something in DocumentUpdater would just call that lambda. This already solves the "create new class for each action" problem, but we can do even better.

The passthough action can also contain a promise. DocumentUpdater would be resolve that promise with the returned value of the lambda. A helper method that conveniently creates a new PassthoughAction from lambda and returns it's promise can be made. As a result, you'd be able to do something like:

...
var result = await ActionAccumulator.ExecuteInPipeline(a => ...);
...

This makes writing thread safe code very easy.

Now, I swear that 3 months ago when I came up with this idea I saw some giant caveat along the lines of "This is all very cool, but if you capture a wrong this into the lambda and do something with it, everything will break". However hard I try I can't remember what it was, nor can I come up with anything new that would break stuff. So everything's probably fine?

@Equbuxu Equbuxu added the enhancement New feature or request label Feb 4, 2024
@flabbet
Copy link
Member

flabbet commented Feb 5, 2024

That looks very good, I like it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants