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

Call subscription.complete on interpreter stop #3453

Merged
merged 3 commits into from
Aug 3, 2022

Conversation

pixtron
Copy link
Contributor

@pixtron pixtron commented Jul 10, 2022

When an interpreter is stoped, all listeners get removed, therefore a potential subscription should complete.

Closes: #3452

@changeset-bot
Copy link

changeset-bot bot commented Jul 10, 2022

🦋 Changeset detected

Latest commit: 60572d9

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
xstate Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@codesandbox-ci
Copy link

codesandbox-ci bot commented Jul 10, 2022

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit 60572d9:

Sandbox Source
XState Example Template Configuration
XState React Template Configuration
blissful-babbage-i714je Issue #3452

@ghost
Copy link

ghost commented Jul 10, 2022

👇 Click on the image for a new way to code review
  • Make big changes easier — review code in small groups of related files

  • Know where to start — see the whole change at a glance

  • Take a code tour — explore the change with an interactive tour

  • Make comments and review — all fully sync’ed with github

    Try it now!

Review these changes using an interactive CodeSee Map

Legend

CodeSee Map Legend

@@ -388,8 +388,8 @@ export class Interpreter<
return this;
}
public subscribe(
observer: Observer<
State<TContext, TEvent, any, TTypestate, TResolvedTypesMeta>
observer: Partial<
Copy link
Member

Choose a reason for hiding this comment

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

I've rechecked with RxJS and listener types are actually all optional (well, not exactly, but I'm not sure if we want to do this).

It was really annoying in tests for me to implement this:

service.subscribe({
  next: noop,
  error: noop,
  complete: completeCb
})

and I think that it makes sense to make it easier to just provide a single callback type, so I've followed that instinct and wrapped Observer at the parameter position Partial

this.doneListeners.delete(resolvedCompleteListener);
this.listeners.delete(observer.next);
this.doneListeners.delete(completeOnce);
this.stopListeners.delete(completeOnce);
Copy link
Member

Choose a reason for hiding this comment

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

this is an additional bug fix, we were missing a call to remove a stop listener

Comment on lines +431 to +432
this.onDone(completeOnce);
this.onStop(completeOnce);
Copy link
Member

Choose a reason for hiding this comment

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

this was a little bit annoying, but I think it's the cleanest solution possible right now. This probably will be completely refactored in v5 anyway.

Comment on lines +711 to +715
next: ((isObserver ? nextHandler.next : nextHandler) || noop).bind(self),
error: ((isObserver ? nextHandler.error : errorHandler) || noop).bind(self),
complete: (
(isObserver ? nextHandler.complete : completionHandler) || noop
).bind(self)
Copy link
Member

Choose a reason for hiding this comment

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

I don't think that we really need those binds here but we were already binding next listener in the Interpreter#subscribe. So I didn't want to regress this accidentally and I've just used the same code pattern for all the callbacks here.

Copy link
Contributor

@mellson mellson left a comment

Choose a reason for hiding this comment

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

The code looks top quality 👍🏻

@Andarist Andarist merged commit 368ed9b into statelyai:main Aug 3, 2022
@pixtron pixtron deleted the issue-3452 branch August 3, 2022 08:32
@github-actions github-actions bot mentioned this pull request Aug 3, 2022
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.

Bug: completeListener on Subscription is not triggered on stop
3 participants