Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Draft
Hebilicious wants to merge2 commits intostatelyai:main
base:main
Choose a base branch
Loading
fromHebilicious:experimental/scope-dispose

Conversation

@Hebilicious
Copy link

@HebiliciousHebilicious commentedOct 2, 2025
edited
Loading

This is a draft PR that attempts to move away from component life-cycle hooks in composables toeffect scopes.

All tests are passing, with one caveat :

NownextTick needs to be used when passing a callback touseActorRef that access state that is not initialized.

constactor=useActorRef(machine,{},(nextState)=>{nextTick(()=>{state.value=nextState.value;});});conststate=ref(actor.getSnapshot().value);

I feel like this is fine because this is kind of an odd pattern anyways.
UsingonMounted is more idiomatic to vue imo, and maybe we should change the test to :

conststate=ref()//shallowRef would work tooconstactor=useActorRef(machine,{},(nextState)=>{state.value=nextState.value;});onMounted(()=>{state.value=actor.getSnapshot().value});

(Users should not be using useActorRef directly in most cases anyways, so its fine if the test doesn't use shallowRef)

@changeset-bot
Copy link

⚠️ No Changeset found

Latest commit:6d6c6ba

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go.If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

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

@Hebilicious
Copy link
Author

@Andarist I've been testing this in production, it appears to be an improvement. Would you mind taking a look please?

setup() {
const actor=useActorRef(machine, {}, (nextState)=> {
state.value=nextState.value;
nextTick(()=> {
Copy link
Collaborator

Choose a reason for hiding this comment

The 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 usingnextTick. Shouldn't the update be batched with whatever changes are in flight instead of waiting for the current DOM updates to flush and then scheduling a new update by updatingstate.value here?

Copy link
Collaborator

Choose a reason for hiding this comment

The 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 uninitializedstate instead of wrapping this update withnextTick?

Copy link
Author

Choose a reason for hiding this comment

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

I would never write vue code like that in the first place withnextTick either, but it kinda works, even though it is hacky... I don't think we should encourage this pattern tbh, imo lifecycle hooks are the right pattern to initalize a value, as documented in the PR description.
But I wanted to hear your opinion instead of straight-up replacing the text.

Comment on lines +37 to +41
letsub:Subscription|undefined;
if(observerOrListener){
sub=actorRef.subscribe(toObserver(observerOrListener));
}
actorRef.start();
Copy link
Collaborator

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 thesub would get disposed (and when the actor would get stopped!) in that environment?

Copy link
Author

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.

if(getCurrentScope()){if(observerOrListener){sub=actorRef.subscribe(toObserver(observerOrListener));}actorRef.start();}

What do you think ?

actorRef.stop();
sub?.unsubscribe();
});
if(getCurrentScope()){
Copy link
Collaborator

Choose a reason for hiding this comment

The 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

Copy link
Author

Choose a reason for hiding this comment

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

Might as well wrap everything inif(getCurrentScope()) since this should not run on SSR.

@Andarist
Copy link
Collaborator

What would be the advantage of moving to this API? how that would affect supported range of Vue versions?

@Hebilicious
Copy link
Author

Hebilicious commentedOct 30, 2025
edited
Loading

Hi@Andarist, One of the reasons I've started doing this in all my vue + xstate projects is because very often, you want touseSelector in an array of actors.

Consider the following real world example, where I'm constructing data to pass it to tanstack table :

constdata=computed(()=>operations.map(({ id, operation})=>{conststate=useSelector(operation,(state)=>state)const{ text, kind}=statusFromState(state.value.value,state.value.context.direction)return{id,operation,status:state.value.value,statusText:text,statusKind:kind,source:state.value.context.source,destination:state.value.context.destination,amount:state.value.context.amount,hash:state.value.context.transaction?.hash??"",date:state.value.context.transaction?.date.toISOString()??newDate().toISOString()}}))

Current useSelector implementation causes a bunch of "issues" with vue because component lifecycles don't exist outside a component setup function.
SeeRFC for effectScope.

Regarding version support, I believe it got introduced in 3.2https://blog.vuejs.org/posts/vue-3-2, so this is reasonable (4y old +), but would require a bump to 3.2 min (https://github.com/statelyai/xstate/blob/main/packages/xstate-vue/package.json)

I believe this would also resolve#4754; and#5311 partially

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

@AndaristAndaristAndarist left review comments

At least 1 approving review is required to merge this pull request.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

2 participants

@Hebilicious@Andarist

[8]ページ先頭

©2009-2025 Movatter.jp