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

[ui/utils] share sync subscribe logic #23341

Merged

Conversation

spalger
Copy link
Contributor

@spalger spalger commented Sep 19, 2018

In ui/public/config/config.js we have subscription logic that delivers values from an observable synchronously and also ensuring that values are received within a digest cycle. I need to do the same in #23217 but want to keep as few checks for $rootScope.$$phase as possible, so I broke the subscription logic into a shared util.

In order to make the utility a little more helpful it will also trigger fatal errors if an observable errors without having an error handler, or if observer.next(), observer.error(), or observer.complete() throw, which would normally be swallowed by RxJS.

@spalger spalger added review Team:Core Core services & architecture: plugins, logging, config, saved objects, http, ES client, i18n, etc v7.0.0 v6.5.0 labels Sep 19, 2018
@spalger spalger requested a review from azasypkin September 19, 2018 21:39
@elasticmachine

This comment has been minimized.

@elasticmachine

This comment has been minimized.

@elasticmachine
Copy link
Contributor

💚 Build Succeeded

Copy link
Member

@azasypkin azasypkin left a comment

Choose a reason for hiding this comment

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

LGTM.

$scope.$apply(() => fn(...args));
}
} catch (error) {
fatalError(error);
Copy link
Member

Choose a reason for hiding this comment

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

question: not sure if it's a big deal, but shouldn't we unsubscribe from original observable if we call fatalError when next fails?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Rxjs takes care of that because fatalError() rethrows the error and when a next() handler throws the parent is unsubscribed.

Copy link
Member

Choose a reason for hiding this comment

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

Ah, right, I forgot that fatalError always throws.

src/ui/public/utils/subscribe.test.ts Outdated Show resolved Hide resolved
* Subscribe to an observable at a $scope, ensuring that the digest cycle
* is run for subscriber hooks and routing errors to fatalError if not handled.
*/
export function subscribe<T>(
Copy link
Member

Choose a reason for hiding this comment

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

note: subscribe is a bit too generic, but I don't know a better name either, the only thing that comes to my mind is subscribeWithScope, but it's arguable. So feel free to keep it as is, just wanted to note.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I was thinking subscribeAtScope(), but then I kept misremembering it as subscribeWithScope(), so I figured we can try it out. Shouldn't be a problem to rename it if we desire down the road.

@spalger spalger force-pushed the implement/shared-sync-subscription-util branch from 771a7f7 to dd10472 Compare October 17, 2018 00:06
@elasticmachine
Copy link
Contributor

💚 Build Succeeded

@spalger spalger merged commit c0e13fd into elastic:master Oct 17, 2018
spalger pushed a commit to spalger/kibana that referenced this pull request Oct 17, 2018
In `ui/public/config/config.js` we have subscription logic that delivers values from an observable synchronously and also ensuring that values are received within a digest cycle. I need to do the same in elastic#23217 but want to keep as few checks for `$rootScope.$$phase` as possible, so I broke the subscription logic into a shared util.

In order to make the utility a little more helpful it will also trigger fatal errors if an observable errors without having an error handler, or if `observer.next()`, `observer.error()`, or `observer.complete()` throw, which would normally be swallowed by RxJS.
spalger pushed a commit that referenced this pull request Oct 17, 2018
Backports the following commits to 6.x:
 - [ui/utils] share sync subscribe logic  (#23341)
@spalger
Copy link
Contributor Author

spalger commented Oct 17, 2018

6.5/6.x: dd716f3

@spalger spalger deleted the implement/shared-sync-subscription-util branch October 17, 2018 04:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
review Team:Core Core services & architecture: plugins, logging, config, saved objects, http, ES client, i18n, etc v6.5.0 v7.0.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants