$onAction not always firing? #1400
-
Reproductionhttps://stackblitz.com/edit/github-cktvx5-g4kkro?file=src/stores/index.js Steps to reproduce the bugSetup:
I'm writing an undo-redo plugin, and it is semi working. My approach is subscribing to actions, then recording those. The problem is that only some actions seem to "heard"... and worse still it's intermittent 😢 Plugin code so far, any advice most appreciated: export default store((/* { ssrContext } */) => {
const pinia = createPinia()
// add Pinia plugins
pinia.use(history)
return pinia
})
const TIMETRAVEL = 'timetravel'
function history ({ store, options }) {
if (!options.history) return
const past = reactive([])
const future = reactive([])
store.$onAction(action => {
// do not record timetravel actions
if (isTimetravel(action)) return
past.push({ name: action.name, payload: action.args })
while (future.length) future.pop()
})
const replay = () => {
store.$reset()
past.forEach(action => store[action.name](...action.payload, TIMETRAVEL))
}
return {
past,
canUndo: computed(() => past.length > 0),
canRedo: computed(() => future.length > 0),
undo () {
const action = past.pop()
future.push(action)
replay()
},
redo () {
const action = future.pop()
past.push(action)
replay()
}
}
}
function isTimetravel (action) {
return action.args[action.args.length - 1] === TIMETRAVEL
} Expected behaviorthe $onAction callback should fire ever time, on all actions for stores which "turn on" history Actual behaviorWhen I try to use this history plugin in a store, I see one action triggering onAction almost always, and one almost never import { defineStore } from 'pinia'
export const useNetworkDataStore = defineStore('network/data', {
state: () => ({
seedNodes: {
// nodeId: data..
},
directedLinks: {
// A: { B: true }
}
}),
getters: {
// ...
},
actions: {
addNode (node) {
// this one is always heard
},
addDirectedLinks (links) {
// this one is almost never heard
}
},
history: true
// WARNING - records and replays *actions*
// DO NOT put side-effects in actions, otherwise these will all be replayed!
}) Additional informationNo response |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 6 replies
-
@posva is this not a possible bug? If I misread the docs and used the plugin system wrong that would be great (ie no bug). |
Beta Was this translation helpful? Give feedback.
-
An update on this: I defineStore('id', ({ action }) => {
const n = ref(0)
const increment = action((amount = 1) => n.value += amount)
const decrement = action((amount) => increment(-amount))
return { n, increment }
}) With the setup above, calling This approach is opt-in, everything will work the same without it. It feels a bit clunky but it's not invasive and really simple without the need of a build tool. Feedback welcome of course |
Beta Was this translation helpful? Give feedback.
-
Regarding the current situation when I want to call an action from the same store and want to trigger |
Beta Was this translation helpful? Give feedback.
I completely missed this 👀
The problem in the repro is that no action is being called. It works when calling the actions.
A limitation you might have encountered is that
$onAction()
cannot be aware of actions invoked by other actions in setup stores. This is a technical limitation that needs some investigation but should be resolved in a new major. @guyaumetremblay I recommend you to try and boil it down in the playground to see if it's a bugFYI: issues are moved to discussions when they don't follow the reporting guidelines (instructions in "new issue" page). In your case, I can't remember anymore but it was probably that this is just a usage question with an editable example not a bug …