feat(vue): experimental scope dispose#5382
feat(vue): experimental scope dispose#5382Hebilicious wants to merge 2 commits intostatelyai:mainfrom
Conversation
|
|
@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(() => { |
There was a problem hiding this comment.
I'm not familiar with Vue at all so bear with me. I don't understand why this is using nextTick. 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 updating state.value here?
There was a problem hiding this comment.
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 state instead of wrapping this update with nextTick?
There was a problem hiding this comment.
I would never write vue code like that in the first place with nextTick 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.
| let sub: Subscription | undefined; | ||
| if (observerOrListener) { | ||
| sub = actorRef.subscribe(toObserver(observerOrListener)); | ||
| } | ||
| actorRef.start(); |
There was a problem hiding this comment.
how this is meant to work with SSR? when the sub would get disposed (and when the actor would get stopped!) in that environment?
There was a problem hiding this comment.
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()) { |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Might as well wrap everything in if(getCurrentScope()) since this should not run on SSR.
|
What would be the advantage of moving to this API? how that would affect supported range of Vue versions? |
|
Hi @Andarist, One of the reasons I've started doing this in all my vue + xstate projects is because very often, you want to Consider the following real world example, where I'm constructing data to pass it to tanstack table : const data = computed(() =>
operations
.map(({ id, operation }) => {
const state = 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() ??
new Date().toISOString()
}
})
)Current useSelector implementation causes a bunch of "issues" with vue because component lifecycles don't exist outside a component setup function. Regarding version support, I believe it got introduced in 3.2 https://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 |
This is a draft PR that attempts to move away from component life-cycle hooks in composables to effect scopes .
All tests are passing, with one caveat :
Now
nextTickneeds to be used when passing a callback touseActorRefthat access state that is not initialized.I feel like this is fine because this is kind of an odd pattern anyways.
Using
onMountedis more idiomatic to vue imo, and maybe we should change the test to :(Users should not be using useActorRef directly in most cases anyways, so its fine if the test doesn't use shallowRef)