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

runOrScheduleOnMainActor function #18

Merged
merged 8 commits into from
Jan 6, 2025
Merged

runOrScheduleOnMainActor function #18

merged 8 commits into from
Jan 6, 2025

Conversation

lukaskollmer
Copy link
Member

@lukaskollmer lukaskollmer commented Jan 6, 2025

runOrScheduleOnMainActor function

♻️ Current situation & Problem

As part of dealing with the onPreferenceChange API changes, we ended up having to duplicate code in a bunch of places, in a way that always used the same fundamental pattern (ie, if we're on the MainActor, run a piece of code directly, otherwise schedule it onto the MainActor).
This PR simply abstracts this into a new global func runOrScheduleOnMainActor.

⚙️ Release Notes

  • Added runOrScheduleOnMainActor function

📚 Documentation

n/a; all new code is directly documented.

✅ Testing

there are new test cases for the new function

📝 Code of Conduct & Contributing Guidelines

By submitting creating this pull request, you agree to follow our Code of Conduct and Contributing Guidelines:

@lukaskollmer lukaskollmer changed the title RunOrScheduleOnMainActor function runOrScheduleOnMainActor function Jan 6, 2025
the previous version could in some situations fail, if the main loop ran concurrently/before the schedule function returned and the TestActor continued execution (at least that's my guess, based on the fact that in some test runs the bool would unexpectedly be true.
Copy link

codecov bot commented Jan 6, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 90.61%. Comparing base (5b4ad1b) to head (d33722b).
Report is 1 commits behind head on main.

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main      #18      +/-   ##
==========================================
+ Coverage   90.44%   90.61%   +0.18%     
==========================================
  Files          13       14       +1     
  Lines         596      607      +11     
==========================================
+ Hits          539      550      +11     
  Misses         57       57              
Files with missing lines Coverage Δ
...eziFoundation/Concurrency/MainActorExecution.swift 100.00% <100.00%> (ø)

Continue to review full report in Codecov by Sentry.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 5b4ad1b...d33722b. Read the comment docs.

Copy link
Member

@PSchmiedmayer PSchmiedmayer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me; thank you @lukaskollmer 👍

Copy link
Member

@philippzagar philippzagar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR @lukaskollmer! 🚀
Let's tag this quickly so that we can use it within SpeziViews and SpeziChat!

@lukaskollmer lukaskollmer enabled auto-merge (squash) January 6, 2025 16:10
@lukaskollmer lukaskollmer merged commit 502937d into main Jan 6, 2025
10 checks passed
@lukaskollmer lukaskollmer deleted the lukas/mainactor branch January 6, 2025 16:51
philippzagar added a commit to StanfordSpezi/SpeziChat that referenced this pull request Jan 7, 2025
# Use SpeziFoundation `runOrScheduleOnMainActor()`

## ♻️ Current situation & Problem
Currently, SpeziChat manually checks and then runs or schedules (on the
`MainActor`) `onPreferenceClosure()`s changes that modify `MainActor`
isolated `State` properties in the SwiftUI `View`.
With StanfordSpezi/SpeziFoundation#18, this lengthy and repetitive code
piece is now abstracted within SpeziFoundation.


## ⚙️ Release Notes 
- Use [SpeziFoundation
`runOrScheduleOnMainActor()`](StanfordSpezi/SpeziFoundation#18)
function


## 📚 Documentation
Proper documentation changes in DocC and Markdown.


## ✅ Testing
No new test cases added, as behavior stays strictly the same.


## 📝 Code of Conduct & Contributing Guidelines 

By submitting creating this pull request, you agree to follow our [Code
of
Conduct](https://github.com/StanfordSpezi/.github/blob/main/CODE_OF_CONDUCT.md)
and [Contributing
Guidelines](https://github.com/StanfordSpezi/.github/blob/main/CONTRIBUTING.md):
- [x] I agree to follow the [Code of
Conduct](https://github.com/StanfordSpezi/.github/blob/main/CODE_OF_CONDUCT.md)
and [Contributing
Guidelines](https://github.com/StanfordSpezi/.github/blob/main/CONTRIBUTING.md).
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

Successfully merging this pull request may close these issues.

3 participants