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

RUMM-2299 Core Context Application State Publisher #928

Merged
merged 3 commits into from
Jul 20, 2022

Conversation

maxep
Copy link
Member

@maxep maxep commented Jul 12, 2022

What and why?

Publish application state to Datadog Context.

How?

Implement a AppStatePublisher that complies to ContextValuePublisher and provide AppStateHistory to the context.

Review checklist

  • Feature or bugfix MUST have appropriate tests (unit, integration)
  • Make sure each commit and the PR mention the Issue number or JIRA reference
  • Add CHANGELOG entry for user facing changes

Custom CI job configuration (optional)

  • Run unit tests
  • Run integration tests
  • Run smoke tests

@maxep maxep self-assigned this Jul 12, 2022
@maxep maxep force-pushed the maxep/RUMM-2299/core-context-app-state-publisher branch 2 times, most recently from 26dc277 to 36a77a5 Compare July 19, 2022 13:41
@maxep maxep changed the base branch from develop to feature/v2 July 19, 2022 13:48
@maxep maxep force-pushed the maxep/RUMM-2299/core-context-app-state-publisher branch from 36a77a5 to 9c82e05 Compare July 19, 2022 14:02
@maxep maxep marked this pull request as ready for review July 19, 2022 14:03
@maxep maxep requested a review from a team as a code owner July 19, 2022 14:03
@maxep maxep force-pushed the maxep/RUMM-2299/core-context-app-state-publisher branch 3 times, most recently from 29594d9 to bab5809 Compare July 19, 2022 15:45
Copy link
Member

@ncreated ncreated left a comment

Choose a reason for hiding this comment

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

Awesome 🚀. I left few non-blocking feedbacks 🙌.

self.notificationCenter = notificationCenter
}

func publish(to receiver: @escaping ContextValueReceiver<AppStateHistory>) {
Copy link
Member

Choose a reason for hiding this comment

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

nit/ The more often I see this in different publishers, the more I think it should be called startPublishing(to:) to better express what it should do 💭. Just a note, I'm totally fine with keeping existing name (after all, it is well explained in the protocol comment).

Copy link
Member Author

Choose a reason for hiding this comment

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

That would work, current name also correctly reflect what it should do, at the protocol level, IMO. publisher.publish(to receiver:) Lets keep it as is for now 👍

Comment on lines 45 to 58
/// Creates a Application state publisher for publishing application state
/// history.
///
/// - Parameters:
/// - initialState: The initial application state.
/// - queue: The queue for publishing the history.
/// - dateProvider: The date provider for the Application state snapshot timestamp.
/// - notificationCenter: The notification center where this publisher observes `UIApplication` notifications.
init(
initialState: AppState,
queue: DispatchQueue = AppStatePublisher.defaultQueue,
dateProvider: DateProvider = SystemDateProvider(),
notificationCenter: NotificationCenter = .default
) {
Copy link
Member

Choose a reason for hiding this comment

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

nit/ As we have a nice doc string now (🏅), I think it's worth adding in that it needs to be called on the main thread, because it calls application. applicationState transitively:

private static var currentApplicationState: UIApplication.State {
   UIApplication.managedShared?.applicationState ?? .active // fallback to most expected state
}

WDYT?

Suggested change
/// Creates a Application state publisher for publishing application state
/// history.
///
/// - Parameters:
/// - initialState: The initial application state.
/// - queue: The queue for publishing the history.
/// - dateProvider: The date provider for the Application state snapshot timestamp.
/// - notificationCenter: The notification center where this publisher observes `UIApplication` notifications.
init(
initialState: AppState,
queue: DispatchQueue = AppStatePublisher.defaultQueue,
dateProvider: DateProvider = SystemDateProvider(),
notificationCenter: NotificationCenter = .default
) {
/// Creates a Application state publisher for publishing application state
/// history.
///
/// **Note**: It must be called on the main thread.
///
/// - Parameters:
/// - initialState: The initial application state.
/// - queue: The queue for publishing the history.
/// - dateProvider: The date provider for the Application state snapshot timestamp.
/// - notificationCenter: The notification center where this publisher observes `UIApplication` notifications.
init(
initialState: AppState,
queue: DispatchQueue = AppStatePublisher.defaultQueue,
dateProvider: DateProvider = SystemDateProvider(),
notificationCenter: NotificationCenter = .default
) {

Copy link
Member Author

Choose a reason for hiding this comment

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

Oh indeed, but this should be documented in the extension below, the one that uses UIKit.
I didn't know about threading constraint on UIApplication/applicationState. This is a good argument to make the subscription/assignment thread-safe as you mentioned here so we can dispatch the ApplicationStatePublisher init and subscription on the main thread!

Copy link
Member

Choose a reason for hiding this comment

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

Ah, indeed it has a consequence on subscriptions (array) thread-safety. Good call 👍.

@maxep maxep force-pushed the maxep/RUMM-2299/core-context-app-state-publisher branch from bab5809 to 2bb18fa Compare July 20, 2022 08:50
@maxep maxep force-pushed the maxep/RUMM-2299/core-context-app-state-publisher branch from 2bb18fa to f75119a Compare July 20, 2022 09:33
@maxep maxep merged commit 6c7d84b into feature/v2 Jul 20, 2022
@maxep maxep deleted the maxep/RUMM-2299/core-context-app-state-publisher branch July 20, 2022 10:36
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.

2 participants