-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
feat(vue): experimental scope dispose #5382
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| import { onBeforeUnmount, onMounted } from 'vue'; | ||
| import { getCurrentScope, onScopeDispose } from 'vue'; | ||
| import { | ||
| Actor, | ||
| ActorOptions, | ||
|
|
@@ -34,18 +34,18 @@ export function useActorRef<TLogic extends AnyActorLogic>( | |
| ): Actor<TLogic> { | ||
| const actorRef = createActor(actorLogic, options); | ||
|
|
||
| let sub: Subscription; | ||
| onMounted(() => { | ||
| if (observerOrListener) { | ||
| sub = actorRef.subscribe(toObserver(observerOrListener)); | ||
| } | ||
| actorRef.start(); | ||
| }); | ||
| let sub: Subscription | undefined; | ||
| if (observerOrListener) { | ||
| sub = actorRef.subscribe(toObserver(observerOrListener)); | ||
| } | ||
| actorRef.start(); | ||
|
|
||
| onBeforeUnmount(() => { | ||
| actorRef.stop(); | ||
| sub?.unsubscribe(); | ||
| }); | ||
| if (getCurrentScope()) { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. shouldnt we require a scope to be there? if it's going to be missing then cleanup code wouldnt ever run but setup would and its obtained resources would live indefinitely
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Might as well wrap everything in |
||
| onScopeDispose(() => { | ||
| actorRef.stop(); | ||
| sub?.unsubscribe(); | ||
| }); | ||
| } | ||
|
|
||
| return actorRef; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,7 +8,7 @@ | |
| import { useActorRef } from '../src/index.ts'; | ||
| import { createMachine } from 'xstate'; | ||
|
|
||
| import { defineComponent, ref } from 'vue'; | ||
| import { defineComponent, nextTick, ref } from 'vue'; | ||
| const machine = createMachine({ | ||
| initial: 'inactive', | ||
| states: { | ||
|
|
@@ -28,7 +28,9 @@ const machine = createMachine({ | |
| export default defineComponent({ | ||
| setup() { | ||
| const actor = useActorRef(machine, {}, (nextState) => { | ||
| state.value = nextState.value; | ||
| nextTick(() => { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not familiar with Vue at all so bear with me. I don't understand why this is using
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ah, I guess u have touched on it in the PR's description. So a different question - wouldnt it be better to skip the write to a =n uninitialized
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would never write vue code like that in the first place with |
||
| state.value = nextState.value; | ||
| }); | ||
| }); | ||
| const state = ref(actor.getSnapshot().value); | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
how this is meant to work with SSR? when the
subwould get disposed (and when the actor would get stopped!) in that environment?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're right, onMounted and onUnmounted do not get called in SSR. This should work and respect current behaviour.
What do you think ?