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

Simplify User Service #23818

Draft
wants to merge 1 commit into
base: trunk
Choose a base branch
from
Draft

Simplify User Service #23818

wants to merge 1 commit into from

Conversation

jkmassel
Copy link
Contributor

Fixes #

To test:

Regression Notes

  1. Potential unintended areas of impact

  2. What I did to test those areas of impact (or what existing automated tests I relied on)

  3. What automated tests I added (or what prevented me from doing so)

PR submission checklist:

  • I have completed the Regression Notes.
  • I have considered adding unit tests for my changes.
  • I have considered adding accessibility improvements for my changes.
  • I have considered if this change warrants user-facing release notes and have added them to RELEASE-NOTES.txt if necessary.

Testing checklist:

  • WordPress.com sites and self-hosted Jetpack sites.
  • Portrait and landscape orientations.
  • Light and dark modes.
  • Fonts: Larger, smaller and bold text.
  • High contrast.
  • VoiceOver.
  • Languages with large words or with letters/accents not frequently used in English.
  • Right-to-left languages. (Even if translation isn’t complete, formatting should still respect the right-to-left layout)
  • iPhone and iPad.
  • Multi-tasking: Split view and Slide over. (iPad)

@jkmassel jkmassel changed the title Simplifty User Service Simplify User Service Nov 18, 2024
@dangermattic
Copy link
Collaborator

2 Warnings
⚠️ Modules/Package.swift was changed without updating its corresponding Package.resolved. Please resolve the Swift packages as appropriate to your project setup (e.g. in Xcode or by running swift package resolve).
⚠️ View files have been modified, but no screenshot or video is included in the pull request. Consider adding some for clarity.
1 Message
📖 This PR is still a Draft: some checks will be skipped.

Generated by 🚫 Danger

@jkmassel jkmassel force-pushed the simplify/user-service branch from 27a79c5 to 14f860c Compare November 18, 2024 01:25
@wpmobilebot
Copy link
Contributor

Jetpack Alpha📲 You can test the changes from this Pull Request in Jetpack Alpha by scanning the QR code below to install the corresponding build.
App NameJetpack Alpha Jetpack Alpha
ConfigurationRelease-Alpha
Build Numberpr23818-14f860c
Version25.4.2
Bundle IDcom.jetpack.alpha
Commit14f860c
App Center Buildjetpack-installable-builds #10103
Automatticians: You can use our internal self-serve MC tool to give yourself access to App Center if needed.

@wpmobilebot
Copy link
Contributor

WordPress Alpha📲 You can test the changes from this Pull Request in WordPress Alpha by scanning the QR code below to install the corresponding build.
App NameWordPress Alpha WordPress Alpha
ConfigurationRelease-Alpha
Build Numberpr23818-14f860c
Version25.4.2
Bundle IDorg.wordpress.alpha
Commit14f860c
App Center BuildWPiOS - One-Offs #11063
Automatticians: You can use our internal self-serve MC tool to give yourself access to App Center if needed.

[Cacheable](objects.values)
}

func invalidate(_ id: Cacheable.ID) {
Copy link
Contributor

@kean kean Nov 18, 2024

Choose a reason for hiding this comment

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

If it removes an object, let's call it remove.

func invalidate(_ id: Cacheable.ID)
}

actor InMemoryCache<Cacheable: Identifiable & Sendable>: ObjectCache {
Copy link
Contributor

Choose a reason for hiding this comment

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

I think it should ultimately be modeled closely to how NSManagedObjectContext works: data-driven and with a single main-thread confined context with all entities that you can read/write synchronously and you can easily observe.

It's generally not a good idea to introduce actors because you end up with more concurrency than you need, making it hard to understand and debug what's going on and in which order. For the UI layer, I'd use @MainActor. For a model layer, it's generally best to synchronize all subsystems on a single global actor to eliminate as much concurrency as possible.


for try await page in await self.client.api
.users
.sequenceWithEditContext(params: UserListParams(perPage: 5))
Copy link
Contributor

@kean kean Nov 18, 2024

Choose a reason for hiding this comment

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

I'm not sure AsyncThrowingStream is the right API to model paging because if it errors out, the stream terminates. There is no way to restart fetching for a given page, is there?

It may be convenient to use AsyncThrowingStream in the scenario where you want to fetch everything at once – though you should probably never do that – but it can be built on top of a more primitive API like func fetchPage(withID:) async throws -> [Element], likely even in a nice generalized way.

func fetchPaginatedUsers() -> AsyncThrowingStream<[DisplayUser], Error> {
AsyncThrowingStream { continuation in
let handle = Task {
let all = await self.cache.all()
Copy link
Contributor

Choose a reason for hiding this comment

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

I believe there is a retain cycle here: self -> tasks -> handle: Task -> self.

self.users = users
return users
}
func fetchPaginatedUsers() -> AsyncThrowingStream<[DisplayUser], Error> {
Copy link
Contributor

Choose a reason for hiding this comment

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

If I understand this correclty, this stream's outputs are cached users (which may be empty or all the pre-fetched users), users at page 1, users at page 2, ..., END. If that's the case, how fetchPaginatedUsers is used in the View Model is wrong. The View Model should not collect all the stream's outputs into an array.

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.

5 participants