senecajs / seneca-entity

Entity plugin for seneca
MIT License
13 stars 15 forks source link

Fix priors #79

Closed lilsweetcaligula closed 3 months ago

lilsweetcaligula commented 1 year ago

The code below currently crashes with the "no_prior_action" error:

const seneca = Seneca()

seneca.use('entity', {
    transaction: {
        active: true
    }
})

seneca.add('cmd:helloWorld', function (args, reply) {
    console.log('helloWorld 2') // dbg
    reply()
})

seneca.add('cmd:helloWorld', function (args, reply) {
    console.log('helloWorld 1') // dbg

    this.entity.transaction().then(trx => {
        trx.prior(args, reply)
    })
})

seneca.ready(() => {
    seneca.act('cmd:helloWorld', () => {
        console.log('OK') // dbg
    })
})

That's because priors must be invoked on the this instance, and not seneca. Line this.entity.transaction() actually creates a transaction on seneca and not on this, which means the information about the prior-stack is lost.

The provisional solution is to make seneca.entity a function, so that this along with the prior-stack may be preserved.